|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OCA\Butterfly\Service; |
| 6 | + |
| 7 | +final class BundleValidator { |
| 8 | + public const MINIMUM_BUILD_NUMBER = 193; |
| 9 | + |
| 10 | + private const MAX_ENTRIES = 10000; |
| 11 | + private const MAX_UNCOMPRESSED_SIZE = 536870912; |
| 12 | + |
| 13 | + /** |
| 14 | + * @return array{root: string, packageName: string, buildNumber: string} |
| 15 | + */ |
| 16 | + public function validate(\ZipArchive $archive): array { |
| 17 | + if ($archive->numFiles < 1 || $archive->numFiles > self::MAX_ENTRIES) { |
| 18 | + throw new BundleValidationException('The ZIP contains an unsupported number of files.'); |
| 19 | + } |
| 20 | + |
| 21 | + $versionFiles = []; |
| 22 | + $seenNames = []; |
| 23 | + $totalSize = 0; |
| 24 | + for ($index = 0; $index < $archive->numFiles; $index++) { |
| 25 | + $stat = $archive->statIndex($index); |
| 26 | + if (!is_array($stat) || !isset($stat['name'], $stat['size'])) { |
| 27 | + throw new BundleValidationException('The ZIP contains an unreadable entry.'); |
| 28 | + } |
| 29 | + |
| 30 | + $name = $this->normalizeEntryName((string)$stat['name']); |
| 31 | + if (isset($seenNames[$name])) { |
| 32 | + throw new BundleValidationException('The ZIP contains duplicate paths.'); |
| 33 | + } |
| 34 | + $seenNames[$name] = true; |
| 35 | + $totalSize += (int)$stat['size']; |
| 36 | + if ($totalSize > self::MAX_UNCOMPRESSED_SIZE) { |
| 37 | + throw new BundleValidationException('The uncompressed ZIP is larger than 512 MiB.'); |
| 38 | + } |
| 39 | + if ($this->isSymbolicLink($archive, $index)) { |
| 40 | + throw new BundleValidationException('Symbolic links are not allowed in the ZIP.'); |
| 41 | + } |
| 42 | + if (basename($name) === 'version.json') { |
| 43 | + $versionFiles[] = ['index' => $index, 'path' => $name]; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if (count($versionFiles) !== 1) { |
| 48 | + throw new BundleValidationException('The ZIP must contain exactly one version.json.'); |
| 49 | + } |
| 50 | + |
| 51 | + $versionFile = $versionFiles[0]; |
| 52 | + $content = $archive->getFromIndex($versionFile['index']); |
| 53 | + if (!is_string($content)) { |
| 54 | + throw new BundleValidationException('version.json could not be read.'); |
| 55 | + } |
| 56 | + |
| 57 | + try { |
| 58 | + /** @var mixed $decoded */ |
| 59 | + $decoded = json_decode($content, true, 16, JSON_THROW_ON_ERROR); |
| 60 | + } catch (\JsonException $exception) { |
| 61 | + throw new BundleValidationException('version.json is not valid JSON.', 0, $exception); |
| 62 | + } |
| 63 | + |
| 64 | + if (!is_array($decoded) || ($decoded['package_name'] ?? null) !== 'butterfly') { |
| 65 | + throw new BundleValidationException('version.json must have package_name set to "butterfly".'); |
| 66 | + } |
| 67 | + |
| 68 | + $buildNumber = $decoded['build_number'] ?? null; |
| 69 | + if ( |
| 70 | + !is_string($buildNumber) |
| 71 | + || !ctype_digit($buildNumber) |
| 72 | + || (int)$buildNumber < self::MINIMUM_BUILD_NUMBER |
| 73 | + ) { |
| 74 | + throw new BundleValidationException('version.json must have a string build_number of "193" or higher.'); |
| 75 | + } |
| 76 | + |
| 77 | + $root = dirname($versionFile['path']); |
| 78 | + $root = $root === '.' ? '' : $root . '/'; |
| 79 | + if ($archive->locateName($root . 'index.html') === false) { |
| 80 | + throw new BundleValidationException('The ZIP must contain index.html next to version.json.'); |
| 81 | + } |
| 82 | + |
| 83 | + return [ |
| 84 | + 'root' => $root, |
| 85 | + 'packageName' => 'butterfly', |
| 86 | + 'buildNumber' => $buildNumber, |
| 87 | + ]; |
| 88 | + } |
| 89 | + |
| 90 | + public function normalizeEntryName(string $name): string { |
| 91 | + if ($name === '' || preg_match('/[\x00-\x1f\x7f]/', $name) === 1 || str_contains($name, '\\')) { |
| 92 | + throw new BundleValidationException('The ZIP contains an invalid path.'); |
| 93 | + } |
| 94 | + |
| 95 | + if (str_starts_with($name, '/')) { |
| 96 | + throw new BundleValidationException('The ZIP contains an unsafe path.'); |
| 97 | + } |
| 98 | + $parts = explode('/', rtrim($name, '/')); |
| 99 | + if (in_array('', $parts, true) || in_array('.', $parts, true) || in_array('..', $parts, true)) { |
| 100 | + throw new BundleValidationException('The ZIP contains an unsafe path.'); |
| 101 | + } |
| 102 | + |
| 103 | + return implode('/', $parts) . (str_ends_with($name, '/') ? '/' : ''); |
| 104 | + } |
| 105 | + |
| 106 | + private function isSymbolicLink(\ZipArchive $archive, int $index): bool { |
| 107 | + $attributes = 0; |
| 108 | + $operationsSystem = 0; |
| 109 | + if (!$archive->getExternalAttributesIndex($index, $operationsSystem, $attributes)) { |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + return (($attributes >> 16) & 0170000) === 0120000; |
| 114 | + } |
| 115 | +} |
0 commit comments