Debian Codegraph @

modules/caddyhttp/reverseproxy/fastcgi/fastcgi_test.go

raw symbols SCIP
1 package fastcgi
2
3 import (
4 "strings"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 "github.com/stretchr/testify/require"
9
10 "github.com/caddyserver/caddy/v2"
11 )
12
13 func TestProvisionSplitPath(t *testing.T) {
14 tests := []struct {
15 name string
16 splitPath []string
17 wantErr error
18 wantSplitPath []string
19 }{
20 {
21 name: "valid lowercase split path",
22 splitPath: []string{".php"},
23 wantErr: nil,
24 wantSplitPath: []string{".php"},
25 },
26 {
27 name: "valid uppercase split path normalized",
28 splitPath: []string{".PHP"},
29 wantErr: nil,
30 wantSplitPath: []string{".php"},
31 },
32 {
33 name: "valid mixed case split path normalized",
34 splitPath: []string{".PhP", ".PHTML"},
35 wantErr: nil,
36 wantSplitPath: []string{".php", ".phtml"},
37 },
38 {
39 name: "empty split path",
40 splitPath: []string{},
41 wantErr: nil,
42 wantSplitPath: []string{},
43 },
44 {
45 name: "non-ASCII character in split path rejected",
46 splitPath: []string{".php", ".Ⱥphp"},
47 wantErr: ErrInvalidSplitPath,
48 },
49 {
50 name: "unicode character in split path rejected",
51 splitPath: []string{".phpⱥ"},
52 wantErr: ErrInvalidSplitPath,
53 },
54 }
55
56 for _, tt := range tests {
57 t.Run(tt.name, func(t *testing.T) {
58 tr := Transport{SplitPath: tt.splitPath}
59 err := tr.Provision(caddy.Context{})
60
61 if tt.wantErr != nil {
62 require.ErrorIs(t, err, tt.wantErr)
63
64 return
65 }
66
67 require.NoError(t, err)
68 assert.Equal(t, tt.wantSplitPath, tr.SplitPath)
69 })
70 }
71 }
72
73 func TestSplitPos(t *testing.T) {
74 tests := []struct {
75 name string
76 path string
77 splitPath []string
78 wantPos int
79 }{
80 {
81 name: "simple php extension",
82 path: "/path/to/script.php",
83 splitPath: []string{".php"},
84 wantPos: 19,
85 },
86 {
87 name: "php extension with path info",
88 path: "/path/to/script.php/some/path",
89 splitPath: []string{".php"},
90 wantPos: 19,
91 },
92 {
93 name: "case insensitive match",
94 path: "/path/to/script.PHP",
95 splitPath: []string{".php"},
96 wantPos: 19,
97 },
98 {
99 name: "mixed case match",
100 path: "/path/to/script.PhP/info",
101 splitPath: []string{".php"},
102 wantPos: 19,
103 },
104 {
105 name: "no match",
106 path: "/path/to/script.txt",
107 splitPath: []string{".php"},
108 wantPos: -1,
109 },
110 {
111 name: "empty split path",
112 path: "/path/to/script.php",
113 splitPath: []string{},
114 wantPos: 0,
115 },
116 {
117 name: "multiple split paths first match",
118 path: "/path/to/script.php",
119 splitPath: []string{".php", ".phtml"},
120 wantPos: 19,
121 },
122 {
123 name: "multiple split paths second match",
124 path: "/path/to/script.phtml",
125 splitPath: []string{".php", ".phtml"},
126 wantPos: 21,
127 },
128 // Unicode case-folding tests (security fix for GHSA-g966-83w7-6w38)
129 // U+023A (Ⱥ) lowercases to U+2C65 (ⱥ), which has different UTF-8 byte length
130 // Ⱥ: 2 bytes (C8 BA), ⱥ: 3 bytes (E2 B1 A5)
131 {
132 name: "unicode path with case-folding length expansion",
133 path: "/ȺȺȺȺshell.php",
134 splitPath: []string{".php"},
135 wantPos: 18, // correct position in original string
136 },
137 {
138 name: "unicode path with extension after expansion chars",
139 path: "/ȺȺȺȺshell.php/path/info",
140 splitPath: []string{".php"},
141 wantPos: 18,
142 },
143 {
144 name: "unicode in filename with multiple php occurrences",
145 path: "/ȺȺȺȺshell.php.txt.php",
146 splitPath: []string{".php"},
147 wantPos: 18, // should match first .php, not be confused by byte offset shift
148 },
149 {
150 name: "unicode case insensitive extension",
151 path: "/ȺȺȺȺshell.PHP",
152 splitPath: []string{".php"},
153 wantPos: 18,
154 },
155 {
156 name: "unicode in middle of path",
157 path: "/path/Ⱥtest/script.php",
158 splitPath: []string{".php"},
159 wantPos: 23, // Ⱥ is 2 bytes, so path is 23 bytes total, .php ends at byte 23
160 },
161 {
162 name: "unicode only in directory not filename",
163 path: "/Ⱥ/script.php",
164 splitPath: []string{".php"},
165 wantPos: 14,
166 },
167 // Additional Unicode characters that expand when lowercased
168 // U+0130 (İ - Turkish capital I with dot) lowercases to U+0069 + U+0307
169 {
170 name: "turkish capital I with dot",
171 path: "/İtest.php",
172 splitPath: []string{".php"},
173 wantPos: 11,
174 },
175 // Ensure standard ASCII still works correctly
176 {
177 name: "ascii only path with case variation",
178 path: "/PATH/TO/SCRIPT.PHP/INFO",
179 splitPath: []string{".php"},
180 wantPos: 19,
181 },
182 {
183 name: "path at root",
184 path: "/index.php",
185 splitPath: []string{".php"},
186 wantPos: 10,
187 },
188 {
189 name: "extension in middle of filename",
190 path: "/test.php.bak",
191 splitPath: []string{".php"},
192 wantPos: 9,
193 },
194 // Regression tests adapted from FrankenPHP advisories
195 // GHSA-3g8v-8r37-cgjm and GHSA-v4h7-cj44-8fc8: search.IgnoreCase
196 // matched Unicode equivalents of ASCII letters as ".php", and an
197 // inner non-ASCII byte path could leave the match flag stale.
198 {
199 name: "non-ascii byte after dot must not match",
200 path: "/PoC-match-unset.¡.txt",
201 splitPath: []string{".php"},
202 wantPos: -1,
203 },
204 {
205 name: "non-ascii byte mid-extension must not match",
206 path: "/script.p\xc2\xa1p",
207 splitPath: []string{".php"},
208 wantPos: -1,
209 },
210 {
211 name: "small full stop ﹒ in extension must not match",
212 path: "/shell﹒php",
213 splitPath: []string{".php"},
214 wantPos: -1,
215 },
216 {
217 name: "fullwidth full stop . in extension must not match",
218 path: "/shell.php",
219 splitPath: []string{".php"},
220 wantPos: -1,
221 },
222 {
223 name: "fullwidth p in extension must not match",
224 path: "/shell.php",
225 splitPath: []string{".php"},
226 wantPos: -1,
227 },
228 {
229 name: "circled php must not match",
230 path: "/shell.ⓟⓗⓟ",
231 splitPath: []string{".php"},
232 wantPos: -1,
233 },
234 {
235 name: "mathematical sans-serif bold php must not match",
236 path: "/shell.\U0001D5FD\U0001D5F5\U0001D5FD",
237 splitPath: []string{".php"},
238 wantPos: -1,
239 },
240 {
241 name: "mathematical script php must not match",
242 path: "/shell.\U0001D4C5\U0001D4BD\U0001D4C5",
243 splitPath: []string{".php"},
244 wantPos: -1,
245 },
246 {
247 name: "circled php with later real php still picks the real one",
248 path: "/shell.ⓟⓗⓟ.anything-after-payload.php",
249 splitPath: []string{".php"},
250 // "/shell." (7) + "ⓟⓗⓟ" (3*3 bytes) + ".anything-after-payload.php" (27) = 43
251 wantPos: 43,
252 },
253 }
254
255 for _, tt := range tests {
256 t.Run(tt.name, func(t *testing.T) {
257 gotPos := Transport{SplitPath: tt.splitPath}.splitPos(tt.path)
258 assert.Equal(t, tt.wantPos, gotPos, "splitPos(%q, %v)", tt.path, tt.splitPath)
259
260 // Verify that the split produces valid substrings
261 if gotPos > 0 && gotPos <= len(tt.path) {
262 scriptName := tt.path[:gotPos]
263 pathInfo := tt.path[gotPos:]
264
265 // The script name should end with one of the split extensions (case-insensitive)
266 hasValidEnding := false
267 for _, split := range tt.splitPath {
268 if strings.HasSuffix(strings.ToLower(scriptName), split) {
269 hasValidEnding = true
270 break
271 }
272 }
273 assert.True(t, hasValidEnding, "script name %q should end with one of %v", scriptName, tt.splitPath)
274
275 // Original path should be reconstructable
276 assert.Equal(t, tt.path, scriptName+pathInfo, "path should be reconstructable from split parts")
277 }
278 })
279 }
280 }
281
282 // TestSplitPosUnicodeSecurityRegression specifically tests the vulnerability
283 // described in GHSA-g966-83w7-6w38 where Unicode case-folding caused
284 // incorrect SCRIPT_NAME/PATH_INFO splitting
285 func TestSplitPosUnicodeSecurityRegression(t *testing.T) {
286 // U+023A: Ⱥ (UTF-8: C8 BA). Lowercase is ⱥ (UTF-8: E2 B1 A5), longer in bytes.
287 path := "/ȺȺȺȺshell.php.txt.php"
288 split := []string{".php"}
289
290 pos := Transport{SplitPath: split}.splitPos(path)
291
292 // The vulnerable code would return 22 (computed on lowercased string)
293 // The correct code should return 18 (position in original string)
294 expectedPos := strings.Index(path, ".php") + len(".php")
295 assert.Equal(t, expectedPos, pos, "split position should match first .php in original string")
296 assert.Equal(t, 18, pos, "split position should be 18, not 22")
297
298 if pos > 0 && pos <= len(path) {
299 scriptName := path[:pos]
300 pathInfo := path[pos:]
301
302 assert.Equal(t, "/ȺȺȺȺshell.php", scriptName, "script name should be the path up to first .php")
303 assert.Equal(t, ".txt.php", pathInfo, "path info should be the remainder after first .php")
304 }
305 }
306
307 // TestHeaderNameReplacer asserts the CGI header-to-env normalization rule:
308 // hyphens are mapped to underscores while every other character (including
309 // spaces) is passed through. Spaces are not RFC 7230 tokens, so they cannot
310 // reach this function from the wire; the only header names that survive
311 // untouched at the server layer are sanitized by the underscore filter in
312 // caddyhttp.Server.serveHTTP (see GHSA-f59h-q822-g45g).
313 func TestHeaderNameReplacer(t *testing.T) {
314 tests := []struct {
315 in, want string
316 }{
317 {"X-Forwarded-For", "X_Forwarded_For"},
318 {"Remote-User", "Remote_User"},
319 // Underscores are preserved (the server has already dropped any
320 // underscore-named headers when the filter is on).
321 {"Remote_User", "Remote_User"},
322 // Spaces are not rewritten because Go's HTTP parser rejects whitespace in
323 // header field names.
324 {"Foo Bar", "Foo Bar"},
325 }
326 for _, tt := range tests {
327 assert.Equal(t, tt.want, headerNameReplacer.Replace(tt.in), "input %q", tt.in)
328 }
329 }
330
331 // TestSplitPosSecurityRegressionUnicodeBypass guards against the FrankenPHP
332 // advisories GHSA-3g8v-8r37-cgjm (uninitialized match flag on inner non-ASCII
333 // byte) and GHSA-v4h7-cj44-8fc8 (Unicode equivalence via search.IgnoreCase
334 // folding fullwidth/mathematical/circled letters onto ASCII). Every payload
335 // below produced a false positive in the vulnerable implementation; none
336 // must match here.
337 func TestSplitPosSecurityRegressionUnicodeBypass(t *testing.T) {
338 t.Parallel()
339
340 tr := Transport{SplitPath: []string{".php"}}
341 payloads := []string{
342 "/PoC-match-unset.¡.txt", // GHSA-3g8v: stale match=true on IndexString fallback
343 "/shell﹒php", // U+FE52 small full stop
344 "/shell.php", // U+FF0E fullwidth full stop
345 "/shell.php", // U+FF50 fullwidth p
346 "/shell.php", // U+FF48 fullwidth h
347 "/shell.php", // U+FF50 fullwidth p (trailing)
348 "/shell.\U0001D5C1\U0001D5B5\U0001D5C1", // mathematical sans-serif p/h
349 "/shell.\U0001D5FD\U0001D5F5\U0001D5FD", // mathematical sans-serif bold p/h
350 "/shell.\U0001D4C5\U0001D4BD\U0001D4C5", // mathematical script p/h
351 "/shell.ⓟⓗⓟ", // circled latin small
352 }
353
354 for _, p := range payloads {
355 assert.Equalf(t, -1, tr.splitPos(p), "payload %q must not be detected as .php", p)
356 }
357 }