From 7382e86f97fb9f67824670b76c489bef0519d7ef Mon Sep 17 00:00:00 2001 From: Scott Benson <80784472+scttbnsn@users.noreply.github.com> Date: Mon, 23 Nov 2026 15:14:31 -0500 Subject: [PATCH 1/2] ci(security): add CodeQL, Scorecard and gitleaks scanning (#159) * ci(security): add CodeQL, Scorecard and gitleaks scanning Adds CodeQL (javascript-typescript, actions) and OpenSSF Scorecard workflows, a checksum-verified gitleaks history scan in the verify job, and lefthook hooks for staged gitleaks (pre-commit) and qlty (pre-push). workflow.test.mjs now enforces SHA pins, permissions and the new triggers. * ci(security): pin CodeQL to the commit and scope the gitleaks allowlist to exact secrets --- .github/workflows/codeql.yml | 43 ++++++++++++++ .github/workflows/scorecard.yml | 36 ++++++++++++ .github/workflows/website.yml | 14 +++++ .gitleaks.toml | 13 +++++ frontend/test/workflow.test.mjs | 99 ++++++++++++++++++++++++++++++++- lefthook.yml | 23 ++++++++ 6 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/codeql.yml create mode 100644 .github/workflows/scorecard.yml create mode 100644 .gitleaks.toml diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml new file mode 100644 index 0000000..06387b7 --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,43 @@ +name: CodeQL + +on: + push: + branches: + - main + pull_request: + branches: + - main + schedule: + - cron: "23 4 * * 1" + +permissions: + contents: read + +jobs: + analyze: + runs-on: ubuntu-latest + timeout-minutes: 30 + permissions: + security-events: write + actions: read + contents: read + strategy: + fail-fast: false + matrix: + language: + - javascript-typescript + - actions + steps: + - name: Check out repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - name: Initialize CodeQL + uses: github/codeql-action/init@1bd86f2fdb4b86187fa4059f16f31a3202acc9ea # v4.39.4 + with: + languages: ${{ matrix.language }} + build-mode: none + - name: Perform CodeQL analysis + uses: github/codeql-action/analyze@1bd86f2fdb4b86187fa4059f16f31a3202acc9ea # v4.39.4 + with: + category: /language:${{ matrix.language }} diff --git a/.github/workflows/scorecard.yml b/.github/workflows/scorecard.yml new file mode 100644 index 0000000..2f594d7 --- /dev/null +++ b/.github/workflows/scorecard.yml @@ -0,0 +1,36 @@ +name: OpenSSF Scorecard + +on: + push: + branches: + - main + schedule: + - cron: "37 5 * * 2" + branch_protection_rule: + +permissions: read-all + +jobs: + analysis: + runs-on: ubuntu-latest + timeout-minutes: 15 + permissions: + security-events: write + id-token: write + contents: read + actions: read + steps: + - name: Check out repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - name: Run Scorecard analysis + uses: ossf/scorecard-action@23f22cd43608bae7a1f1fa2603d7314dee9538a5 # v2.6.0 + with: + results_file: results.sarif + results_format: sarif + publish_results: true + - name: Upload SARIF to code scanning + uses: github/codeql-action/upload-sarif@1bd86f2fdb4b86187fa4059f16f31a3202acc9ea # v4.39.4 + with: + sarif_file: results.sarif diff --git a/.github/workflows/website.yml b/.github/workflows/website.yml index a7d545d..ccc8f18 100644 --- a/.github/workflows/website.yml +++ b/.github/workflows/website.yml @@ -31,6 +31,20 @@ jobs: uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false + fetch-depth: 0 + - name: Scan history for secrets (gitleaks) + # Pinned release binary, verified against the sha256 from the v8.30.1 checksums file. + working-directory: . + env: + GITLEAKS_VERSION: 8.30.1 + GITLEAKS_SHA256: 551f6fc83ea457d62a0d98237cbad105af8d557003051f41f3e7ca7b3f2470eb + run: | + archive="gitleaks_${GITLEAKS_VERSION}_linux_x64.tar.gz" + scratch="$(mktemp -d)" + curl -fsSL -o "$scratch/$archive" "https://github.com/gitleaks/gitleaks/releases/download/v${GITLEAKS_VERSION}/$archive" + echo "$GITLEAKS_SHA256 $scratch/$archive" | sha256sum --check --strict + tar -xzf "$scratch/$archive" -C "$scratch" gitleaks + "$scratch/gitleaks" git --redact --no-banner --verbose - name: Set up Node.js uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: diff --git a/.gitleaks.toml b/.gitleaks.toml new file mode 100644 index 0000000..8c3f96e --- /dev/null +++ b/.gitleaks.toml @@ -0,0 +1,13 @@ +title = "codeswhat.com gitleaks config" + +[extend] +useDefault = true + +# Only proven public test fixtures are allowlisted. Real keys live in Vercel. +[[allowlists]] +description = "Public test fixtures" +regexTarget = "secret" +regexes = [ + '''^1x00000000000000000000AA$''', + '''^phc_ci_fixture$''', +] diff --git a/frontend/test/workflow.test.mjs b/frontend/test/workflow.test.mjs index 0eacc9e..5b45e37 100644 --- a/frontend/test/workflow.test.mjs +++ b/frontend/test/workflow.test.mjs @@ -1,5 +1,5 @@ import assert from "node:assert/strict"; -import { readFileSync } from "node:fs"; +import { readdirSync, readFileSync } from "node:fs"; import { test } from "node:test"; const workflow = readFileSync( @@ -160,3 +160,100 @@ test("the verification guard rejects a removed step or an if:/continue-on-error: ); } }); + +// Every workflow in the repo, including the security scanners, shares the same +// supply-chain invariants. +const WORKFLOW_DIR = new URL("../../.github/workflows/", import.meta.url); +const workflowFiles = readdirSync(WORKFLOW_DIR).filter((name) => /\.ya?ml$/.test(name)); +const readWorkflow = (name) => readFileSync(new URL(name, WORKFLOW_DIR), "utf8"); + +test("the security scanning workflows exist alongside website.yml", () => { + for (const name of ["website.yml", "codeql.yml", "scorecard.yml"]) { + assert.ok(workflowFiles.includes(name), `${name} must exist`); + } +}); + +test("every workflow pins each action to a full commit SHA with a version comment", () => { + for (const name of workflowFiles) { + const lines = stripComments(readWorkflow(name)) + .split("\n") + .filter((line) => /^\s*(?:- )?uses:/.test(line)); + assert.ok(lines.length > 0, `${name} uses at least one action`); + } + for (const name of workflowFiles) { + // Read the raw file: the version comment is a trailing comment, not a comment-only line. + for (const line of readWorkflow(name).split("\n")) { + if (!/^\s*(?:- )?uses:/.test(line)) continue; + assert.match( + line, + /uses:\s*[\w.-]+\/[\w./-]+@[0-9a-f]{40}\s+# v\d+\.\d+\.\d+\s*$/, + `${name}: ${line.trim()}`, + ); + } + } +}); + +test("no workflow uses pull_request_target or persists checkout credentials", () => { + for (const name of workflowFiles) { + const text = stripComments(readWorkflow(name)); + assert.doesNotMatch(text, /pull_request_target/, `${name} must not use pull_request_target`); + const checkouts = [...text.matchAll(/uses: actions\/checkout@[^\n]*\n((?: {8,}.*\n)*)/g)]; + assert.ok(checkouts.length > 0, `${name} checks out the repository`); + for (const [, withBlock] of checkouts) { + assert.match(withBlock, /persist-credentials: false/, `${name} checkout`); + } + } +}); + +test("every workflow declares least-privilege top-level permissions", () => { + for (const name of workflowFiles) { + const text = stripComments(readWorkflow(name)); + const top = text.match(/^permissions:[ \t]*(\S*)\n((?: {2}.*\n)*)/m); + assert.ok(top, `${name} must declare top-level permissions`); + const granted = top[1] || top[2]; + assert.doesNotMatch(granted, /write/, `${name} top-level permissions must not grant write`); + } +}); + +test("codeql.yml scans javascript-typescript and actions on push, PR and a schedule", () => { + const text = stripComments(readWorkflow("codeql.yml")); + const on = extractBlocks(text).triggers; + assert.match(on, /^ {2}push:\n {4}branches:\n {6}- main$/m); + assert.match(on, /^ {2}pull_request:\n {4}branches:\n {6}- main$/m); + assert.match(on, /^ {2}schedule:\n {4}- cron:/m); + assert.match(text, /^ {10}- javascript-typescript$/m); + assert.match(text, /^ {10}- actions$/m); + assert.match(text, /^permissions:\n {2}contents: read$/m); + assert.match(text, /^ {6}security-events: write$/m); + assert.match(text, /^ {6}actions: read$/m); +}); + +test("scorecard.yml follows the OpenSSF template and publishes results", () => { + const text = stripComments(readWorkflow("scorecard.yml")); + const on = extractBlocks(text).triggers; + assert.match(on, /^ {2}push:\n {4}branches:\n {6}- main$/m); + assert.match(on, /^ {2}schedule:\n {4}- cron:/m); + assert.match(on, /^ {2}branch_protection_rule:/m); + assert.match(text, /^permissions: read-all$/m); + assert.match(text, /^ {6}security-events: write$/m); + assert.match(text, /^ {6}id-token: write$/m); + assert.match(text, /uses: ossf\/scorecard-action@/); + assert.match(text, /publish_results: true/); + assert.match(text, /uses: github\/codeql-action\/upload-sarif@/); +}); + +test("verify downloads a pinned gitleaks binary, checks its sha256 and scans history", () => { + const step = extractSteps(verifyJob).find((candidate) => /gitleaks/.test(candidate.body)); + assert.ok(step, "verify must have a gitleaks step"); + assert.deepEqual(step.bypass, [], "the gitleaks step must be unconditional and blocking"); + assert.match(step.body, /GITLEAKS_VERSION: \d+\.\d+\.\d+/); + assert.match(step.body, /GITLEAKS_SHA256: [0-9a-f]{64}\b/); + assert.match(step.body, /sha256sum --check --strict/); + assert.match(step.body, /gitleaks" git --redact/); + assert.doesNotMatch(workflow, /gitleaks\/gitleaks-action/); + assert.match(verifyJob, /fetch-depth: 0/); + assert.ok( + verifyJob.indexOf("sha256sum --check") < verifyJob.indexOf('gitleaks" git'), + "the checksum must be verified before the binary runs", + ); +}); diff --git a/lefthook.yml b/lefthook.yml index 59ceba1..c7bc0b8 100644 --- a/lefthook.yml +++ b/lefthook.yml @@ -10,3 +10,26 @@ pre-commit: - name: typecheck root: frontend/ run: npx tsc --noEmit + + - name: gitleaks + run: | + if command -v gitleaks >/dev/null 2>&1; then + gitleaks git --staged --redact --no-banner + else + echo "gitleaks not installed, skipping secret scan (brew install gitleaks)" + fi + +pre-push: + jobs: + - name: qlty + run: | + if command -v qlty >/dev/null 2>&1 || [ -x "$HOME/.qlty/bin/qlty" ]; then + PATH="$HOME/.qlty/bin:$PATH" + if git rev-parse --verify --quiet origin/dev >/dev/null; then + qlty check --upstream origin/dev + else + qlty check + fi + else + echo "qlty not installed, skipping qlty check (https://qlty.sh)" + fi From 8c074b19b792fe83e0bb98f6d615657490ac6d52 Mon Sep 17 00:00:00 2001 From: Scott Benson <80784472+scttbnsn@users.noreply.github.com> Date: Mon, 23 Nov 2026 15:36:06 -0500 Subject: [PATCH 2/2] ci(website): run verify in the Playwright image and reuse a green dev result on promotion (#160) * ci(website): run verify in the Playwright image and reuse a green dev result on promotion * style(test): format workflow guards * ci(website): gate verify through an always-run job so a cancelled reuse can't skip it --- .github/workflows/website.yml | 120 ++++++++++++++++++++++++- frontend/test/workflow.test.mjs | 150 +++++++++++++++++++++++++++----- 2 files changed, 247 insertions(+), 23 deletions(-) diff --git a/.github/workflows/website.yml b/.github/workflows/website.yml index ccc8f18..8826732 100644 --- a/.github/workflows/website.yml +++ b/.github/workflows/website.yml @@ -20,9 +20,99 @@ concurrency: cancel-in-progress: ${{ github.event_name == 'pull_request' }} jobs: - verify: + reuse: + # Decides whether tests can be skipped because the identical tree already passed on dev. + # Every doubt must resolve to reuse=false. continue-on-error keeps a failure here + # (checkout, API, anything) from skipping tests through a failed needs job. + # The required check is the verify gate job below, which always runs: a skipped + # required job reports as passing, so a cancelled reuse must never leave it skipped. + runs-on: ubuntu-latest + timeout-minutes: 5 + continue-on-error: true + permissions: + contents: read + actions: read + outputs: + reuse: ${{ steps.decide.outputs.reuse }} + steps: + - name: Check out repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + fetch-depth: 2 + - name: Decide whether a green dev run covers this tree + id: decide + env: + GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + EVENT_NAME: ${{ github.event_name }} + REF_NAME: ${{ github.ref_name }} + PR_BASE_REF: ${{ github.event.pull_request.base.ref }} + PR_HEAD_REF: ${{ github.event.pull_request.head.ref }} + PR_HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }} + PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }} + run: | + set -u + echo "reuse=false" >> "$GITHUB_OUTPUT" + + decide() { + local source_sha + case "$EVENT_NAME" in + pull_request) + [ "$PR_BASE_REF" = "main" ] || { echo "not a PR into main"; return 1; } + [ "$PR_HEAD_REF" = "dev" ] || { echo "PR head is not dev"; return 1; } + [ "$PR_HEAD_REPO" = "$GH_REPO" ] || { echo "PR head is a fork"; return 1; } + source_sha="$PR_HEAD_SHA" + ;; + push) + [ "$REF_NAME" = "main" ] || { echo "push is not to main"; return 1; } + source_sha="$(git rev-parse --verify --quiet HEAD^2)" || { echo "HEAD is not a merge commit"; return 1; } + ;; + *) + echo "event $EVENT_NAME never reuses" + return 1 + ;; + esac + case "$source_sha" in + *[!0-9a-f]* | "") echo "bad source sha"; return 1 ;; + esac + [ "${#source_sha}" -eq 40 ] || { echo "bad source sha length"; return 1; } + + # The merge commit must carry exactly the tree that dev already verified. + [ "$(git rev-parse --verify --quiet HEAD^2)" = "$source_sha" ] || { echo "HEAD^2 is not the source sha"; return 1; } + local head_tree source_tree + head_tree="$(git rev-parse --verify "HEAD^{tree}")" || return 1 + source_tree="$(git rev-parse --verify "${source_sha}^{tree}")" || return 1 + [ "$head_tree" = "$source_tree" ] || { echo "tree differs from dev head ($head_tree vs $source_tree)"; return 1; } + + local run_id + run_id="$(gh api "repos/$GH_REPO/actions/workflows/website.yml/runs?branch=dev&event=push&head_sha=$source_sha&status=success&per_page=1" \ + --jq '.workflow_runs[0] | select(.head_sha == "'"$source_sha"'" and .head_branch == "dev" and .event == "push" and .conclusion == "success") | .id')" || { echo "run lookup failed"; return 1; } + case "$run_id" in + "" | *[!0-9]*) echo "no successful dev push run for $source_sha"; return 1 ;; + esac + + echo "reuse=true" >> "$GITHUB_OUTPUT" + echo "Reusing dev run $run_id (head $source_sha, tree $head_tree); verify skipped." >> "$GITHUB_STEP_SUMMARY" + echo "reusing dev run $run_id for tree $head_tree" + } + + if ! decide; then + echo "Full verify runs: no reusable dev result." >> "$GITHUB_STEP_SUMMARY" + fi + tests: + name: tests + needs: reuse + if: ${{ !cancelled() && needs.reuse.outputs.reuse != 'true' }} runs-on: ubuntu-latest timeout-minutes: 25 + container: + # Keep this tag equal to the playwright-core version in frontend/package-lock.json. + # The image ships the browsers at /ms-playwright, so no install step is needed. + image: mcr.microsoft.com/playwright:v1.65.0-noble@sha256:9beb546099e11f52d62e505537914870e01a67e0e6b00ae41da4775324e73dbe + env: + # Firefox in the Playwright image expects HOME=/root; the runner would set /github/home. + HOME: /root defaults: run: working-directory: frontend @@ -72,7 +162,31 @@ jobs: run: npm run build:test - name: Verify PostHog production response run: npm run test:posthog:browser - - name: Install browsers for production checks - run: node node_modules/playwright-core/cli.js install --with-deps chromium firefox webkit - name: Verify production page and API boundaries (Chromium, Firefox, WebKit) run: npm run test:browser:all + verify: + # Required status check. Always runs and fails closed: it passes only when tests + # succeeded, or when tests were skipped because a green dev run covers this tree. + # A cancelled run, or a skipped tests job without a confirmed reuse, fails here. + name: verify + needs: [reuse, tests] + if: always() + runs-on: ubuntu-latest + timeout-minutes: 2 + permissions: {} + steps: + - name: Require tests or a confirmed reuse + env: + REUSE: ${{ needs.reuse.outputs.reuse }} + TESTS_RESULT: ${{ needs.tests.result }} + run: | + if [ "$REUSE" = "true" ] && [ "$TESTS_RESULT" = "skipped" ]; then + echo "tests skipped: a green dev run covers this tree" + exit 0 + fi + if [ "$TESTS_RESULT" = "success" ]; then + echo "tests passed" + exit 0 + fi + echo "verify failed: reuse='$REUSE' tests='$TESTS_RESULT'" + exit 1 diff --git a/frontend/test/workflow.test.mjs b/frontend/test/workflow.test.mjs index 5b45e37..f0c538b 100644 --- a/frontend/test/workflow.test.mjs +++ b/frontend/test/workflow.test.mjs @@ -18,6 +18,8 @@ function extractBlocks(yaml) { const text = stripComments(yaml); return { jobs: text.match(/^jobs:\n((?: {2,}.*\n|\n)*)/m)?.[1], + reuseJob: text.match(/^ {2}reuse:\n((?: {4}.*\n|\n)*)/m)?.[1], + testsJob: text.match(/^ {2}tests:\n((?: {4}.*\n|\n)*)/m)?.[1], verifyJob: text.match(/^ {2}verify:\n((?: {4}.*\n|\n)*)/m)?.[1], triggers: text.match(/^on:\n((?: {2}.*\n|\n)*)/m)?.[1], }; @@ -55,35 +57,76 @@ function verificationProblems(verify) { return problems; } -const { jobs, verifyJob, triggers } = extractBlocks(workflow); +const { jobs, reuseJob, testsJob, verifyJob, triggers } = extractBlocks(workflow); const PATH_FILTER = /\bpaths(-ignore)?:/; -test("the workflow defines triggers and a verify job", () => { +test("the workflow defines triggers, a tests job and a verify gate", () => { assert.ok(triggers, "on: must exist"); assert.ok(jobs, "jobs: must exist"); + assert.ok(testsJob, "jobs.tests must exist"); assert.ok(verifyJob, "jobs.verify must exist"); }); -test("verify has no job-level condition, so it can never be skipped", () => { - assert.doesNotMatch(verifyJob, /^ {4}if:/m); +test("tests' only job-level condition is the reuse skip, and a cancelled run does not skip it", () => { + assert.deepEqual( + [...testsJob.matchAll(/^ {4}if:.*$/gm)].map((m) => m[0]), + [" if: $" + "{{ !cancelled() && needs.reuse.outputs.reuse != 'true' }}"], + ); + assert.match(testsJob, /^ {4}needs: reuse$/m); + assert.match(testsJob, /^ {4}name: tests$/m); }); -test("verify is the only job, keeps its key as the check name, and has no matrix", () => { +test("jobs are reuse, tests and verify, and verify is the always-run gate that keeps the check name", () => { assert.deepEqual( [...jobs.matchAll(/^ {2}([\w-]+):/gm)].map((m) => m[1]), - ["verify"], + ["reuse", "tests", "verify"], ); - assert.doesNotMatch(verifyJob, /^ {4}name:/m, "a name: would change the check context"); + assert.match(verifyJob, /^ {4}name: verify$/m); + assert.doesNotMatch(testsJob, /\bmatrix\b/); assert.doesNotMatch(verifyJob, /\bmatrix\b/); + assert.deepEqual( + [...verifyJob.matchAll(/^ {4}if:.*$/gm)].map((m) => m[0]), + [" if: always()"], + ); + assert.match(verifyJob, /^ {4}needs: \[reuse, tests\]$/m); + assert.match(verifyJob, /^ {4}runs-on: ubuntu-latest$/m); + assert.match(verifyJob, /^ {4}timeout-minutes: 2$/m); + assert.match(verifyJob, /^ {4}permissions: \{\}$/m); + assert.doesNotMatch(verifyJob, /^ {4}container:/m); + assert.doesNotMatch(verifyJob, /actions\/checkout/); + assert.doesNotMatch(verifyJob, /continue-on-error/); +}); + +test("the verify gate passes only on tests success or a confirmed reuse with tests skipped", () => { + assert.match(verifyJob, /^ {10}REUSE: \$\{\{ needs\.reuse\.outputs\.reuse \}\}$/m); + assert.match(verifyJob, /^ {10}TESTS_RESULT: \$\{\{ needs\.tests\.result \}\}$/m); + const script = verifyJob.match(/^ {8}run: \|\n((?: {10}.*\n|\n)*)/m)?.[1]; + assert.ok(script, "verify must have a gate script"); + const lines = script + .split("\n") + .map((line) => line.trim()) + .filter(Boolean); + assert.deepEqual(lines, [ + 'if [ "$REUSE" = "true" ] && [ "$TESTS_RESULT" = "skipped" ]; then', + 'echo "tests skipped: a green dev run covers this tree"', + "exit 0", + "fi", + 'if [ "$TESTS_RESULT" = "success" ]; then', + 'echo "tests passed"', + "exit 0", + "fi", + "echo \"verify failed: reuse='$REUSE' tests='$TESTS_RESULT'\"", + "exit 1", + ]); }); test("triggers have no paths or paths-ignore filters", () => { assert.doesNotMatch(triggers, PATH_FILTER); }); -test("verify installs and runs all three browser engines", () => { - assert.match(verifyJob, /install --with-deps chromium firefox webkit/); - assert.match(verifyJob, /npm run test:browser:all/); +test("tests runs all three browser engines from the Playwright image, with no install step", () => { + assert.doesNotMatch(testsJob, /playwright(-core)?\/cli\.js install|playwright install/); + assert.match(testsJob, /npm run test:browser:all/); const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")); for (const engine of ["chromium", "firefox", "webkit"]) { assert.match(pkg.scripts["test:browser:all"], new RegExp(`BROWSER_ENGINE=${engine}\\b`)); @@ -112,13 +155,13 @@ test("a column-zero comment inside on: does not hide a later paths filter", () = assert.match(blocks.verifyJob, /runs-on/); }); -test("verify runs the PostHog response contract after the production build", () => { - assert.match(verifyJob, /npm run test:posthog:browser/); - assert.ok(verifyJob.indexOf("npm run build") < verifyJob.indexOf("npm run test:posthog:browser")); +test("tests runs the PostHog response contract after the production build", () => { + assert.match(testsJob, /npm run test:posthog:browser/); + assert.ok(testsJob.indexOf("npm run build") < testsJob.indexOf("npm run test:posthog:browser")); }); -test("verify keeps every required verification step, unconditional and blocking", () => { - assert.deepEqual(verificationProblems(verifyJob), []); +test("tests keeps every required verification step, unconditional and blocking", () => { + assert.deepEqual(verificationProblems(testsJob), []); }); test("the verification guard rejects a removed step or an if:/continue-on-error: bypass", () => { @@ -242,18 +285,85 @@ test("scorecard.yml follows the OpenSSF template and publishes results", () => { assert.match(text, /uses: github\/codeql-action\/upload-sarif@/); }); -test("verify downloads a pinned gitleaks binary, checks its sha256 and scans history", () => { - const step = extractSteps(verifyJob).find((candidate) => /gitleaks/.test(candidate.body)); - assert.ok(step, "verify must have a gitleaks step"); +test("tests downloads a pinned gitleaks binary, checks its sha256 and scans history", () => { + const step = extractSteps(testsJob).find((candidate) => /gitleaks/.test(candidate.body)); + assert.ok(step, "tests must have a gitleaks step"); assert.deepEqual(step.bypass, [], "the gitleaks step must be unconditional and blocking"); assert.match(step.body, /GITLEAKS_VERSION: \d+\.\d+\.\d+/); assert.match(step.body, /GITLEAKS_SHA256: [0-9a-f]{64}\b/); assert.match(step.body, /sha256sum --check --strict/); assert.match(step.body, /gitleaks" git --redact/); assert.doesNotMatch(workflow, /gitleaks\/gitleaks-action/); - assert.match(verifyJob, /fetch-depth: 0/); + assert.match(testsJob, /fetch-depth: 0/); assert.ok( - verifyJob.indexOf("sha256sum --check") < verifyJob.indexOf('gitleaks" git'), + testsJob.indexOf("sha256sum --check") < testsJob.indexOf('gitleaks" git'), "the checksum must be verified before the binary runs", ); }); + +test("the Playwright container image tag matches playwright-core in package-lock.json", () => { + const lock = JSON.parse(readFileSync(new URL("../package-lock.json", import.meta.url), "utf8")); + const locked = lock.packages["node_modules/playwright-core"].version; + const image = testsJob.match( + /^ {4}container:\n {6}image: (mcr\.microsoft\.com\/playwright:v([\d.]+)-[\w.-]+@sha256:[0-9a-f]{64})$/m, + ); + assert.ok(image, "tests must run in a digest-pinned mcr.microsoft.com/playwright container"); + assert.equal( + image[2], + locked, + `Playwright image is v${image[2]} but package-lock.json locks playwright-core ${locked}. ` + + "Bump the image tag and digest in website.yml together with playwright-core.", + ); + assert.match(testsJob, /^ {6}HOME: \/root$/m, "Firefox in the Playwright image needs HOME=/root"); +}); + +test("reuse only considers dev-to-main promotion and fails closed", () => { + assert.ok(reuseJob, "jobs.reuse must exist"); + assert.match(reuseJob, /^ {4}continue-on-error: true$/m, "a reuse failure must not skip verify"); + assert.match(reuseJob, /^ {6}contents: read$/m); + assert.match(reuseJob, /^ {6}actions: read$/m); + assert.doesNotMatch(reuseJob, /^ {4}container:/m); + assert.doesNotMatch(reuseJob, /write/); + assert.match(reuseJob, /persist-credentials: false/); + assert.match( + reuseJob, + /^ {10}echo "reuse=false" >> "\$GITHUB_OUTPUT"$/m, + "default must be false", + ); + const script = reuseJob.match(/^ {8}run: \|\n((?: {10}.*\n|\n)*)/m)?.[1]; + assert.ok(script, "reuse must have a decide script"); + assert.match(script, /\[ "\$PR_BASE_REF" = "main" \]/); + assert.match(script, /\[ "\$PR_HEAD_REF" = "dev" \]/); + assert.match(script, /\[ "\$PR_HEAD_REPO" = "\$GH_REPO" \]/); + assert.match(script, /\[ "\$REF_NAME" = "main" \]/); + assert.match(script, /HEAD\^2/); + assert.match(script, /\[ "\$head_tree" = "\$source_tree" \]/); + assert.match(script, /branch=dev&event=push&head_sha=\$source_sha&status=success/); +}); + +test("run scripts take every GitHub expression through env, never inline", () => { + for (const name of workflowFiles) { + const text = stripComments(readWorkflow(name)); + // Walk each run: block (single-line or literal block) and reject ${{ }} inside it. + const lines = text.split("\n"); + for (let i = 0; i < lines.length; i++) { + const start = lines[i].match(/^(\s*)(?:- )?run:\s*(.*)$/); + if (!start) continue; + const indent = start[1].length; + const body = [start[2]]; + if (/^[|>]/.test(start[2])) { + body.length = 0; + for (let j = i + 1; j < lines.length; j++) { + const lead = lines[j].match(/^\s*/)[0].length; + if (lines[j].trim() !== "" && lead <= indent) break; + body.push(lines[j]); + } + } + assert.doesNotMatch( + body.join("\n"), + /\$\{\{/, + `${name}: run script at line ${i + 1} interpolates an expression`, + ); + } + } +});