Repository navigation
fix(lanes): scan every lane and eval upload for credentials before it leaves #11806
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
53326e3
fix(lanes): scan every lane and eval upload for credentials before it…
kyle-sexton a26ff79
fix(lanes): gate expression-form upload conditions on the scan
kyle-sexton eab9ef5
fix(lanes): run the upload scan from a staged copy where a head check…
kyle-sexton fbfd156
Merge commit '3a15fc15bcc45082433a63b2f5eb510882bf6410' into fix/scan…
kyle-sexton 801bde5
Merge branch 'main' into fix/scan-every-lane-upload
kyle-sexton 7d3fba3
test(ci): run the scan-upload suite in the test-node lane
kyle-sexton 44049c4
test(ci): build the scan-upload password fixture at run time
kyle-sexton da15fc2
fix(ci): let a missing scanner not skip the ci-status re-run
kyle-sexton c1afd2b
fix(ci): do not scan the pr-merge integer-only run records
kyle-sexton 946099b
fix(ci): keep pr-merge's best-effort uploads best-effort after a scan…
kyle-sexton e981c5a
Merge remote-tracking branch 'origin/main' into fix/scan-every-lane-u…
kyle-sexton 192879b
fix(lanes): scan the pr-merge run records again and fit the tests to …
kyle-sexton 98da50e
Merge origin/main
kyle-sexton dd68bff
fix(lanes): let a failed pr-merge record scan fail the job
kyle-sexton baed6fd
Merge origin/main
kyle-sexton 54c0f87
Merge origin/main into fix/scan-every-lane-upload
kyle-sexton 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,29 @@ | ||
| name: Scan an upload for credentials | ||
| description: > | ||
| Run lib/publish-gate.mjs scanLeak over every file an actions/upload-artifact | ||
| step is about to send, with the same `path` input. A credential, a hidden | ||
| assignment, a link, a compressed file or a glob fails the step and sets no | ||
| `clean` output; the upload's `if:` must require clean == 'true', so a hit | ||
| withholds the upload even when the upload runs always(). Nothing printed | ||
| carries a matched value or a file name. Needs node 18 or later on PATH and | ||
| lib/publish-gate.mjs three directories above this action. | ||
|
|
||
| inputs: | ||
| path: | ||
| description: The upload step's `path` input, verbatim. | ||
| required: true | ||
|
|
||
| outputs: | ||
| clean: | ||
| description: "`true` only when every file was judged clean." | ||
| value: ${{ steps.scan.outputs.clean }} | ||
|
|
||
| runs: | ||
| using: composite | ||
| steps: | ||
| - name: Scan the files | ||
| id: scan | ||
| shell: bash | ||
| env: | ||
| SCAN_PATHS: ${{ inputs.path }} | ||
| run: node "$GITHUB_ACTION_PATH/scan-upload.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,7 @@ | ||
| { | ||
| "name": "scan-upload", | ||
| "private": true, | ||
| "scripts": { | ||
| "test": "node --test" | ||
| } | ||
| } |
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,101 @@ | ||
| #!/usr/bin/env node | ||
| // The pre-upload leak scan for lane and eval artifacts: scanLeak from | ||
| // lib/publish-gate.mjs, the rule set the read runner's leak-scan step uses, | ||
| // over every file an upload is about to send, its relative name included. | ||
| // A credential format or a hidden assignment withholds the upload; the broad | ||
| // assignment label only warns. A link, a non-regular file, a glob or `!` | ||
| // entry, a compressed file (whose bytes the scan cannot read) or an | ||
| // unreadable path is a fault, which withholds it too. Nothing it prints | ||
| // carries a matched value or a file name. | ||
| // | ||
| // SCAN_PATHS=<the upload's path input> scan-upload.mjs | ||
| // Writes clean=true to GITHUB_OUTPUT only when every file was judged clean. | ||
| // Exit 0 clean, 1 withheld. | ||
|
|
||
| import { appendFileSync, lstatSync, readdirSync, readFileSync } from "node:fs"; | ||
| import path from "node:path"; | ||
| import process from "node:process"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import { scanLeak, WARN_ONLY } from "../../../lib/publish-gate.mjs"; | ||
|
|
||
| const COMPRESSED = [Buffer.from([0x1f, 0x8b]), Buffer.from("PK\x03\x04", "latin1"), Buffer.from("(\xb5/\xfd", "latin1")]; | ||
|
|
||
| /** | ||
| * Every file under each of `entries` (the upload's path lines), judged by | ||
| * scanLeak. Returns `{ withheld: {label, credential} | null, warned, files }`. | ||
| */ | ||
| export function scanPaths(entries) { | ||
| let warned = false; | ||
| let files = 0; | ||
| const withhold = (label, credential = false) => ({ withheld: { label, credential }, warned, files }); | ||
| const judge = (bytes) => { | ||
| const { block, credential, warn } = scanLeak(bytes); | ||
| if (block) return { label: block, credential }; | ||
| warned ||= Boolean(warn); | ||
| return null; | ||
| }; | ||
| const walk = (at, name) => { | ||
| let stat; | ||
| try { | ||
| stat = lstatSync(at); | ||
| } catch { | ||
| return { label: "a path the scan cannot read" }; | ||
| } | ||
| if (stat.isSymbolicLink()) return { label: "a link, which the upload would follow" }; | ||
| if (stat.isDirectory()) { | ||
| for (const child of readdirSync(at).sort()) { | ||
| const hit = walk(path.join(at, child), name ? `${name}/${child}` : child); | ||
| if (hit) return hit; | ||
| } | ||
| return null; | ||
| } | ||
| if (!stat.isFile()) return { label: "a file that is not a regular file" }; | ||
| files += 1; | ||
| const bytes = readFileSync(at); | ||
| if (COMPRESSED.some((magic) => bytes.subarray(0, magic.length).equals(magic))) { | ||
| return { label: "a compressed file, whose bytes the scan cannot read" }; | ||
| } | ||
| return judge(Buffer.from(name, "utf8")) ?? judge(bytes); | ||
| }; | ||
| for (const line of entries) { | ||
| const entry = line.trim(); | ||
| if (entry === "") continue; | ||
| if (entry.startsWith("!") || /[*?[\]{}]/.test(entry)) return withhold("a glob or exclude pattern, which the scan cannot follow"); | ||
| let exists = true; | ||
| try { | ||
| lstatSync(entry); | ||
| } catch { | ||
| exists = false; | ||
| } | ||
| // A missing path uploads nothing; the upload's if-no-files-found decides it. | ||
| if (!exists) continue; | ||
| const hit = walk(entry, lstatSync(entry).isDirectory() ? "" : path.basename(entry)); | ||
| if (hit) return { withheld: hit, warned, files }; | ||
| } | ||
| return { withheld: null, warned, files }; | ||
| } | ||
|
|
||
| export function main(env = process.env) { | ||
| const entries = String(env.SCAN_PATHS ?? "").split(/\r?\n/); | ||
| let result; | ||
| try { | ||
| result = scanPaths(entries); | ||
| } catch (error) { | ||
| result = { withheld: { label: `no finished scan (${error.name})`, credential: false }, warned: false, files: 0 }; | ||
| } | ||
| const { withheld, warned, files } = result; | ||
| if (withheld) { | ||
| process.stdout.write( | ||
| `::error::scan-upload: ${withheld.label}; nothing is uploaded${withheld.credential ? ". Rotate this credential" : ""}\n`, | ||
| ); | ||
| return 1; | ||
| } | ||
| if (warned) process.stdout.write(`::warning::scan-upload: ${WARN_ONLY} in an upload file; not blocking, check it is no real credential\n`); | ||
| if (env.GITHUB_OUTPUT) appendFileSync(env.GITHUB_OUTPUT, "clean=true\n"); | ||
| process.stdout.write(`scan-upload: ${files} file(s) clean\n`); | ||
| return 0; | ||
| } | ||
|
|
||
| if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { | ||
| process.exitCode = main(); | ||
| } |
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,94 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { spawnSync } from "node:child_process"; | ||
| import { mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import path from "node:path"; | ||
| import { test } from "node:test"; | ||
| import { gzipSync } from "node:zlib"; | ||
|
|
||
| const HERE = import.meta.dirname; | ||
| const SCRIPT = path.join(HERE, "scan-upload.mjs"); | ||
| // Built at run time, so no tracked file holds a credential shape: a GitHub | ||
| // token prefix and 36 letters spelling FAKE, never a live token. | ||
| const PLANTED = ["gh", "p_", "FAKE".repeat(9)].join(""); | ||
|
|
||
| function workDir(t) { | ||
| const dir = mkdtempSync(path.join(tmpdir(), "scan-upload-")); | ||
| t.after(() => rmSync(dir, { recursive: true, force: true })); | ||
| return dir; | ||
| } | ||
|
|
||
| function run(dir, paths) { | ||
| const output = path.join(dir, "github-output"); | ||
| writeFileSync(output, ""); | ||
| const result = spawnSync(process.execPath, [SCRIPT], { | ||
| env: { PATH: process.env.PATH, SCAN_PATHS: paths, GITHUB_OUTPUT: output }, | ||
| encoding: "utf8", | ||
| }); | ||
| return { ...result, outputs: readFileSync(output, "utf8") }; | ||
| } | ||
|
|
||
| function withheld(result, label) { | ||
| assert.equal(result.status, 1, result.stdout + result.stderr); | ||
| assert.equal(result.outputs, "", "a withheld upload sets no clean output"); | ||
| assert.match(result.stdout, new RegExp(`::error::scan-upload: ${label}`)); | ||
| assert.ok(!result.stdout.includes(PLANTED) && !result.stderr.includes(PLANTED), "the matched value is never printed"); | ||
| } | ||
|
|
||
| test("clean files pass and set clean=true", (t) => { | ||
| const dir = workDir(t); | ||
| mkdirSync(path.join(dir, "out", "nested"), { recursive: true }); | ||
| writeFileSync(path.join(dir, "out", "reply.txt"), "a clean reply\n"); | ||
| writeFileSync(path.join(dir, "out", "nested", "verdict.json"), '{"verdict":"merge"}\n'); | ||
| const result = run(dir, `${path.join(dir, "out")}\n${path.join(dir, "absent.json")}\n`); | ||
| assert.equal(result.status, 0, result.stdout + result.stderr); | ||
| assert.equal(result.outputs, "clean=true\n"); | ||
| assert.match(result.stdout, /2 file\(s\) clean/); | ||
| }); | ||
|
|
||
| test("a planted credential withholds the upload and asks for rotation, never printing the value", (t) => { | ||
| const dir = workDir(t); | ||
| mkdirSync(path.join(dir, "out")); | ||
| writeFileSync(path.join(dir, "out", "clean.txt"), "nothing here\n"); | ||
| writeFileSync(path.join(dir, "out", "handoff.json"), `{"review":"token ${PLANTED} in the text"}\n`); | ||
| const result = run(dir, path.join(dir, "out")); | ||
| withheld(result, "GitHub token; nothing is uploaded"); | ||
| assert.match(result.stdout, /Rotate this credential/); | ||
| }); | ||
|
|
||
| test("a credential in a file name withholds the upload", (t) => { | ||
| const dir = workDir(t); | ||
| mkdirSync(path.join(dir, "out")); | ||
| writeFileSync(path.join(dir, "out", `${PLANTED}.txt`), "clean bytes\n"); | ||
| withheld(run(dir, path.join(dir, "out")), "GitHub token"); | ||
| }); | ||
|
|
||
| test("a link, a compressed file and a glob each withhold the upload", (t) => { | ||
| const dir = workDir(t); | ||
| writeFileSync(path.join(dir, "target.txt"), "clean\n"); | ||
| symlinkSync(path.join(dir, "target.txt"), path.join(dir, "link.txt")); | ||
| withheld(run(dir, path.join(dir, "link.txt")), "a link"); | ||
| writeFileSync(path.join(dir, "trace.gz"), gzipSync(Buffer.from(PLANTED))); | ||
| withheld(run(dir, path.join(dir, "trace.gz")), "a compressed file"); | ||
| withheld(run(dir, path.join(dir, "*.txt")), "a glob or exclude pattern"); | ||
| withheld(run(dir, `!${path.join(dir, "target.txt")}`), "a glob or exclude pattern"); | ||
| }); | ||
|
|
||
| test("the broad assignment label only warns", (t) => { | ||
| const dir = workDir(t); | ||
| // Joined at run time, so no tracked line reads as a password assignment. | ||
| writeFileSync(path.join(dir, "fixture.txt"), `${["pass", "word"].join("")} = "not-a-real-one"\n`); | ||
| const result = run(dir, path.join(dir, "fixture.txt")); | ||
| assert.equal(result.status, 0, result.stdout + result.stderr); | ||
| assert.equal(result.outputs, "clean=true\n"); | ||
| assert.match(result.stdout, /::warning::scan-upload: password or secret assignment/); | ||
| }); | ||
|
|
||
| test("the lane transcript is scanned as plaintext before it is sealed", () => { | ||
| const action = readFileSync(path.join(HERE, "..", "upload-lane-transcript", "action.yml"), "utf8"); | ||
| const scan = action.indexOf("id: scan\n"); | ||
| const seal = action.indexOf("id: seal\n"); | ||
| assert.ok(scan > 0 && scan < seal, "the scan step comes before the seal step"); | ||
| assert.match(action, /SCAN_PATHS: \$\{\{ inputs\.execution-file \}\}\n\s+run: node "\$GITHUB_ACTION_PATH\/\.\.\/scan-upload\/scan-upload\.mjs"/); | ||
| assert.match(action, /id: seal\n\s+if: steps\.scan\.outputs\.clean == 'true'\n/); | ||
| }); |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.