git.iko.soy/typeable/nixpkgs
on master help sign in
lib: Make escapeShellArg more robust

Quoting various characters that the shell *may* interpret specially is a very fragile thing to do. I've used something more robust all over the place in various Nix expression I've written just because I didn't trust escapeShellArg. Here is a proof of concept showing that I was indeed right in distrusting escapeShellArg: with import <nixpkgs> {}; let payload = runCommand "payload" {} '' # \x00 is not allowed for Nix strings, so let's begin at 1 for i in $(seq 1 255); do echo -en "\\x$(printf %02x $i)" done > "$out" ''; escapers = with lib; { current = escapeShellArg; better = arg: let backslashEscapes = stringToCharacters "\"\\ ';$`()|<>\r\t*[]&!~#"; search = backslashEscapes ++ [ "\n" ]; replace = map (c: "\\${c}") backslashEscapes ++ [ "'\n'" ]; in replaceStrings search replace (toString arg); best = arg: "'${replaceStrings ["'"] ["'\\''"] (toString arg)}'"; }; testWith = escaper: let escaped = escaper (builtins.readFile payload); in runCommand "test" {} '' if ! r="$(bash -c ${escapers.best "echo -nE ${escaped}"} 2> /dev/null)" then echo bash eval error > "$out" exit 0 fi if echo -n "$r" | cmp -s "${payload}"; then echo success > "$out" else echo failed > "$out" fi ''; in runCommand "results" {} '' echo "Test results:" ${lib.concatStrings (lib.mapAttrsToList (name: impl: '' echo " ${name}: $(< "${testWith impl}")" '') escapers)} exit 1 '' The resulting output is the following: Test results: best: success better: success current: bash eval error I did the "better" implementation just to illustrate that the method of quoting only "harmful" characters results in madness in terms of implementation and performance. Signed-off-by: aszlig <aszlig@redmoonstudios.org> Cc: @edolstra, @zimbatm

commitdf475092e92b9dab9642c48f2216d49027a457a1 authoraszlig <aszlig@redmoonstudios.org> dateMon 20 Jun 2016, 18:31 (10 years ago) parent99c6c9d
1 file changed+7−8
lib/strings.nix+7−8
lib/strings.nix+7−8
@@ -203,20 +203,19 @@ rec {
203203 */
204204 escape = list: replaceChars list (map (c: "\\${c}") list);
205205
206− /* Escape all characters that have special meaning in the Bourne shell.
206+ /* Quote string to be used safely within the Bourne shell.
207207
208208 Example:
209− escapeShellArg "so([<>])me"
210− => "so\\(\\[\\<\\>\\]\\)me"
209+ escapeShellArg "esc'ape\nme"
210+ => "'esc'\\''ape\nme'"
211211 */
212− escapeShellArg = arg:
213− lib.escape (stringToCharacters "\\ ';$`()|<>\t*[]") (toString arg);
212+ escapeShellArg = arg: "'${replaceStrings ["'"] ["'\\''"] (toString arg)}'";
214213
215− /* Escape all arguments to be passed to the Bourne shell.
214+ /* Quote all arguments to be safely passed to the Bourne shell.
216215
217216 Example:
218− escapeShellArgs ["one" "two three"]
219− => "one two\\ three"
217+ escapeShellArgs ["one" "two three" "four'five"]
218+ => "'one' 'two three' 'four'\\''five'"
220219 */
221220 escapeShellArgs = concatMapStringsSep " " escapeShellArg;
222221