security.yml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. name: Security Audit
  2. on:
  3. schedule:
  4. # Run weekly on Monday at 6:00 UTC
  5. - cron: '0 6 * * 1'
  6. push:
  7. paths:
  8. - 'backend/**'
  9. - 'frontend/**'
  10. - 'Dockerfile'
  11. - 'docker-compose*.yml'
  12. - 'requirements.txt'
  13. - 'frontend/package*.json'
  14. - '.github/workflows/security.yml'
  15. pull_request:
  16. paths:
  17. - 'backend/**'
  18. - 'frontend/**'
  19. - 'Dockerfile'
  20. - 'docker-compose*.yml'
  21. - 'requirements.txt'
  22. - 'frontend/package*.json'
  23. - '.github/workflows/security.yml'
  24. workflow_dispatch:
  25. # Allow manual trigger
  26. env:
  27. PYTHON_VERSION: '3.11'
  28. NODE_VERSION: '22'
  29. # Default permissions for all jobs
  30. permissions:
  31. contents: read
  32. jobs:
  33. bandit:
  34. name: Python Security Analysis (Bandit)
  35. runs-on: ubuntu-latest
  36. permissions:
  37. contents: read
  38. security-events: write
  39. steps:
  40. - uses: actions/checkout@v4
  41. - name: Set up Python
  42. uses: actions/setup-python@v5
  43. with:
  44. python-version: ${{ env.PYTHON_VERSION }}
  45. - name: Install Bandit
  46. run: pip install bandit[sarif]
  47. - name: Run Bandit
  48. run: |
  49. bandit -r backend/ -f sarif -o bandit-results.sarif --severity-level medium || true
  50. - name: Upload Bandit results to GitHub Security
  51. uses: github/codeql-action/upload-sarif@v4
  52. if: always()
  53. with:
  54. sarif_file: bandit-results.sarif
  55. category: bandit
  56. trivy:
  57. name: Container Security Scan (Trivy)
  58. runs-on: ubuntu-latest
  59. permissions:
  60. contents: read
  61. security-events: write
  62. steps:
  63. - uses: actions/checkout@v4
  64. - name: Build Docker image
  65. run: docker build -t bambuddy:security-scan .
  66. - name: Run Trivy vulnerability scanner
  67. uses: aquasecurity/trivy-action@v0.35.0
  68. with:
  69. image-ref: 'bambuddy:security-scan'
  70. format: 'sarif'
  71. output: 'trivy-results.sarif'
  72. severity: 'CRITICAL,HIGH,MEDIUM'
  73. version: 'v0.69.1'
  74. - name: Upload Trivy results to GitHub Security
  75. uses: github/codeql-action/upload-sarif@v4
  76. if: always() && hashFiles('trivy-results.sarif') != ''
  77. with:
  78. sarif_file: trivy-results.sarif
  79. category: trivy
  80. - name: Run Trivy for Dockerfile/IaC
  81. uses: aquasecurity/trivy-action@v0.35.0
  82. with:
  83. scan-type: 'config'
  84. scan-ref: '.'
  85. format: 'sarif'
  86. output: 'trivy-config-results.sarif'
  87. severity: 'CRITICAL,HIGH,MEDIUM'
  88. version: 'v0.69.1'
  89. - name: Upload Trivy config results
  90. uses: github/codeql-action/upload-sarif@v4
  91. if: always() && hashFiles('trivy-config-results.sarif') != ''
  92. with:
  93. sarif_file: trivy-config-results.sarif
  94. category: trivy-config
  95. backend-audit:
  96. name: Backend Security Audit
  97. runs-on: ubuntu-latest
  98. permissions:
  99. contents: read
  100. issues: write
  101. steps:
  102. - uses: actions/checkout@v4
  103. - name: Set up Python
  104. uses: actions/setup-python@v5
  105. with:
  106. python-version: ${{ env.PYTHON_VERSION }}
  107. - name: Install dependencies
  108. run: |
  109. python -m pip install --upgrade pip
  110. pip install -r requirements.txt
  111. pip install pip-audit
  112. - name: Run pip-audit
  113. id: pip-audit
  114. run: |
  115. # CVE-2026-4539: low-severity ReDoS in Pygments AdlLexer (indirect dep via mkdocs-material/pytest/rich).
  116. # No fix available yet. Remove --ignore-vuln once Pygments releases a patched version.
  117. pip-audit --desc on --format json --output pip-audit-results.json --ignore-vuln CVE-2026-4539 || echo "vulnerabilities_found=true" >> $GITHUB_OUTPUT
  118. pip-audit --desc on --ignore-vuln CVE-2026-4539 || true
  119. - name: Upload audit results
  120. if: always()
  121. uses: actions/upload-artifact@v4
  122. with:
  123. name: pip-audit-results
  124. path: pip-audit-results.json
  125. retention-days: 30
  126. - name: Create or close pip security issue
  127. if: always()
  128. uses: actions/github-script@v7
  129. with:
  130. script: |
  131. const fs = require('fs');
  132. // Check for existing open issue
  133. const existingIssues = await github.rest.issues.listForRepo({
  134. owner: context.repo.owner,
  135. repo: context.repo.repo,
  136. state: 'open',
  137. labels: 'security,automated'
  138. });
  139. const existingIssue = existingIssues.data.find(i => i.title.startsWith('Security Alert:') && i.title.includes('Python'));
  140. // If no vulnerabilities found, auto-close any stale issue
  141. if ('${{ steps.pip-audit.outputs.vulnerabilities_found }}' !== 'true') {
  142. if (existingIssue) {
  143. await github.rest.issues.createComment({
  144. owner: context.repo.owner,
  145. repo: context.repo.repo,
  146. issue_number: existingIssue.number,
  147. body: 'All Python vulnerabilities have been resolved. Closing automatically.'
  148. });
  149. await github.rest.issues.update({
  150. owner: context.repo.owner,
  151. repo: context.repo.repo,
  152. issue_number: existingIssue.number,
  153. state: 'closed'
  154. });
  155. console.log(`Auto-closed resolved issue #${existingIssue.number}`);
  156. }
  157. return;
  158. }
  159. let results;
  160. try {
  161. results = JSON.parse(fs.readFileSync('pip-audit-results.json', 'utf8'));
  162. } catch {
  163. console.log('Could not read audit results');
  164. return;
  165. }
  166. // Build vulnerability table
  167. let table = '| Package | Version | Vulnerability | Fix Version |\n';
  168. table += '|---------|---------|---------------|-------------|\n';
  169. for (const vuln of results.dependencies || []) {
  170. for (const v of vuln.vulns || []) {
  171. table += `| ${vuln.name} | ${vuln.version} | ${v.id} | ${v.fix_versions?.join(', ') || 'N/A'} |\n`;
  172. }
  173. }
  174. const vulnCount = results.dependencies?.reduce((acc, d) => acc + (d.vulns?.length || 0), 0) || 0;
  175. if (vulnCount === 0) {
  176. console.log('No vulnerabilities to report');
  177. if (existingIssue) {
  178. await github.rest.issues.createComment({
  179. owner: context.repo.owner,
  180. repo: context.repo.repo,
  181. issue_number: existingIssue.number,
  182. body: 'All Python vulnerabilities have been resolved. Closing automatically.'
  183. });
  184. await github.rest.issues.update({
  185. owner: context.repo.owner,
  186. repo: context.repo.repo,
  187. issue_number: existingIssue.number,
  188. state: 'closed'
  189. });
  190. console.log(`Auto-closed resolved issue #${existingIssue.number}`);
  191. }
  192. return;
  193. }
  194. const title = `Security Alert: ${vulnCount} Python vulnerabilities found`;
  195. const body = `## Automated Security Audit Results
  196. The weekly security audit found vulnerabilities in Python dependencies.
  197. ${table}
  198. ### Recommended Actions
  199. 1. Review each vulnerability
  200. 2. Update affected packages: \`pip install --upgrade <package>\`
  201. 3. Run \`pip-audit\` locally to verify fixes
  202. ---
  203. *This issue was automatically created by the security audit workflow.*`;
  204. if (existingIssue) {
  205. await github.rest.issues.update({
  206. owner: context.repo.owner,
  207. repo: context.repo.repo,
  208. issue_number: existingIssue.number,
  209. body: body
  210. });
  211. console.log(`Updated existing issue #${existingIssue.number}`);
  212. } else {
  213. await github.rest.issues.create({
  214. owner: context.repo.owner,
  215. repo: context.repo.repo,
  216. title: title,
  217. body: body,
  218. labels: ['security', 'automated', 'dependencies']
  219. });
  220. console.log('Created new security issue');
  221. }
  222. frontend-audit:
  223. name: Frontend Security Audit
  224. runs-on: ubuntu-latest
  225. permissions:
  226. contents: read
  227. issues: write
  228. steps:
  229. - uses: actions/checkout@v4
  230. - name: Set up Node.js
  231. uses: actions/setup-node@v4
  232. with:
  233. node-version: ${{ env.NODE_VERSION }}
  234. cache: 'npm'
  235. cache-dependency-path: frontend/package-lock.json
  236. - name: Install dependencies
  237. working-directory: frontend
  238. run: npm ci
  239. - name: Run npm audit
  240. id: npm-audit
  241. working-directory: frontend
  242. run: |
  243. npm audit --omit=dev --json > npm-audit-raw.json 2>/dev/null || true
  244. # Filter audit results to only include actual project dependencies.
  245. # npm 10.x audit/ls reports vulns in its own bundled deps (npm, tar, minimatch)
  246. # so we parse package-lock.json directly to get the real prod dep list.
  247. node -e "
  248. const fs = require('fs');
  249. const raw = fs.readFileSync('npm-audit-raw.json', 'utf8');
  250. let results;
  251. try { results = JSON.parse(raw); } catch { results = { vulnerabilities: {} }; }
  252. const lock = JSON.parse(fs.readFileSync('package-lock.json', 'utf8'));
  253. const prodDeps = new Set();
  254. for (const [path, info] of Object.entries(lock.packages || {})) {
  255. if (path && !info.dev && !info.devOptional) {
  256. prodDeps.add(path.split('node_modules/').pop());
  257. }
  258. }
  259. const vulns = results.vulnerabilities || {};
  260. const filtered = {};
  261. for (const [name, info] of Object.entries(vulns)) {
  262. if (prodDeps.has(name)) filtered[name] = info;
  263. }
  264. results.vulnerabilities = filtered;
  265. fs.writeFileSync('npm-audit-results.json', JSON.stringify(results, null, 2));
  266. const count = Object.keys(filtered).length;
  267. console.log(count > 0
  268. ? count + ' production vulnerabilities found'
  269. : 'No production vulnerabilities (filtered ' + Object.keys(vulns).length + ' npm-internal entries)');
  270. if (count > 0) process.exit(1);
  271. " || echo "vulnerabilities_found=true" >> $GITHUB_OUTPUT
  272. npm audit --omit=dev --audit-level=high || true
  273. - name: Upload audit results
  274. if: always()
  275. uses: actions/upload-artifact@v4
  276. with:
  277. name: npm-audit-results
  278. path: frontend/npm-audit-results.json
  279. retention-days: 30
  280. - name: Create or close npm security issue
  281. if: always()
  282. uses: actions/github-script@v7
  283. with:
  284. script: |
  285. const fs = require('fs');
  286. // Check for existing open issue
  287. const existingIssues = await github.rest.issues.listForRepo({
  288. owner: context.repo.owner,
  289. repo: context.repo.repo,
  290. state: 'open',
  291. labels: 'security,automated'
  292. });
  293. const existingIssue = existingIssues.data.find(i => i.title.startsWith('Security Alert:') && i.title.includes('npm'));
  294. // If filter didn't flag vulnerabilities, auto-close any stale issue
  295. if ('${{ steps.npm-audit.outputs.vulnerabilities_found }}' !== 'true') {
  296. if (existingIssue) {
  297. await github.rest.issues.createComment({
  298. owner: context.repo.owner,
  299. repo: context.repo.repo,
  300. issue_number: existingIssue.number,
  301. body: 'All npm production vulnerabilities have been resolved. Closing automatically.'
  302. });
  303. await github.rest.issues.update({
  304. owner: context.repo.owner,
  305. repo: context.repo.repo,
  306. issue_number: existingIssue.number,
  307. state: 'closed'
  308. });
  309. console.log(`Auto-closed resolved issue #${existingIssue.number}`);
  310. }
  311. return;
  312. }
  313. let results;
  314. try {
  315. results = JSON.parse(fs.readFileSync('frontend/npm-audit-results.json', 'utf8'));
  316. } catch {
  317. console.log('Could not read filtered audit results');
  318. return;
  319. }
  320. const vulns = results.vulnerabilities || {};
  321. const vulnCount = Object.keys(vulns).length;
  322. if (vulnCount === 0) {
  323. console.log('No vulnerabilities to report');
  324. if (existingIssue) {
  325. await github.rest.issues.createComment({
  326. owner: context.repo.owner,
  327. repo: context.repo.repo,
  328. issue_number: existingIssue.number,
  329. body: 'All npm production vulnerabilities have been resolved. Closing automatically.'
  330. });
  331. await github.rest.issues.update({
  332. owner: context.repo.owner,
  333. repo: context.repo.repo,
  334. issue_number: existingIssue.number,
  335. state: 'closed'
  336. });
  337. console.log(`Auto-closed resolved issue #${existingIssue.number}`);
  338. }
  339. return;
  340. }
  341. // Build vulnerability table
  342. let table = '| Package | Severity | Via | Fix |\n';
  343. table += '|---------|----------|-----|-----|\n';
  344. for (const [name, info] of Object.entries(vulns)) {
  345. const via = Array.isArray(info.via) ? info.via.map(v => typeof v === 'string' ? v : v.name).join(', ') : info.via;
  346. table += `| ${name} | ${info.severity} | ${via} | ${info.fixAvailable ? 'Yes' : 'No'} |\n`;
  347. }
  348. const title = `Security Alert: ${vulnCount} npm vulnerabilities found`;
  349. const body = `## Automated Security Audit Results
  350. The weekly security audit found vulnerabilities in npm dependencies.
  351. ${table}
  352. ### Recommended Actions
  353. 1. Review each vulnerability: \`npm audit\`
  354. 2. Auto-fix if possible: \`npm audit fix\`
  355. 3. Manual fix for breaking changes: \`npm audit fix --force\` (review changes!)
  356. ---
  357. *This issue was automatically created by the security audit workflow.*`;
  358. if (existingIssue) {
  359. await github.rest.issues.update({
  360. owner: context.repo.owner,
  361. repo: context.repo.repo,
  362. issue_number: existingIssue.number,
  363. body: body
  364. });
  365. console.log(`Updated existing issue #${existingIssue.number}`);
  366. } else {
  367. await github.rest.issues.create({
  368. owner: context.repo.owner,
  369. repo: context.repo.repo,
  370. title: title,
  371. body: body,
  372. labels: ['security', 'automated', 'dependencies']
  373. });
  374. console.log('Created new security issue');
  375. }