|
| 1 | +name: security |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - dev |
| 7 | + pull_request: |
| 8 | + workflow_dispatch: |
| 9 | + schedule: |
| 10 | + - cron: "0 6 * * 1" # every Monday 06:00 UTC |
| 11 | + |
| 12 | +concurrency: |
| 13 | + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} |
| 14 | + cancel-in-progress: true |
| 15 | + |
| 16 | +permissions: |
| 17 | + contents: read |
| 18 | + issues: write |
| 19 | + |
| 20 | +env: |
| 21 | + FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true |
| 22 | + |
| 23 | +jobs: |
| 24 | + audit: |
| 25 | + name: dependency-audit |
| 26 | + runs-on: blacksmith-4vcpu-ubuntu-2404 |
| 27 | + defaults: |
| 28 | + run: |
| 29 | + shell: bash |
| 30 | + steps: |
| 31 | + - name: Checkout repository |
| 32 | + uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 |
| 33 | + with: |
| 34 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 35 | + |
| 36 | + - name: Setup Node |
| 37 | + uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 |
| 38 | + with: |
| 39 | + node-version: "24" |
| 40 | + |
| 41 | + - name: Setup Bun |
| 42 | + uses: ./.github/actions/setup-bun |
| 43 | + |
| 44 | + - name: Run dependency audit |
| 45 | + id: audit |
| 46 | + continue-on-error: true |
| 47 | + run: | |
| 48 | + bun audit --audit-level=high --json > audit-result.json 2>&1 || true |
| 49 | +
|
| 50 | + - name: Analyze audit results |
| 51 | + id: analyze |
| 52 | + run: | |
| 53 | + if [ ! -f audit-result.json ] || [ "$(wc -l < audit-result.json)" -lt 5 ]; then |
| 54 | + echo "No high/critical vulnerabilities found. ✅" |
| 55 | + echo "has_vulns=false" >> "$GITHUB_OUTPUT" |
| 56 | + exit 0 |
| 57 | + fi |
| 58 | +
|
| 59 | + node << 'EOF' >> "$GITHUB_OUTPUT" |
| 60 | + const data = JSON.parse(require('fs').readFileSync('audit-result.json', 'utf-8')); |
| 61 | + const packages = Object.keys(data); |
| 62 | + let total = 0; |
| 63 | + let table = ''; |
| 64 | + const details = []; |
| 65 | +
|
| 66 | + for (const [pkg, advisories] of Object.entries(data)) { |
| 67 | + const high = advisories.filter(a => a.severity === 'high').length; |
| 68 | + const critical = advisories.filter(a => a.severity === 'critical').length; |
| 69 | + total += advisories.length; |
| 70 | +
|
| 71 | + const worst = critical > 0 ? 'critical' : 'high'; |
| 72 | + const label = critical > 0 ? '🔴' : '🟡'; |
| 73 | + table += `| ${pkg} | ${label} ${worst} | ${advisories.length} |\n`; |
| 74 | +
|
| 75 | + for (const adv of advisories) { |
| 76 | + details.push({ pkg, id: adv.id, title: adv.title, severity: adv.severity, url: adv.url, cvss: adv.cvss?.score || 'N/A' }); |
| 77 | + } |
| 78 | + } |
| 79 | +
|
| 80 | + console.log(`has_vulns=true`); |
| 81 | + console.log(`total=${total}`); |
| 82 | + console.log(`packages_count=${packages.length}`); |
| 83 | +
|
| 84 | + const summary = `### 🔒 Audit: ${total} high/critical vulnerabilities\n\n| Package | Severity | Count |\n|---|---|---|\n${table}`; |
| 85 | + require('fs').writeFileSync('audit-summary.md', summary); |
| 86 | + require('fs').writeFileSync('audit-details.json', JSON.stringify(details, null, 2)); |
| 87 | + EOF |
| 88 | +
|
| 89 | + - name: Post summary to PR |
| 90 | + if: github.event_name == 'pull_request' && fromJSON(steps.analyze.outputs.has_vulns) |
| 91 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 92 | + with: |
| 93 | + script: | |
| 94 | + const fs = require('fs'); |
| 95 | + const summary = fs.readFileSync('audit-summary.md', 'utf-8'); |
| 96 | + const body = summary + '\n_Run `bun audit` locally for full details._\n\n> This is a soft gate — the workflow does not fail. Review and prioritize fixes.'; |
| 97 | + const { data: comment } = await github.rest.issues.createComment({ |
| 98 | + issue_number: context.issue.number, |
| 99 | + owner: context.repo.owner, |
| 100 | + repo: context.repo.repo, |
| 101 | + body: body |
| 102 | + }); |
| 103 | + core.notice(`Audit results posted to PR #${context.issue.number}`); |
| 104 | +
|
| 105 | + - name: Upload audit report |
| 106 | + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 |
| 107 | + with: |
| 108 | + name: audit-report-${{ github.run_attempt }} |
| 109 | + if-no-files-found: warn |
| 110 | + retention-days: 30 |
| 111 | + path: | |
| 112 | + audit-result.json |
| 113 | + audit-details.json |
0 commit comments