Repository navigation
chore(ci): audit only the dependencies a PR adds #6337
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
6 commits
Select commit
Hold shift + click to select a range
06d1de3
chore(ci): remove NPM token from PR build and stop using npx wait-on@…
jbocce f72fd88
chore(ci): audit only the dependencies a PR adds, in a GitHub workflow
jbocce cd4cb49
chore(ci): show audit ignores a PR adds as a neutral check
jbocce 715a6c8
chore(ci): flag new audit ignores with a commit status
jbocce d26655f
fix(ci): harden the dependency audit per review
jbocce 62bf66a
test(ci): add tests for the dependency audit script
jbocce 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,102 @@ | ||
| name: Dependency Audit | ||
|
|
||
| # Fails a PR that adds a package version with a high or critical advisory to | ||
| # pnpm-lock.yaml. Only versions the PR adds count, compared with the point where | ||
| # it branched off its target branch, so advisories already on the target never | ||
| # block an unrelated PR. The logic is .scripts/dependency-audit/audit.mjs. | ||
| # | ||
| # Runs on pull_request_target so the workflow and script always come from the | ||
| # default branch: a PR cannot edit its own check. The PR's files are only | ||
| # downloaded and read as data (lockfile, pnpm-workspace.yaml); none of its code is | ||
| # checked out or run. Keep it that way — never check out the PR head here. | ||
| # Changes to this file or the script take effect once they are on master. | ||
| # | ||
| # No `edited` trigger: a title edit would start a run that cancels the real one | ||
| # and is then skipped, and a skipped check counts as passing. After retargeting | ||
| # a PR to another branch, push a commit (or close and reopen) to rerun. | ||
|
|
||
| on: | ||
| pull_request_target: | ||
| types: [opened, synchronize, reopened] | ||
|
|
||
| permissions: {} | ||
|
|
||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ github.event.pull_request.number }} | ||
| cancel-in-progress: true | ||
|
|
||
| jobs: | ||
| audit: | ||
| name: Dependency audit | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 10 | ||
| permissions: | ||
| contents: read | ||
| statuses: write # the "New dependency audit ignores" status | ||
| steps: | ||
| - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 | ||
| with: | ||
| ref: ${{ github.event.repository.default_branch }} | ||
| persist-credentials: false | ||
| sparse-checkout: .scripts/dependency-audit | ||
| - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0 | ||
| with: | ||
| node-version: 24.15.0 | ||
| - name: Download the PR's lockfile and the target branch's lockfile | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| REPO: ${{ github.repository }} | ||
| BASE_SHA: ${{ github.event.pull_request.base.sha }} | ||
| HEAD_SHA: ${{ github.event.pull_request.head.sha }} | ||
| run: | | ||
| set -euo pipefail | ||
| mkdir -p "$RUNNER_TEMP/audit" | ||
| # Compare against where the PR branched off, not the target's tip, so | ||
| # packages the target changed since then are not credited to the PR. | ||
| merge_base=$(gh api "repos/$REPO/compare/$BASE_SHA...$HEAD_SHA" --jq .merge_base_commit.sha) | ||
| echo "Target branch point: $merge_base" | ||
| # <path> <ref> <dest>. Only a 404 means "no such file" (dest left | ||
| # absent); any other error fails the job rather than skip the audit. | ||
| fetch() { | ||
| if ! gh api -H 'Accept: application/vnd.github.raw' "repos/$REPO/contents/$1?ref=$2" > "$3" 2> "$3.err"; then | ||
| grep -q 'HTTP 404' "$3.err" || { cat "$3.err" >&2; exit 1; } | ||
| rm -f "$3" | ||
| fi | ||
| } | ||
| fetch pnpm-lock.yaml "$merge_base" "$RUNNER_TEMP/audit/base-lock.yaml" | ||
| fetch pnpm-lock.yaml "$HEAD_SHA" "$RUNNER_TEMP/audit/head-lock.yaml" | ||
| fetch pnpm-workspace.yaml "$merge_base" "$RUNNER_TEMP/audit/base-workspace.yaml" | ||
| fetch pnpm-workspace.yaml "$HEAD_SHA" "$RUNNER_TEMP/audit/head-workspace.yaml" | ||
| - name: Install the audit script's dependencies | ||
| working-directory: .scripts/dependency-audit | ||
| run: npm ci --ignore-scripts --no-audit --no-fund | ||
| # No GH_TOKEN here: the script and its dependency never get the token. | ||
| - name: Audit the package versions this PR adds | ||
| id: audit | ||
| run: | | ||
| node .scripts/dependency-audit/audit.mjs \ | ||
| --base "$RUNNER_TEMP/audit/base-lock.yaml" \ | ||
| --head "$RUNNER_TEMP/audit/head-lock.yaml" \ | ||
| --base-workspace "$RUNNER_TEMP/audit/base-workspace.yaml" \ | ||
| --head-workspace "$RUNNER_TEMP/audit/head-workspace.yaml" | ||
| # Ignores the PR adds also get a line of their own in the PR's checks | ||
| # list, not only on this run's page. A commit status rather than a check | ||
| # run: an API-created check run joins whichever check suite GitHub picks | ||
| # for the commit, and when that suite is a skipped one the PR hides it. | ||
| # Statuses always show. State is success, so it never blocks a merge. | ||
| # Runs even when the audit failed. | ||
| - name: Flag new ignores in the PR's checks list | ||
| if: ${{ !cancelled() && steps.audit.outputs.new_ignores != '' && steps.audit.outputs.new_ignores != '0' }} | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| REPO: ${{ github.repository }} | ||
| HEAD_SHA: ${{ github.event.pull_request.head.sha }} | ||
| COUNT: ${{ steps.audit.outputs.new_ignores }} | ||
| RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} | ||
| run: | | ||
| gh api --method POST "repos/$REPO/statuses/$HEAD_SHA" \ | ||
| -f state=success \ | ||
| -f context='New dependency audit ignores' \ | ||
| -f description="⚠️ $COUNT GHSA(s) added to auditConfig.ignoreGhsas: review pnpm-workspace.yaml" \ | ||
| -f target_url="$RUN_URL" \ | ||
| --jq '"\(.context): \(.description)"' | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two points about the trigger:
masterandrelease/*when this PR merges.masternow. When an advisory appears for a version that is already onmaster, no job reports the advisory. I suggest aschedulerun ofpnpm audit --audit-level highonmasterthat does not block, or an explicit decision to rely on Dependabot alerts.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
masterandrelease/*once this merges. GitHub only offers a check there after it has run at least once.masteris planned as a separate task. It needs to run privately, since this repo's Actions logs and issues are public, so it will live outside this repo. Dependabot alerts don't work as the source for us: GitHub's dependency graph can't read our pnpm 12 lockfile (two YAML documents), so the alerts only reflectpackage.jsondeclarations, not the installed versions.