|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +function out(string $message): void |
| 6 | +{ |
| 7 | + fwrite(STDOUT, $message . PHP_EOL); |
| 8 | +} |
| 9 | + |
| 10 | +function fail(string $message, int $code = 1): never |
| 11 | +{ |
| 12 | + fwrite(STDERR, $message . PHP_EOL); |
| 13 | + exit($code); |
| 14 | +} |
| 15 | + |
| 16 | +function loadJson(string $path): array |
| 17 | +{ |
| 18 | + $raw = @file_get_contents($path); |
| 19 | + if ($raw === false) { |
| 20 | + fail("Cannot read JSON file: {$path}"); |
| 21 | + } |
| 22 | + $decoded = json_decode($raw, true); |
| 23 | + if (!is_array($decoded)) { |
| 24 | + fail("Invalid JSON structure: {$path}"); |
| 25 | + } |
| 26 | + return $decoded; |
| 27 | +} |
| 28 | + |
| 29 | +function shouldApplyRule(array $rule): bool |
| 30 | +{ |
| 31 | + if (!isset($rule['when']) || !is_array($rule['when'])) { |
| 32 | + return true; |
| 33 | + } |
| 34 | + |
| 35 | + $when = $rule['when']; |
| 36 | + $env = (string)($when['env'] ?? ''); |
| 37 | + if ($env === '') { |
| 38 | + return true; |
| 39 | + } |
| 40 | + |
| 41 | + $expected = (string)($when['equals'] ?? '1'); |
| 42 | + $current = (string)getenv($env); |
| 43 | + |
| 44 | + return $current === $expected; |
| 45 | +} |
| 46 | + |
| 47 | +function evaluateRule(array $rule, array $config): ?array |
| 48 | +{ |
| 49 | + $id = (string)($rule['id'] ?? 'UNKNOWN'); |
| 50 | + $key = (string)($rule['key'] ?? ''); |
| 51 | + $type = (string)($rule['type'] ?? ''); |
| 52 | + $severity = strtolower((string)($rule['severity'] ?? 'low')); |
| 53 | + |
| 54 | + if ($key === '' || $type === '') { |
| 55 | + return [ |
| 56 | + 'id' => $id, |
| 57 | + 'severity' => 'high', |
| 58 | + 'message' => 'Invalid policy rule definition', |
| 59 | + ]; |
| 60 | + } |
| 61 | + |
| 62 | + $value = array_key_exists($key, $config) ? $config[$key] : ($rule['default'] ?? null); |
| 63 | + |
| 64 | + if ($type === 'exact') { |
| 65 | + $expected = $rule['expected'] ?? null; |
| 66 | + if ($value !== $expected) { |
| 67 | + return [ |
| 68 | + 'id' => $id, |
| 69 | + 'severity' => $severity, |
| 70 | + 'message' => "Expected {$key}=" . json_encode($expected) . ", got " . json_encode($value), |
| 71 | + ]; |
| 72 | + } |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + if ($type === 'non_empty_string') { |
| 77 | + if (!is_string($value) || trim($value) === '') { |
| 78 | + return [ |
| 79 | + 'id' => $id, |
| 80 | + 'severity' => $severity, |
| 81 | + 'message' => "Expected non-empty string for {$key}", |
| 82 | + ]; |
| 83 | + } |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + if ($type === 'forbid_exact') { |
| 88 | + $forbidden = $rule['forbidden'] ?? null; |
| 89 | + if ($value === $forbidden) { |
| 90 | + return [ |
| 91 | + 'id' => $id, |
| 92 | + 'severity' => $severity, |
| 93 | + 'message' => "Forbidden value {$key}=" . json_encode($forbidden), |
| 94 | + ]; |
| 95 | + } |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + return [ |
| 100 | + 'id' => $id, |
| 101 | + 'severity' => 'high', |
| 102 | + 'message' => "Unsupported policy rule type: {$type}", |
| 103 | + ]; |
| 104 | +} |
| 105 | + |
| 106 | +$policyPath = getenv('PSFS_SECURITY_HARDENING_POLICY') ?: 'security/contracts/hardening-policy.json'; |
| 107 | +$configPath = getenv('PSFS_SECURITY_CONFIG_FILE') ?: 'config/config.json'; |
| 108 | +$reportPath = getenv('PSFS_SECURITY_HARDENING_REPORT') ?: 'security/reports/hardening-gate.json'; |
| 109 | + |
| 110 | +$policy = loadJson($policyPath); |
| 111 | +$config = loadJson($configPath); |
| 112 | +$rules = $policy['rules'] ?? []; |
| 113 | +if (!is_array($rules)) { |
| 114 | + fail('Invalid hardening policy rules'); |
| 115 | +} |
| 116 | + |
| 117 | +$violations = []; |
| 118 | +foreach ($rules as $rule) { |
| 119 | + if (!is_array($rule) || !shouldApplyRule($rule)) { |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + $violation = evaluateRule($rule, $config); |
| 124 | + if (is_array($violation)) { |
| 125 | + $violations[] = $violation; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +$blocking = array_values(array_filter($violations, static fn(array $violation): bool => in_array( |
| 130 | + strtolower((string)($violation['severity'] ?? 'low')), |
| 131 | + ['high', 'critical'], |
| 132 | + true |
| 133 | +))); |
| 134 | + |
| 135 | +$status = empty($blocking) ? 'pass' : 'block'; |
| 136 | + |
| 137 | +$report = [ |
| 138 | + 'version' => '1.0', |
| 139 | + 'generated_at' => gmdate('c'), |
| 140 | + 'status' => $status, |
| 141 | + 'summary' => [ |
| 142 | + 'rules_evaluated' => count($rules), |
| 143 | + 'violations' => count($violations), |
| 144 | + 'blocking' => count($blocking), |
| 145 | + ], |
| 146 | + 'violations' => $violations, |
| 147 | +]; |
| 148 | + |
| 149 | +if (!is_dir(dirname($reportPath))) { |
| 150 | + mkdir(dirname($reportPath), 0775, true); |
| 151 | +} |
| 152 | +file_put_contents($reportPath, json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
| 153 | +out("[HARDENING_GATE] status={$status} report={$reportPath}"); |
| 154 | + |
| 155 | +exit($status === 'pass' ? 0 : 1); |
0 commit comments