Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
node-version: '22'
cache: 'npm'
cache-dependency-path: client-vscode/package-lock.json

Expand Down
176 changes: 176 additions & 0 deletions .github/workflows/npm-audit-fix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
name: npm audit fix

on:
schedule:
- cron: '0 6 * * 2'
workflow_dispatch:

permissions:
contents: read

concurrency:
group: npm-audit-fix
cancel-in-progress: false

jobs:
# Runs updated third-party code (npm ci, tsc), so it gets no write access and no persisted credentials.
audit-fix:
runs-on: ubuntu-latest
outputs:
fixed: ${{ steps.after.outputs.fixed }}
defaults:
run:
working-directory: client-vscode

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
persist-credentials: false

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
cache: 'npm'
cache-dependency-path: client-vscode/package-lock.json

- name: Install dependencies
run: npm ci --ignore-scripts

- name: Audit before fix
id: before
run: |
f="$RUNNER_TEMP/audit-before.json"
npm audit --json > "$f" || true
if ! jq -e '.metadata.vulnerabilities.total | numbers' "$f" > /dev/null 2>&1; then
echo "::error::npm audit did not return a valid report"
cat "$f"
exit 1
fi
echo "count=$(jq '.metadata.vulnerabilities.total' "$f")" >> "$GITHUB_OUTPUT"

# Without --force: only in-range updates, so no breaking major bumps of direct deps.
# Exits non-zero whenever vulnerabilities remain, so only real npm errors are surfaced.
- name: Apply npm audit fix
if: steps.before.outputs.count != '0'
run: |
log="$RUNNER_TEMP/audit-fix.log"
if ! npm audit fix --ignore-scripts > "$log" 2>&1; then
if grep -qE '^npm (ERR!|error)' "$log"; then
echo "::warning::npm audit fix reported errors; see log below"
fi
fi
cat "$log"

- name: Audit after fix
id: after
if: steps.before.outputs.count != '0'
env:
BEFORE: ${{ steps.before.outputs.count }}
run: |
f="$RUNNER_TEMP/audit-after.json"
npm audit --json > "$f" || true
if ! jq -e '.metadata.vulnerabilities.total | numbers' "$f" > /dev/null 2>&1; then
echo "::error::npm audit did not return a valid report after fix"
cat "$f"
exit 1
fi
after=$(jq '.metadata.vulnerabilities.total' "$f")
if [ "$after" -lt "$BEFORE" ] && ! git diff --quiet -- package.json package-lock.json; then
echo "fixed=true" >> "$GITHUB_OUTPUT"
else
echo "fixed=false" >> "$GITHUB_OUTPUT"
fi

- name: Verify fixed tree installs and compiles
if: steps.after.outputs.fixed == 'true'
run: |
npm ci --ignore-scripts
npm run compile

- name: Build PR body and patch
if: steps.after.outputs.fixed == 'true'
run: |
out="$RUNNER_TEMP/audit-fix-out"
mkdir -p "$out"
git diff -- package.json package-lock.json > "$out/fix.patch"
{
echo "Automated \`npm audit fix\` for vulnerable dependencies that Dependabot did not update."
echo
echo "### Fixed"
jq -r --slurpfile after "$RUNNER_TEMP/audit-after.json" '
($after[0].vulnerabilities // {}) as $a
| (.vulnerabilities // {}) | to_entries[]
| select(.key as $k | $a | has($k) | not)
| ([.value.via[] | objects | .title] | unique | join("; ")) as $t
| "- `\(.key)` (\(.value.severity)): "
+ (if $t == "" then "via " + ([.value.via[] | strings] | unique | join(", ")) else $t end)
' "$RUNNER_TEMP/audit-before.json" | grep . || echo "_None_"
echo
echo "### Still vulnerable (needs a major bump or \`overrides\`)"
jq -r '
(.vulnerabilities // {}) | to_entries[]
| "- `\(.key)` (\(.value.severity)) via \([.value.via[] | if type == "string" then . else .name end] | unique | join(", "))"
' "$RUNNER_TEMP/audit-after.json" | grep . || echo "_None_"
echo
echo "### Changes"
echo '```'
git diff --stat -- package.json package-lock.json
echo '```'
} > "$out/pr-body.md"
cat "$out/pr-body.md" >> "$GITHUB_STEP_SUMMARY"

- name: Upload fix
if: steps.after.outputs.fixed == 'true'
uses: actions/upload-artifact@v4
with:
name: audit-fix
path: ${{ runner.temp }}/audit-fix-out/
retention-days: 1

- name: Report unfixable vulnerabilities
if: steps.before.outputs.count != '0' && steps.after.outputs.fixed != 'true'
run: |
{
echo "### npm audit: vulnerabilities with no in-range fix"
echo '```'
npm audit || true
echo '```'
} >> "$GITHUB_STEP_SUMMARY"

# Never runs project code; only applies the verified patch and opens the PR.
# AUDIT_FIX_TOKEN (not GITHUB_TOKEN) so the PR triggers build.yml.
create-pr:
needs: audit-fix
if: needs.audit-fix.outputs.fixed == 'true'
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
persist-credentials: false

- name: Download fix
uses: actions/download-artifact@v4
with:
name: audit-fix
path: ${{ runner.temp }}/audit-fix

- name: Apply patch
run: git apply "$RUNNER_TEMP/audit-fix/fix.patch"

- name: Create pull request
uses: peter-evans/create-pull-request@v7
with:
token: ${{ secrets.AUDIT_FIX_TOKEN }}
branch: automation/npm-audit-fix
delete-branch: true
add-paths: |
client-vscode/package.json
client-vscode/package-lock.json
commit-message: Fix vulnerable npm dependencies via npm audit fix
title: Fix vulnerable npm dependencies (npm audit fix)
body-path: ${{ runner.temp }}/audit-fix/pr-body.md
labels: dependencies, security
22 changes: 4 additions & 18 deletions client-vscode/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading