Files
compras/app/Services/StandaloneArtifact.php
T
nala 7da8d6c87a
tests / ci (push) Canceled after 0s
commit compras2 2911
2026-11-29 10:48:22 +02:00

91 lines
4.4 KiB
PHP

<?php
namespace App\Services;
use FilesystemIterator;
use LogicException;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
/** Verify every byte before an executable standalone package can be loaded. */
final class StandaloneArtifact
{
public const CONTRACT = 'COMPRAS-STANDALONE-v1';
public const PACKAGES = ['budget-control', 'purchasing', 'payables', 'general-ledger'];
/** @return array{root:string,manifest:array<string,mixed>,paths:array<string,string>} */
public function verify(string $directory, string $pin): array
{
$root = realpath($directory);
if ($root === false || is_link($directory) || ! preg_match('/^[a-f0-9]{64}$/D', $pin)) {
throw new LogicException('COMPRAS_RUNTIME_LOCATION');
}
$manifestFile = $root.'/manifest.json';
if (! is_file($manifestFile) || is_link($manifestFile)) {
throw new LogicException('COMPRAS_RUNTIME_MANIFEST');
}
$bytes = file_get_contents($manifestFile);
if (! is_string($bytes) || ! hash_equals($pin, hash('sha256', $bytes))) {
throw new LogicException('COMPRAS_RUNTIME_MANIFEST_HASH');
}
$manifest = json_decode($bytes, true, 64, JSON_THROW_ON_ERROR);
if (! is_array($manifest) || ($manifest['contract'] ?? null) !== self::CONTRACT
|| ($manifest['product'] ?? null) !== 'compras' || ($manifest['version'] ?? null) !== 1
|| ! is_array($manifest['files'] ?? null) || ! array_is_list($manifest['files']) || count($manifest['files']) < 4
|| ! is_array($manifest['capabilities'] ?? null) || ! array_is_list($manifest['capabilities'])
|| count(array_unique($manifest['capabilities'], SORT_REGULAR)) !== count($manifest['capabilities'])) {
throw new LogicException('COMPRAS_RUNTIME_CONTRACT');
}
foreach ($manifest['capabilities'] as $capability) {
if (! is_string($capability) || ! preg_match('/^[a-z][a-z0-9-]{1,63}$/D', $capability)) {
throw new LogicException('COMPRAS_RUNTIME_CAPABILITY');
}
}
$seen = ['manifest.json' => true];
foreach ($manifest['files'] as $entry) {
if (! is_array($entry)) {
throw new LogicException('COMPRAS_RUNTIME_FILE_IDENTITY');
}
$file = $entry['file'] ?? null;
$expected = $entry['sha256'] ?? null;
if (! is_string($file) || ! preg_match('~^[A-Za-z0-9_-]+(?:/[A-Za-z0-9_.-]+)+$~D', $file)
|| in_array('.', explode('/', $file), true) || in_array('..', explode('/', $file), true)
|| isset($seen[$file]) || ! is_string($expected) || ! preg_match('/^[a-f0-9]{64}$/D', $expected)) {
throw new LogicException('COMPRAS_RUNTIME_FILE_IDENTITY');
}
$actual = realpath($root.'/'.$file);
$actualHash = $actual !== false && is_file($actual) ? hash_file('sha256', $actual) : false;
if ($actual === false || ! str_starts_with($actual, $root.DIRECTORY_SEPARATOR)
|| ! is_file($actual) || is_link($root.'/'.$file) || ! is_string($actualHash) || ! hash_equals($expected, $actualHash)) {
throw new LogicException('COMPRAS_RUNTIME_FILE_HASH');
}
$seen[$file] = true;
}
$actualFiles = [];
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST) as $file) {
if ($file->isLink() || (! $file->isFile() && ! $file->isDir())) {
throw new LogicException('COMPRAS_RUNTIME_LINK');
}
if ($file->isFile()) {
$actualFiles[] = str_replace('\\', '/', substr($file->getPathname(), strlen($root) + 1));
}
}
$expectedFiles = array_keys($seen);
sort($actualFiles);
sort($expectedFiles);
if ($actualFiles !== $expectedFiles) {
throw new LogicException('COMPRAS_RUNTIME_FILE_SET');
}
$paths = [];
foreach (self::PACKAGES as $package) {
if (! isset($seen[$package.'/bootstrap.php'])) {
throw new LogicException('COMPRAS_RUNTIME_PACKAGE_MISSING');
}
$paths[$package] = $root.'/'.$package;
}
return ['root' => $root, 'manifest' => $manifest, 'paths' => $paths];
}
}