76 lines
4.1 KiB
PHP
76 lines
4.1 KiB
PHP
<?php
|
|
|
|
// Read-only release verification. No Laravel bootstrap, database or business commands.
|
|
$arguments = $argv ?? [];
|
|
$root = realpath($arguments[1] ?? '');
|
|
try {
|
|
if ($root === false || ! is_dir($root)) {
|
|
throw new LogicException('Compras release directory required.');
|
|
}
|
|
$bytes = file_get_contents($root.'/compras-release.json');
|
|
if (! is_string($bytes)) {
|
|
throw new LogicException('Compras release manifest missing.');
|
|
}
|
|
$manifest = json_decode($bytes, true, 512, JSON_THROW_ON_ERROR);
|
|
if (($manifest['product'] ?? null) !== 'compras' || ($manifest['contract'] ?? null) !== 'compras.release.v1') {
|
|
throw new LogicException('Unexpected product or release contract.');
|
|
}
|
|
$identity = $manifest;
|
|
unset($identity['release_id']);
|
|
if (! hash_equals($manifest['release_id'], 'compras-'.substr(hash('sha256', json_encode($identity, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR)), 0, 16))) {
|
|
throw new LogicException('Release identity mismatch.');
|
|
}
|
|
$verify = static function (string $base, string $file, string $digest, bool $normalise = false): void {
|
|
$contents = is_file($base.'/'.$file) ? file_get_contents($base.'/'.$file) : false;
|
|
$actual = is_string($contents) ? hash('sha256', $normalise ? str_replace("\r\n", "\n", $contents) : $contents) : false;
|
|
if ($file === '' || str_starts_with($file, '/') || str_contains($file, '..') || str_contains($file, '\\')
|
|
|| is_link($base.'/'.$file) || ! is_string($actual) || ! hash_equals($digest, $actual)) {
|
|
throw new LogicException('Release file mismatch: '.$file);
|
|
}
|
|
};
|
|
foreach ($manifest['source_files'] as $file => $digest) {
|
|
$verify($root, $file, $digest, ($manifest['source_eol'] ?? 'exact') === 'lf' && ! in_array($file, $manifest['exact_source_files'] ?? [], true));
|
|
}
|
|
$compiled = in_array('--compiled', $arguments, true) || in_array('--installed', $arguments, true);
|
|
if ($compiled) {
|
|
$verify($root, 'public/build/manifest.json', $manifest['frontend']['manifest_sha256']);
|
|
if (isset($manifest['frontend']['fonts_manifest_sha256'])) {
|
|
$verify($root, 'public/build/fonts-manifest.json', $manifest['frontend']['fonts_manifest_sha256']);
|
|
}
|
|
foreach ($manifest['frontend']['files'] as $file => $digest) {
|
|
$verify($root.'/public/build', $file, $digest);
|
|
}
|
|
}
|
|
if (in_array('--installed', $arguments, true)) {
|
|
$settings = parse_ini_file($root.'/.env', false, INI_SCANNER_RAW);
|
|
if (! is_array($settings) || ($settings['COMPRAS_RUNTIME_ENABLED'] ?? null) !== 'true') {
|
|
throw new LogicException('Integrated Compras runtime is not selected.');
|
|
}
|
|
foreach ($manifest['components'] as $component) {
|
|
$componentRoot = realpath($settings[$component['setting']] ?? '');
|
|
if ($componentRoot === false) {
|
|
throw new LogicException('Selected component unavailable.');
|
|
}
|
|
$verify($componentRoot, 'manifest.json', $component['manifest_sha256']);
|
|
$componentBytes = file_get_contents($componentRoot.'/manifest.json');
|
|
if (! is_string($componentBytes)) {
|
|
throw new LogicException('Component manifest unreadable.');
|
|
}
|
|
$componentManifest = json_decode($componentBytes, true, 512, JSON_THROW_ON_ERROR);
|
|
foreach ($componentManifest['files'] as $key => $entry) {
|
|
if (is_array($entry)) {
|
|
$verify($componentRoot, $entry['file'], $entry['sha256']);
|
|
} else {
|
|
$verify($componentRoot, $key, $entry);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
echo json_encode(['state' => 'passed', 'product' => 'compras', 'release_id' => $manifest['release_id'],
|
|
'source_files' => count($manifest['source_files']), 'asset_files' => $compiled ? count($manifest['frontend']['files']) : 0,
|
|
'installed_components' => in_array('--installed', $arguments, true) ? count($manifest['components']) : 0], JSON_THROW_ON_ERROR).PHP_EOL;
|
|
} catch (Throwable $error) {
|
|
fwrite(STDERR, $error->getMessage().PHP_EOL);
|
|
exit(1);
|
|
}
|