-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcastor.php
More file actions
215 lines (169 loc) · 7.57 KB
/
Copy pathcastor.php
File metadata and controls
215 lines (169 loc) · 7.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/*
* This file is part of the vinceamstoutz/symfony-security-auditor package.
*
* (c) Vincent Amstoutz <vincent.amstoutz.dev@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
use Castor\Attribute\AsTask;
use VinceAmstoutz\SymfonySecurityAuditor\Tooling\Eval\EvalReport;
use VinceAmstoutz\SymfonySecurityAuditor\Tooling\Eval\EvalScorer;
use VinceAmstoutz\SymfonySecurityAuditor\Tooling\Eval\GroundTruthManifest;
use function Castor\io;
use function Castor\run;
#[AsTask(name: 'up', description: 'Install dependencies')]
function setup(): void
{
run('docker compose up --wait');
}
#[AsTask(name: 'down', description: 'Stop all containers and remove orphans')]
function down(): void
{
run('docker compose down --remove-orphans');
}
#[AsTask(name: 'release:bump', description: 'Rewrite every release version pin (schema $id, GitHub Action uses: examples) to the given X.Y.Z tag')]
function releaseBump(string $version): void
{
if (1 !== preg_match('/^\\d+\\.\\d+\\.\\d+$/', $version)) {
io()->error(sprintf('"%s" is not a X.Y.Z version.', $version));
exit(1);
}
$pinnedFiles = [
'resources/schema.json' => '#(symfony-security-auditor/)\\d+\\.\\d+\\.\\d+(/resources/schema\\.json)#',
'README.md' => '#(uses: vinceamstoutz/symfony-security-auditor@)\\d+\\.\\d+\\.\\d+#',
'docs/ci.md' => '#(uses: vinceamstoutz/symfony-security-auditor@)\\d+\\.\\d+\\.\\d+#',
'docs/versioning.md' => '#(uses: vinceamstoutz/symfony-security-auditor@)\\d+\\.\\d+\\.\\d+#',
];
foreach ($pinnedFiles as $file => $pattern) {
$content = file_get_contents($file);
if (false === $content) {
io()->error(sprintf('Could not read %s.', $file));
exit(1);
}
$rewritten = preg_replace($pattern, '${1}'.$version.'${2}', $content, -1, $count);
if (null === $rewritten || 0 === $count) {
io()->error(sprintf('No version pin matched in %s — the pin list in castor.php is stale.', $file));
exit(1);
}
file_put_contents($file, $rewritten);
io()->writeln(sprintf(' <info>OK</info> %s (%d pin%s)', $file, $count, 1 === $count ? '' : 's'));
}
io()->success(sprintf('All version pins now point at %s.', $version));
}
#[AsTask(name: 'eval', description: 'Audit a ground-truth fixture and score detection precision/recall against its manifest')]
function evaluate(
string $target = 'examples/vulnerable-app',
string $groundTruth = 'examples/vulnerable-app/ground-truth.json',
float $minPrecision = 0.0,
float $minRecall = 0.0,
): void {
$reportPath = sprintf('%s/ssa-eval-%s.json', sys_get_temp_dir(), bin2hex(random_bytes(4)));
io()->section('Running the auditor against the fixture (uses real LLM calls)');
run(sprintf('docker compose exec php bin/console audit:run %s --format=json --output=%s', $target, $reportPath));
$manifest = GroundTruthManifest::fromFile($groundTruth);
$evalReport = (new EvalScorer())->score($manifest, actualFindingsFromReport($reportPath));
printEvalReport($evalReport);
if (!$evalReport->meetsThresholds($minPrecision, $minRecall)) {
io()->error(sprintf('Below thresholds: precision >= %.2f and recall >= %.2f required.', $minPrecision, $minRecall));
exit(1);
}
io()->success('Detection quality meets the configured thresholds.');
}
/**
* @return list<array{file: string, type: string}>
*/
function actualFindingsFromReport(string $reportPath): array
{
$decoded = json_decode((string) file_get_contents($reportPath), true, flags: \JSON_THROW_ON_ERROR);
$vulnerabilities = is_array($decoded) ? ($decoded['vulnerabilities'] ?? []) : [];
$findings = [];
foreach (is_array($vulnerabilities) ? $vulnerabilities : [] as $vulnerability) {
if (is_array($vulnerability) && is_string($vulnerability['file'] ?? null) && is_string($vulnerability['type'] ?? null)) {
$findings[] = ['file' => $vulnerability['file'], 'type' => $vulnerability['type']];
}
}
return $findings;
}
function printEvalReport(EvalReport $evalReport): void
{
$rows = [];
foreach ([$evalReport->overall, ...$evalReport->perClass] as $classScore) {
$rows[] = [
$classScore->type,
sprintf('%.0f%%', $classScore->precision() * 100),
sprintf('%.0f%%', $classScore->recall() * 100),
sprintf('%.2f', $classScore->f1()),
sprintf('%d / %d / %d', $classScore->truePositives, $classScore->falsePositives, $classScore->falseNegatives),
];
}
io()->table(['Class', 'Precision', 'Recall', 'F1', 'TP / FP / FN'], $rows);
}
#[AsTask(name: 'lint', description: 'Check code style and analyze code')]
function lint(): void
{
runCodeQualityTools();
}
#[AsTask(name: 'lint:fix', description: 'Fix code style and apply refactorings')]
function fix(): void
{
runCodeQualityTools(fixMode: true);
}
#[AsTask(name: 'lint:docs', description: 'Check Markdown formatting only (fast pre-push check)')]
function lintDocs(): void
{
$userFlag = sprintf('--user %d:%d', posix_getuid(), posix_getgid());
io()->section('Prettier Markdown');
run(sprintf(
'docker run --rm %s -v "%s:/work" -w /work tmknom/prettier:3.6.2 --check "**/*.md"',
$userFlag,
getcwd(),
));
io()->section('Markdown lint');
run(sprintf(
'docker run --rm %s -v "%s:/workdir" davidanson/markdownlint-cli2:latest',
$userFlag,
getcwd(),
));
io()->success('Markdown looks good.');
}
function runCodeQualityTools(bool $fixMode = false): void
{
$userFlag = sprintf('--user %d:%d', posix_getuid(), posix_getgid());
io()->section('Prettier Markdown');
run(sprintf(
'docker run --rm %s -v "%s:/work" -w /work tmknom/prettier:3.6.2 --%s "**/*.md"',
$userFlag,
getcwd(),
$fixMode ? 'write' : 'check',
));
io()->section('Markdown lint');
run(sprintf(
'docker run --rm %s -v "%s:/workdir" davidanson/markdownlint-cli2:latest%s',
$userFlag,
getcwd(),
$fixMode ? ' --fix' : '',
));
io()->section('Composer Normalize');
run('docker compose exec php composer normalize'.($fixMode ? '' : ' --dry-run'));
io()->section('PHP CS Fixer');
run('docker compose exec php vendor/bin/php-cs-fixer fix'.($fixMode ? '' : ' --dry-run --diff'));
io()->section('Rector');
run('docker compose exec php vendor/bin/rector process'.($fixMode ? '' : ' --dry-run'));
io()->section('PHPStan');
run('docker compose exec php vendor/bin/phpstan analyse --memory-limit=500M');
io()->section('Deptrac');
run('docker compose exec php vendor/bin/deptrac analyse --no-progress');
io()->section('Swiss Knife');
run('docker compose exec php vendor/bin/swiss-knife check-commented-code src tests tools');
run('docker compose exec php vendor/bin/swiss-knife check-conflicts src tests tools');
io()->section('Install script tests');
run('sh tests/Shell/install_script_test.sh');
io()->section('PHPUnit');
run('docker compose exec php vendor/bin/phpunit --coverage-clover=build/coverage/clover.xml --coverage-xml=build/coverage/coverage-xml --log-junit=build/coverage/junit.xml');
io()->section('Infection');
run('docker compose exec php php -d memory_limit=2G bin/infection --configuration=infection.json5 --threads=max --coverage=build/coverage --skip-initial-tests --min-msi=100 --min-covered-msi=100');
io()->success($fixMode ? 'Fixing complete.' : 'Linting complete.');
}