-
Notifications
You must be signed in to change notification settings - Fork 0
183 lines (156 loc) · 6.59 KB
/
Copy pathsecurity-audit.yml
File metadata and controls
183 lines (156 loc) · 6.59 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
name: Security Audit
on:
push:
branches: [main, develop]
paths:
- "requirements.txt"
- "constraints.txt"
- "app/**/*.py"
- "worker/**/*.py"
- ".github/workflows/security-audit.yml"
pull_request:
branches: [main, develop]
paths:
- "requirements.txt"
- "constraints.txt"
- "app/**/*.py"
- "worker/**/*.py"
- ".github/workflows/security-audit.yml"
schedule:
# Run weekly on Mondays at 9:00 AM UTC
- cron: "0 9 * * 1"
workflow_dispatch:
permissions:
contents: read
security-events: write
issues: write
jobs:
dependency-audit:
name: Audit Python Dependencies
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: "pip"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pip-audit safety
- name: Run pip-audit (fail on high severity)
id: pip-audit
continue-on-error: true
run: |
echo "## pip-audit Report" >> $GITHUB_STEP_SUMMARY
pip-audit --requirement requirements.txt --desc --format markdown >> $GITHUB_STEP_SUMMARY || true
# Generate JSON report
pip-audit --requirement requirements.txt --format json --output pip-audit-report.json || true
# Run again with exit code for high/critical severity
pip-audit --requirement requirements.txt --vulnerability-service osv
- name: Run Safety check
id: safety
continue-on-error: true
run: |
echo "## Safety Check Report" >> $GITHUB_STEP_SUMMARY
# Note: Safety free version has limited JSON output
# Run in text mode and capture output
safety check --file requirements.txt > safety-report.txt 2>&1 || true
if [ -f safety-report.txt ]; then
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
cat safety-report.txt >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
# Check for vulnerabilities in output
if grep -iq "vulnerabilit" safety-report.txt; then
echo "::warning::Safety detected potential vulnerabilities. Please review the report above."
else
echo "No known vulnerabilities found by Safety." >> $GITHUB_STEP_SUMMARY
fi
fi
- name: Upload pip-audit results
if: always()
uses: actions/upload-artifact@v4
with:
name: pip-audit-report
path: pip-audit-report.json
retention-days: 30
- name: Check results and fail if needed
if: steps.pip-audit.outcome == 'failure'
run: |
echo "::error::High severity vulnerabilities detected by pip-audit"
exit 1
sast-analysis:
name: SAST - Python Security Analysis
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install bandit
run: |
python -m pip install --upgrade pip
pip install bandit[sarif]
- name: Run bandit security scan
id: bandit
continue-on-error: true
run: |
echo "## Bandit SAST Report" >> $GITHUB_STEP_SUMMARY
# Run bandit and generate SARIF for GitHub Security
bandit -r app/ worker/ -f sarif -o bandit-results.sarif || true
# Also run for human-readable output
bandit -r app/ worker/ -ll -f txt > bandit-report.txt 2>&1 || true
# Show summary
if [ -f bandit-report.txt ]; then
echo '```' >> $GITHUB_STEP_SUMMARY
head -50 bandit-report.txt >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
fi
- name: Upload bandit SARIF to GitHub Security
if: always()
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: bandit-results.sarif
category: bandit-sast
- name: Upload bandit report
if: always()
uses: actions/upload-artifact@v4
with:
name: bandit-report
path: |
bandit-results.sarif
bandit-report.txt
retention-days: 30
secret-scanning:
name: Scan for Secrets
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Full history for gitleaks
- name: Run Gitleaks
uses: gitleaks/gitleaks-action@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITLEAKS_CONFIG: .gitleaks.toml
security-summary:
name: Security Summary
runs-on: ubuntu-latest
needs: [dependency-audit, sast-analysis, secret-scanning]
if: always()
steps:
- name: Summary
run: |
echo "## Security Audit Complete" >> $GITHUB_STEP_SUMMARY
echo "- Dependency Audit: ${{ needs.dependency-audit.result }}" >> $GITHUB_STEP_SUMMARY
echo "- SAST Analysis: ${{ needs.sast-analysis.result }}" >> $GITHUB_STEP_SUMMARY
echo "- Secret Scanning: ${{ needs.secret-scanning.result }}" >> $GITHUB_STEP_SUMMARY
if [[ "${{ needs.dependency-audit.result }}" == "failure" ]] || [[ "${{ needs.sast-analysis.result }}" == "failure" ]] || [[ "${{ needs.secret-scanning.result }}" == "failure" ]]; then
echo "::error::Security checks failed. Please review the audit results."
exit 1
fi