actions-warden 0.3.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +60 -12
- package/CONTRIBUTING.md +6 -2
- package/README.md +180 -58
- package/RELEASING.md +3 -2
- package/SECURITY.md +23 -8
- package/docs/AI-AGENTS.md +125 -29
- package/docs/CLI.md +346 -69
- package/docs/CONFIGURATION.md +34 -11
- package/docs/DEVELOPMENT.md +114 -18
- package/docs/GITHUB-ACTION.md +163 -29
- package/docs/JAVASCRIPT-API.md +177 -11
- package/docs/OUTPUTS.md +408 -40
- package/docs/README.md +1 -1
- package/examples/README.md +21 -0
- package/examples/baseline-adoption/README.md +79 -0
- package/examples/baseline-adoption/github-actions.yml +56 -0
- package/examples/baseline-adoption/policy.yml +7 -0
- package/examples/ci-violation-reporting/README.md +82 -0
- package/examples/ci-violation-reporting/github-actions.yml +55 -0
- package/examples/ci-violation-reporting/policy.yml +7 -0
- package/examples/ci-violation-reporting/violating-workflow.yml +17 -0
- package/examples/csv-reporting/README.md +50 -0
- package/examples/csv-reporting/github-actions.yml +59 -0
- package/examples/html-organization-report/README.md +35 -0
- package/examples/html-organization-report/github-actions.yml +61 -0
- package/examples/notifications/README.md +67 -0
- package/examples/notifications/github-actions.yml +163 -0
- package/examples/org-scan.yml +24 -5
- package/examples/sarif-code-scanning/README.md +42 -0
- package/examples/sarif-code-scanning/github-actions.yml +66 -0
- package/examples/upgrade-pr.yml +1 -1
- package/llms.txt +14 -5
- package/package.json +8 -4
- package/skills/actions-warden/SKILL.md +91 -39
- package/src/action.js +353 -187
- package/src/cli.js +416 -209
- package/src/commands/audit.js +17 -30
- package/src/commands/org-scan.js +452 -286
- package/src/commands/pin.js +9 -5
- package/src/commands/report.js +7 -2
- package/src/commands/upgrade.js +9 -5
- package/src/commands/verify.js +24 -4
- package/src/index.js +5 -1
- package/src/lib/action-summary.js +656 -0
- package/src/lib/annotations.js +4 -6
- package/src/lib/baseline.js +11 -1
- package/src/lib/config.js +10 -5
- package/src/lib/destination.js +98 -0
- package/src/lib/execution.js +5 -1
- package/src/lib/formats/csv.js +125 -0
- package/src/lib/formats/html.js +394 -0
- package/src/lib/formats/json.js +17 -0
- package/src/lib/formats/sarif.js +229 -0
- package/src/lib/formats/text.js +30 -0
- package/src/lib/formats/toon.js +50 -0
- package/src/lib/formatter.js +25 -192
- package/src/lib/github-org.js +208 -102
- package/src/lib/org-artifacts.js +211 -0
- package/src/lib/org-checkpoint.js +159 -35
- package/src/lib/org-progress.js +182 -4
- package/src/lib/org-report-comparison.js +505 -0
- package/src/lib/org-report-directory.js +251 -0
- package/src/lib/org-report.js +223 -0
- package/src/lib/org-run.js +305 -0
- package/src/lib/org-schema.js +89 -0
- package/src/lib/org-source-links.js +39 -0
- package/src/lib/parser.js +5 -0
- package/src/lib/path-equality.js +30 -0
- package/src/lib/paths.js +18 -1
- package/src/lib/redact.js +58 -7
- package/src/lib/resolver.js +66 -9
- package/src/lib/types.js +137 -0
- package/src/lib/writer.js +3 -0
- package/src/rules/pull-request-target-checkout.js +5 -0
- package/src/rules/unpinned-action.js +2 -3
- package/src/rules/workflow-structure.js +2 -4
- package/src/version.js +1 -1
- package/src/lib/agent-mode.js +0 -175
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* Organization scan runner shared by the CLI and the GitHub Action.
|
|
4
|
+
*
|
|
5
|
+
* The runner owns everything between parsed adapter options and a finished
|
|
6
|
+
* scan: automatic artifact names, checkpoint selection, every destination and
|
|
7
|
+
* control-file check (all before the first network request), previous-report
|
|
8
|
+
* loading, the scan, reused-repository counting, and comparison. Adapters keep
|
|
9
|
+
* input parsing and output.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { isAbsolute, relative, resolve, sep } from 'node:path';
|
|
13
|
+
import {
|
|
14
|
+
DEFAULT_CONFIG_PATHS,
|
|
15
|
+
assertDestinationsDoNotReplaceControls,
|
|
16
|
+
assertDestinationsUseSafeNames,
|
|
17
|
+
validateWriteDestination,
|
|
18
|
+
} from './destination.js';
|
|
19
|
+
import { createOrganizationScanArtifacts } from './org-artifacts.js';
|
|
20
|
+
import { buildOrganizationReport } from './org-report.js';
|
|
21
|
+
import { compareOrganizationReports, loadOrganizationReport } from './org-report-comparison.js';
|
|
22
|
+
import { validateOrganizationReportDirectory } from './org-report-directory.js';
|
|
23
|
+
import { pathExists } from './paths.js';
|
|
24
|
+
import { sameFilePath } from './path-equality.js';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @typedef {Awaited<ReturnType<typeof createOrganizationScanArtifacts>>} ScanArtifacts
|
|
28
|
+
* @typedef {{option?: string, label?: string, path: string}} Destination
|
|
29
|
+
* @typedef {{directory: string|null, outputPath: string|undefined,
|
|
30
|
+
* destination: Destination|null}} ReportDestination
|
|
31
|
+
* @typedef {{path: string|undefined, resume: boolean,
|
|
32
|
+
* destination: Destination|null}} CheckpointSelection
|
|
33
|
+
* @typedef {{emit: (event: import('./types.js').ScanProgressEvent) => unknown,
|
|
34
|
+
* failScan: (organization: string, error: unknown) => void}} ProgressChannel
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @typedef {object} OrganizationRunOptions
|
|
39
|
+
* @property {Omit<import('./types.js').OrganizationScanOptions,
|
|
40
|
+
* 'checkpointPath'|'resume'|'onProgress'>} scan Options for `scanOrganization`; the
|
|
41
|
+
* runner supplies the checkpoint, resume flag, and progress callback.
|
|
42
|
+
* @property {(options: import('./types.js').OrganizationScanOptions) =>
|
|
43
|
+
* Promise<import('./types.js').OrganizationScanResult>} scanOrganization The scan
|
|
44
|
+
* implementation. Callers inject it so this library module does not depend on
|
|
45
|
+
* `src/commands/`.
|
|
46
|
+
* @property {'toon'|'json'|'text'|'csv'|'sarif'|'html'} format Report format.
|
|
47
|
+
* @property {'file'|'stdout'} output Whether the report is saved or printed.
|
|
48
|
+
* @property {string} [outputPath] Explicit report path; defaults to the automatic one.
|
|
49
|
+
* @property {string} [reportDirectory] Dedicated report directory, instead of one file.
|
|
50
|
+
* @property {string} [checkpointPath] Explicit checkpoint to create or resume.
|
|
51
|
+
* @property {string} [resumePath] Explicit checkpoint that must be resumed.
|
|
52
|
+
* @property {boolean} [fresh] Replace rather than resume the selected checkpoint.
|
|
53
|
+
* @property {boolean} [autoCheckpoint] Use the scope-keyed automatic checkpoint (default true).
|
|
54
|
+
* @property {string} [previousReportPath] Prior organization JSON report to compare against.
|
|
55
|
+
* @property {{output: string, checkpoint: string, resume: string}} labels Option names
|
|
56
|
+
* used in destination error messages, such as `--checkpoint` or `checkpoint-path`.
|
|
57
|
+
* @property {() => ProgressChannel} openProgress Called once, after preflight and
|
|
58
|
+
* before the scan, to obtain the progress reporter.
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Run a guarded organization scan: resolve artifacts, validate every
|
|
63
|
+
* destination, load any previous report, scan, and attach the comparison.
|
|
64
|
+
*
|
|
65
|
+
* @param {OrganizationRunOptions} options
|
|
66
|
+
* @returns {Promise<{result: object, reportPath: string|undefined,
|
|
67
|
+
* checkpointPath: string|null, resumed: boolean, repositoriesReused: number,
|
|
68
|
+
* outputDestination: object|null, checkpointDestination: object|null,
|
|
69
|
+
* reportDirectory: string|null}>}
|
|
70
|
+
*/
|
|
71
|
+
export async function runOrganizationScan(options) {
|
|
72
|
+
const { scan, format } = options;
|
|
73
|
+
const artifacts = await createOrganizationScanArtifacts({ ...scan, reportFormat: format });
|
|
74
|
+
const report = await selectReportDestination(options, artifacts);
|
|
75
|
+
const checkpoint = await selectCheckpoint(options, artifacts);
|
|
76
|
+
const previousPath = options.previousReportPath
|
|
77
|
+
? resolve(scan.cwd, options.previousReportPath)
|
|
78
|
+
: null;
|
|
79
|
+
await assertNoDestinationCollisions({ artifacts, report, checkpoint, previousPath, scan });
|
|
80
|
+
const previousReport = options.previousReportPath
|
|
81
|
+
? await loadOrganizationReport({ path: options.previousReportPath, cwd: scan.cwd })
|
|
82
|
+
: null;
|
|
83
|
+
|
|
84
|
+
const progress = options.openProgress();
|
|
85
|
+
const { result, repositoriesReused } = await scanWithProgress(options, checkpoint, progress);
|
|
86
|
+
await assertDestinationsDoNotReplaceControls(
|
|
87
|
+
report.destination ? [report.destination] : [],
|
|
88
|
+
[
|
|
89
|
+
{ label: 'active config', path: result.configPath },
|
|
90
|
+
{ label: 'active baseline', path: result.baseline.path },
|
|
91
|
+
{ label: 'previous report', path: previousPath },
|
|
92
|
+
],
|
|
93
|
+
);
|
|
94
|
+
if (previousReport) {
|
|
95
|
+
result.comparison = compareOrganizationReports({
|
|
96
|
+
previous: previousReport,
|
|
97
|
+
current: buildOrganizationReport(result, { cwd: scan.cwd }),
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
result,
|
|
102
|
+
reportPath: report.outputPath,
|
|
103
|
+
checkpointPath: checkpoint.path ?? null,
|
|
104
|
+
resumed: checkpoint.resume,
|
|
105
|
+
repositoriesReused,
|
|
106
|
+
outputDestination: report.destination,
|
|
107
|
+
checkpointDestination: checkpoint.destination,
|
|
108
|
+
reportDirectory: report.directory,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @param {OrganizationRunOptions} options
|
|
114
|
+
* @param {ScanArtifacts} artifacts
|
|
115
|
+
* @returns {Promise<ReportDestination>}
|
|
116
|
+
*/
|
|
117
|
+
async function selectReportDestination(options, artifacts) {
|
|
118
|
+
const { cwd } = options.scan;
|
|
119
|
+
if (options.reportDirectory) {
|
|
120
|
+
const directory = await validateOrganizationReportDirectory({
|
|
121
|
+
path: options.reportDirectory,
|
|
122
|
+
cwd,
|
|
123
|
+
});
|
|
124
|
+
assertSafeReportDirectoryName(directory, cwd);
|
|
125
|
+
return { directory, outputPath: undefined, destination: null };
|
|
126
|
+
}
|
|
127
|
+
if (options.output !== 'file') {
|
|
128
|
+
return { directory: null, outputPath: undefined, destination: null };
|
|
129
|
+
}
|
|
130
|
+
const outputPath = options.outputPath ?? artifacts.reportPath;
|
|
131
|
+
const destination = await validateWriteDestination({
|
|
132
|
+
label: options.labels.output,
|
|
133
|
+
path: outputPath,
|
|
134
|
+
cwd,
|
|
135
|
+
});
|
|
136
|
+
await assertDestinationsUseSafeNames([destination], cwd);
|
|
137
|
+
return { directory: null, outputPath, destination };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* @param {OrganizationRunOptions} options
|
|
142
|
+
* @param {ScanArtifacts} artifacts
|
|
143
|
+
* @returns {Promise<CheckpointSelection>}
|
|
144
|
+
*/
|
|
145
|
+
async function selectCheckpoint(options, artifacts) {
|
|
146
|
+
const { cwd } = options.scan;
|
|
147
|
+
const selection = await chooseCheckpoint(options, artifacts);
|
|
148
|
+
if (!selection) return { path: undefined, resume: false, destination: null };
|
|
149
|
+
const destination = await validateWriteDestination({
|
|
150
|
+
label: selection.label,
|
|
151
|
+
path: selection.path,
|
|
152
|
+
cwd,
|
|
153
|
+
});
|
|
154
|
+
await assertDestinationsUseSafeNames([destination], cwd);
|
|
155
|
+
return { ...selection, destination };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* @param {OrganizationRunOptions} options
|
|
160
|
+
* @param {ScanArtifacts} artifacts
|
|
161
|
+
* @returns {Promise<{path: string, resume: boolean, label: string}|null>}
|
|
162
|
+
*/
|
|
163
|
+
async function chooseCheckpoint(options, artifacts) {
|
|
164
|
+
const { labels, fresh, resumePath, checkpointPath, autoCheckpoint = true } = options;
|
|
165
|
+
if (resumePath) return { path: resumePath, resume: true, label: labels.resume };
|
|
166
|
+
if (checkpointPath) {
|
|
167
|
+
const exists = await pathExists(resolve(options.scan.cwd, checkpointPath));
|
|
168
|
+
return { path: checkpointPath, resume: !fresh && exists, label: labels.checkpoint };
|
|
169
|
+
}
|
|
170
|
+
if (!autoCheckpoint) return null;
|
|
171
|
+
return {
|
|
172
|
+
path: artifacts.checkpointPath,
|
|
173
|
+
resume: !fresh && artifacts.resume,
|
|
174
|
+
label: labels.checkpoint,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @param {{artifacts: ScanArtifacts, report: ReportDestination,
|
|
180
|
+
* checkpoint: CheckpointSelection, previousPath: string|null,
|
|
181
|
+
* scan: OrganizationRunOptions['scan']}} state
|
|
182
|
+
*/
|
|
183
|
+
async function assertNoDestinationCollisions({
|
|
184
|
+
artifacts,
|
|
185
|
+
report,
|
|
186
|
+
checkpoint,
|
|
187
|
+
previousPath,
|
|
188
|
+
scan,
|
|
189
|
+
}) {
|
|
190
|
+
const output = report.destination;
|
|
191
|
+
const saved = checkpoint.destination;
|
|
192
|
+
await assertDifferentPaths(
|
|
193
|
+
saved?.path,
|
|
194
|
+
output?.path,
|
|
195
|
+
'checkpoint and report output paths must be different',
|
|
196
|
+
);
|
|
197
|
+
await assertDifferentPaths(
|
|
198
|
+
previousPath,
|
|
199
|
+
saved?.path,
|
|
200
|
+
'checkpoint and previous report paths must be different',
|
|
201
|
+
);
|
|
202
|
+
await assertDifferentPaths(
|
|
203
|
+
previousPath,
|
|
204
|
+
output?.path,
|
|
205
|
+
'output and previous report paths must be different',
|
|
206
|
+
);
|
|
207
|
+
const controls = [
|
|
208
|
+
{ label: 'active config', path: artifacts.configPath },
|
|
209
|
+
{ label: 'active baseline', path: artifacts.baselinePath },
|
|
210
|
+
];
|
|
211
|
+
const destinations = [];
|
|
212
|
+
if (output) destinations.push(output);
|
|
213
|
+
if (saved) destinations.push(saved);
|
|
214
|
+
await assertDestinationsDoNotReplaceControls(destinations, controls);
|
|
215
|
+
if (report.directory) {
|
|
216
|
+
assertDirectoryDoesNotOverlapControls(report.directory, [
|
|
217
|
+
{ label: 'checkpoint', path: saved?.path },
|
|
218
|
+
...controls,
|
|
219
|
+
{ label: 'previous report', path: previousPath },
|
|
220
|
+
...DEFAULT_CONFIG_PATHS.map(path => ({
|
|
221
|
+
label: 'reserved config path',
|
|
222
|
+
path: resolve(scan.cwd, path),
|
|
223
|
+
})),
|
|
224
|
+
]);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* @param {string|null|undefined} left
|
|
230
|
+
* @param {string|null|undefined} right
|
|
231
|
+
* @param {string} message
|
|
232
|
+
*/
|
|
233
|
+
async function assertDifferentPaths(left, right, message) {
|
|
234
|
+
if (left && right && await sameFilePath(left, right)) throw new Error(message);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* @param {OrganizationRunOptions} options
|
|
239
|
+
* @param {CheckpointSelection} checkpoint
|
|
240
|
+
* @param {ProgressChannel} progress
|
|
241
|
+
* @returns {Promise<{result: import('./types.js').OrganizationScanResult,
|
|
242
|
+
* repositoriesReused: number}>}
|
|
243
|
+
*/
|
|
244
|
+
async function scanWithProgress(options, checkpoint, progress) {
|
|
245
|
+
let repositoriesReused = 0;
|
|
246
|
+
try {
|
|
247
|
+
const result = await options.scanOrganization({
|
|
248
|
+
...options.scan,
|
|
249
|
+
checkpointPath: checkpoint.path,
|
|
250
|
+
resume: checkpoint.resume,
|
|
251
|
+
onProgress: event => {
|
|
252
|
+
if (event.type === 'repository-completed' && event.reused) repositoriesReused += 1;
|
|
253
|
+
progress.emit(event);
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
return { result, repositoriesReused };
|
|
257
|
+
} catch (error) {
|
|
258
|
+
progress.failScan(options.scan.organization, error);
|
|
259
|
+
throw error;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* @param {string} path
|
|
265
|
+
* @param {string} cwd
|
|
266
|
+
*/
|
|
267
|
+
function assertSafeReportDirectoryName(path, cwd) {
|
|
268
|
+
const localPath = relative(resolve(cwd), path).split(sep).join('/');
|
|
269
|
+
if (
|
|
270
|
+
localPath === '.github/workflows'
|
|
271
|
+
|| localPath.startsWith('.github/workflows/')
|
|
272
|
+
|| localPath === '.github/actions'
|
|
273
|
+
|| localPath.startsWith('.github/actions/')
|
|
274
|
+
|| /(^|\/)action\.ya?ml(?:\/|$)/i.test(localPath)
|
|
275
|
+
) {
|
|
276
|
+
throw new Error('--report-dir cannot use a workflow discovery path');
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* @param {string} directory
|
|
282
|
+
* @param {Array<{label: string, path: string|null|undefined}>} controls
|
|
283
|
+
*/
|
|
284
|
+
function assertDirectoryDoesNotOverlapControls(directory, controls) {
|
|
285
|
+
for (const control of controls) {
|
|
286
|
+
if (!control.path) continue;
|
|
287
|
+
const directoryPath = resolve(directory);
|
|
288
|
+
const controlPath = resolve(control.path);
|
|
289
|
+
const controlFromDirectory = relative(directoryPath, controlPath);
|
|
290
|
+
const directoryFromControl = relative(controlPath, directoryPath);
|
|
291
|
+
if (isInsideOrSame(controlFromDirectory) || isInsideOrSame(directoryFromControl)) {
|
|
292
|
+
throw new Error(`--report-dir cannot overlap the ${control.label}`);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** @param {string} path */
|
|
298
|
+
function isInsideOrSame(path) {
|
|
299
|
+
return path === '' || (!isOutsidePath(path) && !isAbsolute(path));
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** @param {string} path */
|
|
303
|
+
function isOutsidePath(path) {
|
|
304
|
+
return path === '..' || path.startsWith(`..${sep}`);
|
|
305
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* Shared definitions for organization scans, reports, checkpoints, and
|
|
4
|
+
* comparisons: identifier patterns, closed value sets, and small structural
|
|
5
|
+
* guards. Each definition lives here once so validators cannot drift apart.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** Pattern accepted for GitHub organization, owner, and repository names. */
|
|
9
|
+
export const NAME_RE = /^[A-Za-z0-9_.-]+$/;
|
|
10
|
+
|
|
11
|
+
/** Pattern for a full 40-hex Git object ID. */
|
|
12
|
+
export const GIT_SHA_RE = /^[0-9a-f]{40}$/i;
|
|
13
|
+
|
|
14
|
+
/** Finding severities ordered from lowest to highest. */
|
|
15
|
+
export const SEVERITY_LEVELS = Object.freeze(['low', 'medium', 'high', 'critical']);
|
|
16
|
+
|
|
17
|
+
/** Finding severities as a set for membership checks. */
|
|
18
|
+
export const SEVERITIES = new Set(SEVERITY_LEVELS);
|
|
19
|
+
|
|
20
|
+
/** Visibility values GitHub reports for a repository. */
|
|
21
|
+
export const REPOSITORY_VISIBILITIES = Object.freeze(['public', 'private', 'internal']);
|
|
22
|
+
|
|
23
|
+
/** Visibility filters accepted for an organization scan. */
|
|
24
|
+
export const VISIBILITY_FILTERS = Object.freeze(['all', ...REPOSITORY_VISIBILITIES]);
|
|
25
|
+
|
|
26
|
+
/** Visibility filters as a set for membership checks. */
|
|
27
|
+
export const VISIBILITIES = new Set(VISIBILITY_FILTERS);
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Return whether a value is a plain, non-array object.
|
|
31
|
+
*
|
|
32
|
+
* @param {unknown} value
|
|
33
|
+
* @returns {boolean}
|
|
34
|
+
*/
|
|
35
|
+
export function isRecord(value) {
|
|
36
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Return whether a value is finite JSON data without cycles.
|
|
41
|
+
*
|
|
42
|
+
* @param {unknown} value
|
|
43
|
+
* @param {Set<object>} [seen]
|
|
44
|
+
* @returns {boolean}
|
|
45
|
+
*/
|
|
46
|
+
export function isJsonValue(value, seen = new Set()) {
|
|
47
|
+
if (value === null || ['string', 'boolean'].includes(typeof value)) return true;
|
|
48
|
+
if (typeof value === 'number') return Number.isFinite(value);
|
|
49
|
+
if (!value || typeof value !== 'object' || seen.has(value)) return false;
|
|
50
|
+
seen.add(value);
|
|
51
|
+
const valid = Array.isArray(value)
|
|
52
|
+
? value.every(item => isJsonValue(item, seen))
|
|
53
|
+
: Object.entries(value).every(([key, item]) => (
|
|
54
|
+
typeof key === 'string' && isJsonValue(item, seen)
|
|
55
|
+
));
|
|
56
|
+
seen.delete(value);
|
|
57
|
+
return valid;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Validate repository filters and remove duplicates while keeping order.
|
|
62
|
+
*
|
|
63
|
+
* @param {unknown} patterns Undefined, or an array of non-empty strings.
|
|
64
|
+
* @returns {string[]}
|
|
65
|
+
*/
|
|
66
|
+
export function normalizePatterns(patterns) {
|
|
67
|
+
if (patterns === undefined) return [];
|
|
68
|
+
if (
|
|
69
|
+
!Array.isArray(patterns)
|
|
70
|
+
|| patterns.some(pattern => typeof pattern !== 'string' || !pattern)
|
|
71
|
+
) throw new Error('repository filters must be non-empty strings');
|
|
72
|
+
return [...new Set(patterns)];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Scan outcomes recorded for each workflow file in a repository result. */
|
|
76
|
+
export const FILE_STATUSES = new Set(['scanned', 'parse-error', 'unreadable']);
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Compare repository paths by UTF-16 code unit so ordering does not depend on
|
|
80
|
+
* the runtime locale.
|
|
81
|
+
*
|
|
82
|
+
* @param {string} left
|
|
83
|
+
* @param {string} right
|
|
84
|
+
* @returns {number}
|
|
85
|
+
*/
|
|
86
|
+
export function comparePaths(left, right) {
|
|
87
|
+
if (left < right) return -1;
|
|
88
|
+
return left > right ? 1 : 0;
|
|
89
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Links from organization-scan evidence to the GitHub source that produced it.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Link to a repo-relative path on the default branch. Falls back to the
|
|
7
|
+
* repository page when no default branch is known.
|
|
8
|
+
*
|
|
9
|
+
* @param {{htmlUrl: string, defaultBranch: string|null}} repository
|
|
10
|
+
* @param {string} path Repo-relative POSIX path.
|
|
11
|
+
* @param {number} [line]
|
|
12
|
+
* @returns {string}
|
|
13
|
+
*/
|
|
14
|
+
export function sourceUrl(repository, path, line) {
|
|
15
|
+
if (!repository.defaultBranch) return repository.htmlUrl;
|
|
16
|
+
return blobUrl(repository, encodeURIComponent(repository.defaultBranch), path, line);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Link to a repo-relative path at an exact commit, so the evidence stays
|
|
21
|
+
* valid after the branch moves. Uses the default-branch link when the commit
|
|
22
|
+
* is unknown.
|
|
23
|
+
*
|
|
24
|
+
* @param {{htmlUrl: string, defaultBranch: string|null}} repository
|
|
25
|
+
* @param {string|null} commitSha Lowercase 40-hex commit ID, or null.
|
|
26
|
+
* @param {string} path Repo-relative POSIX path.
|
|
27
|
+
* @param {number} [line] Adds an `#L<line>` fragment when positive.
|
|
28
|
+
* @returns {string}
|
|
29
|
+
*/
|
|
30
|
+
export function sourcePermalink(repository, commitSha, path, line) {
|
|
31
|
+
if (!commitSha) return sourceUrl(repository, path, line);
|
|
32
|
+
return blobUrl(repository, commitSha, path, line);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function blobUrl(repository, ref, path, line) {
|
|
36
|
+
const encodedPath = path.split('/').map(encodeURIComponent).join('/');
|
|
37
|
+
const suffix = Number.isInteger(line) && line > 0 ? `#L${line}` : '';
|
|
38
|
+
return `${repository.htmlUrl}/blob/${ref}/${encodedPath}${suffix}`;
|
|
39
|
+
}
|
package/src/lib/parser.js
CHANGED
|
@@ -16,6 +16,8 @@ import {
|
|
|
16
16
|
isScalar,
|
|
17
17
|
} from 'yaml';
|
|
18
18
|
|
|
19
|
+
const MAX_PARALLEL_DEPTH = 10;
|
|
20
|
+
|
|
19
21
|
/**
|
|
20
22
|
* @typedef {object} ActionRef
|
|
21
23
|
* @property {string} raw - e.g. `actions/checkout@v3` or `./local`
|
|
@@ -300,6 +302,9 @@ function extractSteps(source, stepsNode, doc, parallelDepth = 0) {
|
|
|
300
302
|
}
|
|
301
303
|
steps.push(step);
|
|
302
304
|
if (parallelPair && isSeq(parallelPair.value)) {
|
|
305
|
+
if (parallelDepth >= MAX_PARALLEL_DEPTH) {
|
|
306
|
+
throw new Error(`parallel step nesting exceeded maximum depth of ${MAX_PARALLEL_DEPTH}`);
|
|
307
|
+
}
|
|
303
308
|
steps.push(...extractSteps(source, parallelPair.value, doc, parallelDepth + 1));
|
|
304
309
|
}
|
|
305
310
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { realpath } from 'node:fs/promises';
|
|
2
|
+
import { basename, dirname, join } from 'node:path';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Compare two existing or prospective file paths after resolving their real
|
|
6
|
+
* parents. Existing entries are fully resolved so a control-file symlink is
|
|
7
|
+
* compared with its target rather than only with the symlink name.
|
|
8
|
+
*/
|
|
9
|
+
export async function sameFilePath(left, right) {
|
|
10
|
+
const [leftPath, rightPath] = await Promise.all([
|
|
11
|
+
canonicalPath(left),
|
|
12
|
+
canonicalPath(right),
|
|
13
|
+
]);
|
|
14
|
+
return comparablePath(leftPath) === comparablePath(rightPath);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async function canonicalPath(path) {
|
|
18
|
+
try {
|
|
19
|
+
return await realpath(path);
|
|
20
|
+
} catch (error) {
|
|
21
|
+
if (error.code !== 'ENOENT') throw error;
|
|
22
|
+
return join(await realpath(dirname(path)), basename(path));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function comparablePath(path) {
|
|
27
|
+
return process.platform === 'win32' || process.platform === 'darwin'
|
|
28
|
+
? path.toLowerCase()
|
|
29
|
+
: path;
|
|
30
|
+
}
|
package/src/lib/paths.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Workflow file discovery with safe path handling.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import { readdir, stat, realpath } from 'node:fs/promises';
|
|
5
|
+
import { lstat, readdir, stat, realpath } from 'node:fs/promises';
|
|
6
6
|
import { resolve, join, relative, isAbsolute, sep } from 'node:path';
|
|
7
7
|
import picomatch from 'picomatch';
|
|
8
8
|
|
|
@@ -18,6 +18,23 @@ export const DEFAULT_WORKFLOW_PATTERNS = [
|
|
|
18
18
|
'**/action.yaml',
|
|
19
19
|
];
|
|
20
20
|
|
|
21
|
+
/**
|
|
22
|
+
* Return whether a path exists without following a final symbolic link. Errors other
|
|
23
|
+
* than a missing path are rethrown.
|
|
24
|
+
*
|
|
25
|
+
* @param {string} path
|
|
26
|
+
* @returns {Promise<boolean>}
|
|
27
|
+
*/
|
|
28
|
+
export async function pathExists(path) {
|
|
29
|
+
try {
|
|
30
|
+
await lstat(path);
|
|
31
|
+
return true;
|
|
32
|
+
} catch (error) {
|
|
33
|
+
if (error.code === 'ENOENT') return false;
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
21
38
|
/**
|
|
22
39
|
* Reject path-traversal and absolute-escape attempts.
|
|
23
40
|
*
|
package/src/lib/redact.js
CHANGED
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
* with `<redacted>`. Conservative by design: prefers false positives over leaks.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
+
import { RULES } from '../rules/index.js';
|
|
9
|
+
|
|
8
10
|
const TOKEN_PATTERNS = [
|
|
9
11
|
/ghp_[A-Za-z0-9]{30,}/g,
|
|
10
12
|
/ghs_[A-Za-z0-9]{30,}/g,
|
|
@@ -25,11 +27,28 @@ const TOKEN_PATTERNS = [
|
|
|
25
27
|
const SENSITIVE_KEY = /(?:token|secret|password|api[_-]?key|auth[_-]?token|access[_-]?key|private[_-]?key)$/i;
|
|
26
28
|
const KV_TOKEN_KEYS = /([\w-]*(?:token|secret|password|api[_-]?key|auth[_-]?token|access[_-]?key|private[_-]?key))(\s*[:=]\s*)["']?([^"'\s,]+)/gi;
|
|
27
29
|
const HIGH_ENTROPY = /[A-Za-z0-9_+/-]{32,}={0,2}/g;
|
|
28
|
-
//
|
|
29
|
-
//
|
|
30
|
-
//
|
|
30
|
+
// Source links the scanner itself builds: https://github.com/<owner>/<repo>/blob/<ref>/<path>
|
|
31
|
+
// with an optional #L<line>. A commit SHA in the ref makes the whole link one long
|
|
32
|
+
// high-entropy run, so these links are exempt from the entropy heuristic only; explicit
|
|
33
|
+
// token patterns above still redact credential-shaped text inside them. The shape is exact
|
|
34
|
+
// (encodeURIComponent characters only) so it cannot shelter arbitrary secret-like text.
|
|
35
|
+
const URL_SEGMENT = "[A-Za-z0-9._~%!'()*-]+";
|
|
36
|
+
const GITHUB_SOURCE_URL = new RegExp(
|
|
37
|
+
`https://github\\.com/[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+/blob/${URL_SEGMENT}`
|
|
38
|
+
+ `(?:/${URL_SEGMENT})+(?:#L\\d{1,9})?`,
|
|
39
|
+
'g',
|
|
40
|
+
);
|
|
41
|
+
// Public rule IDs and artifact kinds long enough to trip the generic entropy
|
|
42
|
+
// heuristic. Keep the exceptions exact instead of weakening detection for
|
|
43
|
+
// arbitrary kebab-case values, which may still be credentials or passphrases.
|
|
31
44
|
const SAFE_PUBLIC_VALUES = new Set([
|
|
32
|
-
|
|
45
|
+
...RULES.map(rule => rule.id),
|
|
46
|
+
'actions-warden-agent-receipt',
|
|
47
|
+
'actions-warden-org-scan-receipt',
|
|
48
|
+
'actions-warden-org-scan-directory',
|
|
49
|
+
'actions-warden-org-scan-manifest',
|
|
50
|
+
'actions-warden-org-scan-progress',
|
|
51
|
+
'actions-warden-org-scan-repository',
|
|
33
52
|
]);
|
|
34
53
|
|
|
35
54
|
/**
|
|
@@ -43,10 +62,23 @@ export function redact(input) {
|
|
|
43
62
|
let s = typeof input === 'string' ? input : String(input);
|
|
44
63
|
for (const re of TOKEN_PATTERNS) s = s.replace(re, '<redacted>');
|
|
45
64
|
s = s.replace(KV_TOKEN_KEYS, (_, key, separator) => `${key}${separator}<redacted>`);
|
|
46
|
-
|
|
65
|
+
return redactHighEntropyOutsideSourceUrls(s);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function redactHighEntropyOutsideSourceUrls(input) {
|
|
69
|
+
let output = '';
|
|
70
|
+
let position = 0;
|
|
71
|
+
for (const match of input.matchAll(GITHUB_SOURCE_URL)) {
|
|
72
|
+
output += redactHighEntropy(input.slice(position, match.index)) + match[0];
|
|
73
|
+
position = match.index + match[0].length;
|
|
74
|
+
}
|
|
75
|
+
return output + redactHighEntropy(input.slice(position));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function redactHighEntropy(text) {
|
|
79
|
+
return text.replace(HIGH_ENTROPY, value => (
|
|
47
80
|
shouldRedactHighEntropy(value) ? '<redacted>' : value
|
|
48
81
|
));
|
|
49
|
-
return s;
|
|
50
82
|
}
|
|
51
83
|
|
|
52
84
|
/**
|
|
@@ -62,7 +94,26 @@ export function redactDeep(input, seen = new WeakSet()) {
|
|
|
62
94
|
if (seen.has(input)) return '<circular>';
|
|
63
95
|
seen.add(input);
|
|
64
96
|
let output;
|
|
65
|
-
if (
|
|
97
|
+
if (input instanceof Date) {
|
|
98
|
+
output = input.toISOString();
|
|
99
|
+
} else if (input instanceof Error) {
|
|
100
|
+
output = {
|
|
101
|
+
name: redact(input.name),
|
|
102
|
+
message: redact(input.message),
|
|
103
|
+
};
|
|
104
|
+
for (const [key, value] of Object.entries(input)) {
|
|
105
|
+
output[key] = SENSITIVE_KEY.test(key) ? '<redacted>' : redactDeep(value, seen);
|
|
106
|
+
}
|
|
107
|
+
} else if (input instanceof Set) {
|
|
108
|
+
output = [...input].map(item => redactDeep(item, seen));
|
|
109
|
+
} else if (input instanceof Map) {
|
|
110
|
+
output = Object.fromEntries(
|
|
111
|
+
[...input.entries()].map(([k, v]) => [
|
|
112
|
+
redact(String(k)),
|
|
113
|
+
SENSITIVE_KEY.test(String(k)) ? '<redacted>' : redactDeep(v, seen),
|
|
114
|
+
]),
|
|
115
|
+
);
|
|
116
|
+
} else if (Array.isArray(input)) {
|
|
66
117
|
output = input.map(item => redactDeep(item, seen));
|
|
67
118
|
} else {
|
|
68
119
|
output = {};
|