39 lines
2.0 KiB
PHP
39 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
/** Verify the separately selected release before any of its executable PHP is loaded. */
|
|
final class BudgetDistributionArtifact
|
|
{
|
|
private const FILES = ['bootstrap.php', 'src/BudgetContext.php', 'src/BudgetFailure.php', 'src/BudgetControl.php', 'src/BudgetDistributionPlan.php', 'src/BudgetDistributions.php', 'README.md'];
|
|
|
|
public static function verify(mixed $directory, mixed $manifest, mixed $pin): void
|
|
{
|
|
if (! is_string($directory) || ! is_string($manifest) || ! is_string($pin)
|
|
|| ! preg_match('/^[a-f0-9]{64}$/D', $pin) || is_link($directory) || is_link($manifest)) {
|
|
throw new \LogicException('BUDGET_DISTRIBUTION_ARTIFACT_CONFIGURATION');
|
|
}
|
|
$root = realpath($directory);
|
|
$bytes = is_file($manifest) ? file_get_contents($manifest) : false;
|
|
if ($root === false || ! is_string($bytes) || ! hash_equals($pin, hash('sha256', $bytes))) {
|
|
throw new \LogicException('BUDGET_DISTRIBUTION_MANIFEST_MISMATCH');
|
|
}
|
|
$data = json_decode($bytes, true, 32, JSON_THROW_ON_ERROR);
|
|
if (! is_array($data) || ! isset($data['files']) || ! is_array($data['files'])
|
|
|| array_keys($data['files']) !== self::FILES) {
|
|
throw new \LogicException('BUDGET_DISTRIBUTION_INVENTORY_MISMATCH');
|
|
}
|
|
foreach (self::FILES as $file) {
|
|
$path = $root.'/'.$file;
|
|
$expected = $data['files'][$file];
|
|
$resolved = realpath($path);
|
|
$actual = is_file($path) ? hash_file('sha256', $path) : false;
|
|
if (! is_string($expected) || ! preg_match('/^[a-f0-9]{64}$/D', $expected)
|
|
|| $resolved === false || ! str_starts_with(str_replace('\\', '/', $resolved), str_replace('\\', '/', $root).'/')
|
|
|| is_link($path) || ! is_string($actual) || ! hash_equals($expected, $actual)) {
|
|
throw new \LogicException('BUDGET_DISTRIBUTION_FILE_MISMATCH:'.$file);
|
|
}
|
|
}
|
|
}
|
|
}
|