1+ name : NPM Audit Security Check
2+
3+ on :
4+ workflow_call :
5+ workflow_dispatch :
6+
7+ permissions :
8+ contents : read
9+
10+ jobs :
11+ npm-audit :
12+ runs-on : ubuntu-latest
13+ steps :
14+ - name : Checkout Code
15+ uses : actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
16+
17+ - name : Setup Node.js
18+ uses : actions/setup-node@820762786026740c76f36085b0efc47a31fe5020
19+ with :
20+ node-version : ' 20'
21+ cache : ' npm'
22+
23+ - name : Run NPM Audit & Generate Summary
24+ run : |
25+ # 1. Run npm audit in JSON mode to capture raw audit metadata
26+ AUDIT_JSON=$(npm audit --audit-level=high --json 2>/dev/null) || true
27+
28+ # 2. Extract counts using node's built-in JSON parser
29+ CRITICAL_COUNT=$(echo "$AUDIT_JSON" | node -e "let d=''; process.stdin.on('data', c => d += c); process.stdin.on('end', () => { try { console.log(JSON.parse(d).metadata.vulnerabilities.critical || 0) } catch { console.log(0) } })")
30+ HIGH_COUNT=$(echo "$AUDIT_JSON" | node -e "let d=''; process.stdin.on('data', c => d += c); process.stdin.on('end', () => { try { console.log(JSON.parse(d).metadata.vulnerabilities.high || 0) } catch { console.log(0) } })")
31+
32+ # 3. Build GitHub Job Summary Markdown
33+ echo "## 🛡️ NPM Dependency Audit Summary" >> $GITHUB_STEP_SUMMARY
34+ echo "" >> $GITHUB_STEP_SUMMARY
35+
36+ if [ "$CRITICAL_COUNT" -eq 0 ] && [ "$HIGH_COUNT" -eq 0 ]; then
37+ echo "✅ **No high or critical vulnerabilities found!** All JS packages passed security checks." >> $GITHUB_STEP_SUMMARY
38+ else
39+ echo "⚠️ **High/Critical Vulnerabilities Detected in npm Dependencies**" >> $GITHUB_STEP_SUMMARY
40+ echo "" >> $GITHUB_STEP_SUMMARY
41+ echo "| Vulnerability Severity | Total Count |" >> $GITHUB_STEP_SUMMARY
42+ echo "| :--- | :--- |" >> $GITHUB_STEP_SUMMARY
43+ echo "| 🔴 **Critical** | $CRITICAL_COUNT \vert{}" >> $GITHUB_STEP_SUMMARY
44+ echo "| 🟠 **High** | $HIGH_COUNT \vert{}" >> $GITHUB_STEP_SUMMARY
45+ echo "" >> $GITHUB_STEP_SUMMARY
46+ echo "### Detailed Audit Report" >> $GITHUB_STEP_SUMMARY
47+ echo '```text' >> $GITHUB_STEP_SUMMARY
48+ # Output human-readable audit text into the summary block
49+ npm audit --audit-level=high 2>/dev/null || true
50+ echo '```' >> $GITHUB_STEP_SUMMARY
51+
52+ # 4. Fail the workflow step
53+ echo "::error::npm audit detected $CRITICAL_COUNT critical and $HIGH_COUNT high vulnerabilities."
54+ exit 1
55+ fi
0 commit comments