This repository was archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
fix: Escape shell arguments #227
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cmd | ||
|
||
import "testing" | ||
|
||
func TestShellEscape(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
Name string | ||
Input string | ||
Escaped string | ||
}{ | ||
{ | ||
Name: "single space", | ||
Input: "hello world", | ||
Escaped: `hello\ world`, | ||
}, | ||
{ | ||
Name: "multiple spaces", | ||
Input: "test message hello world", | ||
Escaped: `test\ message\ hello\ \ world`, | ||
}, | ||
{ | ||
Name: "mixed quotes", | ||
Input: `"''"`, | ||
Escaped: `\"\'\'\"`, | ||
}, | ||
{ | ||
Name: "mixed escaped quotes", | ||
Input: `"'\"\"'"`, | ||
Escaped: `\"\'\\\"\\\"\'\"`, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
if e, a := test.Escaped, shellEscape(test.Input); e != a { | ||
t.Fatalf("test %q failed; expected: %q, got %q (input: %q)", | ||
test.Name, test.Escaped, a, test.Input) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think eventually we should be relying on shlex (or similar) to be parsing and escaping shell commands from strings into arg arrays, and avoid rolling our own for things like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tychoish Agree, I did a quick search bit didn't find any good ones. Happy to switch over now, or we can do it later, WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/google/shlex is functional.
I think this might also solve the %q question in the other PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tychoish Oh yeah, that looks much better. Looks like there were a few cases that I missed.
I don't think this will work for #226 because we need the shell to interpret the subshell calls for getent/etc, but I will give it a try
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm it looks like shlex is for parsing shell expressions, how do I use it to escape things?