security.yml 15 KB

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