forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
323 lines (288 loc) · 12.6 KB
/
Copy pathdependency-scan.yml
File metadata and controls
323 lines (288 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
name: Dependency Vulnerability Scan
on:
push:
branches: [main, develop]
pull_request:
branches: [main, develop]
schedule:
# Weekly full scan every Monday at 08:00 UTC
- cron: '0 8 * * 1'
workflow_dispatch:
permissions:
contents: read
pull-requests: write # needed to post PR comments
jobs:
# ── depcheck: unused dependency detection ───────────────────────────────────
depcheck:
name: Unused Dependency Check (depcheck)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: 20
- name: Install backend deps
run: npm ci
working-directory: novaRewards/backend
- name: Install frontend deps
run: npm ci
working-directory: novaRewards/frontend
- name: Install depcheck globally
run: npm install -g depcheck@1.4.7
- name: Run depcheck (backend)
id: depcheck_backend
run: |
set +e
REPORT=$(depcheck . --json 2>&1)
EXIT=$?
echo "report<<EOF" >> $GITHUB_OUTPUT
echo "$REPORT" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "exit_code=$EXIT" >> $GITHUB_OUTPUT
# Human-readable summary
echo "$REPORT" | node -e "
const d = JSON.parse(require('fs').readFileSync('/dev/stdin','utf8'));
const unused = d.dependencies || [];
const unusedDev = d.devDependencies || [];
const missing = Object.keys(d.missing || {});
console.log('Unused dependencies:', unused.length ? unused.join(', ') : 'none');
console.log('Unused devDependencies:', unusedDev.length ? unusedDev.join(', ') : 'none');
console.log('Missing (used but not declared):', missing.length ? missing.join(', ') : 'none');
" 2>/dev/null || echo "$REPORT"
working-directory: novaRewards/backend
- name: Run depcheck (frontend)
id: depcheck_frontend
run: |
set +e
REPORT=$(depcheck . --json 2>&1)
EXIT=$?
echo "report<<EOF" >> $GITHUB_OUTPUT
echo "$REPORT" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "exit_code=$EXIT" >> $GITHUB_OUTPUT
echo "$REPORT" | node -e "
const d = JSON.parse(require('fs').readFileSync('/dev/stdin','utf8'));
const unused = d.dependencies || [];
const unusedDev = d.devDependencies || [];
const missing = Object.keys(d.missing || {});
console.log('Unused dependencies:', unused.length ? unused.join(', ') : 'none');
console.log('Unused devDependencies:', unusedDev.length ? unusedDev.join(', ') : 'none');
console.log('Missing (used but not declared):', missing.length ? missing.join(', ') : 'none');
" 2>/dev/null || echo "$REPORT"
working-directory: novaRewards/frontend
- name: Post depcheck PR comment
continue-on-error: true
if: github.event_name == 'pull_request'
uses: actions/github-script@v9
with:
script: |
function parseReport(raw) {
try { return JSON.parse(raw); } catch { return {}; }
}
const backendReport = parseReport(`${{ steps.depcheck_backend.outputs.report }}`);
const frontendReport = parseReport(`${{ steps.depcheck_frontend.outputs.report }}`);
function section(label, report) {
const unused = report.dependencies || [];
const unusedDev = report.devDependencies || [];
const missing = Object.keys(report.missing || {});
const lines = [`**${label}**`];
if (unused.length) lines.push(`- Unused deps: \`${unused.join('`, `')}\``);
if (unusedDev.length) lines.push(`- Unused devDeps: \`${unusedDev.join('`, `')}\``);
if (missing.length) lines.push(`- Missing (used but undeclared): \`${missing.join('`, `')}\``);
if (!unused.length && !unusedDev.length && !missing.length) lines.push('- ✅ No unused or missing dependencies');
return lines.join('\n');
}
const body = [
'## 🧹 depcheck — Unused Dependency Report',
'',
section('Backend (`novaRewards/backend`)', backendReport),
'',
section('Frontend (`novaRewards/frontend`)', frontendReport),
'',
'> Review unused entries and remove them to reduce install time, bundle size, and attack surface.',
].join('\n');
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const existing = comments.find(c => c.body.includes('depcheck — Unused Dependency Report'));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
});
}
# ── npm audit: vulnerability scanning ───────────────────────────────────────
npm-audit:
name: npm audit
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: 20
- name: Install backend deps
run: npm ci
working-directory: novaRewards/backend
- name: Install frontend deps
run: npm ci
working-directory: novaRewards/frontend
- name: Run npm audit (backend)
id: audit_backend
run: |
set +e
REPORT=$(npm audit --audit-level=high --json 2>&1)
EXIT=$?
echo "report<<EOF" >> $GITHUB_OUTPUT
echo "$REPORT" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "exit_code=$EXIT" >> $GITHUB_OUTPUT
# Human-readable summary for step log
echo "$REPORT" | node -e "
const d=JSON.parse(require('fs').readFileSync('/dev/stdin','utf8'));
const v=d.metadata?.vulnerabilities||{};
console.log('critical:',v.critical||0,'high:',v.high||0,'moderate:',v.moderate||0,'low:',v.low||0);
" 2>/dev/null || echo "$REPORT"
working-directory: novaRewards/backend
- name: Run npm audit (frontend)
id: audit_frontend
run: |
set +e
REPORT=$(npm audit --audit-level=high --json 2>&1)
EXIT=$?
echo "report<<EOF" >> $GITHUB_OUTPUT
echo "$REPORT" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "exit_code=$EXIT" >> $GITHUB_OUTPUT
echo "$REPORT" | node -e "
const d=JSON.parse(require('fs').readFileSync('/dev/stdin','utf8'));
const v=d.metadata?.vulnerabilities||{};
console.log('critical:',v.critical||0,'high:',v.high||0,'moderate:',v.moderate||0,'low:',v.low||0);
" 2>/dev/null || echo "$REPORT"
working-directory: novaRewards/frontend
- name: Post PR comment
continue-on-error: true
if: github.event_name == 'pull_request'
uses: actions/github-script@v9
with:
script: |
const backendReport = JSON.parse(`${{ steps.audit_backend.outputs.report }}` || '{}');
const frontendReport = JSON.parse(`${{ steps.audit_frontend.outputs.report }}` || '{}');
function summary(label, report) {
const v = report?.metadata?.vulnerabilities || {};
const total = (v.critical||0) + (v.high||0) + (v.moderate||0) + (v.low||0);
if (total === 0) return `**${label}**: ✅ No vulnerabilities found`;
const icon = (v.critical||0) > 0 ? '🔴' : (v.high||0) > 0 ? '🟠' : '🟡';
return `**${label}**: ${icon} critical: ${v.critical||0}, high: ${v.high||0}, moderate: ${v.moderate||0}, low: ${v.low||0}`;
}
const backendExit = '${{ steps.audit_backend.outputs.exit_code }}';
const frontendExit = '${{ steps.audit_frontend.outputs.exit_code }}';
const blocked = backendExit !== '0' || frontendExit !== '0';
const body = [
'## 🔍 npm Dependency Vulnerability Scan',
'',
summary('Backend (novaRewards/backend)', backendReport),
summary('Frontend (novaRewards/frontend)', frontendReport),
'',
blocked
? '> ❌ **Critical/high vulnerabilities detected — merge is blocked until resolved.**'
: '> ✅ No critical or high severity vulnerabilities found.',
].join('\n');
// Update existing comment if present, otherwise create
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const existing = comments.find(c => c.body.includes('npm Dependency Vulnerability Scan'));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
});
}
- name: Fail on critical/high vulnerabilities
if: steps.audit_backend.outputs.exit_code != '0' || steps.audit_frontend.outputs.exit_code != '0'
run: |
echo "Critical or high severity npm vulnerabilities found. Blocking merge."
exit 1
cargo-audit:
name: cargo audit
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- name: Install cargo-audit
run: cargo install cargo-audit --locked
- name: Run cargo audit
id: cargo_audit
run: |
set +e
REPORT=$(cargo audit --json 2>&1)
EXIT=$?
echo "exit_code=$EXIT" >> $GITHUB_OUTPUT
echo "report<<EOF" >> $GITHUB_OUTPUT
echo "$REPORT" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
# Human-readable output to log
cargo audit 2>&1 || true
working-directory: contracts
- name: Post PR comment (cargo)
continue-on-error: true
if: github.event_name == 'pull_request'
uses: actions/github-script@v9
with:
script: |
const exitCode = '${{ steps.cargo_audit.outputs.exit_code }}';
const blocked = exitCode !== '0';
const body = [
'## 🦀 Cargo Dependency Vulnerability Scan',
'',
blocked
? '> ❌ **Vulnerabilities found in Rust dependencies — merge is blocked until resolved.**\n>\n> Run `cargo audit` locally in `contracts/` for details.'
: '> ✅ No vulnerabilities found in Rust dependencies.',
].join('\n');
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const existing = comments.find(c => c.body.includes('Cargo Dependency Vulnerability Scan'));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
});
}
- name: Fail on vulnerabilities
if: steps.cargo_audit.outputs.exit_code != '0'
run: |
echo "Cargo audit found vulnerabilities. Blocking merge."
exit 1