|
1 | 1 | package frankenphp |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "strings" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "github.com/stretchr/testify/assert" |
@@ -31,3 +32,179 @@ func TestEnsureLeadingSlash(t *testing.T) { |
31 | 32 | }) |
32 | 33 | } |
33 | 34 | } |
| 35 | + |
| 36 | +func TestSplitPos(t *testing.T) { |
| 37 | + tests := []struct { |
| 38 | + name string |
| 39 | + path string |
| 40 | + splitPath []string |
| 41 | + wantPos int |
| 42 | + }{ |
| 43 | + { |
| 44 | + name: "simple php extension", |
| 45 | + path: "/path/to/script.php", |
| 46 | + splitPath: []string{".php"}, |
| 47 | + wantPos: 19, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "php extension with path info", |
| 51 | + path: "/path/to/script.php/some/path", |
| 52 | + splitPath: []string{".php"}, |
| 53 | + wantPos: 19, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "case insensitive match", |
| 57 | + path: "/path/to/script.PHP", |
| 58 | + splitPath: []string{".php"}, |
| 59 | + wantPos: 19, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "mixed case match", |
| 63 | + path: "/path/to/script.PhP/info", |
| 64 | + splitPath: []string{".php"}, |
| 65 | + wantPos: 19, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "no match", |
| 69 | + path: "/path/to/script.txt", |
| 70 | + splitPath: []string{".php"}, |
| 71 | + wantPos: -1, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "empty split path", |
| 75 | + path: "/path/to/script.php", |
| 76 | + splitPath: []string{}, |
| 77 | + wantPos: 0, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "multiple split paths first match", |
| 81 | + path: "/path/to/script.php", |
| 82 | + splitPath: []string{".php", ".phtml"}, |
| 83 | + wantPos: 19, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "multiple split paths second match", |
| 87 | + path: "/path/to/script.phtml", |
| 88 | + splitPath: []string{".php", ".phtml"}, |
| 89 | + wantPos: 21, |
| 90 | + }, |
| 91 | + // Unicode case-folding tests (security fix for GHSA-g966-83w7-6w38) |
| 92 | + // U+023A (Ⱥ) lowercases to U+2C65 (ⱥ), which has different UTF-8 byte length |
| 93 | + // Ⱥ: 2 bytes (C8 BA), ⱥ: 3 bytes (E2 B1 A5) |
| 94 | + { |
| 95 | + name: "unicode path with case-folding length expansion", |
| 96 | + path: "/ȺȺȺȺshell.php", |
| 97 | + splitPath: []string{".php"}, |
| 98 | + wantPos: 18, // correct position in original string |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "unicode path with extension after expansion chars", |
| 102 | + path: "/ȺȺȺȺshell.php/path/info", |
| 103 | + splitPath: []string{".php"}, |
| 104 | + wantPos: 18, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "unicode in filename with multiple php occurrences", |
| 108 | + path: "/ȺȺȺȺshell.php.txt.php", |
| 109 | + splitPath: []string{".php"}, |
| 110 | + wantPos: 18, // should match first .php, not be confused by byte offset shift |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "unicode case insensitive extension", |
| 114 | + path: "/ȺȺȺȺshell.PHP", |
| 115 | + splitPath: []string{".php"}, |
| 116 | + wantPos: 18, |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "unicode in middle of path", |
| 120 | + path: "/path/Ⱥtest/script.php", |
| 121 | + splitPath: []string{".php"}, |
| 122 | + wantPos: 23, // Ⱥ is 2 bytes, so path is 23 bytes total, .php ends at byte 23 |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "unicode only in directory not filename", |
| 126 | + path: "/Ⱥ/script.php", |
| 127 | + splitPath: []string{".php"}, |
| 128 | + wantPos: 14, |
| 129 | + }, |
| 130 | + // Additional Unicode characters that expand when lowercased |
| 131 | + // U+0130 (İ - Turkish capital I with dot) lowercases to U+0069 + U+0307 |
| 132 | + { |
| 133 | + name: "turkish capital I with dot", |
| 134 | + path: "/İtest.php", |
| 135 | + splitPath: []string{".php"}, |
| 136 | + wantPos: 11, |
| 137 | + }, |
| 138 | + // Ensure standard ASCII still works correctly |
| 139 | + { |
| 140 | + name: "ascii only path with case variation", |
| 141 | + path: "/PATH/TO/SCRIPT.PHP/INFO", |
| 142 | + splitPath: []string{".php"}, |
| 143 | + wantPos: 19, |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "path at root", |
| 147 | + path: "/index.php", |
| 148 | + splitPath: []string{".php"}, |
| 149 | + wantPos: 10, |
| 150 | + }, |
| 151 | + { |
| 152 | + name: "extension in middle of filename", |
| 153 | + path: "/test.php.bak", |
| 154 | + splitPath: []string{".php"}, |
| 155 | + wantPos: 9, |
| 156 | + }, |
| 157 | + } |
| 158 | + |
| 159 | + for _, tt := range tests { |
| 160 | + t.Run(tt.name, func(t *testing.T) { |
| 161 | + gotPos := splitPos(tt.path, tt.splitPath) |
| 162 | + assert.Equal(t, tt.wantPos, gotPos, "splitPos(%q, %v)", tt.path, tt.splitPath) |
| 163 | + |
| 164 | + // Verify that the split produces valid substrings |
| 165 | + if gotPos > 0 && gotPos <= len(tt.path) { |
| 166 | + scriptName := tt.path[:gotPos] |
| 167 | + pathInfo := tt.path[gotPos:] |
| 168 | + |
| 169 | + // The script name should end with one of the split extensions (case-insensitive) |
| 170 | + hasValidEnding := false |
| 171 | + for _, split := range tt.splitPath { |
| 172 | + if strings.HasSuffix(strings.ToLower(scriptName), split) { |
| 173 | + hasValidEnding = true |
| 174 | + |
| 175 | + break |
| 176 | + } |
| 177 | + } |
| 178 | + assert.True(t, hasValidEnding, "script name %q should end with one of %v", scriptName, tt.splitPath) |
| 179 | + |
| 180 | + // Original path should be reconstructable |
| 181 | + assert.Equal(t, tt.path, scriptName+pathInfo, "path should be reconstructable from split parts") |
| 182 | + } |
| 183 | + }) |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +// TestSplitPosUnicodeSecurityRegression specifically tests the vulnerability |
| 188 | +// described in GHSA-g966-83w7-6w38 where Unicode case-folding caused |
| 189 | +// incorrect SCRIPT_NAME/PATH_INFO splitting |
| 190 | +func TestSplitPosUnicodeSecurityRegression(t *testing.T) { |
| 191 | + // U+023A: Ⱥ (UTF-8: C8 BA). Lowercase is ⱥ (UTF-8: E2 B1 A5), longer in bytes. |
| 192 | + path := "/ȺȺȺȺshell.php.txt.php" |
| 193 | + split := []string{".php"} |
| 194 | + |
| 195 | + pos := splitPos(path, split) |
| 196 | + |
| 197 | + // The vulnerable code would return 22 (computed on lowercased string) |
| 198 | + // The correct code should return 18 (position in original string) |
| 199 | + expectedPos := strings.Index(path, ".php") + len(".php") |
| 200 | + assert.Equal(t, expectedPos, pos, "split position should match first .php in original string") |
| 201 | + assert.Equal(t, 18, pos, "split position should be 18, not 22") |
| 202 | + |
| 203 | + if pos > 0 && pos <= len(path) { |
| 204 | + scriptName := path[:pos] |
| 205 | + pathInfo := path[pos:] |
| 206 | + |
| 207 | + assert.Equal(t, "/ȺȺȺȺshell.php", scriptName, "script name should be the path up to first .php") |
| 208 | + assert.Equal(t, ".txt.php", pathInfo, "path info should be the remainder after first .php") |
| 209 | + } |
| 210 | +} |
0 commit comments