diff --git a/.github/workflows/threat-model.yml b/.github/workflows/threat-model.yml new file mode 100644 index 0000000..c58dae4 --- /dev/null +++ b/.github/workflows/threat-model.yml @@ -0,0 +1,68 @@ +name: Threat model anchor check + +on: + push: + branches: [main] + paths: + - 'docs/THREAT_MODEL.md' + - 'docs/SECURITY.md' + - 'SECURITY.md' + - 'contracts/**' + - 'packages/**' + - 'services/**' + - 'scripts/check-threat-model-anchors.mjs' + - 'scripts/check-threat-model-anchors.test.mjs' + - 'scripts/threat-model-ci.test.mjs' + - 'package.json' + - '.github/workflows/threat-model.yml' + pull_request: + paths: + - 'docs/THREAT_MODEL.md' + - 'docs/SECURITY.md' + - 'SECURITY.md' + - 'contracts/**' + - 'packages/**' + - 'services/**' + - 'scripts/check-threat-model-anchors.mjs' + - 'scripts/check-threat-model-anchors.test.mjs' + - 'scripts/threat-model-ci.test.mjs' + - 'package.json' + - '.github/workflows/threat-model.yml' + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + threat-model: + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + # Read-only workflow: downstream steps do not push + - uses: actions/checkout@v4 + with: + persist-credentials: false + + - uses: pnpm/action-setup@v4 + with: + version: 10.13.1 + + - uses: actions/setup-node@v4 + with: + node-version: "22" + cache: pnpm + + - name: Install JS dependencies + run: pnpm install --frozen-lockfile --ignore-scripts + + - name: Check threat-model workflow configuration + run: node --test scripts/threat-model-ci.test.mjs + + - name: Run threat-model anchor script tests + run: pnpm threat-model:test + + - name: Validate threat-model anchors + run: pnpm threat-model:check diff --git a/docs/CI.md b/docs/CI.md index bd5cf0b..4e5c67b 100644 --- a/docs/CI.md +++ b/docs/CI.md @@ -195,6 +195,22 @@ print order are asserted by: node --test scripts/check-bindings-cli.test.mjs ``` +## Threat-model anchor check + +The `Threat model anchor check` workflow (`.github/workflows/threat-model.yml`) +runs when threat-model docs, security docs, or security-relevant code paths +change (`docs/THREAT_MODEL.md`, `SECURITY.md`, `docs/SECURITY.md`, +`contracts/**`, `packages/**`, `services/**`, and the checker scripts). Its +blocking job installs locked JS dependencies with lifecycle scripts disabled, +then runs `pnpm threat-model:test` and `pnpm threat-model:check` in order so +`docs/THREAT_MODEL.md` keeps the five required sealed-round risk anchors. + +```bash +pnpm threat-model:test # unit tests for the anchor checker +pnpm threat-model:check # docs/THREAT_MODEL.md must cover required anchors +node --test scripts/threat-model-ci.test.mjs # verifies the CI wiring +``` + ## Rust dependency advisory audit Rust dependencies in the committed `Cargo.lock` are audited against the RustSec diff --git a/scripts/threat-model-ci.test.mjs b/scripts/threat-model-ci.test.mjs new file mode 100644 index 0000000..b41973b --- /dev/null +++ b/scripts/threat-model-ci.test.mjs @@ -0,0 +1,51 @@ +// Copyright (c) 2026 Sub Rosa contributors +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import { test } from "node:test"; + +const workflow = readFileSync( + new URL("../.github/workflows/threat-model.yml", import.meta.url), + "utf8", +); +const scripts = JSON.parse( + readFileSync(new URL("../package.json", import.meta.url), "utf8"), +).scripts; + +test("threat-model CI runs threat-model:test and threat-model:check as blocking steps", () => { + assert.equal( + scripts["threat-model:check"], + "node scripts/check-threat-model-anchors.mjs docs/THREAT_MODEL.md", + ); + assert.equal( + scripts["threat-model:test"], + "node --test scripts/check-threat-model-anchors.test.mjs", + ); + + assert.match(workflow, /^permissions:\n(?:\s+#.*\n)* contents: read$/m); + assert.match( + workflow, + /^concurrency:\n(?:\s+#.*\n)* group: \$\{\{ github\.workflow \}\}-\$\{\{ github\.ref \}\}\n cancel-in-progress: true$/m, + ); + assert.doesNotMatch(workflow, /\bcontinue-on-error\s*:/); + + assert.match(workflow, /docs\/THREAT_MODEL\.md/); + assert.match(workflow, /SECURITY\.md/); + assert.match(workflow, /contracts\/\*\*/); + assert.match(workflow, /packages\/\*\*/); + assert.match(workflow, /services\/\*\*/); + + const job = workflow.match(/^ threat-model:\n([\s\S]*)$/m)?.[1]; + assert.ok(job, "threat-model job must exist"); + assert.doesNotMatch(job, /^ [a-z][\w-]*:/m, "checks must stay in the threat-model job"); + + let previousStep = -1; + for (const step of [ + "node --test scripts/threat-model-ci.test.mjs", + "pnpm threat-model:test", + "pnpm threat-model:check", + ]) { + const index = job.indexOf(` run: ${step}\n`); + assert.ok(index > previousStep, `${step} must run as a blocking step in order`); + previousStep = index; + } +});