|
16 | 16 | use CodeIgniter\Test\CIUnitTestCase; |
17 | 17 | use CodeIgniter\Test\Mock\MockSecurity; |
18 | 18 | use Config\Security as SecurityConfig; |
| 19 | +use PHPUnit\Framework\Attributes\DataProvider; |
19 | 20 | use PHPUnit\Framework\Attributes\Group; |
20 | 21 | use Tests\Support\Config\Services; |
21 | 22 |
|
@@ -45,6 +46,161 @@ public function testSanitizeFilenameStripsExtras(): void |
45 | 46 | $this->assertSame('foo ', sanitize_filename($filename)); |
46 | 47 | } |
47 | 48 |
|
| 49 | + #[DataProvider('provideSanitizeFilenamePaths')] |
| 50 | + public function testSanitizeFilenamePaths(string $filename, bool $relativePath, string $expected): void |
| 51 | + { |
| 52 | + $this->assertSame($expected, sanitize_filename($filename, $relativePath)); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @return iterable<string, array{string, bool, string}> |
| 57 | + */ |
| 58 | + public static function provideSanitizeFilenamePaths(): iterable |
| 59 | + { |
| 60 | + $backslash = chr(92); |
| 61 | + $doubleBackslash = str_repeat($backslash, 2); |
| 62 | + $tripleBackslash = str_repeat($backslash, 3); |
| 63 | + $fourBackslashes = str_repeat($backslash, 4); |
| 64 | + $eightBackslashes = str_repeat($backslash, 8); |
| 65 | + |
| 66 | + return [ |
| 67 | + 'empty filename' => ['', false, ''], |
| 68 | + 'escaped NUL in filename' => ['a' . $backslash . '0b', false, 'a0b'], |
| 69 | + 'escaped NUL in relative path' => ['a' . $backslash . '0b', true, 'a0b'], |
| 70 | + 'literal NUL in filename' => ['a' . chr(0) . 'b', false, 'ab'], |
| 71 | + 'literal NUL in relative path' => ['a' . chr(0) . 'b', true, 'ab'], |
| 72 | + 'strip forward path' => [ |
| 73 | + 'approved/folder/file.txt', |
| 74 | + false, |
| 75 | + 'approvedfolderfile.txt', |
| 76 | + ], |
| 77 | + 'preserve forward relative path' => [ |
| 78 | + 'approved/folder/file.txt', |
| 79 | + true, |
| 80 | + 'approved/folder/file.txt', |
| 81 | + ], |
| 82 | + 'strip forward traversal' => ['../../public/file.txt', false, 'publicfile.txt'], |
| 83 | + 'relative forward traversal' => ['../../public/file.txt', true, 'public/file.txt'], |
| 84 | + 'forward traversal exposed by filters' => ['.<.>/public/file.txt', true, 'public/file.txt'], |
| 85 | + 'forward traversal split by backslash' => ['.' . $backslash . './file.txt', true, 'file.txt'], |
| 86 | + 'backslash traversal split by backslash' => [ |
| 87 | + '.' . $backslash . '.' . $doubleBackslash . 'file.txt', |
| 88 | + true, |
| 89 | + '..file.txt', |
| 90 | + ], |
| 91 | + 'mixed separators' => [ |
| 92 | + '..' . $doubleBackslash . '../public' . $doubleBackslash . 'file.txt', |
| 93 | + true, |
| 94 | + '..publicfile.txt', |
| 95 | + ], |
| 96 | + 'backslashes exposed by filters' => [ |
| 97 | + 'folder' . $backslash . '<' . $backslash . 'file.txt', |
| 98 | + true, |
| 99 | + 'folderfile.txt', |
| 100 | + ], |
| 101 | + 'filename traversal with single backslash' => [ |
| 102 | + '..' . $backslash . '..' . $backslash . 'public' . $backslash . 'file.txt', |
| 103 | + false, |
| 104 | + '....publicfile.txt', |
| 105 | + ], |
| 106 | + 'filename path with single backslash' => [ |
| 107 | + 'folder' . $backslash . 'file.txt', |
| 108 | + false, |
| 109 | + 'folderfile.txt', |
| 110 | + ], |
| 111 | + 'relative traversal with single backslash' => [ |
| 112 | + '..' . $backslash . '..' . $backslash . 'public' . $backslash . 'file.txt', |
| 113 | + true, |
| 114 | + '....publicfile.txt', |
| 115 | + ], |
| 116 | + 'relative path with single backslash' => [ |
| 117 | + 'folder' . $backslash . 'file.txt', |
| 118 | + true, |
| 119 | + 'folderfile.txt', |
| 120 | + ], |
| 121 | + 'filename traversal with double backslashes' => [ |
| 122 | + '..' . $doubleBackslash . '..' . $doubleBackslash . 'public' . $doubleBackslash . 'file.txt', |
| 123 | + false, |
| 124 | + '....publicfile.txt', |
| 125 | + ], |
| 126 | + 'filename path with double backslashes' => [ |
| 127 | + 'folder' . $doubleBackslash . 'file.txt', |
| 128 | + false, |
| 129 | + 'folderfile.txt', |
| 130 | + ], |
| 131 | + 'relative traversal with double backslashes' => [ |
| 132 | + '..' . $doubleBackslash . '..' . $doubleBackslash . 'public' . $doubleBackslash . 'file.txt', |
| 133 | + true, |
| 134 | + '....publicfile.txt', |
| 135 | + ], |
| 136 | + 'relative path with double backslashes' => [ |
| 137 | + 'folder' . $doubleBackslash . 'file.txt', |
| 138 | + true, |
| 139 | + 'folderfile.txt', |
| 140 | + ], |
| 141 | + 'filename traversal with triple backslashes' => [ |
| 142 | + '..' . $tripleBackslash . '..' . $tripleBackslash . 'public' . $tripleBackslash . 'file.txt', |
| 143 | + false, |
| 144 | + '....publicfile.txt', |
| 145 | + ], |
| 146 | + 'filename path with triple backslashes' => [ |
| 147 | + 'folder' . $tripleBackslash . 'file.txt', |
| 148 | + false, |
| 149 | + 'folderfile.txt', |
| 150 | + ], |
| 151 | + 'relative traversal with triple backslashes' => [ |
| 152 | + '..' . $tripleBackslash . '..' . $tripleBackslash . 'public' . $tripleBackslash . 'file.txt', |
| 153 | + true, |
| 154 | + '....publicfile.txt', |
| 155 | + ], |
| 156 | + 'relative path with triple backslashes' => [ |
| 157 | + 'folder' . $tripleBackslash . 'file.txt', |
| 158 | + true, |
| 159 | + 'folderfile.txt', |
| 160 | + ], |
| 161 | + 'filename traversal with four backslashes' => [ |
| 162 | + '..' . $fourBackslashes . '..' . $fourBackslashes . 'public' . $fourBackslashes . 'file.txt', |
| 163 | + false, |
| 164 | + '....publicfile.txt', |
| 165 | + ], |
| 166 | + 'filename path with four backslashes' => [ |
| 167 | + 'folder' . $fourBackslashes . 'file.txt', |
| 168 | + false, |
| 169 | + 'folderfile.txt', |
| 170 | + ], |
| 171 | + 'relative traversal with four backslashes' => [ |
| 172 | + '..' . $fourBackslashes . '..' . $fourBackslashes . 'public' . $fourBackslashes . 'file.txt', |
| 173 | + true, |
| 174 | + '....publicfile.txt', |
| 175 | + ], |
| 176 | + 'relative path with four backslashes' => [ |
| 177 | + 'folder' . $fourBackslashes . 'file.txt', |
| 178 | + true, |
| 179 | + 'folderfile.txt', |
| 180 | + ], |
| 181 | + 'filename traversal with eight backslashes' => [ |
| 182 | + '..' . $eightBackslashes . '..' . $eightBackslashes . 'public' . $eightBackslashes . 'file.txt', |
| 183 | + false, |
| 184 | + '....publicfile.txt', |
| 185 | + ], |
| 186 | + 'filename path with eight backslashes' => [ |
| 187 | + 'folder' . $eightBackslashes . 'file.txt', |
| 188 | + false, |
| 189 | + 'folderfile.txt', |
| 190 | + ], |
| 191 | + 'relative traversal with eight backslashes' => [ |
| 192 | + '..' . $eightBackslashes . '..' . $eightBackslashes . 'public' . $eightBackslashes . 'file.txt', |
| 193 | + true, |
| 194 | + '....publicfile.txt', |
| 195 | + ], |
| 196 | + 'relative path with eight backslashes' => [ |
| 197 | + 'folder' . $eightBackslashes . 'file.txt', |
| 198 | + true, |
| 199 | + 'folderfile.txt', |
| 200 | + ], |
| 201 | + ]; |
| 202 | + } |
| 203 | + |
48 | 204 | public function testStripImageTags(): void |
49 | 205 | { |
50 | 206 | $this->assertSame('http://example.com/spacer.gif', strip_image_tags('http://example.com/spacer.gif')); |
|
0 commit comments