Debian Codegraph @

shsprintf/shsprintf_test.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Copyright 2023 Google LLC.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package shsprintf_test
16
17 import (
18 "strings"
19 "testing"
20
21 "github.com/google/safetext/shsprintf"
22 )
23
24 func TestSafetextShsprintf(t *testing.T) {
25 type testCase struct {
26 format string
27 replacements []any
28 err error
29 }
30
31 testCases := []testCase{
32 // Negative cases
33 // ====================================================
34 {
35 format: "echo %s",
36 replacements: []any{
37 "hello",
38 },
39 err: nil,
40 },
41
42 {
43 format: `echo "%s"`,
44 replacements: []any{
45 "hello hello",
46 },
47 err: nil,
48 },
49
50 {
51 format: `echo '%s''s '`,
52 replacements: []any{
53 "blas hello",
54 },
55 err: nil,
56 },
57
58 {
59 format: `command "x-%s-y-%s"`,
60 replacements: []any{
61 "ar\\\"g-yz",
62 "bla",
63 },
64 err: nil,
65 },
66
67 // Truncation
68 {
69 format: `command "%.4s"`,
70 replacements: []any{
71 "bla",
72 },
73 err: nil,
74 },
75
76 {
77 format: `ls %s`,
78 replacements: []any{
79 "/tmp" + "/bla",
80 },
81 err: nil,
82 },
83
84 {
85 format: "echo `cat %s`",
86 replacements: []any{
87 "hello",
88 },
89 err: nil,
90 },
91
92 {
93 format: `#! /bin/bash
94 end=$((SECONDS+%d))
95
96 while [ $SECONDS -lt $end ]; do
97 done`,
98 replacements: []any{
99 3,
100 },
101 err: nil,
102 },
103
104 {
105 format: `echo "$(( %s + %s ))"`,
106 replacements: []any{
107 "12",
108 "10",
109 },
110 err: nil,
111 },
112
113 // String values in items in loops can change (as long as new expressions not introduced)
114 {
115 format: `for VARIABLE in file1 %s file3
116 do
117 cat $VARIABLE
118 done`,
119 replacements: []any{
120 "bla",
121 },
122 err: nil,
123 },
124
125 // C-style loop
126 {
127 format: `for (( c=1; c<=%s; c++ ))
128 do
129 shell_COMMANDS
130 done`,
131 replacements: []any{
132 "5",
133 },
134 err: nil,
135 },
136
137 // If statement conditions fine to change
138 {
139 format: `if [ %s ] ; then
140 command
141 fi`,
142 replacements: []any{
143 "condition",
144 },
145 err: nil,
146 },
147
148 // Extended if
149 {
150 format: `
151 if [[ -e %s ]]
152 then
153 echo "File exists"
154 fi`,
155 replacements: []any{
156 "file",
157 },
158 err: nil,
159 },
160
161 // Positive cases
162 // ====================================================
163 // Command injection
164 {
165 format: "echo %s",
166 replacements: []any{
167 "`./command`",
168 },
169 err: shsprintf.ErrShInjection,
170 },
171
172 // Multiple arguments
173 {
174 format: "cat %s",
175 replacements: []any{
176 "one two three",
177 },
178 err: shsprintf.ErrShInjection,
179 },
180
181 // Flags
182 {
183 format: "cat %s",
184 replacements: []any{
185 "-flag",
186 },
187 err: shsprintf.ErrShInjection,
188 },
189
190 // Condition with new expressions
191 {
192 format: `
193 if [[ -e %s ]]
194 then
195 echo "File exists"
196 fi`,
197 replacements: []any{
198 "file || (1==1)",
199 },
200 err: shsprintf.ErrShInjection,
201 },
202
203 // Command injection 2
204 {
205 format: "echo %s",
206 replacements: []any{
207 "$(./command)",
208 },
209 err: shsprintf.ErrShInjection,
210 },
211
212 // Command injection 3
213 {
214 format: "echo %s",
215 replacements: []any{
216 "foobar\ncommand",
217 },
218 err: shsprintf.ErrShInjection,
219 },
220
221 // Command injection 4
222 {
223 format: "echo %s foobar",
224 replacements: []any{
225 ";",
226 },
227 err: shsprintf.ErrShInjection,
228 },
229
230 // Command injection 5
231 {
232 format: "echo %s",
233 replacements: []any{
234 "foo$(./command)bar",
235 },
236 err: shsprintf.ErrShInjection,
237 },
238
239 // Command injection 6
240 {
241 format: "echo %s",
242 replacements: []any{
243 "foo`./command`bar",
244 },
245 err: shsprintf.ErrShInjection,
246 },
247
248 // Command injection 7
249 {
250 format: "echo %s",
251 replacements: []any{
252 `"foo$(./command)bar"`,
253 },
254 err: shsprintf.ErrShInjection,
255 },
256
257 // Command injection 7 - cross check
258 {
259 format: "echo %s",
260 replacements: []any{
261 `foo/commandbar`,
262 },
263 err: nil,
264 },
265
266 // Command injection 8
267 {
268 format: `echo "%s"`,
269 replacements: []any{
270 `foo$(./command)bar`,
271 },
272 err: shsprintf.ErrShInjection,
273 },
274
275 // Command injection 9
276 {
277 format: `echo '%s'`,
278 replacements: []any{
279 `foo' ./command 'bar`,
280 },
281 err: shsprintf.ErrShInjection,
282 },
283
284 // Argument injection due to glob operator in literal strings
285 {
286 format: `touch %s; echo %s`,
287 replacements: []any{
288 "./--some-param=value",
289 "*value",
290 },
291 err: shsprintf.ErrShInjection,
292 },
293
294 // ANSI-C style
295 {
296 format: `command %s`,
297 replacements: []any{
298 `$'\u002d\u002d'flag=value`,
299 },
300 err: shsprintf.ErrShInjection,
301 },
302 }
303
304 for _, tc := range testCases {
305 t.Run(tc.format, func(t *testing.T) {
306 result, err := shsprintf.Sprintf(tc.format, tc.replacements...)
307 if err != tc.err {
308 t.Errorf("Got %v, expected %v", err, tc.err)
309
310 if err == nil {
311 t.Logf("template execution result was %s", result)
312 }
313 }
314 })
315 }
316 }
317
318 // The reason we don't need the mutation pass for shell scripting, like is done in the YAML code, is because the parser operates on the AST-level and can parse the exact number of quotes / comments, detecting cross-argument injections where the structure would otherwise match, as below:
319 func TestSafetextShsprintfComment(t *testing.T) {
320 controlled := "x; commandB injection #"
321 notControlled := "notControlled"
322
323 result, err := shsprintf.Sprintf("commandA %s; commandB %s # Comment", controlled, notControlled)
324
325 if err != shsprintf.ErrShInjection {
326 t.Errorf("Got %v, expected %v", err, shsprintf.ErrShInjection)
327
328 if err == nil {
329 t.Logf("template execution result was %s", result)
330 }
331 }
332 }
333
334 func TestSafetextShsprintfQuote(t *testing.T) {
335 controlled := "x; commandB injection; commandC \""
336 notControlled := "notControlled"
337 c := "\\"
338
339 result, err := shsprintf.Sprintf("commandA %s; commandB %s; commandC \"%s\"", controlled, notControlled, c)
340
341 if err != shsprintf.ErrShInjection {
342 t.Errorf("Got %v, expected %v\n", err, shsprintf.ErrShInjection)
343
344 if err == nil {
345 t.Logf("template execution result was %s", result)
346 }
347 }
348 }
349
350 // Demonstration of how to paste multiple arguments
351 func TestSafetextShsprintfMultipleArguments(t *testing.T) {
352 files := []any{"file1", "file2", "file3"}
353
354 _, err := shsprintf.Sprintf("cat"+strings.Repeat(" %s", len(files)), files...)
355
356 if err != nil {
357 t.Errorf("Unexpected Sprintf error: %v", err)
358 }
359 }
360
361 func generateExportVariables(env map[string]string) (string, error) {
362 script := new(strings.Builder)
363
364 // Note: iteration order undefined
365 for key, value := range env {
366 result, err := shsprintf.Sprintf("export %s=%s\n", key, value)
367
368 if err != nil {
369 return "", err
370 }
371
372 script.WriteString(result)
373 }
374
375 return script.String(), nil
376 }
377
378 // Demonstration of using map of environment variables
379 func TestSafetextShsprintfMapsNegative(t *testing.T) {
380 _, err := generateExportVariables(map[string]string{"one": "a", "two": "b", "three": "c"})
381
382 if err != nil {
383 t.Errorf("Unexpected Sprintf error: %v", err)
384 }
385 }
386
387 func TestSafetextShsprintfMapsPositive(t *testing.T) {
388 _, err := generateExportVariables(map[string]string{"one": "a", "two": "b", "three": "c four=d"})
389
390 if err != shsprintf.ErrShInjection {
391 t.Errorf("Got %v, expected %v", err, shsprintf.ErrShInjection)
392 }
393 }
394
395 func TestSafetextShsprintfMultilineEof(t *testing.T) {
396 path := "bla"
397 content := `test
398 another line`
399
400 _, err := shsprintf.Sprintf(`cat > %s/settings.xml << 'EOF'
401 %s
402 EOF`, path, content)
403
404 if err != nil {
405 t.Errorf("Unexpected Sprintf error: %v", err)
406 }
407 }
408
409 func TestSafetextShsprintfEscape(t *testing.T) {
410 arg := "bla"
411
412 for i := 0; i < 256; i++ {
413 arg += string(rune(i))
414 }
415
416 _, err := shsprintf.Sprintf(`cmd --arg=%s`, shsprintf.EscapeDefaultContext(arg))
417
418 if err != nil {
419 t.Errorf("Unexpected Sprintf error: %v", err)
420 }
421 }