Merge pull request #16377 from aszlig/improve-escape-shell-arg

lib: Make escapeShellArg more robust
This commit is contained in:
zimbatm 2016-06-21 14:32:45 +01:00 committed by GitHub
commit bc6b93511f

View File

@ -203,20 +203,19 @@ rec {
*/ */
escape = list: replaceChars list (map (c: "\\${c}") list); escape = list: replaceChars list (map (c: "\\${c}") list);
/* Escape all characters that have special meaning in the Bourne shell. /* Quote string to be used safely within the Bourne shell.
Example: Example:
escapeShellArg "so([<>])me" escapeShellArg "esc'ape\nme"
=> "so\\(\\[\\<\\>\\]\\)me" => "'esc'\\''ape\nme'"
*/ */
escapeShellArg = arg: escapeShellArg = arg: "'${replaceStrings ["'"] ["'\\''"] (toString arg)}'";
lib.escape (stringToCharacters "\\ ';$`()|<>\t*[]") (toString arg);
/* Escape all arguments to be passed to the Bourne shell. /* Quote all arguments to be safely passed to the Bourne shell.
Example: Example:
escapeShellArgs ["one" "two three"] escapeShellArgs ["one" "two three" "four'five"]
=> "one two\\ three" => "'one' 'two three' 'four'\\''five'"
*/ */
escapeShellArgs = concatMapStringsSep " " escapeShellArg; escapeShellArgs = concatMapStringsSep " " escapeShellArg;