dir = sys_get_temp_dir().'/inhaus-zip-'.bin2hex(random_bytes(4)); mkdir($this->dir); } protected function tearDown(): void { Filesystem::deleteDirectory($this->dir); parent::tearDown(); } /** * @param array $files */ private function zip(array $files): string { $path = $this->dir.'/archiv.zip'; $zip = new ZipArchive; $zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE); foreach ($files as $name => $contents) { $zip->addFromString($name, $contents); } $zip->close(); return $path; } public function test_common_top_level_folder_is_stripped(): void { $zip = $this->zip([ 'repo-abc/app/Version.php' => ' 'Hallo', ]); $count = (new ZipExtractor)->extract($zip, $this->dir.'/staging'); $this->assertSame(2, $count); $this->assertFileExists($this->dir.'/staging/app/Version.php'); $this->assertFileExists($this->dir.'/staging/README.md'); } public function test_zip_slip_creates_nothing_outside_staging(): void { $zip = $this->zip([ 'repo-abc/ok.txt' => 'ok', 'repo-abc/../../evil.php' => 'extract($zip, $this->dir.'/staging'); $this->fail('Das Archiv hätte abgelehnt werden müssen.'); } catch (RuntimeException $e) { $this->assertStringContainsString('Unsicherer Pfad', $e->getMessage()); } $this->assertFileDoesNotExist($this->dir.'/evil.php'); $this->assertFileDoesNotExist(dirname($this->dir).'/evil.php'); $this->assertFileDoesNotExist($this->dir.'/staging/../evil.php'); } public function test_dangerous_entry_names_are_rejected(): void { foreach (['../evil', 'a/../../evil', '/etc/passwd', 'C:\\Windows\\evil.dll', "a\0b", '..\\evil'] as $name) { $this->assertFalse(ZipExtractor::isSafeEntryName($name), $name); } foreach (['app/Version.php', 'repo-abc/public/.htaccess', 'a/..b/c'] as $name) { $this->assertTrue(ZipExtractor::isSafeEntryName($name), $name); } } public function test_protected_paths_are_skipped_and_nothing_is_deleted(): void { $staging = $this->dir.'/staging'; $target = $this->dir.'/target'; foreach ([ 'app/Neu.php' => 'neu', 'app/Version.php' => 'neue Version', '.env' => 'APP_KEY=aus-dem-archiv', 'storage/app/license.json' => '{}', 'public/uploads/bild.jpg' => 'fremd', 'plugins/kunde/plugin.json' => '{}', 'vendor/autoload.php' => 'neu', ] as $file => $contents) { @mkdir(dirname("{$staging}/{$file}"), 0777, true); file_put_contents("{$staging}/{$file}", $contents); } foreach ([ 'app/Version.php' => 'alte Version', 'app/Alt.php' => 'bleibt', '.env' => 'APP_KEY=geheim', 'storage/app/license.json' => '{"email":"lizenz@example.com"}', ] as $file => $contents) { @mkdir(dirname("{$target}/{$file}"), 0777, true); file_put_contents("{$target}/{$file}", $contents); } $result = (new StagingApplier(new ProtectedPaths(config('updater.protected_paths'))))->apply($staging, $target); $this->assertSame(['copied' => 3, 'skipped' => 4], $result); $this->assertSame('neue Version', file_get_contents("{$target}/app/Version.php")); $this->assertSame('neu', file_get_contents("{$target}/app/Neu.php")); $this->assertSame('neu', file_get_contents("{$target}/vendor/autoload.php")); $this->assertSame('bleibt', file_get_contents("{$target}/app/Alt.php")); $this->assertSame('APP_KEY=geheim', file_get_contents("{$target}/.env")); $this->assertSame('{"email":"lizenz@example.com"}', file_get_contents("{$target}/storage/app/license.json")); $this->assertFileDoesNotExist("{$target}/public/uploads/bild.jpg"); $this->assertFileDoesNotExist("{$target}/plugins/kunde/plugin.json"); $this->assertEmpty(glob("{$target}/app/*.update-*")); } public function test_protected_path_matching(): void { $paths = new ProtectedPaths(['.env', 'storage/', 'config/local.php']); $this->assertTrue($paths->isProtected('.env')); $this->assertTrue($paths->isProtected('storage')); $this->assertTrue($paths->isProtected('storage/logs/x.log')); $this->assertTrue($paths->isProtected('config/local.php')); $this->assertFalse($paths->isProtected('.env.example')); $this->assertFalse($paths->isProtected('storagebox/a')); $this->assertFalse($paths->isProtected('config/app.php')); } }