@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
final class SgfWorkflowExtension
|
||||
{
|
||||
public static function transform(string $php): string
|
||||
{
|
||||
$result = '';
|
||||
foreach (token_get_all($php, TOKEN_PARSE) as $token) {
|
||||
if (! is_array($token)) {
|
||||
$result .= $token;
|
||||
|
||||
continue;
|
||||
}
|
||||
[$type, $text] = $token;
|
||||
if (in_array($type, [T_NAME_QUALIFIED, T_NAME_FULLY_QUALIFIED], true)) {
|
||||
$text = preg_replace('/^(\\\\?)App\\\\/', '$1Tempo\\Purchasing\\Embedded\\', $text);
|
||||
}
|
||||
$result .= $text;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function prepare(string $source, string $base, string $baseHash, string $output, array $updates): array
|
||||
{
|
||||
if (! preg_match('/^[a-f0-9]{64}$/D', $baseHash) || is_link($base) || is_link($output)
|
||||
|| ! hash_equals($baseHash, hash_file('sha256', $base.'/manifest.json'))) {
|
||||
throw new LogicException('Workflow predecessor mismatch.');
|
||||
}
|
||||
$manifest = json_decode(file_get_contents($base.'/manifest.json'), true, 64, JSON_THROW_ON_ERROR);
|
||||
$bodies = [];
|
||||
foreach ($manifest['files'] as $path => $hash) {
|
||||
self::path($path);
|
||||
if (is_link($base.'/'.$path) || ! hash_equals($hash, hash_file('sha256', $base.'/'.$path))) {
|
||||
throw new LogicException('Workflow predecessor file mismatch.');
|
||||
}
|
||||
$bodies[$path] = file_get_contents($base.'/'.$path);
|
||||
}
|
||||
foreach ($updates as $path) {
|
||||
self::path($path);
|
||||
if (! str_starts_with($path, 'app/') || is_link($source.'/'.$path) || ! is_file($source.'/'.$path)) {
|
||||
throw new LogicException('Canonical workflow source missing.');
|
||||
}
|
||||
$bytes = file_get_contents($source.'/'.$path);
|
||||
$manifest['originals'][$path] = hash('sha256', $bytes);
|
||||
$bodies[$path] = self::transform($bytes);
|
||||
}
|
||||
ksort($bodies);
|
||||
$manifest['files'] = array_map(fn ($bytes) => hash('sha256', $bytes), $bodies);
|
||||
$manifest['predecessor_sha256'] = $baseHash;
|
||||
$manifest['extension_contract'] = 'COMPRAS-SGF-EXECUTION-v1';
|
||||
$bytes = json_encode($manifest, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR)."\n";
|
||||
$hash = hash('sha256', $bytes);
|
||||
$directory = $output.'/'.substr($hash, 0, 16).'/bundle';
|
||||
foreach ($bodies + ['manifest.json' => $bytes] as $path => $body) {
|
||||
$file = $directory.'/'.$path;
|
||||
if (is_link($file) || (is_file($file) && ! hash_equals(hash('sha256', $body), hash_file('sha256', $file)))) {
|
||||
throw new LogicException('Immutable workflow candidate differs.');
|
||||
}
|
||||
if (! is_dir(dirname($file))) {
|
||||
mkdir(dirname($file), 0700, true);
|
||||
}
|
||||
if (! is_file($file)) {
|
||||
file_put_contents($file, $body);
|
||||
}
|
||||
if (! hash_equals(hash('sha256', $body), hash_file('sha256', $file))) {
|
||||
throw new LogicException('Incomplete workflow candidate.');
|
||||
}
|
||||
}
|
||||
|
||||
return ['directory' => $directory, 'sha256' => $hash, 'predecessor_sha256' => $baseHash, 'updated_sources' => array_intersect_key($manifest['originals'], array_flip($updates))];
|
||||
}
|
||||
|
||||
private static function path(string $path): void
|
||||
{
|
||||
if (! preg_match('~^(app|fortify)/[A-Za-z0-9_/]+\.php$~D', $path)) {
|
||||
throw new LogicException('Unsafe workflow path.');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user