-
Notifications
You must be signed in to change notification settings - Fork 2
402 lines (363 loc) · 16.9 KB
/
Copy pathsecurity.yml
File metadata and controls
402 lines (363 loc) · 16.9 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
---
# Pullbox Security Pipeline
# Dependency audits, secret scanning, and static analysis hardening.
# GitHub Actions is the canonical security runner. Same-repository maintainer
# PRs run the full security gate after `ci:full`.
#
# Security: All actions pinned to full SHA. No pull_request_target.
# See: docs/development/INFRASTRUCTURE.md
#
# Tools:
# pip-audit → PyPI advisory service (shared blocking exception policy)
# safety → Safety DB (secondary, different vulnerability coverage)
# gitleaks → blocking secret scan
# bandit → advisory static analysis during initial rollout
# CodeQL → public-repo static analysis, informational until promoted
name: Security
on:
pull_request:
branches:
- develop
- main
types:
- opened
- synchronize
- reopened
- ready_for_review
- labeled
- unlabeled
workflow_dispatch:
permissions:
contents: read
concurrency:
group: security-${{ github.ref || github.run_id }}
cancel-in-progress: true
jobs:
release_sync_check:
name: Release Sync Check
runs-on: ubuntu-latest
timeout-minutes: 5
outputs:
is_sync: ${{ steps.release-sync.outputs.is_sync }}
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
- name: Detect safe post-release sync PR
id: release-sync
env:
RELEASE_SYNC_EVENT_NAME: ${{ github.event_name }}
RELEASE_SYNC_BASE_REF: ${{ github.base_ref || '' }}
RELEASE_SYNC_HEAD_REF: ${{ github.head_ref || '' }}
RELEASE_SYNC_REPOSITORY: ${{ github.repository }}
RELEASE_SYNC_HEAD_REPOSITORY: ${{ github.event.pull_request.head.repo.full_name || '' }}
RELEASE_SYNC_ACTOR: ${{ github.actor }}
run: |
git fetch --no-tags origin main:refs/remotes/origin/main
if ! git cat-file -e origin/main:.github/scripts/validate-release-sync-pr.py 2>/dev/null; then
echo "is_sync=false" >> "$GITHUB_OUTPUT"
echo "reason=trusted release sync validator is not available on origin/main yet" >> "$GITHUB_OUTPUT"
echo "release_sync=false"
echo "reason=trusted release sync validator is not available on origin/main yet"
exit 0
fi
git show origin/main:.github/scripts/validate-release-sync-pr.py > /tmp/pullbox-release-sync-validator.py
python /tmp/pullbox-release-sync-validator.py
full_ci_check:
name: Full Security Gate
runs-on: ubuntu-latest
timeout-minutes: 5
outputs:
run_full: ${{ steps.gate.outputs.run_full }}
label_required: ${{ steps.gate.outputs.label_required }}
permissions:
contents: read
steps:
- name: Resolve full security gate
id: gate
run: |
echo "run_full=${{ github.event_name == 'workflow_dispatch' || (github.event_name == 'pull_request' && !github.event.pull_request.draft && (github.event.pull_request.head.repo.full_name != github.repository || github.actor == 'dependabot[bot]' || startsWith(github.event.pull_request.head.ref, 'dependabot/') || contains(github.event.pull_request.labels.*.name, 'ci:full'))) }}" >> "$GITHUB_OUTPUT"
echo "label_required=${{ github.event_name == 'pull_request' && !github.event.pull_request.draft && github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]' && !startsWith(github.event.pull_request.head.ref, 'dependabot/') && !contains(github.event.pull_request.labels.*.name, 'ci:full') }}" >> "$GITHUB_OUTPUT"
# ──────────────────────────────────────────────
# Job 1: gitleaks (blocking secret scan)
# ──────────────────────────────────────────────
gitleaks:
name: Gitleaks
needs: [release_sync_check, full_ci_check]
if: needs.release_sync_check.outputs.is_sync != 'true' && needs.full_ci_check.outputs.run_full == 'true'
runs-on: ${{ fromJSON(((github.event_name == 'pull_request' && (github.event.pull_request.head.repo.full_name != github.repository || github.actor == 'dependabot[bot]')) || vars.PULLBOX_CHECKS_RUNNER == 'github-hosted') && '["ubuntu-latest"]' || '["self-hosted","Linux","X64","checks"]') }}
timeout-minutes: 20
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
- name: Runner preflight
run: .github/scripts/preflight-runner.sh docker
- name: Resolve scan range
id: range
env:
EVENT_NAME: ${{ github.event_name }}
BEFORE_SHA: ${{ github.event.before }}
AFTER_SHA: ${{ github.sha }}
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
if [ "$EVENT_NAME" = "pull_request" ]; then
echo "mode=git" >> "$GITHUB_OUTPUT"
{
echo "log_opts<<EOF"
echo "--no-merges ${BASE_SHA}..${HEAD_SHA}"
echo "EOF"
} >> "$GITHUB_OUTPUT"
elif [ "$EVENT_NAME" = "push" ] && [ "$BEFORE_SHA" != "0000000000000000000000000000000000000000" ]; then
echo "mode=git" >> "$GITHUB_OUTPUT"
{
echo "log_opts<<EOF"
echo "--no-merges ${BEFORE_SHA}..${AFTER_SHA}"
echo "EOF"
} >> "$GITHUB_OUTPUT"
else
echo "mode=dir" >> "$GITHUB_OUTPUT"
fi
- name: Run gitleaks on new commits
if: steps.range.outputs.mode == 'git'
env:
GITLEAKS_IMAGE: ghcr.io/gitleaks/gitleaks@sha256:c00b6bd0aeb3071cbcb79009cb16a60dd9e0a7c60e2be9ab65d25e6bc8abbb7f
run: |
docker run --rm \
--user "$(id -u):$(id -g)" \
-v "$PWD:/repo" \
-w /repo \
"$GITLEAKS_IMAGE" \
git . \
--log-opts="${{ steps.range.outputs.log_opts }}" \
--no-banner \
--redact \
--timeout=300 \
--report-format sarif \
--report-path gitleaks.sarif
- name: Run gitleaks on current tree
if: steps.range.outputs.mode == 'dir'
env:
GITLEAKS_IMAGE: ghcr.io/gitleaks/gitleaks@sha256:c00b6bd0aeb3071cbcb79009cb16a60dd9e0a7c60e2be9ab65d25e6bc8abbb7f
run: |
docker run --rm \
--user "$(id -u):$(id -g)" \
-v "$PWD:/repo" \
-w /repo \
"$GITLEAKS_IMAGE" \
dir . \
--no-banner \
--redact \
--timeout=300 \
--report-format sarif \
--report-path gitleaks.sarif
- name: Upload gitleaks report
if: always()
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: gitleaks-report
path: gitleaks.sarif
retention-days: 14
# ──────────────────────────────────────────────
# Job 2: pip-audit (PyPI advisory service)
# ──────────────────────────────────────────────
dependency-audit:
name: pip-audit
needs: [release_sync_check, full_ci_check]
if: needs.release_sync_check.outputs.is_sync != 'true' && needs.full_ci_check.outputs.run_full == 'true'
runs-on: ${{ fromJSON(((github.event_name == 'pull_request' && (github.event.pull_request.head.repo.full_name != github.repository || github.actor == 'dependabot[bot]')) || vars.PULLBOX_CHECKS_RUNNER == 'github-hosted') && '["ubuntu-latest"]' || '["self-hosted","Linux","X64","checks"]') }}
timeout-minutes: 15
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
with:
python-version: "3.14"
- name: Setup runner-local venv
run: .github/scripts/setup-runner-venv.sh "dev"
- name: Runner preflight
run: .github/scripts/preflight-runner.sh python
- name: Run pip-audit
run: |
# Generate requirements from installed packages, excluding editable pullbox
requirements_file="$(mktemp /tmp/pullbox-requirements-audit.XXXXXX.txt)"
trap 'rm -f "$requirements_file"' EXIT
python -m pip freeze --exclude-editable | grep -v "^pullbox==" > "$requirements_file"
python scripts/run_dependency_audit.py -r "$requirements_file"
- name: Upload dependency audit report
if: always()
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: dependency-audit-report
path: dependency-audit-report.json
retention-days: 14
# ──────────────────────────────────────────────
# Job 3: safety (Safety DB — secondary scanner)
# ──────────────────────────────────────────────
safety-check:
name: Safety Check
needs: [release_sync_check, full_ci_check]
if: needs.release_sync_check.outputs.is_sync != 'true' && needs.full_ci_check.outputs.run_full == 'true'
runs-on: ${{ fromJSON(((github.event_name == 'pull_request' && (github.event.pull_request.head.repo.full_name != github.repository || github.actor == 'dependabot[bot]')) || vars.PULLBOX_CHECKS_RUNNER == 'github-hosted') && '["ubuntu-latest"]' || '["self-hosted","Linux","X64","checks"]') }}
timeout-minutes: 15
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
with:
python-version: "3.14"
- name: Setup runner-local venv
run: .github/scripts/setup-runner-venv.sh "dev"
- name: Runner preflight
run: .github/scripts/preflight-runner.sh python
# Non-blocking initially (|| true) — safety free tier has limitations.
# Remove || true once confirmed stable.
- name: Run safety check
run: safety check --save-json safety-report.json || true
- name: Upload safety report
if: always()
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: safety-report
path: safety-report.json
retention-days: 14
# ──────────────────────────────────────────────
# Job 4: bandit (advisory rollout)
# ──────────────────────────────────────────────
bandit:
name: Bandit
needs: [release_sync_check, full_ci_check]
if: needs.release_sync_check.outputs.is_sync != 'true' && needs.full_ci_check.outputs.run_full == 'true'
runs-on: ${{ fromJSON(((github.event_name == 'pull_request' && (github.event.pull_request.head.repo.full_name != github.repository || github.actor == 'dependabot[bot]')) || vars.PULLBOX_CHECKS_RUNNER == 'github-hosted') && '["ubuntu-latest"]' || '["self-hosted","Linux","X64","checks"]') }}
timeout-minutes: 15
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
with:
python-version: "3.14"
- name: Setup runner-local venv
run: .github/scripts/setup-runner-venv.sh "dev"
- name: Runner preflight
run: .github/scripts/preflight-runner.sh python
- name: Run Bandit
id: bandit
continue-on-error: true
run: bandit -r src/pullbox/ -ll -f json -o bandit-report.json
- name: Summarize Bandit result
if: always()
run: |
if [ "${{ steps.bandit.outcome }}" = "failure" ]; then
echo "::warning::Bandit reported medium-or-higher findings. Review the uploaded artifact."
else
echo "Bandit found no medium-or-higher issues."
fi
- name: Upload Bandit report
if: always()
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: bandit-report
path: bandit-report.json
retention-days: 14
# ──────────────────────────────────────────────
# Job 5: CodeQL Static Analysis
# ──────────────────────────────────────────────
codeql:
name: CodeQL Analysis
needs: [release_sync_check, full_ci_check]
if: needs.release_sync_check.outputs.is_sync != 'true' && needs.full_ci_check.outputs.run_full == 'true' && (github.repository_visibility == 'public' || vars.PULLBOX_ENABLE_CODEQL == 'true')
runs-on: ubuntu-latest
timeout-minutes: 20
permissions:
contents: read
actions: read
security-events: write
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Initialize CodeQL
uses: github/codeql-action/init@cdf488f595d80d6e07e03d4674febd5ab45fa938 # v4.37.9
with:
languages: python
queries: +security-extended
config-file: ./.github/codeql/codeql-config.yml
- name: Perform CodeQL analysis
uses: github/codeql-action/analyze@cdf488f595d80d6e07e03d4674febd5ab45fa938 # v4.37.9
with:
category: "/language:python"
# Stable aggregate required check for branch rulesets.
security-required:
name: Security Required
runs-on: ubuntu-latest
needs:
- release_sync_check
- full_ci_check
- gitleaks
- dependency-audit
- safety-check
- bandit
- codeql
if: always()
timeout-minutes: 5
permissions:
contents: read
steps:
- name: Verify required security jobs
env:
NEEDS_CONTEXT: ${{ toJson(needs) }}
CODEQL_BLOCKING: ${{ vars.PULLBOX_REQUIRE_CODEQL == 'true' }}
RELEASE_SYNC_PR: ${{ needs.release_sync_check.outputs.is_sync }}
RUN_FULL: ${{ needs.full_ci_check.outputs.run_full }}
LABEL_REQUIRED: ${{ needs.full_ci_check.outputs.label_required }}
run: |
python - <<'PY'
import json
import os
import sys
needs = json.loads(os.environ["NEEDS_CONTEXT"])
codeql_blocking = os.environ["CODEQL_BLOCKING"].lower() == "true"
release_sync_pr = os.environ["RELEASE_SYNC_PR"].lower() == "true"
run_full = os.environ["RUN_FULL"].lower() == "true"
label_required = os.environ["LABEL_REQUIRED"].lower() == "true"
if release_sync_pr:
release_sync = needs.get("release_sync_check", {}).get("result")
if release_sync != "success":
print(f"Release sync validation failed: {release_sync}", file=sys.stderr)
sys.exit(1)
unexpected = {
name: info.get("result")
for name, info in needs.items()
if name != "release_sync_check" and info.get("result") not in {"skipped", "success"}
}
if unexpected:
print(f"Release sync fast path saw unexpected job results: {unexpected}", file=sys.stderr)
sys.exit(1)
print("Release sync fast path validated; heavyweight security jobs intentionally skipped.")
sys.exit(0)
if not run_full:
if label_required:
print("Security gate is waiting for ci:full before running the full required checks.", file=sys.stderr)
else:
print("Security gate was intentionally not requested for this PR state.", file=sys.stderr)
sys.exit(1)
failed = {}
for name, info in needs.items():
result = info.get("result")
if name == "codeql" and not codeql_blocking:
if result != "success":
print(f"CodeQL is informational for now; observed result: {result}")
continue
if result != "success":
failed[name] = result
if failed:
print(f"Required security jobs did not all pass: {failed}", file=sys.stderr)
sys.exit(1)
print("All required security jobs passed.")
PY