|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter; |
| 15 | + |
| 16 | +use CodeIgniter\Test\CIUnitTestCase; |
| 17 | +use FilesystemIterator; |
| 18 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 19 | +use PHPUnit\Framework\Attributes\Group; |
| 20 | +use RecursiveDirectoryIterator; |
| 21 | +use RecursiveIteratorIterator; |
| 22 | + |
| 23 | +/** |
| 24 | + * Regression tests for system/rewrite.php path traversal (todo #36). |
| 25 | + * |
| 26 | + * Proves that the fixed rewrite.php does NOT serve files outside |
| 27 | + * DOCUMENT_ROOT via ../ or encoded traversal, while still serving |
| 28 | + * legitimate public assets. |
| 29 | + * |
| 30 | + * @internal |
| 31 | + */ |
| 32 | +#[Group('Others')] |
| 33 | +final class RewriteTest extends CIUnitTestCase |
| 34 | +{ |
| 35 | + private string $tmpRoot = ''; |
| 36 | + private string $docRoot = ''; |
| 37 | + |
| 38 | + protected function setUp(): void |
| 39 | + { |
| 40 | + parent::setUp(); |
| 41 | + |
| 42 | + $this->tmpRoot = sys_get_temp_dir() . '/ci4_rewrite_' . uniqid('', true); |
| 43 | + $this->docRoot = $this->tmpRoot . '/public'; |
| 44 | + |
| 45 | + mkdir($this->docRoot . '/assets', 0777, true); |
| 46 | + mkdir($this->tmpRoot . '/public2', 0777, true); |
| 47 | + |
| 48 | + // Legitimate public files |
| 49 | + file_put_contents($this->docRoot . '/index.php', '<?php echo "index";'); |
| 50 | + file_put_contents($this->docRoot . '/test.txt', 'hello'); |
| 51 | + file_put_contents($this->docRoot . '/assets/app.css', 'body{}'); |
| 52 | + |
| 53 | + // Files OUTSIDE docroot that must NOT be served |
| 54 | + file_put_contents($this->tmpRoot . '/outside.txt', 'secret'); |
| 55 | + file_put_contents($this->tmpRoot . '/public2/also.txt', 'also'); |
| 56 | + file_put_contents($this->docRoot . '/../outside2.txt', 'secret2'); |
| 57 | + } |
| 58 | + |
| 59 | + protected function tearDown(): void |
| 60 | + { |
| 61 | + parent::tearDown(); |
| 62 | + $this->removeDir($this->tmpRoot); |
| 63 | + } |
| 64 | + |
| 65 | + private function removeDir(string $dir): void |
| 66 | + { |
| 67 | + if (! is_dir($dir)) { |
| 68 | + return; |
| 69 | + } |
| 70 | + $it = new RecursiveIteratorIterator( |
| 71 | + new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS), |
| 72 | + RecursiveIteratorIterator::CHILD_FIRST, |
| 73 | + ); |
| 74 | + |
| 75 | + foreach ($it as $file) { |
| 76 | + $file->isDir() ? rmdir($file->getPathname()) : unlink($file->getPathname()); |
| 77 | + } |
| 78 | + rmdir($dir); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Mirrors the FIXED logic from system/rewrite.php:24-49. |
| 83 | + * Returns true if rewrite.php would "return false" (serve file directly). |
| 84 | + */ |
| 85 | + private function shouldServeFileFixed(string $requestUri, string $docRoot): bool |
| 86 | + { |
| 87 | + $uri = urldecode(parse_url('https://codeigniter.com' . $requestUri, PHP_URL_PATH) ?? ''); |
| 88 | + |
| 89 | + $path = $docRoot . DIRECTORY_SEPARATOR . ltrim($uri, '/'); |
| 90 | + |
| 91 | + $realPath = realpath($path); |
| 92 | + $realDocRoot = realpath($docRoot); |
| 93 | + |
| 94 | + return (bool) ( |
| 95 | + $uri !== '/' |
| 96 | + && $realPath !== false |
| 97 | + && $realDocRoot !== false |
| 98 | + && ($realPath === $realDocRoot || str_starts_with($realPath, $realDocRoot . DIRECTORY_SEPARATOR)) |
| 99 | + && (is_file($realPath) || is_dir($realPath)) |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * VULNERABLE logic before fix (for proof): no realpath prefix check. |
| 105 | + */ |
| 106 | + private function shouldServeFileVulnerable(string $requestUri, string $docRoot): bool |
| 107 | + { |
| 108 | + $uri = urldecode(parse_url('https://codeigniter.com' . $requestUri, PHP_URL_PATH) ?? ''); |
| 109 | + $path = $docRoot . DIRECTORY_SEPARATOR . ltrim($uri, '/'); |
| 110 | + |
| 111 | + return $uri !== '/' && (is_file($path) || is_dir($path)); |
| 112 | + } |
| 113 | + |
| 114 | + public function testServesLegitimatePublicFile(): void |
| 115 | + { |
| 116 | + $this->assertTrue($this->shouldServeFileFixed('/test.txt', $this->docRoot)); |
| 117 | + $this->assertTrue($this->shouldServeFileFixed('/assets/app.css', $this->docRoot)); |
| 118 | + $this->assertTrue($this->shouldServeFileFixed('/assets', $this->docRoot)); |
| 119 | + } |
| 120 | + |
| 121 | + public function testDoesNotServeRoot(): void |
| 122 | + { |
| 123 | + $this->assertFalse($this->shouldServeFileFixed('/', $this->docRoot)); |
| 124 | + } |
| 125 | + |
| 126 | + public function testDoesNotServeNonExistingFile(): void |
| 127 | + { |
| 128 | + $this->assertFalse($this->shouldServeFileFixed('/nope.txt', $this->docRoot)); |
| 129 | + } |
| 130 | + |
| 131 | + #[DataProvider('provideBlocksPathTraversalOutsideDocRoot')] |
| 132 | + public function testBlocksPathTraversalOutsideDocRoot(string $payload): void |
| 133 | + { |
| 134 | + // Fixed version must NOT serve |
| 135 | + $this->assertFalse( |
| 136 | + $this->shouldServeFileFixed($payload, $this->docRoot), |
| 137 | + 'Fixed rewrite.php should block traversal payload: ' . $payload, |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @return iterable<string, array{string}> |
| 143 | + */ |
| 144 | + public static function provideBlocksPathTraversalOutsideDocRoot(): iterable |
| 145 | + { |
| 146 | + yield 'plain dotdot' => ['/../outside.txt']; |
| 147 | + |
| 148 | + yield 'nested dotdot' => ['/../../outside.txt']; |
| 149 | + |
| 150 | + yield 'dotdot with subdir' => ['/assets/../../outside.txt']; |
| 151 | + |
| 152 | + yield 'encoded dotdot' => ['/%2e%2e/outside.txt']; |
| 153 | + |
| 154 | + yield 'double encoded' => ['/%252e%252e/outside.txt']; // becomes %2e after one urldecode -> safe |
| 155 | + |
| 156 | + yield 'sibling public2 file' => ['/../public2/also.txt']; |
| 157 | + |
| 158 | + yield 'sibling public2 dir' => ['/../public2']; |
| 159 | + |
| 160 | + yield 'traversal via subdir' => ['/assets/../../../outside.txt']; |
| 161 | + |
| 162 | + yield 'absolute outside2' => ['/../outside2.txt']; |
| 163 | + |
| 164 | + // Prefix bypass: docroot /public must not match /public2 |
| 165 | + yield 'prefix bypass' => ['/../public2/also.txt']; |
| 166 | + } |
| 167 | + |
| 168 | + public function testProvesVulnerabilityBeforeFix(): void |
| 169 | + { |
| 170 | + // This proves the OLD code WAS vulnerable: it would have served outside files |
| 171 | + $this->assertTrue( |
| 172 | + $this->shouldServeFileVulnerable('/../outside.txt', $this->docRoot), |
| 173 | + 'Vulnerable logic serves /../outside.txt outside docroot', |
| 174 | + ); |
| 175 | + $this->assertTrue( |
| 176 | + $this->shouldServeFileVulnerable('/%2e%2e/outside.txt', $this->docRoot), |
| 177 | + 'Vulnerable logic serves encoded traversal', |
| 178 | + ); |
| 179 | + $this->assertTrue( |
| 180 | + $this->shouldServeFileVulnerable('/../public2/also.txt', $this->docRoot), |
| 181 | + 'Vulnerable logic serves sibling docroot file', |
| 182 | + ); |
| 183 | + |
| 184 | + // Fixed logic blocks them |
| 185 | + $this->assertFalse($this->shouldServeFileFixed('/../outside.txt', $this->docRoot)); |
| 186 | + $this->assertFalse($this->shouldServeFileFixed('/%2e%2e/outside.txt', $this->docRoot)); |
| 187 | + $this->assertFalse($this->shouldServeFileFixed('/../public2/also.txt', $this->docRoot)); |
| 188 | + } |
| 189 | + |
| 190 | + public function testRewriteFileContainsTraversalProtection(): void |
| 191 | + { |
| 192 | + $content = (string) file_get_contents(SYSTEMPATH . 'rewrite.php'); |
| 193 | + |
| 194 | + // Must use realpath + prefix check |
| 195 | + $this->assertStringContainsString('realpath', $content, 'rewrite.php must use realpath()'); |
| 196 | + $this->assertStringContainsString('realpath($_SERVER', $content); |
| 197 | + $this->assertStringContainsString('str_starts_with', $content, 'rewrite.php must verify prefix'); |
| 198 | + $this->assertStringContainsString('realDocRoot', $content); |
| 199 | + $this->assertStringContainsString('DIRECTORY_SEPARATOR', $content); |
| 200 | + } |
| 201 | + |
| 202 | + public function testEncodedTraversalDecodedOnce(): void |
| 203 | + { |
| 204 | + // urldecode() in rewrite.php decodes %2e once; double encoding stays safe |
| 205 | + $uri = urldecode(parse_url('https://codeigniter.com/%252e%252e/outside.txt', PHP_URL_PATH) ?? ''); |
| 206 | + $this->assertSame('/%2e%2e/outside.txt', $uri, 'Double encoding decodes only once'); |
| 207 | + |
| 208 | + // Single encoding must decode to ../ |
| 209 | + $uri2 = urldecode(parse_url('https://codeigniter.com/%2e%2e/outside.txt', PHP_URL_PATH) ?? ''); |
| 210 | + $this->assertSame('/../outside.txt', $uri2); |
| 211 | + $this->assertFalse($this->shouldServeFileFixed('/%2e%2e/outside.txt', $this->docRoot)); |
| 212 | + } |
| 213 | +} |
0 commit comments