diff --git a/.changeset/ban-pull-request-target.md b/.changeset/ban-pull-request-target.md new file mode 100644 index 00000000..9377cc1d --- /dev/null +++ b/.changeset/ban-pull-request-target.md @@ -0,0 +1,5 @@ +--- +"@stll/oxlint-config": minor +--- + +`stll-toolchain-check` rejects the `pull_request_target` workflow trigger with the new mandatory `workflow-trigger` rule. It has no allowlist or opt-out: use an unprivileged `pull_request` workflow and, only for a step that needs secrets or write access, a separate `workflow_run` workflow that treats the first run's output as untrusted data. diff --git a/.github/workflows/cla-request.yml b/.github/workflows/cla-request.yml new file mode 100644 index 00000000..0230fead --- /dev/null +++ b/.github/workflows/cla-request.yml @@ -0,0 +1,21 @@ +name: CLA Request + +# Unprivileged half of the CLA check: no secrets and a read-only token, so it +# is safe on fork pull requests. Its completion triggers the CLA Assistant +# workflow (`workflow_run`), which runs default-branch code, resolves the pull +# request and reports the CLA check on its head. Never name this job `cla`: a +# job of that name would report a check that verifies nothing. + +on: + pull_request: + types: [opened, synchronize, reopened, ready_for_review] + +permissions: {} + +jobs: + request: + name: CLA request + runs-on: ubuntu-latest + timeout-minutes: 2 + steps: + - run: echo "The CLA Assistant workflow verifies this head." diff --git a/.github/workflows/cla.yml b/.github/workflows/cla.yml index b14ee79c..88953227 100644 --- a/.github/workflows/cla.yml +++ b/.github/workflows/cla.yml @@ -5,31 +5,48 @@ on: types: [created] merge_group: types: [checks_requested] - pull_request_target: # zizmor: ignore[dangerous-triggers] delegates to pinned reusable CLA workflow; never check out PR code here - types: [opened, synchronize, reopened, ready_for_review] + # Pull requests: the unprivileged CLA Request workflow completes, then this + # default-branch workflow verifies the head it ran on. + workflow_run: # zizmor: ignore[dangerous-triggers] API-only; the triggering run is untrusted data and nothing from it executes + workflows: [CLA Request] + types: [completed] -permissions: - contents: read - issues: write - pull-requests: read - checks: write # Reports the contributor check on PR and merge-group heads. +permissions: {} jobs: cla: + # Serializes runs for one head or pull request; job level, so unrelated + # comments (skipped by `if`) never replace a pending check. + concurrency: + group: cla-${{ github.event.workflow_run.head_sha || github.event.issue.number || github.run_id }} + cancel-in-progress: false + permissions: + contents: read + checks: write # Reports the CLA check on the pull request and merge-group head. + issues: write # Maintains the signing prompt. + pull-requests: read # Resolves the pull request of a triggering run. if: >- - ( - github.event_name == 'pull_request_target' - || github.event_name == 'merge_group' - ) || ( - github.event_name == 'issue_comment' - && github.event.issue.pull_request - && ( - github.event.comment.body == 'recheck' - || github.event.comment.body == 'I have read the CLA Document and I hereby sign the CLA' + !github.event.repository.private + && ( + ( + github.event_name == 'workflow_run' + && github.event.workflow_run.event == 'pull_request' + ) + || github.event_name == 'merge_group' || ( + github.event_name == 'issue_comment' + && github.event.issue.pull_request + && ( + github.event.comment.body == 'recheck' + || github.event.comment.body == 'I have read the CLA Document and I hereby sign the CLA' + ) ) ) - uses: stella/.github/.github/workflows/cla.yml@28dce62a9e1916b19ce03c8c47a1c44e1a145fdf + uses: stella/.github/.github/workflows/cla.yml@156afa9596e6a469c10e329f459168d34a00e0fd with: + # Automation accounts cannot sign a CLA, so any app that opens PRs here + # has to be listed or its every PR fails the check permanently. allowlist: dependabot[bot],renovate[bot],github-actions[bot],stella-provenance-updater[bot],google-labs-jules[bot],cursoragent + # The context the main ruleset requires on pull request heads. + check-name: cla secrets: CLA_APP_PRIVATE_KEY: ${{ secrets.CLA_APP_PRIVATE_KEY }} diff --git a/README.md b/README.md index e3d8aae9..13c4b71a 100644 --- a/README.md +++ b/README.md @@ -900,8 +900,7 @@ check-changes: needs: [trust-check] if: >- always() && !cancelled() - && ((github.event_name != 'pull_request' - && github.event_name != 'pull_request_target') + && (github.event_name != 'pull_request' || needs.trust-check.outputs.trusted == 'true') runs-on: ubuntu-latest steps: diff --git a/packages/oxlint-config/README.md b/packages/oxlint-config/README.md index 2ba1980b..86894786 100644 --- a/packages/oxlint-config/README.md +++ b/packages/oxlint-config/README.md @@ -410,9 +410,10 @@ fixtures do not. Package-manager selection follows the nearest declaration or lo workspace members share their owner's update entry. Bundler `Gemfile`/`Gemfile.lock` and Composer `composer.json`/`composer.lock` declare roots at their own directories. -`rust-version`, `node-engine`, `workflow-trust`, `type-aware-projects`, -`cloud-setup-drift` and `quarantine-policy` are mandatory: opt-outs cannot -bypass them (Rust uses expiring `rustExceptions` instead). +`rust-version`, `node-engine`, `workflow-trust`, `workflow-trigger`, +`type-aware-projects`, `cloud-setup-drift` and `quarantine-policy` are +mandatory: opt-outs cannot bypass them (Rust uses expiring `rustExceptions` +instead). Opt-outs apply to the entire repository for that rule. Unknown or duplicate rules, empty reasons, and malformed configuration fail validation. See the @@ -977,6 +978,14 @@ exits successfully even when history is unreadable, reporting `changed: true` and `status: "unreadable"`. Use `changed` to force the full CI depth on both pull requests and merge-queue runs. An unreadable snapshot never skips checks. +The mandatory `workflow-trigger` rule rejects the `pull_request_target` +trigger in every workflow, with no allowlist: it runs base-repository code with +secrets on pull request events, and GitHub restricts it on public repositories. +Use an unprivileged `pull_request` workflow and, only for a step that needs +secrets or write access, a separate `workflow_run` workflow that treats the +first run's output as untrusted data (see the CLA and provenance workflows in +`stella/.github`). + Run repository-installed tools only after the workflow's existing trust decision. The mandatory `workflow-trust` rule applies to PR workflows declaring a `trust-check` job. Repository execution requires a direct dependency and a @@ -1057,8 +1066,7 @@ check-changes: needs: [trust-check] if: >- always() && !cancelled() - && ((github.event_name != 'pull_request' - && github.event_name != 'pull_request_target') + && (github.event_name != 'pull_request' || needs.trust-check.outputs.trusted == 'true') runs-on: ubuntu-latest steps: diff --git a/packages/oxlint-config/src/toolchain-dynamic-selectors.test.ts b/packages/oxlint-config/src/toolchain-dynamic-selectors.test.ts index 0b3a1503..da88e3d3 100644 --- a/packages/oxlint-config/src/toolchain-dynamic-selectors.test.ts +++ b/packages/oxlint-config/src/toolchain-dynamic-selectors.test.ts @@ -245,13 +245,7 @@ test("Docker line decisions cover unresolved FROM values only", () => { }); test("event-specific source refs never classify a PR head as inspected source", () => { - for (const event of [ - "push", - "merge_group", - "pull_request", - "pull_request_target", - "workflow_run", - ]) { + for (const event of ["push", "merge_group", "pull_request", "workflow_run"]) { const source = (ref: string) => `on: ${event}\njobs:\n example:\n steps:\n - uses: ${action("actions/checkout")}\n with: {ref: '${ref}'}\n - uses: ./.github/actions/example\n`; expect(check(source("${{ github.sha }}")).diagnostics).toEqual([]); @@ -284,12 +278,7 @@ test("dynamic refs retain static sparse requirements and root-anchored manifest }); test("PR-head manifest selectors delegate without reading the inspected manifest", () => { - for (const event of [ - "pull_request", - "pull_request_target", - "push", - "merge_group", - ]) + for (const event of ["pull_request", "push", "merge_group"]) for (const ref of [ "${{ github.event.pull_request.head.sha }}", "${{ github.event.pull_request.head.ref }}", diff --git a/packages/oxlint-config/src/toolchain-guard.test.ts b/packages/oxlint-config/src/toolchain-guard.test.ts index c3ee4d32..965ad2be 100644 --- a/packages/oxlint-config/src/toolchain-guard.test.ts +++ b/packages/oxlint-config/src/toolchain-guard.test.ts @@ -383,6 +383,7 @@ test("permitted named rules have reasoned opt-outs; mandatory and malformed choi rule === "cloud-setup-drift" || rule === "type-aware-projects" || rule === "workflow-trust" || + rule === "workflow-trigger" || rule === "quarantine-policy" ) { expect(() => diff --git a/packages/oxlint-config/src/toolchain-guard.ts b/packages/oxlint-config/src/toolchain-guard.ts index 56fc1af9..cab5b770 100644 --- a/packages/oxlint-config/src/toolchain-guard.ts +++ b/packages/oxlint-config/src/toolchain-guard.ts @@ -157,6 +157,10 @@ export const parseToolchainConfiguration = (input: unknown) => { throw new Error( "workflow-trust cannot be opted out; repository commands must follow the declared trust decision", ); + if (entry["rule"] === "workflow-trigger") + throw new Error( + "workflow-trigger cannot be opted out; pull_request_target has no allowlist", + ); if (entry["rule"] === "cloud-setup-drift") throw new Error( "cloud-setup-drift cannot be opted out; generated setup must match shared policy", diff --git a/packages/oxlint-config/src/toolchain-runtime.test.ts b/packages/oxlint-config/src/toolchain-runtime.test.ts index ce98cbe7..39df886f 100644 --- a/packages/oxlint-config/src/toolchain-runtime.test.ts +++ b/packages/oxlint-config/src/toolchain-runtime.test.ts @@ -893,6 +893,12 @@ const cases = [ pass: "on: pull_request\njobs:\n trust-check: {runs-on: ubuntu-latest, steps: [{run: 'echo trusted'}]}\n detector:\n needs: trust-check\n if: needs.trust-check.outputs.trusted == 'true'\n steps: [{run: 'bunx --no-install stll-toolchain-changed --since origin/main'}]\n", fail: "on: pull_request\njobs:\n trust-check: {runs-on: ubuntu-latest, steps: [{run: 'echo trusted'}]}\n detector:\n steps: [{run: 'bunx --no-install stll-toolchain-changed --since origin/main'}]\n", }, + { + rule: "workflow-trigger", + file: ".github/workflows/cla.yml", + pass: "on:\n workflow_run:\n workflows: [CLA Request]\n types: [completed]\njobs:\n cla: {runs-on: ubuntu-latest, steps: [{run: 'echo verified'}]}\n", + fail: "on:\n issue_comment:\n pull_request_target:\n types: [opened]\njobs:\n cla: {runs-on: ubuntu-latest, steps: [{run: 'echo verified'}]}\n", + }, ] satisfies { rule: (typeof runtimeRules)[number]; file: string; @@ -928,6 +934,43 @@ jobs: ).toEqual([]); }); +test.each([ + ["on: pull_request_target\n", 1], + ["on: [push, pull_request_target]\n", 1], + ["'on':\n push:\n 'pull_request_target':\n types: [opened]\n", 3], +])( + "workflow-trigger rejects pull_request_target in every trigger form: %p", + (triggers, line) => { + const file = ".github/workflows/any.yml"; + const findings = check( + file, + `${triggers}jobs:\n echo: {runs-on: ubuntu-latest, steps: [{run: 'echo ok'}]}\n`, + ).filter(({ rule }) => rule === "workflow-trigger"); + expect(findings).toMatchObject([ + { rule: "workflow-trigger", path: file, line }, + ]); + expect(findings[0]?.message).toContain( + "GitHub restricts it on public repositories", + ); + }, +); + +test("workflow-trigger reports pull_request_target even past the alias expansion bound", () => { + const file = ".github/workflows/any.yml"; + const references = Array.from( + { length: 101 }, + (_, index) => ` copy${index}: *shared\n`, + ).join(""); + const findings = check( + file, + `on:\n pull_request_target:\n types: [opened]\nx-shared: &shared {a: 1}\nx-copies:\n${references}jobs:\n echo: {runs-on: ubuntu-latest, steps: [{run: 'echo ok'}]}\n`, + ); + expect(findings.map(({ rule }) => rule)).toContain("runtime-workflow"); + expect( + findings.filter(({ rule }) => rule === "workflow-trigger"), + ).toMatchObject([{ rule: "workflow-trigger", path: file, line: 2 }]); +}); + test("workflow trust diagnostics locate non-Bun manager commands", () => { const file = ".github/workflows/ci.yml"; for (const command of ["npm test", "pnpm install", "yarn test"]) { diff --git a/packages/oxlint-config/src/toolchain-runtime.ts b/packages/oxlint-config/src/toolchain-runtime.ts index 7a374dbf..2528556b 100644 --- a/packages/oxlint-config/src/toolchain-runtime.ts +++ b/packages/oxlint-config/src/toolchain-runtime.ts @@ -43,6 +43,7 @@ import type { NodeCompatibility } from "./toolchain-node-compatibility"; import { checkRustReferences, type RustException } from "./toolchain-rust"; import type { RustInstallFixture } from "./toolchain-rust-fixtures"; import { + checkWorkflowTriggers, checkWorkflowTrust, workflowTrustRules, } from "./toolchain-workflow-trust"; @@ -1928,6 +1929,46 @@ export const checkRuntimeFile = ({ currentLocation = undefined; } }; + if (automationKind === "workflow") { + // Read triggers from the syntax tree, not the alias expansion below, so + // a workflow that exceeds the expansion bound still reports a banned + // trigger that no runtime-workflow opt-out can hide. + let triggers: unknown; + try { + triggers = getNode(document.contents, "on"); + } catch { + triggers = undefined; + } + const scalarValue = (node: unknown) => + isScalar(node) && typeof node.value === "string" ? [node.value] : []; + let declared: unknown; + if (isMap(triggers)) + declared = Object.fromEntries( + triggers.items.flatMap((pair) => + scalarValue(pair.key).map((event) => [event, null]), + ), + ); + else if (isSeq(triggers)) + declared = triggers.items.flatMap((item) => { + try { + return scalarValue(resolveNode(item)); + } catch { + return []; + } + }); + else declared = scalarValue(triggers).at(0); + for (const { event, message } of checkWorkflowTriggers({ on: declared })) { + const key = isMap(triggers) + ? triggers.items.find((pair) => scalarValue(pair.key).at(0) === event) + ?.key + : undefined; + add({ + rule: "workflow-trigger", + line: nodeLine(key ?? triggers ?? document.contents), + message, + }); + } + } try { // Resolve aliases first with an expansion bound before visiting executable fields. const workflow: unknown = document.toJS({ maxAliasCount: 100 }); diff --git a/packages/oxlint-config/src/toolchain-workflow-trust.test.ts b/packages/oxlint-config/src/toolchain-workflow-trust.test.ts index 53a6a9cb..ae40b6db 100644 --- a/packages/oxlint-config/src/toolchain-workflow-trust.test.ts +++ b/packages/oxlint-config/src/toolchain-workflow-trust.test.ts @@ -527,7 +527,7 @@ test("the documented workflow example obeys the trust contract", () => { if (!isRecord(detector)) throw new Error("documented detector job is missing"); const sample = { - on: ["pull_request", "pull_request_target"], + on: ["pull_request"], jobs: { "trust-check": { "runs-on": "ubuntu-latest", diff --git a/packages/oxlint-config/src/toolchain-workflow-trust.ts b/packages/oxlint-config/src/toolchain-workflow-trust.ts index 036f3fb2..75875136 100644 --- a/packages/oxlint-config/src/toolchain-workflow-trust.ts +++ b/packages/oxlint-config/src/toolchain-workflow-trust.ts @@ -316,7 +316,10 @@ const safeEnvironment = (value: unknown) => const configuredShell = (value: unknown) => record(value) && record(value["run"]) ? value["run"]["shell"] : undefined; -export const workflowTrustRules = ["workflow-trust"] as const; +export const workflowTrustRules = [ + "workflow-trust", + "workflow-trigger", +] as const; type WorkflowTrustFinding = { job: string; @@ -333,6 +336,24 @@ const workflowEvents = (triggers: unknown): unknown[] => { return []; }; +// pull_request_target runs base-repository workflow code with secrets and a +// write token on events that fork pull requests control, and GitHub restricts +// it on public repositories. There is no allowlist: an unprivileged +// pull_request workflow plus, where a step needs privilege, a workflow_run +// workflow that treats the first run's output as untrusted data replaces it. +export const bannedWorkflowTriggers = { + pull_request_target: + "pull_request_target is banned: it runs base-repository code with secrets on pull request events and GitHub restricts it on public repositories; use pull_request, plus a workflow_run workflow for any step that needs secrets or write access", +} as const satisfies Record; + +/** Name every banned trigger a workflow declares. */ +export const checkWorkflowTriggers = (workflow: unknown) => + record(workflow) + ? Object.entries(bannedWorkflowTriggers) + .filter(([event]) => workflowEvents(workflow["on"]).includes(event)) + .map(([event, message]) => ({ event, message })) + : []; + /** Enforce a workflow's declared trust boundary before repository dependency execution. */ export const checkWorkflowTrust = (workflow: unknown) => { const findings: WorkflowTrustFinding[] = []; diff --git a/scripts/rust-ci.test.ts b/scripts/rust-ci.test.ts index 99555d91..eef9e293 100644 --- a/scripts/rust-ci.test.ts +++ b/scripts/rust-ci.test.ts @@ -105,8 +105,10 @@ test("the required-check workflows handle merge groups and use the reviewed veri ) throw new Error("Missing CLA workflow contract"); expect(cla["on"]["merge_group"]).toEqual({ types: ["checks_requested"] }); - expect(cla["on"]["pull_request_target"]).toEqual({ - types: ["opened", "synchronize", "reopened", "ready_for_review"], + expect(cla["on"]).not.toHaveProperty("pull_request_target"); + expect(cla["on"]["workflow_run"]).toEqual({ + workflows: ["CLA Request"], + types: ["completed"], }); // The pin itself belongs to Dependabot; the contract is the reviewed // reusable workflow at an immutable commit. @@ -116,10 +118,11 @@ test("the required-check workflows handle merge groups and use the reviewed veri expect(cla["jobs"]["cla"]["if"]).toContain( "github.event_name == 'merge_group'", ); - expect(cla["permissions"]).toEqual({ + expect(cla["permissions"]).toEqual({}); + expect(cla["jobs"]["cla"]["permissions"]).toEqual({ contents: "read", + checks: "write", issues: "write", "pull-requests": "read", - checks: "write", }); }, 15_000);