$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; } }