Debian Codegraph @

shtemplate/shtemplate_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 shtemplate_test
16
17 import (
18 template "github.com/google/safetext/shtemplate"
19
20 "bytes"
21 "testing"
22 )
23
24 func TestSafetextShtemplate(t *testing.T) {
25 type testCase struct {
26 tmplText string
27 replacements map[any]any
28 err error
29 }
30
31 testCases := []testCase{
32 // Negative cases
33 // ====================================================
34 {
35 tmplText: "echo {{ .addressee }}",
36 replacements: map[any]any{
37 "addressee": "hello",
38 },
39 err: nil,
40 },
41
42 {
43 tmplText: `echo "{{ .addressee }}"`,
44 replacements: map[any]any{
45 "addressee": "hello hello",
46 },
47 err: nil,
48 },
49
50 {
51 tmplText: `ls {{ range .Paths }}{{.}} {{end}}`,
52 replacements: map[any]any{
53 "Paths": []string{"/tmp", "/bla"},
54 },
55 err: nil,
56 },
57
58 {
59 tmplText: "echo `cat {{ .file }}`",
60 replacements: map[any]any{
61 "file": "hello",
62 },
63 err: nil,
64 },
65
66 {
67 tmplText: `#! /bin/bash
68 end=$((SECONDS+{{.wait}}))
69
70 while [ $SECONDS -lt $end ]; do
71 done`,
72 replacements: map[any]any{
73 "wait": 3,
74 },
75 err: nil,
76 },
77
78 {
79 tmplText: `echo "$(( {{.a}} + {{.b}} ))"`,
80 replacements: map[any]any{
81 "a": "12",
82 "b": "10",
83 },
84 err: nil,
85 },
86
87 // String values in items in loops can change (as long as new expressions not introduced)
88 {
89 tmplText: `for VARIABLE in file1 {{.a}} file3
90 do
91 cat $VARIABLE
92 done`,
93 replacements: map[any]any{
94 "a": "bla",
95 },
96 err: nil,
97 },
98
99 // C-style loop
100 {
101 tmplText: `for (( c=1; c<={{.a}}; c++ ))
102 do
103 shell_COMMANDS
104 done`,
105 replacements: map[any]any{
106 "a": "5",
107 },
108 err: nil,
109 },
110
111 // If statement conditions fine to change
112 {
113 tmplText: `if [ {{.c}} ] ; then
114 command
115 fi`,
116 replacements: map[any]any{
117 "c": "condition",
118 },
119 err: nil,
120 },
121
122 // Extended if
123 {
124 tmplText: `
125 if [[ -e {{.x}} ]]
126 then
127 echo "File exists"
128 fi`,
129 replacements: map[any]any{
130 "x": "file",
131 },
132 err: nil,
133 },
134
135 // Explicitly annotated command
136 {
137 tmplText: "echo $(({{ (StructuralData .addressee) }}))",
138 replacements: map[any]any{
139 "addressee": "./command",
140 },
141 err: nil,
142 },
143
144 // Explicitly annotated filename
145 {
146 tmplText: "bla > {{ StructuralData .addressee }}",
147 replacements: map[any]any{
148 "addressee": "filename",
149 },
150 err: nil,
151 },
152
153 // Explicitly annotated flag
154 {
155 tmplText: "bla {{ AllowFlags .addressee }}",
156 replacements: map[any]any{
157 "addressee": "-flag",
158 },
159 err: nil,
160 },
161
162 // Positive cases
163 // ====================================================
164 // Unannotated command
165 {
166 tmplText: "echo $(({{ .addressee }}))",
167 replacements: map[any]any{
168 "addressee": "./command",
169 },
170 err: template.ErrShInjection,
171 },
172
173 // Command injection
174 {
175 tmplText: "echo {{ .addressee }}",
176 replacements: map[any]any{
177 "addressee": "`./command`",
178 },
179 err: template.ErrShInjection,
180 },
181
182 // Multiple arguments
183 {
184 tmplText: "cat {{ .addressee }}",
185 replacements: map[any]any{
186 "addressee": "one two three",
187 },
188 err: template.ErrShInjection,
189 },
190
191 // Unannotated flags
192 {
193 tmplText: "cat {{ .addressee }}",
194 replacements: map[any]any{
195 "addressee": "-flag",
196 },
197 err: template.ErrShInjection,
198 },
199
200 // Unannotated filenames
201 {
202 tmplText: "bla > {{ .addressee }}",
203 replacements: map[any]any{
204 "addressee": "filename",
205 },
206 err: template.ErrShInjection,
207 },
208
209 // Filename with syntax manipulation
210 {
211 tmplText: "bla {{ .addressee }}>out",
212 replacements: map[any]any{
213 "addressee": "x>evil#",
214 },
215 err: template.ErrShInjection,
216 },
217
218 // Condition with new expressions
219 {
220 tmplText: `
221 if [[ -e {{.x}} ]]
222 then
223 echo "File exists"
224 fi`,
225 replacements: map[any]any{
226 "x": "file || (1==1)",
227 },
228 err: template.ErrShInjection,
229 },
230
231 // Command injection 2
232 {
233 tmplText: "echo {{ .addressee }}",
234 replacements: map[any]any{
235 "addressee": "$(./command)",
236 },
237 err: template.ErrShInjection,
238 },
239
240 // Command injection 3
241 {
242 tmplText: "echo {{ .addressee }}",
243 replacements: map[any]any{
244 "addressee": "foobar\ncommand",
245 },
246 err: template.ErrShInjection,
247 },
248
249 // Command injection 4
250 {
251 tmplText: "echo {{ .addressee }} foobar",
252 replacements: map[any]any{
253 "addressee": ";",
254 },
255 err: template.ErrShInjection,
256 },
257
258 // Command injection 5
259 {
260 tmplText: "echo {{ .addressee }}",
261 replacements: map[any]any{
262 "addressee": "foo$(./command)bar",
263 },
264 err: template.ErrShInjection,
265 },
266
267 // Command injection 6
268 {
269 tmplText: "echo {{ .addressee }}",
270 replacements: map[any]any{
271 "addressee": "foo`./command`bar",
272 },
273 err: template.ErrShInjection,
274 },
275
276 // Command injection 7
277 {
278 tmplText: "echo {{ .addressee }}",
279 replacements: map[any]any{
280 "addressee": `"foo$(./command)bar"`,
281 },
282 err: template.ErrShInjection,
283 },
284
285 // Command injection 7 - cross check
286 {
287 tmplText: "echo {{ .addressee }}",
288 replacements: map[any]any{
289 "addressee": `foo/commandbar`,
290 },
291 err: nil,
292 },
293
294 // Command injection 8
295 {
296 tmplText: `echo "{{ .addressee }}"`,
297 replacements: map[any]any{
298 "addressee": `foo$(./command)bar`,
299 },
300 err: template.ErrShInjection,
301 },
302
303 // Command injection 9
304 {
305 tmplText: `echo '{{ .addressee }}'`,
306 replacements: map[any]any{
307 "addressee": `foo' ./command 'bar`,
308 },
309 err: template.ErrShInjection,
310 },
311
312 // Argument injection due to glob operator in literal strings
313 {
314 tmplText: `touch {{.a}}; echo {{.b}}`,
315 replacements: map[any]any{
316 "a": "./--some-param=value",
317 "b": "*value",
318 },
319 err: template.ErrShInjection,
320 },
321 }
322
323 for _, tc := range testCases {
324 t.Run(tc.tmplText, func(t *testing.T) {
325 tmpl, _ := template.New("test").Parse(tc.tmplText)
326 var buf bytes.Buffer
327 err := tmpl.Execute(&buf, tc.replacements)
328
329 if err != tc.err {
330 t.Errorf("Expected %v, got %v\n", tc.err, err)
331
332 if err == nil {
333 t.Logf("template execution result was %s\n", buf.String())
334 }
335 }
336 })
337 }
338 }