78 lines
3.8 KiB
PHP
78 lines
3.8 KiB
PHP
<?php
|
|
|
|
/** Read-only comparison: an accepted old bundle is not evidence of current workflow equivalence. */
|
|
final class SgfWorkflowInventory
|
|
{
|
|
public static function inspect(string $source, string $bundle, string $manifestHash, array $processes): array
|
|
{
|
|
if (! preg_match('/^[a-f0-9]{64}$/D', $manifestHash)) {
|
|
throw new RuntimeException('Manifest hash required.');
|
|
}
|
|
$manifestPath = self::file($bundle, 'manifest.json');
|
|
if (! hash_equals($manifestHash, hash_file('sha256', $manifestPath))) {
|
|
throw new RuntimeException('Unrecognised workflow manifest.');
|
|
}
|
|
$manifest = json_decode(file_get_contents($manifestPath), true, 64, JSON_THROW_ON_ERROR);
|
|
if (! is_array($manifest['files'] ?? null) || ! is_array($manifest['originals'] ?? null)) {
|
|
throw new RuntimeException('Original and distributed hashes required.');
|
|
}
|
|
// Check the whole supplied bundle, including dependencies outside the process selection.
|
|
foreach ($manifest['files'] as $path => $hash) {
|
|
if (! is_string($hash) || ! preg_match('/^[a-f0-9]{64}$/D', $hash)
|
|
|| ! hash_equals($hash, hash_file('sha256', self::file($bundle, $path)))) {
|
|
throw new RuntimeException('Workflow bundle integrity failure.');
|
|
}
|
|
}
|
|
$result = [];
|
|
foreach ($processes as $process => $paths) {
|
|
$components = [];
|
|
foreach ($paths as $path) {
|
|
self::relative($path);
|
|
$current = is_file($source.'/'.$path) ? hash_file('sha256', self::file($source, $path)) : null;
|
|
$original = $manifest['originals'][$path] ?? null;
|
|
if ($original !== null && (! is_string($original) || ! preg_match('/^[a-f0-9]{64}$/D', $original))) {
|
|
throw new RuntimeException('Invalid original source hash.');
|
|
}
|
|
$status = $current === null ? 'source_missing' : ($original === null || ! isset($manifest['files'][$path])
|
|
? 'not_transferred' : (hash_equals($original, $current) ? 'source_unchanged' : 'source_changed'));
|
|
$components[$path] = ['status' => $status, 'source_sha256' => $current, 'accepted_source_sha256' => $original];
|
|
}
|
|
$result[$process] = ['source_equivalent' => $components !== [] && count(array_filter($components, fn ($entry) => $entry['status'] !== 'source_unchanged')) === 0, 'components' => $components];
|
|
}
|
|
|
|
return ['format' => 'tempo.sgf-workflow-inventory.v1', 'manifest_sha256' => $manifestHash,
|
|
'bundle_integrity' => true, 'read_only' => true, 'functional_acceptance' => false, 'processes' => $result];
|
|
}
|
|
|
|
private static function relative(string $path): void
|
|
{
|
|
if (! preg_match('#^[A-Za-z0-9_.-]+(?:/[A-Za-z0-9_.-]+)*$#D', $path)
|
|
|| in_array('..', explode('/', $path), true) || in_array('.', explode('/', $path), true)) {
|
|
throw new RuntimeException('Invalid workflow path.');
|
|
}
|
|
}
|
|
|
|
private static function file(string $root, string $path): string
|
|
{
|
|
self::relative($path);
|
|
if (is_link($root)) {
|
|
throw new RuntimeException('Workflow links are forbidden.');
|
|
}
|
|
$base = realpath($root);
|
|
$file = realpath($root.'/'.$path);
|
|
$cursor = $root;
|
|
foreach (explode('/', $path) as $part) {
|
|
$cursor .= '/'.$part;
|
|
if (is_link($cursor)) {
|
|
throw new RuntimeException('Workflow links are forbidden.');
|
|
}
|
|
}
|
|
if (! $base || ! $file || ! is_file($file)
|
|
|| ! str_starts_with(strtolower($file), strtolower($base.DIRECTORY_SEPARATOR))) {
|
|
throw new RuntimeException('Workflow file outside the supplied root or missing.');
|
|
}
|
|
|
|
return $file;
|
|
}
|
|
}
|