-
Notifications
You must be signed in to change notification settings - Fork 1.2k
test (escape) : Migrate escape to Ginkgo/Gomega and improve coverage #5635
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -25,28 +25,42 @@ import ( | |||||||||||||||||||||||||||||
| // a -> a | ||||||||||||||||||||||||||||||
| // a b -> a b | ||||||||||||||||||||||||||||||
| // $a -> $'$a' | ||||||||||||||||||||||||||||||
| // $'a' -> $'$\'$a'\' | ||||||||||||||||||||||||||||||
| // $'a' -> $'$\'a\” | ||||||||||||||||||||||||||||||
| func EscapeBashStr(s string) string { | ||||||||||||||||||||||||||||||
| if !containsOne(s, []rune{'$', '`', '&', ';', '>', '|', '(', ')'}) { | ||||||||||||||||||||||||||||||
| // Check if string contains any shell-sensitive characters that require escaping | ||||||||||||||||||||||||||||||
| // Added '\', '\'', '\n', '\r', and '\t' to the list as identified by security review | ||||||||||||||||||||||||||||||
| if !containsOne(s, []rune{'$', '`', '&', ';', '>', '|', '(', ')', '\'', '\\', '\n', '\r', '\t', ' '}) { | ||||||||||||||||||||||||||||||
| return s | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| s = strings.ReplaceAll(s, `\`, `\\`) | ||||||||||||||||||||||||||||||
| s = strings.ReplaceAll(s, `'`, `\'`) | ||||||||||||||||||||||||||||||
| if strings.Contains(s, `\\`) { | ||||||||||||||||||||||||||||||
| s = strings.ReplaceAll(s, `\\\\`, `\\`) | ||||||||||||||||||||||||||||||
| s = strings.ReplaceAll(s, `\\\'`, `\'`) | ||||||||||||||||||||||||||||||
| s = strings.ReplaceAll(s, `\\"`, `\"`) | ||||||||||||||||||||||||||||||
| s = strings.ReplaceAll(s, `\\a`, `\a`) | ||||||||||||||||||||||||||||||
| s = strings.ReplaceAll(s, `\\b`, `\b`) | ||||||||||||||||||||||||||||||
| s = strings.ReplaceAll(s, `\\e`, `\e`) | ||||||||||||||||||||||||||||||
| s = strings.ReplaceAll(s, `\\E`, `\E`) | ||||||||||||||||||||||||||||||
| s = strings.ReplaceAll(s, `\\n`, `\n`) | ||||||||||||||||||||||||||||||
| s = strings.ReplaceAll(s, `\\r`, `\r`) | ||||||||||||||||||||||||||||||
| s = strings.ReplaceAll(s, `\\t`, `\t`) | ||||||||||||||||||||||||||||||
| s = strings.ReplaceAll(s, `\\v`, `\v`) | ||||||||||||||||||||||||||||||
| s = strings.ReplaceAll(s, `\\?`, `\?`) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Build the escaped string manually to handle all special cases correctly | ||||||||||||||||||||||||||||||
| var result strings.Builder | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| for _, ch := range s { | ||||||||||||||||||||||||||||||
| switch ch { | ||||||||||||||||||||||||||||||
| case '\\': | ||||||||||||||||||||||||||||||
| // Escape backslashes by doubling them | ||||||||||||||||||||||||||||||
| result.WriteString(`\\`) | ||||||||||||||||||||||||||||||
| case '\'': | ||||||||||||||||||||||||||||||
| // Escape single quotes | ||||||||||||||||||||||||||||||
| result.WriteString(`\'`) | ||||||||||||||||||||||||||||||
| case '\n': | ||||||||||||||||||||||||||||||
| // Preserve newline as literal \n in ANSI-C quoted string | ||||||||||||||||||||||||||||||
| result.WriteString(`\n`) | ||||||||||||||||||||||||||||||
| case '\r': | ||||||||||||||||||||||||||||||
| // Preserve carriage return as literal \r in ANSI-C quoted string | ||||||||||||||||||||||||||||||
| result.WriteString(`\r`) | ||||||||||||||||||||||||||||||
| case '\t': | ||||||||||||||||||||||||||||||
| // Preserve tab as literal \t in ANSI-C quoted string | ||||||||||||||||||||||||||||||
|
Comment on lines
+48
to
+54
|
||||||||||||||||||||||||||||||
| // Preserve newline as literal \n in ANSI-C quoted string | |
| result.WriteString(`\n`) | |
| case '\r': | |
| // Preserve carriage return as literal \r in ANSI-C quoted string | |
| result.WriteString(`\r`) | |
| case '\t': | |
| // Preserve tab as literal \t in ANSI-C quoted string | |
| // Encode newline using \n escape for ANSI-C quoted string | |
| result.WriteString(`\n`) | |
| case '\r': | |
| // Encode carriage return using \r escape for ANSI-C quoted string | |
| result.WriteString(`\r`) | |
| case '\t': | |
| // Encode tab using \t escape for ANSI-C quoted string |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,26 +16,247 @@ limitations under the License. | |||||||||||||||
|
|
||||||||||||||||
| package security | ||||||||||||||||
|
|
||||||||||||||||
| import "testing" | ||||||||||||||||
|
|
||||||||||||||||
| func TestEscapeBashStr(t *testing.T) { | ||||||||||||||||
| cases := [][]string{ | ||||||||||||||||
| {"abc", "abc"}, | ||||||||||||||||
| {"test-volume", "test-volume"}, | ||||||||||||||||
| {"http://minio.kube-system:9000/minio/dynamic-ce", "http://minio.kube-system:9000/minio/dynamic-ce"}, | ||||||||||||||||
| {"$(cat /proc/self/status | grep CapEff > /test.txt)", "$'$(cat /proc/self/status | grep CapEff > /test.txt)'"}, | ||||||||||||||||
| {"hel`cat /proc/self/status`lo", "$'hel`cat /proc/self/status`lo'"}, | ||||||||||||||||
| {"'h'el`cat /proc/self/status`lo", "$'\\'h\\'el`cat /proc/self/status`lo'"}, | ||||||||||||||||
| {"\\'h\\'el`cat /proc/self/status`lo", "$'\\'h\\'el`cat /proc/self/status`lo'"}, | ||||||||||||||||
| {"$'h'el`cat /proc/self/status`lo", "$'$\\'h\\'el`cat /proc/self/status`lo'"}, | ||||||||||||||||
| {"hel\\`cat /proc/self/status`lo", "$'hel\\\\`cat /proc/self/status`lo'"}, | ||||||||||||||||
| {"hel\\\\`cat /proc/self/status`lo", "$'hel\\\\`cat /proc/self/status`lo'"}, | ||||||||||||||||
| {"hel\\'`cat /proc/self/status`lo", "$'hel\\'`cat /proc/self/status`lo'"}, | ||||||||||||||||
| } | ||||||||||||||||
| for _, c := range cases { | ||||||||||||||||
| escaped := EscapeBashStr(c[0]) | ||||||||||||||||
| if escaped != c[1] { | ||||||||||||||||
| t.Errorf("escapeBashVar(%s) = %s, want %s", c[0], escaped, c[1]) | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
| import ( | ||||||||||||||||
| . "github.com/onsi/ginkgo/v2" | ||||||||||||||||
| . "github.com/onsi/gomega" | ||||||||||||||||
| "testing" | ||||||||||||||||
|
Comment on lines
+20
to
+22
|
||||||||||||||||
| . "github.com/onsi/ginkgo/v2" | |
| . "github.com/onsi/gomega" | |
| "testing" | |
| "testing" | |
| . "github.com/onsi/ginkgo/v2" | |
| . "github.com/onsi/gomega" |
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.
This comment is now out of sync with the code: the trigger list also includes a space (
' '), but the comment only mentions adding backslash/quote/newline/CR/tab. Please update the comment to reflect the full set of newly included characters (or rephrase to avoid listing them explicitly).