-
Notifications
You must be signed in to change notification settings - Fork 38
470 lines (409 loc) · 16.8 KB
/
privsignal.yml
File metadata and controls
470 lines (409 loc) · 16.8 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
name: PrivSignal
on:
pull_request:
branches:
- master
- hotfix-*
- prerelease-*
- nextgen-ux
jobs:
lockfile-refresh-gate:
name: Detect lockfile refresh head commit
runs-on: ubuntu-latest
outputs:
skip: ${{ steps.detect.outputs.skip }}
reason: ${{ steps.detect.outputs.reason }}
steps:
- name: 🛎️ Checkout head commit
uses: actions/checkout@v4
with:
fetch-depth: 2
ref: ${{ format('refs/pull/{0}/head', github.event.pull_request.number) }}
- name: 🔍 Detect lockfile-refresh-only head commit
id: detect
shell: bash
run: bash .github/scripts/should_skip_for_lockfile_refresh.sh
privsignal:
name: PrivSignal (informational)
runs-on: ubuntu-latest
needs: [lockfile-refresh-gate]
if: needs.lockfile-refresh-gate.outputs.skip != 'true'
permissions:
contents: read
issues: write
pull-requests: write
steps:
- name: 🛎️ Checkout PR branch
uses: actions/checkout@v4
with:
fetch-depth: 0
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.ref }}
- name: 🧪 Setup Elixir
uses: erlef/setup-elixir@v1
with:
elixir-version: 1.19.2
otp-version: 28.1.1
- name: ⬇️ Install dependencies
continue-on-error: true
run: mix deps.get
- name: 🔨 Compile PrivSignal dependency
continue-on-error: true
run: mix deps.compile priv_signal
- name: ♻️ Regenerate lockfile (informational)
id: lockfile
shell: bash
run: |
set +e
mkdir -p tmp/privsignal
mix priv_signal.scan --quiet --json-path priv_signal.lockfile.json
scan_exit=$?
lockfile_changed=false
if [ "$scan_exit" -eq 0 ]; then
cp priv_signal.lockfile.json tmp/privsignal/ci.lockfile.json
if ! git diff --quiet -- priv_signal.lockfile.json; then
lockfile_changed=true
fi
fi
echo "scan_exit=$scan_exit" >> "$GITHUB_OUTPUT"
echo "lockfile_changed=$lockfile_changed" >> "$GITHUB_OUTPUT"
# Informational only.
exit 0
- name: 🔎 Run PrivSignal diff/score (informational)
id: privsignal
if: always()
shell: bash
run: |
set +e
mkdir -p tmp/privsignal
DIFF_PATH="tmp/privsignal/ci.diff.json"
SCORE_PATH="tmp/privsignal/ci.score.json"
BASE_BRANCH_REF="origin/${{ github.base_ref }}"
BASE_SHA_REF="${{ github.event.pull_request.base.sha }}"
BASE_REF=""
# Ensure latest target branch state is available locally.
git fetch --no-tags origin "${{ github.base_ref }}"
if [ -f "priv_signal.lockfile.json" ]; then
if git cat-file -e "${BASE_BRANCH_REF}:priv_signal.lockfile.json" 2>/dev/null; then
BASE_REF="$BASE_BRANCH_REF"
elif git cat-file -e "${BASE_SHA_REF}:priv_signal.lockfile.json" 2>/dev/null; then
BASE_REF="$BASE_SHA_REF"
fi
if [ -n "$BASE_REF" ]; then
mix priv_signal.diff \
--base "$BASE_REF" \
--candidate-path priv_signal.lockfile.json \
--format json \
--output "$DIFF_PATH"
diff_exit=$?
else
diff_exit=1
fi
else
diff_exit=1
fi
if [ "$diff_exit" -eq 0 ]; then
mix priv_signal.score --diff "$DIFF_PATH" --output "$SCORE_PATH"
score_exit=$?
else
score_exit=1
fi
if [ -f "$SCORE_PATH" ]; then
score=$(jq -r '.score // "ERROR"' "$SCORE_PATH")
else
score="ERROR"
fi
echo "diff_exit=$diff_exit" >> "$GITHUB_OUTPUT"
echo "score_exit=$score_exit" >> "$GITHUB_OUTPUT"
echo "score=$score" >> "$GITHUB_OUTPUT"
echo "base_ref=$BASE_REF" >> "$GITHUB_OUTPUT"
# Informational only.
exit 0
- name: 🧾 Build PrivSignal report
if: always()
continue-on-error: true
shell: bash
run: |
mkdir -p tmp/privsignal
mix run --no-start .github/scripts/priv_signal_ci_report.exs -- \
tmp/privsignal/ci.score.json \
tmp/privsignal/ci.diff.json \
tmp/privsignal/ci.report.md \
tmp/privsignal/ci.report.json \
25
- name: 🏷️ Sync privacy label
if: always()
id: privacy-label
uses: actions/github-script@v7
env:
PRIVSIGNAL_SCORE: ${{ steps.privsignal.outputs.score }}
with:
script: |
const pr = context.payload.pull_request;
if (!pr) {
core.info("No pull_request context found; skipping privacy label sync.");
core.setOutput("privacy_tier", "skipped");
core.setOutput("privacy_label", "");
return;
}
if (pr.head.repo.full_name !== `${context.repo.owner}/${context.repo.repo}`) {
core.warning("Fork PR detected; GitHub does not grant label write access to this workflow token. Skipping privacy label sync.");
core.setOutput("privacy_tier", "fork-skip");
core.setOutput("privacy_label", "");
return;
}
const score = String(process.env.PRIVSIGNAL_SCORE || "").trim().toLowerCase();
const validTiers = new Set(["none", "low", "medium", "high"]);
const managedLabels = [...validTiers].map((tier) => `privacy-${tier}`);
if (!validTiers.has(score)) {
core.warning(`PrivSignal score '${score || "missing"}' is not a recognized tier; leaving privacy labels unchanged.`);
core.setOutput("privacy_tier", "unavailable");
core.setOutput("privacy_label", "");
return;
}
const desiredLabel = `privacy-${score}`;
const { owner, repo } = context.repo;
const issue_number = pr.number;
const labelColors = {
none: "2da44e",
low: "1f6feb",
medium: "d4a72c",
high: "cf222e",
};
try {
try {
await github.rest.issues.getLabel({ owner, repo, name: desiredLabel });
} catch (error) {
if (error.status !== 404) {
throw error;
}
await github.rest.issues.createLabel({
owner,
repo,
name: desiredLabel,
color: labelColors[score],
description: `PrivSignal privacy score: ${score}`,
});
}
const existing = await github.paginate(github.rest.issues.listLabelsOnIssue, {
owner,
repo,
issue_number,
per_page: 100,
});
for (const label of existing) {
if (managedLabels.includes(label.name) && label.name !== desiredLabel) {
core.info(`Removing stale privacy label '${label.name}' from PR #${issue_number}`);
await github.rest.issues.removeLabel({
owner,
repo,
issue_number,
name: label.name,
});
}
}
if (!existing.some((label) => label.name === desiredLabel)) {
core.info(`Adding privacy label '${desiredLabel}' to PR #${issue_number}`);
await github.rest.issues.addLabels({
owner,
repo,
issue_number,
labels: [desiredLabel],
});
}
core.setOutput("privacy_tier", score);
core.setOutput("privacy_label", desiredLabel);
} catch (error) {
core.warning(`Unable to sync privacy label: ${error.message}`);
core.setOutput("privacy_tier", "unavailable");
core.setOutput("privacy_label", "");
}
- name: 📌 Publish job summary
if: always()
shell: bash
run: |
if [ -f tmp/privsignal/ci.report.md ]; then
cat tmp/privsignal/ci.report.md >> "$GITHUB_STEP_SUMMARY"
if [ -n "${{ steps.privacy-label.outputs.privacy_label }}" ]; then
{
echo
echo "Applied PR label: \`${{ steps.privacy-label.outputs.privacy_label }}\`"
} >> "$GITHUB_STEP_SUMMARY"
fi
else
{
echo "## PrivSignal (Informational)"
echo
echo "Unable to generate report."
echo
echo "- scan exit: ${{ steps.lockfile.outputs.scan_exit }}"
echo "- lockfile changed: ${{ steps.lockfile.outputs.lockfile_changed }}"
echo "- base ref used: ${{ steps.privsignal.outputs.base_ref }}"
echo "- diff exit: ${{ steps.privsignal.outputs.diff_exit }}"
echo "- score exit: ${{ steps.privsignal.outputs.score_exit }}"
echo "- privacy label: ${{ steps.privacy-label.outputs.privacy_label }}"
} >> "$GITHUB_STEP_SUMMARY"
fi
- name: 📦 Upload PrivSignal artifacts
if: always()
id: upload-artifact
uses: actions/upload-artifact@v4
with:
name: privsignal-artifacts-${{ github.event.pull_request.number }}
path: |
priv_signal.lockfile.json
tmp/privsignal/ci.lockfile.json
tmp/privsignal/ci.diff.json
tmp/privsignal/ci.score.json
tmp/privsignal/ci.report.md
tmp/privsignal/ci.report.json
if-no-files-found: warn
- name: 🔗 Publish artifact link
if: always() && steps.upload-artifact.outputs.artifact-url != ''
shell: bash
run: |
{
echo
echo "Artifact bundle: [privsignal-artifacts-${{ github.event.pull_request.number }}](${{ steps.upload-artifact.outputs.artifact-url }})"
} >> "$GITHUB_STEP_SUMMARY"
- name: 💬 Upsert PR comment
if: always()
uses: actions/github-script@v7
env:
PRIVSIGNAL_SCORE: ${{ steps.privsignal.outputs.score }}
PRIVSIGNAL_BASE_REF: ${{ steps.privsignal.outputs.base_ref }}
PRIVSIGNAL_DIFF_EXIT: ${{ steps.privsignal.outputs.diff_exit }}
PRIVSIGNAL_SCORE_EXIT: ${{ steps.privsignal.outputs.score_exit }}
PRIVSIGNAL_SCAN_EXIT: ${{ steps.lockfile.outputs.scan_exit }}
PRIVSIGNAL_LOCKFILE_CHANGED: ${{ steps.lockfile.outputs.lockfile_changed }}
PRIVSIGNAL_LABEL: ${{ steps.privacy-label.outputs.privacy_label }}
PRIVSIGNAL_ARTIFACT_URL: ${{ steps.upload-artifact.outputs.artifact-url }}
with:
script: |
const fs = require("node:fs");
const marker = "<!-- privsignal-report -->";
const pr = context.payload.pull_request;
if (!pr) {
core.info("No pull_request context found; skipping PrivSignal PR comment.");
return;
}
if (pr.head.repo.full_name !== `${context.repo.owner}/${context.repo.repo}`) {
core.warning("Fork PR detected; GitHub does not grant comment write access to this workflow token. Skipping PrivSignal PR comment.");
return;
}
const readJson = (path) => {
try {
return JSON.parse(fs.readFileSync(path, "utf8"));
} catch (_error) {
return null;
}
};
const report = readJson("tmp/privsignal/ci.report.json");
const esc = (value) =>
String(value ?? "n/a")
.replace(/\|/g, "\\|")
.replace(/\n/g, " ");
const renderTable = (rows) => {
if (!rows.length) {
return "_None_";
}
const header = [
"| Rule | Type | Class | Source | Sink | Location |",
"|---|---|---|---|---|---|",
];
const body = rows.map((row) =>
`| ${esc(row.rule_id)} | ${esc(row.event_type)} | ${esc(row.event_class)} | ${esc(row.source)} | ${esc(row.sink)} | ${esc(row.location)} |`
);
return [...header, ...body].join("\n");
};
const score = process.env.PRIVSIGNAL_SCORE || "ERROR";
const summary = report?.summary || {};
const reasons = Array.isArray(report?.reason_details) ? report.reason_details : [];
const samples = Array.isArray(report?.events_sample) ? report.events_sample : [];
const artifactUrl = process.env.PRIVSIGNAL_ARTIFACT_URL || "";
const privacyLabel = process.env.PRIVSIGNAL_LABEL || "";
const links = artifactUrl
? [`[artifact bundle](${artifactUrl})`]
: [];
const lines = [
marker,
"## PrivSignal Report",
"",
`Top-level result: \`${esc(score)}\`${privacyLabel ? ` with PR label \`${esc(privacyLabel)}\`` : ""}`,
"",
"| Score | High | Medium | Low | Total |",
"|---|---:|---:|---:|---:|",
`| \`${esc(score)}\` | ${esc(summary.events_high || 0)} | ${esc(summary.events_medium || 0)} | ${esc(summary.events_low || 0)} | ${esc(summary.events_total || 0)} |`,
"",
];
if (links.length) {
lines.push(`Links: ${links.join(" | ")}`);
lines.push("");
}
lines.push("| Detail | Value |");
lines.push("|---|---|");
lines.push(`| Base ref used | \`${esc(process.env.PRIVSIGNAL_BASE_REF || "n/a")}\` |`);
lines.push(`| Scan exit | \`${esc(process.env.PRIVSIGNAL_SCAN_EXIT || "n/a")}\` |`);
lines.push(`| Diff exit | \`${esc(process.env.PRIVSIGNAL_DIFF_EXIT || "n/a")}\` |`);
lines.push(`| Score exit | \`${esc(process.env.PRIVSIGNAL_SCORE_EXIT || "n/a")}\` |`);
lines.push(`| Lockfile changed | \`${esc(process.env.PRIVSIGNAL_LOCKFILE_CHANGED || "n/a")}\` |`);
lines.push(`| Reason events | \`${reasons.length}\` |`);
lines.push(`| Sample events shown | \`${samples.length}\` |`);
lines.push("");
lines.push("### Reason Events");
lines.push("");
lines.push(renderTable(reasons));
lines.push("");
lines.push("### Sample Of All Events");
lines.push("");
lines.push(renderTable(samples));
if (!report) {
lines.splice(
2,
lines.length - 2,
"",
"Unable to load `tmp/privsignal/ci.report.json` for this run.",
"",
"| Detail | Value |",
"|---|---|",
`| Score output | \`${esc(score)}\` |`,
`| Base ref used | \`${esc(process.env.PRIVSIGNAL_BASE_REF || "n/a")}\` |`,
`| Scan exit | \`${esc(process.env.PRIVSIGNAL_SCAN_EXIT || "n/a")}\` |`,
`| Diff exit | \`${esc(process.env.PRIVSIGNAL_DIFF_EXIT || "n/a")}\` |`,
`| Score exit | \`${esc(process.env.PRIVSIGNAL_SCORE_EXIT || "n/a")}\` |`,
`| Lockfile changed | \`${esc(process.env.PRIVSIGNAL_LOCKFILE_CHANGED || "n/a")}\` |`
);
if (links.length) {
lines.push("");
lines.push(`Links: ${links.join(" | ")}`);
}
}
const body = lines.join("\n");
const { owner, repo } = context.repo;
const issue_number = pr.number;
try {
const comments = await github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number,
per_page: 100,
});
const existing = comments.find((comment) => comment.body && comment.body.includes(marker));
if (existing) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body,
});
}
} catch (error) {
core.warning(`Unable to upsert PrivSignal PR comment: ${error.message}`);
}