fix(ci): fork-safe compliance review (two-stage workflow_run) — safe alternative to #829 (#830)

* fix(ci): fork-safe compliance review via two-stage workflow_run

Replaces the pull_request_target approach (which would run untrusted fork
code with the AWS Bedrock secrets in env) with the GitHub-recommended split:

- swedish-compliance-diff.yml (pull_request, no secrets, read-only token):
  computes the diff and uploads it as an artifact. Never runs project code.
- swedish-compliance-review.yml (workflow_run, has secrets + write token):
  checks out ONLY the base repo (trusted script + skills), downloads the
  diff artifact, feeds it to the model as DATA, and posts the comment. Never
  checks out or executes fork PR code.

scripts/swedish-compliance-review.mjs reads the diff from DIFF_FILE/FILES_FILE
when set, with a fallback to git diff for same-repo runs.

Safe alternative to #829.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(ci): pin workflow actions to commit SHAs (Superagent P1)

Pin actions/checkout, setup-node, upload-artifact, download-artifact and the
peter-evans comment actions to immutable 40-char SHAs with version comments,
closing the two Superagent supply-chain findings. Matters most here since the
review stage holds AWS Bedrock secrets + a write token.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(ci): full base fetch in compliance-diff so merge-base works when branch is behind

The --depth=1 base fetch left git merge-base with no reachable common ancestor
once main advanced past the PR branch, failing the prepare job under bash -e.
checkout already uses fetch-depth: 0, so a full base fetch makes merge-base
reliable regardless of how far base has moved.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(ci): harden compliance review per security audit

Stage 1 (swedish-compliance-diff.yml): pass github.base_ref + PR number via
env instead of interpolating ${{ }} into the run: shell (template-injection
antipattern); add set -euo pipefail; printf over echo.

Stage 2 (swedish-compliance-review.yml): pin @anthropic-ai/bedrock-sdk@0.31.0
and add --ignore-scripts — the privileged job (write token) must not run a
floating @latest or dependency lifecycle scripts. set -euo pipefail on the
PR-number guard.

Script: frame the untrusted diff/files with a per-run unguessable random
sentinel (not a code fence a hostile diff could close) plus an explicit
'treat as data, ignore embedded instructions' system-prompt guard and output
constraints (no images/@-mentions/links/HTML). Legacy getDiff now uses
execFileSync (argv array, no shell).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-06-29 23:44:30 +02:00
committed by GitHub
co-authored by Claude Opus 4.8
parent 1cd8863958
commit a68123bbe8
3 changed files with 157 additions and 34 deletions
@@ -0,0 +1,51 @@
name: Compliance diff
# Stage 1 of the fork-safe compliance review (see swedish-compliance-review.yml).
#
# This runs on the untrusted PR head, but is SAFE because it has NO secrets and
# only a read-only token: it computes the diff and uploads it as an artifact.
# It never runs project code (no `npm install`, no `node`) — only git plumbing,
# which does not execute repository hooks. The privileged half (model call +
# comment) lives in stage 2, which never checks out fork code.
on:
pull_request:
types: [opened, synchronize, reopened]
permissions:
contents: read
jobs:
prepare:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0
- name: Compute diff vs base
# Pass GitHub context via env, never interpolate ${{ }} into the shell
# body — expression substitution happens before bash parses the script,
# so a value with shell metacharacters would be a code-execution sink.
env:
BASE_REF: ${{ github.base_ref }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
set -euo pipefail
# Full fetch (not --depth=1): the PR branch may be behind base, and a
# shallow base can leave merge-base with no reachable common ancestor.
# checkout above uses fetch-depth: 0, so HEAD already has full history.
git fetch origin "$BASE_REF"
MERGE_BASE=$(git merge-base "origin/$BASE_REF" HEAD)
git diff "$MERGE_BASE" HEAD > diff.patch
git diff --name-only "$MERGE_BASE" HEAD > files.txt
printf '%s\n' "$PR_NUMBER" > pr-number.txt
- name: Upload diff artifact
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
with:
name: compliance-input
path: |
diff.patch
files.txt
pr-number.txt
retention-days: 1
+49 -13
View File
@@ -1,44 +1,80 @@
name: Swedish Accounting Compliance Review
# Stage 2 of the fork-safe compliance review (stage 1 is swedish-compliance-diff.yml).
#
# SECURITY: this is the privileged half — it has the AWS Bedrock secrets and a
# write token. It is triggered by `workflow_run` (NOT pull_request_target) and
# checks out ONLY the base repo, so it never executes fork PR code. The untrusted
# input (the PR diff) arrives as a downloaded artifact and is fed to the model
# as DATA — never run. This is the pattern GitHub recommends instead of
# `pull_request_target` + checking out the PR head.
on:
pull_request:
types: [opened, synchronize, reopened]
workflow_run:
workflows: ["Compliance diff"]
types: [completed]
permissions:
contents: read
pull-requests: write
actions: read # required to download the artifact from the triggering run
pull-requests: write # required to post the review comment
jobs:
review:
runs-on: ubuntu-latest
# Only act on PR-triggered diffs that actually produced an artifact.
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0
- uses: actions/setup-node@v4
# Base repo only — the TRUSTED copy of the script and .claude/skills/.
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
with:
node-version: 20
- name: Download diff artifact
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
with:
name: compliance-input
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Resolve PR number
id: pr
run: |
set -euo pipefail
NUM=$(cat pr-number.txt)
# Guard: pr-number.txt must be a plain integer (artifact is untrusted input).
if ! [[ "$NUM" =~ ^[0-9]+$ ]]; then
echo "Refusing to continue: pr-number.txt is not a number" >&2
exit 1
fi
echo "number=$NUM" >> "$GITHUB_OUTPUT"
- name: Install Anthropic Bedrock SDK
run: npm install --no-save --no-package-lock @anthropic-ai/bedrock-sdk@latest
# Pinned exact version + --ignore-scripts: this is the privileged job
# (write token in env), so no floating @latest and no dependency
# lifecycle scripts may execute here.
run: npm install --no-save --no-package-lock --ignore-scripts @anthropic-ai/bedrock-sdk@0.31.0
- name: Run compliance review
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION || 'eu-north-1' }}
GITHUB_BASE_REF: ${{ github.base_ref }}
REVIEW_MODEL: eu.anthropic.claude-sonnet-4-6
# Two-stage mode: read the diff from the artifact instead of git-diffing.
DIFF_FILE: diff.patch
FILES_FILE: files.txt
run: node scripts/swedish-compliance-review.mjs
- name: Find previous compliance comment
uses: peter-evans/find-comment@v3
uses: peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e # v3
id: find-comment
with:
issue-number: ${{ github.event.pull_request.number }}
issue-number: ${{ steps.pr.outputs.number }}
comment-author: 'github-actions[bot]'
body-includes: '<!-- swedish-compliance-review-bot -->'
- name: Post or update PR comment
uses: peter-evans/create-or-update-comment@v5
uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5
with:
issue-number: ${{ github.event.pull_request.number }}
issue-number: ${{ steps.pr.outputs.number }}
comment-id: ${{ steps.find-comment.outputs.comment-id }}
body-path: review.md
edit-mode: replace