security.yml 15 KB

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