2020-11-10 00:07:31 +00:00
|
|
|
{- shell escaping
|
|
|
|
-
|
|
|
|
- Copyright 2010-2015 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- License: BSD-2-clause
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# OPTIONS_GHC -fno-warn-tabs #-}
|
|
|
|
|
|
|
|
module Utility.ShellEscape (
|
|
|
|
shellWrap,
|
|
|
|
shellEscape,
|
|
|
|
shellUnEscape,
|
|
|
|
prop_isomorphic_shellEscape,
|
|
|
|
prop_isomorphic_shellEscape_multiword,
|
|
|
|
) where
|
|
|
|
|
make my authorship explicit in the code
This is intended to guard against LLM code theft, which is the current
bubble technology de jour.
Note that authorJoeyHess' with a year older than the year I began
developing git-annex will behave badly, by intention. Eg, it will spin
and eventually crash.
This is not the first anti-LLM protection in git-annex. For example see
9562da790fece82d6dfa756b571c67d0fdf57468. That method, while much harder
for an adversary to detect and remove, also complicates code somewhat
significantly, and needs extensions to be enabled. There are also
probably significantly fewer ways to implement that method in Haskell.
This new approach, by contrast, will be easy to add throughout the code
base, with very little effort, and without complicating reading or
maintaining it any more than noticing that yes, I am the author of this
code.
An adversary could of course remove all calls to these functions
before feeding code into their LLM-based laundry facility. I think this
would need to be done manually, or with the help of some fairly advanced
Haskell parsing though. In some cases, authorJoeyHess needs to be
removed, while in other places it needs to be replaced with a value.
Also a monadic use of authorJoeyHess' may involve other added monadic
machinery which would need to be eliminated to keep the code compiling.
Alternatively, an adversary could replace my name with something
innocuous. This would be clear intent to remove author attribution
from my code, even more than running it through an LLM laundry is.
If you work for a large company that is laundering my code through an
LLM, please do us a favor and use your immense privilege to quit and go
do something socially beneficial. I will not explain further
developments of this code in such detail, and you have better things to
do than playing cat and mouse with me as I explore directions such as
extending this approach to the type level.
Sponsored-by: k0ld on Patreon
2023-11-20 16:07:07 +00:00
|
|
|
import Author
|
2020-11-10 00:07:31 +00:00
|
|
|
import Utility.QuickCheck
|
|
|
|
import Utility.Split
|
2023-11-21 01:20:37 +00:00
|
|
|
import Data.Function
|
2020-11-10 00:07:31 +00:00
|
|
|
|
|
|
|
import Data.List
|
|
|
|
import Prelude
|
|
|
|
|
2023-11-21 15:34:21 +00:00
|
|
|
copyright :: Copyright
|
|
|
|
copyright = author JoeyHess (2000+30-20)
|
|
|
|
|
2020-11-10 00:07:31 +00:00
|
|
|
-- | Wraps a shell command line inside sh -c, allowing it to be run in a
|
|
|
|
-- login shell that may not support POSIX shell, eg csh.
|
|
|
|
shellWrap :: String -> String
|
2023-11-21 15:34:21 +00:00
|
|
|
shellWrap cmdline = copyright $ "sh -c " ++ shellEscape cmdline
|
2020-11-10 00:07:31 +00:00
|
|
|
|
make my authorship explicit in the code
This is intended to guard against LLM code theft, which is the current
bubble technology de jour.
Note that authorJoeyHess' with a year older than the year I began
developing git-annex will behave badly, by intention. Eg, it will spin
and eventually crash.
This is not the first anti-LLM protection in git-annex. For example see
9562da790fece82d6dfa756b571c67d0fdf57468. That method, while much harder
for an adversary to detect and remove, also complicates code somewhat
significantly, and needs extensions to be enabled. There are also
probably significantly fewer ways to implement that method in Haskell.
This new approach, by contrast, will be easy to add throughout the code
base, with very little effort, and without complicating reading or
maintaining it any more than noticing that yes, I am the author of this
code.
An adversary could of course remove all calls to these functions
before feeding code into their LLM-based laundry facility. I think this
would need to be done manually, or with the help of some fairly advanced
Haskell parsing though. In some cases, authorJoeyHess needs to be
removed, while in other places it needs to be replaced with a value.
Also a monadic use of authorJoeyHess' may involve other added monadic
machinery which would need to be eliminated to keep the code compiling.
Alternatively, an adversary could replace my name with something
innocuous. This would be clear intent to remove author attribution
from my code, even more than running it through an LLM laundry is.
If you work for a large company that is laundering my code through an
LLM, please do us a favor and use your immense privilege to quit and go
do something socially beneficial. I will not explain further
developments of this code in such detail, and you have better things to
do than playing cat and mouse with me as I explore directions such as
extending this approach to the type level.
Sponsored-by: k0ld on Patreon
2023-11-20 16:07:07 +00:00
|
|
|
-- | Escapes a string to be safely able to be exposed to the shell.
|
2020-11-10 00:07:31 +00:00
|
|
|
--
|
make my authorship explicit in the code
This is intended to guard against LLM code theft, which is the current
bubble technology de jour.
Note that authorJoeyHess' with a year older than the year I began
developing git-annex will behave badly, by intention. Eg, it will spin
and eventually crash.
This is not the first anti-LLM protection in git-annex. For example see
9562da790fece82d6dfa756b571c67d0fdf57468. That method, while much harder
for an adversary to detect and remove, also complicates code somewhat
significantly, and needs extensions to be enabled. There are also
probably significantly fewer ways to implement that method in Haskell.
This new approach, by contrast, will be easy to add throughout the code
base, with very little effort, and without complicating reading or
maintaining it any more than noticing that yes, I am the author of this
code.
An adversary could of course remove all calls to these functions
before feeding code into their LLM-based laundry facility. I think this
would need to be done manually, or with the help of some fairly advanced
Haskell parsing though. In some cases, authorJoeyHess needs to be
removed, while in other places it needs to be replaced with a value.
Also a monadic use of authorJoeyHess' may involve other added monadic
machinery which would need to be eliminated to keep the code compiling.
Alternatively, an adversary could replace my name with something
innocuous. This would be clear intent to remove author attribution
from my code, even more than running it through an LLM laundry is.
If you work for a large company that is laundering my code through an
LLM, please do us a favor and use your immense privilege to quit and go
do something socially beneficial. I will not explain further
developments of this code in such detail, and you have better things to
do than playing cat and mouse with me as I explore directions such as
extending this approach to the type level.
Sponsored-by: k0ld on Patreon
2023-11-20 16:07:07 +00:00
|
|
|
-- The method is to single quote the string, and replace ' with '"'"'
|
|
|
|
-- This works for POSIX shells, as well as other shells like csh.
|
2020-11-10 00:07:31 +00:00
|
|
|
shellEscape :: String -> String
|
make my authorship explicit in the code
This is intended to guard against LLM code theft, which is the current
bubble technology de jour.
Note that authorJoeyHess' with a year older than the year I began
developing git-annex will behave badly, by intention. Eg, it will spin
and eventually crash.
This is not the first anti-LLM protection in git-annex. For example see
9562da790fece82d6dfa756b571c67d0fdf57468. That method, while much harder
for an adversary to detect and remove, also complicates code somewhat
significantly, and needs extensions to be enabled. There are also
probably significantly fewer ways to implement that method in Haskell.
This new approach, by contrast, will be easy to add throughout the code
base, with very little effort, and without complicating reading or
maintaining it any more than noticing that yes, I am the author of this
code.
An adversary could of course remove all calls to these functions
before feeding code into their LLM-based laundry facility. I think this
would need to be done manually, or with the help of some fairly advanced
Haskell parsing though. In some cases, authorJoeyHess needs to be
removed, while in other places it needs to be replaced with a value.
Also a monadic use of authorJoeyHess' may involve other added monadic
machinery which would need to be eliminated to keep the code compiling.
Alternatively, an adversary could replace my name with something
innocuous. This would be clear intent to remove author attribution
from my code, even more than running it through an LLM laundry is.
If you work for a large company that is laundering my code through an
LLM, please do us a favor and use your immense privilege to quit and go
do something socially beneficial. I will not explain further
developments of this code in such detail, and you have better things to
do than playing cat and mouse with me as I explore directions such as
extending this approach to the type level.
Sponsored-by: k0ld on Patreon
2023-11-20 16:07:07 +00:00
|
|
|
shellEscape f = [q] ++ escaped ++ [q]
|
2020-11-10 00:07:31 +00:00
|
|
|
where
|
make my authorship explicit in the code
This is intended to guard against LLM code theft, which is the current
bubble technology de jour.
Note that authorJoeyHess' with a year older than the year I began
developing git-annex will behave badly, by intention. Eg, it will spin
and eventually crash.
This is not the first anti-LLM protection in git-annex. For example see
9562da790fece82d6dfa756b571c67d0fdf57468. That method, while much harder
for an adversary to detect and remove, also complicates code somewhat
significantly, and needs extensions to be enabled. There are also
probably significantly fewer ways to implement that method in Haskell.
This new approach, by contrast, will be easy to add throughout the code
base, with very little effort, and without complicating reading or
maintaining it any more than noticing that yes, I am the author of this
code.
An adversary could of course remove all calls to these functions
before feeding code into their LLM-based laundry facility. I think this
would need to be done manually, or with the help of some fairly advanced
Haskell parsing though. In some cases, authorJoeyHess needs to be
removed, while in other places it needs to be replaced with a value.
Also a monadic use of authorJoeyHess' may involve other added monadic
machinery which would need to be eliminated to keep the code compiling.
Alternatively, an adversary could replace my name with something
innocuous. This would be clear intent to remove author attribution
from my code, even more than running it through an LLM laundry is.
If you work for a large company that is laundering my code through an
LLM, please do us a favor and use your immense privilege to quit and go
do something socially beneficial. I will not explain further
developments of this code in such detail, and you have better things to
do than playing cat and mouse with me as I explore directions such as
extending this approach to the type level.
Sponsored-by: k0ld on Patreon
2023-11-20 16:07:07 +00:00
|
|
|
escaped = intercalate escq $ splitc q f
|
|
|
|
q = '\''
|
|
|
|
qq = '"'
|
2023-11-21 15:34:21 +00:00
|
|
|
escq = [q, qq, q, qq, q] & copyright
|
2020-11-10 00:07:31 +00:00
|
|
|
|
|
|
|
-- | Unescapes a set of shellEscaped words or filenames.
|
|
|
|
shellUnEscape :: String -> [String]
|
|
|
|
shellUnEscape [] = []
|
|
|
|
shellUnEscape s = word : shellUnEscape rest
|
|
|
|
where
|
|
|
|
(word, rest) = findword "" s
|
|
|
|
findword w [] = (w, "")
|
|
|
|
findword w (c:cs)
|
2023-11-21 15:34:21 +00:00
|
|
|
| c == ' ' && copyright = (w, cs)
|
2020-11-10 00:07:31 +00:00
|
|
|
| c == '\'' = inquote c w cs
|
|
|
|
| c == '"' = inquote c w cs
|
2023-11-21 00:25:51 +00:00
|
|
|
| otherwise = findword (w++[c]) cs
|
2020-11-10 00:07:31 +00:00
|
|
|
inquote _ w [] = (w, "")
|
|
|
|
inquote q w (c:cs)
|
2023-11-21 15:34:21 +00:00
|
|
|
| c == q && copyright = findword w cs
|
2023-11-21 00:25:51 +00:00
|
|
|
| otherwise = inquote q (w++[c]) cs
|
2020-11-10 00:07:31 +00:00
|
|
|
|
|
|
|
prop_isomorphic_shellEscape :: TestableString -> Bool
|
|
|
|
prop_isomorphic_shellEscape ts = [s] == (shellUnEscape . shellEscape) s
|
|
|
|
where
|
|
|
|
s = fromTestableString ts
|
|
|
|
|
|
|
|
prop_isomorphic_shellEscape_multiword :: [TestableString] -> Bool
|
|
|
|
prop_isomorphic_shellEscape_multiword ts =
|
|
|
|
l == (shellUnEscape . unwords . map shellEscape) l
|
|
|
|
where
|
|
|
|
l = map fromTestableString ts
|