@blundergoat/gruff-ts 0.5.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/CHANGELOG.md +82 -0
- package/CONTRIBUTING.md +32 -25
- package/README.md +49 -50
- package/SECURITY.md +18 -31
- package/UPGRADING.md +101 -0
- package/bin/gruff-ts +3 -0
- package/docs/README.md +11 -10
- package/docs/agent-hook.md +32 -35
- package/docs/ci-integration.md +38 -24
- package/docs/configuration.md +160 -89
- package/docs/dashboard.md +12 -13
- package/docs/output-formats.md +100 -25
- package/docs/philosophy.md +2 -2
- package/docs/reports-and-ci.md +14 -172
- package/docs/rules.md +126 -98
- package/package.json +10 -8
- package/src/analyser.ts +817 -323
- package/src/baseline-file.ts +867 -0
- package/src/baseline-identity.ts +463 -0
- package/src/baseline-options.ts +139 -34
- package/src/baseline.ts +62 -68
- package/src/blocks.ts +512 -336
- package/src/changed-regions.ts +380 -79
- package/src/check-ignore.ts +28 -13
- package/src/class-rules.ts +445 -436
- package/src/cli-program.ts +915 -172
- package/src/cli.ts +9 -3
- package/src/comment-rules.ts +236 -418
- package/src/comment-scanner.ts +339 -47
- package/src/complexity-metrics.ts +111 -35
- package/src/config-load-error.ts +17 -8
- package/src/config-parse.ts +291 -98
- package/src/config-preservation.ts +81 -26
- package/src/config.ts +751 -223
- package/src/constants.ts +7 -6
- package/src/context-doc-rules.ts +182 -54
- package/src/dashboard.ts +88 -23
- package/src/dead-code-rules.ts +153 -74
- package/src/discovery.ts +493 -140
- package/src/doc-rules.ts +602 -126
- package/src/duplicate-test-group-rules.ts +359 -0
- package/src/findings-helpers.ts +134 -109
- package/src/findings.ts +47 -27
- package/src/fixture-purpose-rules.ts +194 -50
- package/src/github-actions-event-guards.ts +331 -0
- package/src/github-actions-rules.ts +328 -121
- package/src/hook-contract.ts +681 -255
- package/src/init-config.ts +163 -118
- package/src/inline-suppressions.ts +340 -0
- package/src/limit-band.ts +56 -0
- package/src/line-rules.ts +331 -468
- package/src/migrate-config.ts +670 -0
- package/src/naming-pushers.ts +32 -127
- package/src/parsed-script.ts +868 -314
- package/src/pillar-summary.ts +56 -40
- package/src/profiles.ts +86 -74
- package/src/project-config-rules.ts +324 -140
- package/src/project-rules.ts +237 -115
- package/src/public-exports.ts +102 -38
- package/src/report-html.ts +217 -85
- package/src/report-renderers.ts +821 -203
- package/src/rule-list.ts +166 -46
- package/src/rules.ts +100 -104
- package/src/safety-rules.ts +191 -132
- package/src/scoring.ts +249 -55
- package/src/security-flow-rules.ts +791 -115
- package/src/selectors.ts +169 -0
- package/src/sensitive-data-rules.ts +460 -387
- package/src/sensitive-exclusions.ts +435 -0
- package/src/source-text.ts +378 -104
- package/src/static-analysis-redundant-rules.ts +298 -41
- package/src/test-block-rules.ts +343 -253
- package/src/text-scans.ts +62 -11
- package/src/types.ts +220 -71
- package/tsconfig.json +2 -1
- package/src/process-exec-metadata.ts +0 -163
- package/src/test-fixtures.ts +0 -671
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
// GitHub Actions workflow
|
|
1
|
+
// Audits committed GitHub Actions workflow YAML files for supply-chain and pull-request security risks.
|
|
2
|
+
//
|
|
3
|
+
// Path-gated to `.github/workflows/*.yml` and `.yaml` so regular YAML config files never emit CI findings.
|
|
4
|
+
// Checks unpinned third-party actions, remote-shell pipes, unsafe `pull_request_target` checkouts, and secret exposure on PR triggers.
|
|
2
5
|
import type { SourceFile } from "./discovery.ts";
|
|
6
|
+
import { unreachableWorkflowSecretLines } from "./github-actions-event-guards.ts";
|
|
3
7
|
import { makeFinding } from "./findings.ts";
|
|
4
8
|
import type { Finding } from "./types.ts";
|
|
5
9
|
|
|
@@ -22,11 +26,6 @@ interface WorkflowFindingInput {
|
|
|
22
26
|
metadata: Record<string, unknown>;
|
|
23
27
|
}
|
|
24
28
|
|
|
25
|
-
// Shared indentation state for YAML block scans; deleting `indent` exits the current block.
|
|
26
|
-
interface IndentedBlockState {
|
|
27
|
-
indent?: number;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
29
|
// Active `run: |` / `run: >` block scalar. The start line is the finding anchor, while `lines`
|
|
31
30
|
// holds the shell body after YAML indentation has been stripped by `workflowLines`.
|
|
32
31
|
interface RunBlockState {
|
|
@@ -62,36 +61,42 @@ const WRITE_PERMISSION_SCOPES = new Set([
|
|
|
62
61
|
"statuses",
|
|
63
62
|
]);
|
|
64
63
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
]);
|
|
75
|
-
|
|
76
|
-
// Stable rule contract: workflow-only checks ignore non-workflow YAML so example docs avoid findings.
|
|
64
|
+
/**
|
|
65
|
+
* Checks YAML workflow source when `file` has a GitHub workflow display path, keeping example YAML outside workflows exempt.
|
|
66
|
+
* Under the stable rule contract, non-workflow paths leave `findings` unchanged.
|
|
67
|
+
*
|
|
68
|
+
* @param file - Source file descriptor whose `displayPath` gates analysis.
|
|
69
|
+
* @param source - Raw YAML file contents to inspect for workflow security findings.
|
|
70
|
+
* @param findings - Findings array that receives appended workflow security warnings.
|
|
71
|
+
* @returns Nothing. Appends any workflow findings to `findings` and leaves other paths unchanged.
|
|
72
|
+
*/
|
|
77
73
|
function analyseGithubActionsRules(file: SourceFile, source: string, findings: Finding[]): void {
|
|
78
74
|
if (!isGithubWorkflowPath(file.displayPath)) {
|
|
79
75
|
return;
|
|
80
76
|
}
|
|
81
77
|
const lines = workflowLines(source);
|
|
82
78
|
analysePullRequestTarget(file, lines, findings);
|
|
83
|
-
analyseBroadPermissions(file, lines, findings);
|
|
84
79
|
analyseUnpinnedActions(file, lines, findings);
|
|
85
80
|
analyseRemoteShell(file, lines, findings);
|
|
86
81
|
analyseSecretsInPullRequest(file, lines, findings);
|
|
87
82
|
}
|
|
88
83
|
|
|
89
|
-
|
|
84
|
+
/**
|
|
85
|
+
* Checks whether a display path names a YAML file directly under the root `.github/workflows` directory, ignoring case.
|
|
86
|
+
*
|
|
87
|
+
* @param displayPath - Relative repository path to test against the workflow directory pattern.
|
|
88
|
+
* @returns `true` when `displayPath` is a `.yml` or `.yaml` file directly inside `.github/workflows`.
|
|
89
|
+
*/
|
|
90
90
|
function isGithubWorkflowPath(displayPath: string): boolean {
|
|
91
91
|
return /^\.github\/workflows\/[^/]+\.ya?ml$/i.test(displayPath);
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Normalizes raw YAML lines into structured records while preserving leading-space indentation for block-state scans.
|
|
96
|
+
*
|
|
97
|
+
* @param source - Raw YAML workflow text split on LF or CRLF line endings.
|
|
98
|
+
* @returns Normalized line records with stripped inline comments, trimmed text, 1-based line numbers, and space indentation.
|
|
99
|
+
*/
|
|
95
100
|
function workflowLines(source: string): WorkflowLine[] {
|
|
96
101
|
return source.split(/\r?\n/).map((raw, index) => {
|
|
97
102
|
const text = stripInlineComment(raw);
|
|
@@ -100,19 +105,37 @@ function workflowLines(source: string): WorkflowLine[] {
|
|
|
100
105
|
});
|
|
101
106
|
}
|
|
102
107
|
|
|
103
|
-
|
|
108
|
+
/**
|
|
109
|
+
* Removes common YAML inline comments starting at `" #"` without attempting to parse quoted scalars.
|
|
110
|
+
*
|
|
111
|
+
* @param line - Single raw YAML line to strip.
|
|
112
|
+
* @returns Substring before the first `" #"`, or `line` unchanged when none is present.
|
|
113
|
+
*/
|
|
104
114
|
function stripInlineComment(line: string): string {
|
|
105
115
|
const comment = line.indexOf(" #");
|
|
106
116
|
return comment === -1 ? line : line.slice(0, comment);
|
|
107
117
|
}
|
|
108
118
|
|
|
109
|
-
|
|
119
|
+
/**
|
|
120
|
+
* Counts leading ASCII spaces on a line because workflow YAML should only use spaces, never tabs, for indentation.
|
|
121
|
+
*
|
|
122
|
+
* @param line - Raw YAML line whose leading indentation is measured.
|
|
123
|
+
* @returns Number of leading space characters at the start of `line`.
|
|
124
|
+
*/
|
|
110
125
|
function leadingSpaceCount(line: string): number {
|
|
111
126
|
const match = line.match(/^ */);
|
|
112
127
|
return match?.[0].length ?? 0;
|
|
113
128
|
}
|
|
114
129
|
|
|
115
|
-
|
|
130
|
+
/**
|
|
131
|
+
* Checks workflow lines for a `pull_request_target` event paired with a trusted-context risk signal anywhere in the workflow.
|
|
132
|
+
* Uses `file` and the first matching event line to emit a stable finding anchor.
|
|
133
|
+
*
|
|
134
|
+
* @param file - Workflow source file descriptor used for the finding path.
|
|
135
|
+
* @param lines - Normalized workflow lines to scan for the event and risk signals.
|
|
136
|
+
* @param findings - Findings array that receives the warning when both conditions match.
|
|
137
|
+
* @returns Nothing. Appends a `pull_request_target` warning to `findings` when both exist.
|
|
138
|
+
*/
|
|
116
139
|
function analysePullRequestTarget(file: SourceFile, lines: readonly WorkflowLine[], findings: Finding[]): void {
|
|
117
140
|
const eventLine = lines.find((line) => hasPullRequestTargetEvent(line));
|
|
118
141
|
if (!eventLine || !hasPullRequestTargetRiskContext(lines)) {
|
|
@@ -130,17 +153,33 @@ function analysePullRequestTarget(file: SourceFile, lines: readonly WorkflowLine
|
|
|
130
153
|
);
|
|
131
154
|
}
|
|
132
155
|
|
|
133
|
-
|
|
156
|
+
/**
|
|
157
|
+
* Checks whether a nonblank, noncomment workflow line contains the whole `pull_request_target` token using the line's text alone.
|
|
158
|
+
*
|
|
159
|
+
* @param line - Normalized workflow line to inspect.
|
|
160
|
+
* @returns `true` when `line` is not blank or a comment and matches the `pull_request_target` word token.
|
|
161
|
+
*/
|
|
134
162
|
function hasPullRequestTargetEvent(line: WorkflowLine): boolean {
|
|
135
163
|
return !isCommentOrBlank(line) && /\bpull_request_target\b/.test(line.trimmed);
|
|
136
164
|
}
|
|
137
165
|
|
|
138
|
-
|
|
166
|
+
/**
|
|
167
|
+
* Checks whether workflow lines contain any recognized risk signal so an event-only workflow produces no `pull_request_target` warning.
|
|
168
|
+
*
|
|
169
|
+
* @param lines - Normalized workflow lines to scan for risk indicators.
|
|
170
|
+
* @returns `true` when at least one risk context label is detected in `lines`.
|
|
171
|
+
*/
|
|
139
172
|
function hasPullRequestTargetRiskContext(lines: readonly WorkflowLine[]): boolean {
|
|
140
173
|
return pullRequestTargetRiskContext(lines).length > 0;
|
|
141
174
|
}
|
|
142
175
|
|
|
143
|
-
|
|
176
|
+
/**
|
|
177
|
+
* Scans workflow lines for checkout steps, `run` steps, secret references, and broad write permissions.
|
|
178
|
+
* Produces unique risk labels in stable sorted order for finding metadata.
|
|
179
|
+
*
|
|
180
|
+
* @param lines - Normalized workflow lines to inspect.
|
|
181
|
+
* @returns Sorted unique risk context labels; an empty array means no risk signal was recognized.
|
|
182
|
+
*/
|
|
144
183
|
function pullRequestTargetRiskContext(lines: readonly WorkflowLine[]): string[] {
|
|
145
184
|
const contexts = new Set<string>();
|
|
146
185
|
for (const line of lines) {
|
|
@@ -163,70 +202,12 @@ function pullRequestTargetRiskContext(lines: readonly WorkflowLine[]): string[]
|
|
|
163
202
|
return [...contexts].sort();
|
|
164
203
|
}
|
|
165
204
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
pushBroadPermissionFinding(file, findings, line, scope);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// Reports one finding per broad permission scope; stable scope symbols keep remediation targeted.
|
|
178
|
-
function pushBroadPermissionFinding(file: SourceFile, findings: Finding[], line: WorkflowLine, scope: string): void {
|
|
179
|
-
findings.push(
|
|
180
|
-
workflowFinding(file, {
|
|
181
|
-
ruleId: "security.github-actions-broad-permissions",
|
|
182
|
-
message: `Workflow grants broad write permission \`${scope}\`.`,
|
|
183
|
-
line: line.lineNumber,
|
|
184
|
-
symbol: scope,
|
|
185
|
-
remediation: "Reduce workflow permissions to read-only by default and grant write scopes only to trusted jobs.",
|
|
186
|
-
metadata: { permission: scope },
|
|
187
|
-
}),
|
|
188
|
-
);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
// Returns the broad permission scope for either inline permissions or an active permissions block.
|
|
192
|
-
function broadPermissionScope(line: WorkflowLine, state: IndentedBlockState): string | undefined {
|
|
193
|
-
if (isCommentOrBlank(line)) {
|
|
194
|
-
return undefined;
|
|
195
|
-
}
|
|
196
|
-
closeBlockWhenOutdented(state, line);
|
|
197
|
-
const inline = line.trimmed.match(/^permissions:\s*(write-all)\b/i);
|
|
198
|
-
if (inline?.[1]) {
|
|
199
|
-
return "write-all";
|
|
200
|
-
}
|
|
201
|
-
if (/^permissions:\s*$/i.test(line.trimmed)) {
|
|
202
|
-
state.indent = line.indent;
|
|
203
|
-
return undefined;
|
|
204
|
-
}
|
|
205
|
-
if (state.indent === undefined || line.indent <= state.indent) {
|
|
206
|
-
return undefined;
|
|
207
|
-
}
|
|
208
|
-
return scopedWritePermission(line);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
// Clears YAML block state once the scanner reaches a sibling or parent indentation level.
|
|
212
|
-
function closeBlockWhenOutdented(state: IndentedBlockState, line: WorkflowLine): void {
|
|
213
|
-
if (state.indent !== undefined && line.indent <= state.indent) {
|
|
214
|
-
delete state.indent;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
// Extracts the write scopes that grant a repository resource, which is what this rule reports.
|
|
219
|
-
function scopedWritePermission(line: WorkflowLine): string | undefined {
|
|
220
|
-
const scoped = line.trimmed.match(/^([a-z-]+):\s*write\b/i);
|
|
221
|
-
const scope = scoped?.[1] ?? "";
|
|
222
|
-
// A capability scope grants no repository resource, so requesting one is not a broad permission.
|
|
223
|
-
if (CAPABILITY_PERMISSION_SCOPES.has(scope)) {
|
|
224
|
-
return undefined;
|
|
225
|
-
}
|
|
226
|
-
return WRITE_PERMISSION_SCOPES.has(scope) ? scope : undefined;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
// Lightweight permission signal used by the pull_request_target risk-context gate.
|
|
205
|
+
/**
|
|
206
|
+
* Checks whether a line signals `write-all` permissions or a `write` grant for a recognized scope in the `pull_request_target` risk check.
|
|
207
|
+
*
|
|
208
|
+
* @param line - Normalized workflow line whose trimmed text is matched against permission rules.
|
|
209
|
+
* @returns `true` when `line` declares `permissions: write-all` or `<scope>: write` for a known write scope.
|
|
210
|
+
*/
|
|
230
211
|
function broadPermissionSignal(line: WorkflowLine): boolean {
|
|
231
212
|
if (/^permissions:\s*write-all\b/i.test(line.trimmed)) {
|
|
232
213
|
return true;
|
|
@@ -235,7 +216,14 @@ function broadPermissionSignal(line: WorkflowLine): boolean {
|
|
|
235
216
|
return WRITE_PERMISSION_SCOPES.has(scoped?.[1] ?? "");
|
|
236
217
|
}
|
|
237
218
|
|
|
238
|
-
|
|
219
|
+
/**
|
|
220
|
+
* Scans workflow lines for third-party action references whose refs fall outside the stable pinning contract of 40 hexadecimal characters.
|
|
221
|
+
*
|
|
222
|
+
* @param file - Workflow source file descriptor used for warning paths.
|
|
223
|
+
* @param lines - Normalized workflow lines to inspect for `uses:` entries.
|
|
224
|
+
* @param findings - Findings array that receives a warning for each unpinned third-party action.
|
|
225
|
+
* @returns Nothing. Appends warnings to `findings` for refs not pinned to a 40-character hexadecimal SHA.
|
|
226
|
+
*/
|
|
239
227
|
function analyseUnpinnedActions(file: SourceFile, lines: readonly WorkflowLine[], findings: Finding[]): void {
|
|
240
228
|
for (const line of lines) {
|
|
241
229
|
const action = thirdPartyActionUse(line);
|
|
@@ -255,7 +243,13 @@ function analyseUnpinnedActions(file: SourceFile, lines: readonly WorkflowLine[]
|
|
|
255
243
|
}
|
|
256
244
|
}
|
|
257
245
|
|
|
258
|
-
|
|
246
|
+
/**
|
|
247
|
+
* Extracts the action path, owner, and ref from a recognized external `uses:` entry.
|
|
248
|
+
* Local paths and owners `actions` or `github` are skipped because they are exempt from third-party pin checks.
|
|
249
|
+
*
|
|
250
|
+
* @param line - Normalized workflow line to parse for an external action reference.
|
|
251
|
+
* @returns Parsed `{ action, owner, ref }` object, or `undefined` when absent, unsupported, local, or exempt.
|
|
252
|
+
*/
|
|
259
253
|
function thirdPartyActionUse(line: WorkflowLine): { action: string; owner: string; ref: string } | undefined {
|
|
260
254
|
if (isCommentOrBlank(line)) {
|
|
261
255
|
return undefined;
|
|
@@ -273,12 +267,25 @@ function thirdPartyActionUse(line: WorkflowLine): { action: string; owner: strin
|
|
|
273
267
|
return { action, owner, ref };
|
|
274
268
|
}
|
|
275
269
|
|
|
276
|
-
|
|
270
|
+
/**
|
|
271
|
+
* Checks whether an action ref is exactly 40 hexadecimal characters, the required commit-pin form for third-party actions.
|
|
272
|
+
*
|
|
273
|
+
* @param ref - Action version, branch, tag, or commit SHA string after `@`.
|
|
274
|
+
* @returns `true` when `ref` consists of exactly 40 hexadecimal characters.
|
|
275
|
+
*/
|
|
277
276
|
function isPinnedToFullSha(ref: string): boolean {
|
|
278
277
|
return /^[0-9a-f]{40}$/i.test(ref);
|
|
279
278
|
}
|
|
280
279
|
|
|
281
|
-
|
|
280
|
+
/**
|
|
281
|
+
* Checks `run` commands extracted from workflow lines for a remote download piped to a shell.
|
|
282
|
+
* YAML block parsing preserves a stable source anchor for each command.
|
|
283
|
+
*
|
|
284
|
+
* @param file - Workflow source file descriptor used for the report path.
|
|
285
|
+
* @param lines - Normalized workflow lines to extract `run` commands from.
|
|
286
|
+
* @param findings - Findings array that receives a warning for each matching remote-shell command.
|
|
287
|
+
* @returns Nothing. Appends matching remote-shell warnings to `findings`.
|
|
288
|
+
*/
|
|
282
289
|
function analyseRemoteShell(file: SourceFile, lines: readonly WorkflowLine[], findings: Finding[]): void {
|
|
283
290
|
for (const command of runCommands(lines)) {
|
|
284
291
|
if (isRemoteShellCommand(command.command)) {
|
|
@@ -287,7 +294,14 @@ function analyseRemoteShell(file: SourceFile, lines: readonly WorkflowLine[], fi
|
|
|
287
294
|
}
|
|
288
295
|
}
|
|
289
296
|
|
|
290
|
-
|
|
297
|
+
/**
|
|
298
|
+
* Appends a workflow remote-shell warning for a matched `run` command using a stable source anchor.
|
|
299
|
+
*
|
|
300
|
+
* @param file - Workflow source file descriptor used for the finding path.
|
|
301
|
+
* @param findings - Findings array that receives the new warning.
|
|
302
|
+
* @param line - Workflow line anchoring the matched `run` step in the report.
|
|
303
|
+
* @returns Nothing. Appends one remote-shell finding to `findings`.
|
|
304
|
+
*/
|
|
291
305
|
function pushRemoteShellFinding(file: SourceFile, findings: Finding[], line: WorkflowLine): void {
|
|
292
306
|
findings.push(
|
|
293
307
|
workflowFinding(file, {
|
|
@@ -301,8 +315,13 @@ function pushRemoteShellFinding(file: SourceFile, findings: Finding[], line: Wor
|
|
|
301
315
|
);
|
|
302
316
|
}
|
|
303
317
|
|
|
304
|
-
|
|
305
|
-
|
|
318
|
+
/**
|
|
319
|
+
* Collects inline and block-scalar `run` commands with their source lines in workflow source order.
|
|
320
|
+
* Block bodies use trimmed lines so remote-shell findings stay deterministic.
|
|
321
|
+
*
|
|
322
|
+
* @param lines - Normalized workflow lines to scan for `run:` entries and block bodies.
|
|
323
|
+
* @returns Extracted `run` commands paired with their anchor lines, or an empty list if none are recognized.
|
|
324
|
+
*/
|
|
306
325
|
function runCommands(lines: readonly WorkflowLine[]): WorkflowCommand[] {
|
|
307
326
|
const commands: WorkflowCommand[] = [];
|
|
308
327
|
const state: RunBlockState = { lines: [] };
|
|
@@ -317,16 +336,29 @@ function runCommands(lines: readonly WorkflowLine[]): WorkflowCommand[] {
|
|
|
317
336
|
return commands;
|
|
318
337
|
}
|
|
319
338
|
|
|
320
|
-
|
|
321
|
-
|
|
339
|
+
/**
|
|
340
|
+
* Flushes an active `run` block when indentation drops to or below the block header level, allowing the caller to inspect `line` next.
|
|
341
|
+
* Leaves `state` and `commands` unchanged when no block is closed.
|
|
342
|
+
*
|
|
343
|
+
* @param line - Current workflow line whose indentation may close the active block.
|
|
344
|
+
* @param state - Mutable run-block accumulator cleared when the active block closes.
|
|
345
|
+
* @param commands - Command list that receives the flushed block command when a start line is present.
|
|
346
|
+
* @returns Nothing. Appends any closed block command to `commands` and resets `state`.
|
|
347
|
+
*/
|
|
322
348
|
function flushClosedRunBlock(line: WorkflowLine, state: RunBlockState, commands: WorkflowCommand[]): void {
|
|
323
349
|
if (state.indent !== undefined && line.indent <= state.indent) {
|
|
324
350
|
flushRunBlock(state, commands);
|
|
325
351
|
}
|
|
326
352
|
}
|
|
327
353
|
|
|
328
|
-
|
|
329
|
-
|
|
354
|
+
/**
|
|
355
|
+
* Appends a line's trimmed text to `state.lines` when indented deeper than the active `run` key, leaving `state` unchanged otherwise.
|
|
356
|
+
* The remote-shell check uses this normalized text, which can omit content after a space and hash.
|
|
357
|
+
*
|
|
358
|
+
* @param line - Normalized workflow line to test against the active run-block indentation.
|
|
359
|
+
* @param state - Mutable run-block state holding the active `indent` and accumulated `lines`.
|
|
360
|
+
* @returns `true` when `line` belongs to the active block and was captured; `false` otherwise.
|
|
361
|
+
*/
|
|
330
362
|
function runBlockLine(line: WorkflowLine, state: RunBlockState): boolean {
|
|
331
363
|
if (state.indent === undefined || line.indent <= state.indent) {
|
|
332
364
|
return false;
|
|
@@ -335,8 +367,15 @@ function runBlockLine(line: WorkflowLine, state: RunBlockState): boolean {
|
|
|
335
367
|
return true;
|
|
336
368
|
}
|
|
337
369
|
|
|
338
|
-
|
|
339
|
-
|
|
370
|
+
/**
|
|
371
|
+
* Parses a line as a `run` entry, either initializing block-scalar collection in `state` or appending an inline command to `commands`.
|
|
372
|
+
* Blank, comment-only, and non-`run` lines leave `state` and `commands` unchanged.
|
|
373
|
+
*
|
|
374
|
+
* @param line - Normalized workflow line to inspect for a `run:` prefix.
|
|
375
|
+
* @param state - Mutable run-block state reset when `line` starts a block scalar.
|
|
376
|
+
* @param commands - Command list that receives inline `run` commands.
|
|
377
|
+
* @returns Nothing. Mutates `state` for block scalars or appends inline commands to `commands`.
|
|
378
|
+
*/
|
|
340
379
|
function startOrPushRunCommand(line: WorkflowLine, state: RunBlockState, commands: WorkflowCommand[]): void {
|
|
341
380
|
if (isCommentOrBlank(line)) {
|
|
342
381
|
return;
|
|
@@ -354,8 +393,14 @@ function startOrPushRunCommand(line: WorkflowLine, state: RunBlockState, command
|
|
|
354
393
|
}
|
|
355
394
|
}
|
|
356
395
|
|
|
357
|
-
|
|
358
|
-
|
|
396
|
+
/**
|
|
397
|
+
* Emits any pending `run` block command from `state` to `commands` and clears `state`.
|
|
398
|
+
* Missing `indent` or `startLine` skips command emission while still resetting `state`.
|
|
399
|
+
*
|
|
400
|
+
* @param state - Mutable run-block state whose pending lines, `indent`, and `startLine` are cleared.
|
|
401
|
+
* @param commands - Command list that receives the joined block command when active.
|
|
402
|
+
* @returns Nothing. Appends the pending block command to `commands` when complete and resets `state`.
|
|
403
|
+
*/
|
|
359
404
|
function flushRunBlock(state: RunBlockState, commands: WorkflowCommand[]): void {
|
|
360
405
|
if (state.indent !== undefined && state.startLine) {
|
|
361
406
|
commands.push({ command: state.lines.join("\n"), line: state.startLine });
|
|
@@ -365,59 +410,221 @@ function flushRunBlock(state: RunBlockState, commands: WorkflowCommand[]): void
|
|
|
365
410
|
state.lines = [];
|
|
366
411
|
}
|
|
367
412
|
|
|
368
|
-
|
|
413
|
+
/**
|
|
414
|
+
* Checks whether a `run` value matches a YAML block scalar header pattern, including forms such as `|-`, `>2`, or `|+4`.
|
|
415
|
+
*
|
|
416
|
+
* @param command - Inline string following `run:` on the header line.
|
|
417
|
+
* @returns `true` when `command` is a block scalar indicator (`|` or `>`) with optional chomping or indent modifiers.
|
|
418
|
+
*/
|
|
369
419
|
function isRunBlockScalar(command: string): boolean {
|
|
370
420
|
return /^[|>](?:[+-]?\d*|\d*[+-]?)?\s*$/.test(command);
|
|
371
421
|
}
|
|
372
422
|
|
|
373
|
-
|
|
423
|
+
/**
|
|
424
|
+
* Checks whether command text matches a `curl` or `wget` HTTP(S) download piped to `sh`, `bash`, or `zsh`, with optional `sudo`.
|
|
425
|
+
*
|
|
426
|
+
* @param command - Shell command text extracted from an inline or block-scalar `run` step.
|
|
427
|
+
* @returns `true` when `command` pipes an HTTP or HTTPS `curl` or `wget` fetch into a shell.
|
|
428
|
+
*/
|
|
374
429
|
function isRemoteShellCommand(command: string): boolean {
|
|
375
430
|
return /\b(?:curl|wget)\b[^|]*https?:\/\/[^|]*\|\s*(?:sudo\s+)?(?:sh|bash|zsh)\b/i.test(command);
|
|
376
431
|
}
|
|
377
432
|
|
|
378
|
-
|
|
433
|
+
/**
|
|
434
|
+
* Checks workflow lines for named secret references only when the workflow declares `pull_request_target`.
|
|
435
|
+
* Under the stable rule contract, skips `GITHUB_TOKEN` and lines identified as unreachable for this event.
|
|
436
|
+
*
|
|
437
|
+
* @param file - Workflow source file descriptor used for finding paths.
|
|
438
|
+
* @param lines - Normalized workflow lines to check for triggers and secret references.
|
|
439
|
+
* @param findings - Findings array that receives at most one secret warning per line.
|
|
440
|
+
* @returns Nothing. Appends matching secret findings to `findings`.
|
|
441
|
+
*/
|
|
379
442
|
function analyseSecretsInPullRequest(file: SourceFile, lines: readonly WorkflowLine[], findings: Finding[]): void {
|
|
380
|
-
if (!
|
|
443
|
+
if (!declaredWorkflowEvents(lines).has("pull_request_target")) {
|
|
381
444
|
return;
|
|
382
445
|
}
|
|
446
|
+
const unreachable = unreachableWorkflowSecretLines(lines.map((line) => line.raw).join("\n"), ["pull_request_target"]);
|
|
383
447
|
for (const line of lines) {
|
|
384
448
|
const secretName = secretReference(line);
|
|
385
|
-
if (!secretName) {
|
|
449
|
+
if (!secretName || unreachable.has(line.lineNumber)) {
|
|
386
450
|
continue;
|
|
387
451
|
}
|
|
388
452
|
findings.push(
|
|
389
453
|
workflowFinding(file, {
|
|
390
454
|
ruleId: "security.github-actions-secrets-in-pr",
|
|
391
|
-
message: `
|
|
455
|
+
message: `pull_request_target workflow references secret \`${secretName}\`.`,
|
|
392
456
|
line: line.lineNumber,
|
|
393
457
|
symbol: secretName,
|
|
394
458
|
remediation: "Avoid exposing secrets to pull request workflows unless the code path is trusted and tightly gated.",
|
|
395
|
-
metadata: { event: "
|
|
459
|
+
metadata: { event: "pull_request_target", secretName },
|
|
396
460
|
}),
|
|
397
461
|
);
|
|
398
462
|
}
|
|
399
463
|
}
|
|
400
464
|
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
465
|
+
/**
|
|
466
|
+
* Reads recognized event names from the top-level `on` key so `pull_request_target` secret checks only fire on workflows that declare that trigger.
|
|
467
|
+
* Supports scalar, inline/multiline flow, and indented/indentless block sequences because YAML allows all four forms for `on:`.
|
|
468
|
+
*
|
|
469
|
+
* @param lines - Normalized workflow lines to scan for top-level `on` declarations.
|
|
470
|
+
* @returns Set of recognized trigger event names, or an empty set when none are declared so the `pull_request_target` secret check is skipped.
|
|
471
|
+
*/
|
|
472
|
+
function declaredWorkflowEvents(lines: readonly WorkflowLine[]): Set<string> {
|
|
473
|
+
const events = new Set<string>();
|
|
474
|
+
const state: OnBlockState = { inOnBlock: false, eventIndent: undefined, flowDepth: 0 };
|
|
475
|
+
for (const line of lines) {
|
|
476
|
+
if (!isCommentOrBlank(line)) {
|
|
477
|
+
consumeWorkflowEventLine(line, events, state);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
return events;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// Mutable parser state tracked while walking lines under a workflow's top-level `on:` trigger declaration.
|
|
484
|
+
interface OnBlockState {
|
|
485
|
+
inOnBlock: boolean;
|
|
486
|
+
eventIndent: number | undefined;
|
|
487
|
+
flowDepth: number;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Updates trigger-event state from one non-blank workflow line because YAML allows multiline flow arrays and indentless sequence items under `on:`.
|
|
492
|
+
*
|
|
493
|
+
* @param line - Normalized non-comment workflow line being scanned.
|
|
494
|
+
* @param events - Mutable trigger-event set updated in place.
|
|
495
|
+
* @param state - Mutable `on:` block tracking state updated in place.
|
|
496
|
+
* @returns Nothing. Adds any recognized event names from `line` into `events`.
|
|
497
|
+
*/
|
|
498
|
+
function consumeWorkflowEventLine(line: WorkflowLine, events: Set<string>, state: OnBlockState): void {
|
|
499
|
+
// Multiline flow declarations like `on: [\n pull_request_target,\n issues\n]` keep collecting until outer flow depth returns to zero.
|
|
500
|
+
if (state.flowDepth > 0) {
|
|
501
|
+
const cleaned = stripInlineComment(line.trimmed).trim();
|
|
502
|
+
addFlowEvents(events, cleaned);
|
|
503
|
+
state.flowDepth = Math.max(0, state.flowDepth + flowDelimiterDelta(cleaned));
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
// Indentless block sequence items (`on:\n- pull_request_target`) sit at indent 0 and belong to the active `on:` block.
|
|
507
|
+
if (line.indent === 0 && !(state.inOnBlock && line.trimmed.startsWith("-"))) {
|
|
508
|
+
const onKey = line.trimmed.match(/^["']?on["']?\s*:\s*(.*)$/);
|
|
509
|
+
const inlineValue = onKey ? stripInlineComment(onKey[1] ?? "").trim() : "";
|
|
510
|
+
state.inOnBlock = onKey !== null && inlineValue === "";
|
|
511
|
+
state.eventIndent = undefined;
|
|
512
|
+
if (onKey && !state.inOnBlock) {
|
|
513
|
+
addFlowEvents(events, inlineValue);
|
|
514
|
+
state.flowDepth = inlineValue.startsWith("[") || inlineValue.startsWith("{")
|
|
515
|
+
? Math.max(0, flowDelimiterDelta(inlineValue))
|
|
516
|
+
: 0;
|
|
517
|
+
}
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
if (!state.inOnBlock) {
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
consumeNestedOnEventLine(line, events, state);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Extracts trigger events from a line indented under top-level `on:`, supporting both block entries and nested flow collections.
|
|
528
|
+
*
|
|
529
|
+
* @param line - Normalized non-comment workflow line nested under `on:`.
|
|
530
|
+
* @param events - Mutable trigger-event set updated in place.
|
|
531
|
+
* @param state - Mutable `on:` block tracking state updated in place.
|
|
532
|
+
* @returns Nothing. Adds recognized event names from `line` into `events` and updates flow state when nested flow syntax starts.
|
|
533
|
+
*/
|
|
534
|
+
function consumeNestedOnEventLine(line: WorkflowLine, events: Set<string>, state: OnBlockState): void {
|
|
535
|
+
// The first nested line fixes the event level; deeper lines configure one event, such as its branches.
|
|
536
|
+
state.eventIndent ??= line.indent;
|
|
537
|
+
// Deeper lines configure one event, such as its branch filter, and never name a new event.
|
|
538
|
+
if (line.indent !== state.eventIndent) {
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
const cleaned = stripInlineComment(line.trimmed).trim();
|
|
542
|
+
// Flow sequences or mappings nested under `on:` (e.g. `on:\n [push, pull_request_target]`) declare events directly.
|
|
543
|
+
if (cleaned.startsWith("[") || cleaned.startsWith("{")) {
|
|
544
|
+
addFlowEvents(events, cleaned);
|
|
545
|
+
state.flowDepth = Math.max(0, flowDelimiterDelta(cleaned));
|
|
546
|
+
state.inOnBlock = false;
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
const event = cleaned.match(/^(?:-\s*)?["']?([A-Za-z_]+)["']?\s*(?::|$)/)?.[1];
|
|
550
|
+
// A line naming an event, such as `push:` or `- pull_request`, adds it to the workflow's triggers.
|
|
551
|
+
if (event) {
|
|
552
|
+
events.add(event);
|
|
553
|
+
}
|
|
404
554
|
}
|
|
405
555
|
|
|
406
|
-
|
|
556
|
+
/**
|
|
557
|
+
* Counts net flow-collection bracket nesting (`[`/`{` minus `]`/`}`) on one YAML line so nested flow maps do not close `on:` early.
|
|
558
|
+
*
|
|
559
|
+
* @param text - Trimmed YAML flow text with inline comments already stripped.
|
|
560
|
+
* @returns Positive count for unclosed openers, zero for balanced delimiters, or negative when closers exceed openers.
|
|
561
|
+
*/
|
|
562
|
+
function flowDelimiterDelta(text: string): number {
|
|
563
|
+
let delta = 0;
|
|
564
|
+
for (const character of text) {
|
|
565
|
+
if (character === "[" || character === "{") {
|
|
566
|
+
delta += 1;
|
|
567
|
+
} else if (character === "]" || character === "}") {
|
|
568
|
+
delta -= 1;
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
return delta;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* Parses event names from a scalar or simple flow sequence or mapping declaration and adds non-empty names to `events`.
|
|
576
|
+
*
|
|
577
|
+
* @param events - Mutable event set that receives parsed trigger names.
|
|
578
|
+
* @param declaration - Inline value following top-level `on:` after stripping inline comments.
|
|
579
|
+
* @returns Nothing. Adds recognized non-empty event names to `events`.
|
|
580
|
+
*/
|
|
581
|
+
function addFlowEvents(events: Set<string>, declaration: string): void {
|
|
582
|
+
for (const flowEntry of declaration.replace(/^[[{]/, "").replace(/[\]}]$/, "").split(",")) {
|
|
583
|
+
const event = (flowEntry.split(":")[0] ?? "").trim().replace(/^["']|["']$/g, "");
|
|
584
|
+
if (event) {
|
|
585
|
+
events.add(event);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Extracts the first case-sensitive `secrets.NAME` identifier on a line while excluding `GITHUB_TOKEN`.
|
|
592
|
+
* Returning the name alone keeps the raw expression out of finding metadata.
|
|
593
|
+
*
|
|
594
|
+
* @param line - Normalized workflow line whose trimmed text is scanned for secret expressions.
|
|
595
|
+
* @returns First non-`GITHUB_TOKEN` secret name, or `undefined` for blank, comment-only, or unmatched lines.
|
|
596
|
+
*/
|
|
407
597
|
function secretReference(line: WorkflowLine): string | undefined {
|
|
408
598
|
if (isCommentOrBlank(line)) {
|
|
409
599
|
return undefined;
|
|
410
600
|
}
|
|
411
|
-
|
|
412
|
-
|
|
601
|
+
// Excluding GITHUB_TOKEN does not end the search because a later reference on the same line can name another secret.
|
|
602
|
+
for (const match of line.trimmed.matchAll(/\bsecrets\.([A-Za-z_][A-Za-z0-9_]*)\b/g)) {
|
|
603
|
+
if (match[1] !== "GITHUB_TOKEN") {
|
|
604
|
+
return match[1];
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
return undefined;
|
|
413
608
|
}
|
|
414
609
|
|
|
415
|
-
|
|
610
|
+
/**
|
|
611
|
+
* Checks whether a line's trimmed YAML text is empty or comment-only so callers can skip it during text heuristics.
|
|
612
|
+
*
|
|
613
|
+
* @param line - Normalized workflow line to test.
|
|
614
|
+
* @returns `true` when `line.trimmed` is empty or starts with `#`.
|
|
615
|
+
*/
|
|
416
616
|
function isCommentOrBlank(line: WorkflowLine): boolean {
|
|
417
617
|
return line.trimmed.length === 0 || line.trimmed.startsWith("#");
|
|
418
618
|
}
|
|
419
619
|
|
|
420
|
-
|
|
620
|
+
/**
|
|
621
|
+
* Builds a workflow security warning using a stable finding contract across workflow rules.
|
|
622
|
+
* Omits `symbol` when `input` supplies none or an empty string.
|
|
623
|
+
*
|
|
624
|
+
* @param file - Workflow source file descriptor providing `displayPath` for the report path.
|
|
625
|
+
* @param input - Rule identifier, message, source line, optional symbol, remediation, and metadata payload.
|
|
626
|
+
* @returns Constructed security warning `Finding` ready to append to results.
|
|
627
|
+
*/
|
|
421
628
|
function workflowFinding(file: SourceFile, input: WorkflowFindingInput): Finding {
|
|
422
629
|
return makeFinding({
|
|
423
630
|
ruleId: input.ruleId,
|