security.yml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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@v6
  43. - name: Set up Python
  44. uses: actions/setup-python@v6
  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@v6
  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@v6
  105. - name: Set up Python
  106. uses: actions/setup-python@v6
  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-2025-45768 (PYSEC-2025-183 / GHSA-65pc-fj4g-8rjx): disputed by PyJWT maintainers.
  118. # Advisory says "key length is chosen by the application that uses the library" — no
  119. # PyJWT fix exists or will exist. Bambuddy is safe: backend/app/core/auth.py:184 uses
  120. # secrets.token_urlsafe(64) (~86 chars of entropy) for auto-generated secrets and
  121. # rejects file-loaded secrets shorter than 32 chars at :177. Keep ignored permanently.
  122. pip-audit --desc on --format json --output pip-audit-results.json \
  123. --ignore-vuln CVE-2025-45768 \
  124. || echo "vulnerabilities_found=true" >> $GITHUB_OUTPUT
  125. pip-audit --desc on \
  126. --ignore-vuln CVE-2025-45768 \
  127. || true
  128. - name: Upload audit results
  129. if: always()
  130. uses: actions/upload-artifact@v7
  131. with:
  132. name: pip-audit-results
  133. path: pip-audit-results.json
  134. retention-days: 30
  135. - name: Create or close pip security issue
  136. if: always() && (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch')
  137. uses: actions/github-script@v9
  138. with:
  139. script: |
  140. const fs = require('fs');
  141. // Check for existing open issue
  142. const existingIssues = await github.rest.issues.listForRepo({
  143. owner: context.repo.owner,
  144. repo: context.repo.repo,
  145. state: 'open',
  146. labels: 'security,automated'
  147. });
  148. const existingIssue = existingIssues.data.find(i => i.title.startsWith('Security Alert:') && i.title.includes('Python'));
  149. // If no vulnerabilities found, auto-close any stale issue
  150. if ('${{ steps.pip-audit.outputs.vulnerabilities_found }}' !== 'true') {
  151. if (existingIssue) {
  152. await github.rest.issues.createComment({
  153. owner: context.repo.owner,
  154. repo: context.repo.repo,
  155. issue_number: existingIssue.number,
  156. body: 'All Python vulnerabilities have been resolved. Closing automatically.'
  157. });
  158. await github.rest.issues.update({
  159. owner: context.repo.owner,
  160. repo: context.repo.repo,
  161. issue_number: existingIssue.number,
  162. state: 'closed'
  163. });
  164. console.log(`Auto-closed resolved issue #${existingIssue.number}`);
  165. }
  166. return;
  167. }
  168. let results;
  169. try {
  170. results = JSON.parse(fs.readFileSync('pip-audit-results.json', 'utf8'));
  171. } catch {
  172. console.log('Could not read audit results');
  173. return;
  174. }
  175. // Build vulnerability table
  176. let table = '| Package | Version | Vulnerability | Fix Version |\n';
  177. table += '|---------|---------|---------------|-------------|\n';
  178. for (const vuln of results.dependencies || []) {
  179. for (const v of vuln.vulns || []) {
  180. table += `| ${vuln.name} | ${vuln.version} | ${v.id} | ${v.fix_versions?.join(', ') || 'N/A'} |\n`;
  181. }
  182. }
  183. const vulnCount = results.dependencies?.reduce((acc, d) => acc + (d.vulns?.length || 0), 0) || 0;
  184. if (vulnCount === 0) {
  185. console.log('No vulnerabilities to report');
  186. if (existingIssue) {
  187. await github.rest.issues.createComment({
  188. owner: context.repo.owner,
  189. repo: context.repo.repo,
  190. issue_number: existingIssue.number,
  191. body: 'All Python vulnerabilities have been resolved. Closing automatically.'
  192. });
  193. await github.rest.issues.update({
  194. owner: context.repo.owner,
  195. repo: context.repo.repo,
  196. issue_number: existingIssue.number,
  197. state: 'closed'
  198. });
  199. console.log(`Auto-closed resolved issue #${existingIssue.number}`);
  200. }
  201. return;
  202. }
  203. const title = `Security Alert: ${vulnCount} Python vulnerabilities found`;
  204. const body = `## Automated Security Audit Results
  205. The weekly security audit found vulnerabilities in Python dependencies.
  206. ${table}
  207. ### Recommended Actions
  208. 1. Review each vulnerability
  209. 2. Update affected packages: \`pip install --upgrade <package>\`
  210. 3. Run \`pip-audit\` locally to verify fixes
  211. ---
  212. *This issue was automatically created by the security audit workflow.*`;
  213. if (existingIssue) {
  214. await github.rest.issues.update({
  215. owner: context.repo.owner,
  216. repo: context.repo.repo,
  217. issue_number: existingIssue.number,
  218. body: body
  219. });
  220. console.log(`Updated existing issue #${existingIssue.number}`);
  221. } else {
  222. await github.rest.issues.create({
  223. owner: context.repo.owner,
  224. repo: context.repo.repo,
  225. title: title,
  226. body: body,
  227. labels: ['security', 'automated', 'dependencies']
  228. });
  229. console.log('Created new security issue');
  230. }
  231. frontend-audit:
  232. name: Frontend Security Audit
  233. runs-on: ubuntu-latest
  234. permissions:
  235. contents: read
  236. issues: write
  237. steps:
  238. - uses: actions/checkout@v6
  239. - name: Set up Node.js
  240. uses: actions/setup-node@v6
  241. with:
  242. node-version: ${{ env.NODE_VERSION }}
  243. cache: 'npm'
  244. cache-dependency-path: frontend/package-lock.json
  245. - name: Install dependencies
  246. working-directory: frontend
  247. run: npm ci
  248. - name: Run npm audit
  249. id: npm-audit
  250. working-directory: frontend
  251. run: |
  252. npm audit --omit=dev --json > npm-audit-raw.json 2>/dev/null || true
  253. # Filter audit results to only include actual project dependencies.
  254. # npm 10.x audit/ls reports vulns in its own bundled deps (npm, tar, minimatch)
  255. # so we parse package-lock.json directly to get the real prod dep list.
  256. node -e "
  257. const fs = require('fs');
  258. const raw = fs.readFileSync('npm-audit-raw.json', 'utf8');
  259. let results;
  260. try { results = JSON.parse(raw); } catch { results = { vulnerabilities: {} }; }
  261. const lock = JSON.parse(fs.readFileSync('package-lock.json', 'utf8'));
  262. const prodDeps = new Set();
  263. for (const [path, info] of Object.entries(lock.packages || {})) {
  264. if (path && !info.dev && !info.devOptional) {
  265. prodDeps.add(path.split('node_modules/').pop());
  266. }
  267. }
  268. const vulns = results.vulnerabilities || {};
  269. const filtered = {};
  270. for (const [name, info] of Object.entries(vulns)) {
  271. if (prodDeps.has(name)) filtered[name] = info;
  272. }
  273. results.vulnerabilities = filtered;
  274. fs.writeFileSync('npm-audit-results.json', JSON.stringify(results, null, 2));
  275. const count = Object.keys(filtered).length;
  276. console.log(count > 0
  277. ? count + ' production vulnerabilities found'
  278. : 'No production vulnerabilities (filtered ' + Object.keys(vulns).length + ' npm-internal entries)');
  279. if (count > 0) process.exit(1);
  280. " || echo "vulnerabilities_found=true" >> $GITHUB_OUTPUT
  281. npm audit --omit=dev --audit-level=high || true
  282. - name: Upload audit results
  283. if: always()
  284. uses: actions/upload-artifact@v7
  285. with:
  286. name: npm-audit-results
  287. path: frontend/npm-audit-results.json
  288. retention-days: 30
  289. - name: Create or close npm security issue
  290. if: always() && (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch')
  291. uses: actions/github-script@v9
  292. with:
  293. script: |
  294. const fs = require('fs');
  295. // Check for existing open issue
  296. const existingIssues = await github.rest.issues.listForRepo({
  297. owner: context.repo.owner,
  298. repo: context.repo.repo,
  299. state: 'open',
  300. labels: 'security,automated'
  301. });
  302. const existingIssue = existingIssues.data.find(i => i.title.startsWith('Security Alert:') && i.title.includes('npm'));
  303. // If filter didn't flag vulnerabilities, auto-close any stale issue
  304. if ('${{ steps.npm-audit.outputs.vulnerabilities_found }}' !== 'true') {
  305. if (existingIssue) {
  306. await github.rest.issues.createComment({
  307. owner: context.repo.owner,
  308. repo: context.repo.repo,
  309. issue_number: existingIssue.number,
  310. body: 'All npm production vulnerabilities have been resolved. Closing automatically.'
  311. });
  312. await github.rest.issues.update({
  313. owner: context.repo.owner,
  314. repo: context.repo.repo,
  315. issue_number: existingIssue.number,
  316. state: 'closed'
  317. });
  318. console.log(`Auto-closed resolved issue #${existingIssue.number}`);
  319. }
  320. return;
  321. }
  322. let results;
  323. try {
  324. results = JSON.parse(fs.readFileSync('frontend/npm-audit-results.json', 'utf8'));
  325. } catch {
  326. console.log('Could not read filtered audit results');
  327. return;
  328. }
  329. const vulns = results.vulnerabilities || {};
  330. const vulnCount = Object.keys(vulns).length;
  331. if (vulnCount === 0) {
  332. console.log('No vulnerabilities to report');
  333. if (existingIssue) {
  334. await github.rest.issues.createComment({
  335. owner: context.repo.owner,
  336. repo: context.repo.repo,
  337. issue_number: existingIssue.number,
  338. body: 'All npm production vulnerabilities have been resolved. Closing automatically.'
  339. });
  340. await github.rest.issues.update({
  341. owner: context.repo.owner,
  342. repo: context.repo.repo,
  343. issue_number: existingIssue.number,
  344. state: 'closed'
  345. });
  346. console.log(`Auto-closed resolved issue #${existingIssue.number}`);
  347. }
  348. return;
  349. }
  350. // Build vulnerability table
  351. let table = '| Package | Severity | Via | Fix |\n';
  352. table += '|---------|----------|-----|-----|\n';
  353. for (const [name, info] of Object.entries(vulns)) {
  354. const via = Array.isArray(info.via) ? info.via.map(v => typeof v === 'string' ? v : v.name).join(', ') : info.via;
  355. table += `| ${name} | ${info.severity} | ${via} | ${info.fixAvailable ? 'Yes' : 'No'} |\n`;
  356. }
  357. const title = `Security Alert: ${vulnCount} npm vulnerabilities found`;
  358. const body = `## Automated Security Audit Results
  359. The weekly security audit found vulnerabilities in npm dependencies.
  360. ${table}
  361. ### Recommended Actions
  362. 1. Review each vulnerability: \`npm audit\`
  363. 2. Auto-fix if possible: \`npm audit fix\`
  364. 3. Manual fix for breaking changes: \`npm audit fix --force\` (review changes!)
  365. ---
  366. *This issue was automatically created by the security audit workflow.*`;
  367. if (existingIssue) {
  368. await github.rest.issues.update({
  369. owner: context.repo.owner,
  370. repo: context.repo.repo,
  371. issue_number: existingIssue.number,
  372. body: body
  373. });
  374. console.log(`Updated existing issue #${existingIssue.number}`);
  375. } else {
  376. await github.rest.issues.create({
  377. owner: context.repo.owner,
  378. repo: context.repo.repo,
  379. title: title,
  380. body: body,
  381. labels: ['security', 'automated', 'dependencies']
  382. });
  383. console.log('Created new security issue');
  384. }