Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quote shell command correctly for Posix shells #13214

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 55 additions & 25 deletions base/shell.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,34 +125,64 @@ function shell_split(s::AbstractString)
args
end

function print_shell_word(io::IO, word::AbstractString)
if isempty(word)
print(io, "''")
"Quote by enclosing in single quotes"
function _quote_via_single_quote(word::AbstractString)
# Quote by enclosing in single quotes, and replace all single
# quotes with backslash-quote
quoted = "'" * replace(word, r"'", "'\\''") * "'"
# Remove empty leading or trailing quotes
quoted = replace(quoted, r"^''", "")
quoted = replace(quoted, r"'''$(?!\n)", "'")
if isempty(quoted)
quoted = "''"
end
has_single = false
has_special = false
for c in word
if isspace(c) || c=='\\' || c=='\'' || c=='"' || c=='$'
has_special = true
if c == '\''
has_single = true
end
end
quoted
end
"Quote by enclosing in double quotes"
function _quote_via_double_quote(word::AbstractString)
# Quote by enclosing in double quotes and escaping certain special
# characters with a backslash, and by escaping all exclamation
# marks with a backslash
quoted = "\"" * replace(word, r"(?=[$`\"\\])", "\\") * "\""
quoted = replace(quoted, r"!", "\"\\!\"\"")
# Remove empty leading or trailing quotes
quoted = replace(quoted, r"^\"\"", "")
quoted = replace(quoted, r"!\"\"$(?!\n)", "!")
if isempty(quoted)
quoted = "''"
end
if !has_special
print(io, word)
elseif !has_single
print(io, '\'', word, '\'')
else
print(io, '"')
for c in word
if c == '"' || c == '$'
print(io, '\\')
end
print(io, c)
end
print(io, '"')
quoted
end
"Quote by escaping with backslashes"
function _quote_via_backslash(word::AbstractString)
# Quote by escaping all non-alphanumeric characters with a
# backslash, and by enclosing all newlines with single quotes
quoted = replace(word, r"(?=[^-0-9a-zA-Z_./\n])", "\\")
quoted = replace(quoted, r"\n", "'\\n'")
if isempty(quoted)
quoted = "''"
end
quoted
end
"Score a quoted string (lower is better)"
function _score_quoted(quoted::AbstractString)
# Prefer shorted strings, and penalize backslashes (which are
# arguably more confusing than quotes)
length(quoted) + count(c -> c=='\\', quoted)
end
"Quote a word so that it is safe to use with Posix sh"
function _quote_shell_word(word::AbstractString)
# Return the "best" string
quotes = [_quote_via_single_quote(word),
_quote_via_double_quote(word),
_quote_via_backslash(word)]
scores = map(_score_quoted, quotes)
best = findmin(scores)[2]
return quotes[best]
end

function print_shell_word(io::IO, word::AbstractString)
print(io, _quote_shell_word(word))
end

function print_shell_escaped(io::IO, cmd::AbstractString, args::AbstractString...)
Expand Down
2 changes: 1 addition & 1 deletion base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ include("iobuffer.jl")
include("string.jl")
include("unicode.jl")
include("parse.jl")
include("shell.jl")
include("regex.jl")
include("shell.jl")
include("base64.jl")
importall .Base64

Expand Down
12 changes: 12 additions & 0 deletions test/spawn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,18 @@ end
# Backticks should automatically quote where necessary
let cmd = ["foo bar", "baz"]
@test string(`$cmd`) == "`'foo bar' baz`"

# Test various difficult strings, and test multiple quoting levels
strs = ByteString["", "asdf", "'", "\"", "\\", " ", "\$",
"a\"bc\"d", "'012'3", " a ", "\n", "\\n", "\t",
"echo hello", " \${LANG}", "\"println(ARGS)\""]
for level in 1:3
cmd = Cmd(strs)
quoted = Base.shell_escape(cmd)
strs2 = Base.shell_split(quoted)
@test strs2 == strs
strs = ByteString[Base.shell_escape(str) for str in strs]
end
end

@test Base.shell_split("\"\\\\\"") == ["\\"]