security.yml 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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: '20'
  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.33.1
  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()
  77. with:
  78. sarif_file: trivy-results.sarif
  79. category: trivy
  80. - name: Run Trivy for Dockerfile/IaC
  81. uses: aquasecurity/trivy-action@0.33.1
  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()
  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 issue on vulnerability
  125. if: steps.pip-audit.outputs.vulnerabilities_found == 'true'
  126. uses: actions/github-script@v7
  127. with:
  128. script: |
  129. const fs = require('fs');
  130. const results = JSON.parse(fs.readFileSync('pip-audit-results.json', 'utf8'));
  131. // Build vulnerability table
  132. let table = '| Package | Version | Vulnerability | Fix Version |\n';
  133. table += '|---------|---------|---------------|-------------|\n';
  134. for (const vuln of results.dependencies || []) {
  135. for (const v of vuln.vulns || []) {
  136. table += `| ${vuln.name} | ${vuln.version} | ${v.id} | ${v.fix_versions?.join(', ') || 'N/A'} |\n`;
  137. }
  138. }
  139. const title = `Security Alert: ${results.dependencies?.reduce((acc, d) => acc + (d.vulns?.length || 0), 0) || 0} Python vulnerabilities found`;
  140. // Check for existing open issue
  141. const existingIssues = await github.rest.issues.listForRepo({
  142. owner: context.repo.owner,
  143. repo: context.repo.repo,
  144. state: 'open',
  145. labels: 'security,automated'
  146. });
  147. const existingIssue = existingIssues.data.find(i => i.title.startsWith('Security Alert:') && i.title.includes('Python'));
  148. const body = `## Automated Security Audit Results
  149. The weekly security audit found vulnerabilities in Python dependencies.
  150. ${table}
  151. ### Recommended Actions
  152. 1. Review each vulnerability
  153. 2. Update affected packages: \`pip install --upgrade <package>\`
  154. 3. Run \`pip-audit\` locally to verify fixes
  155. 4. Close this issue when resolved
  156. ---
  157. *This issue was automatically created by the security audit workflow.*`;
  158. if (existingIssue) {
  159. await github.rest.issues.update({
  160. owner: context.repo.owner,
  161. repo: context.repo.repo,
  162. issue_number: existingIssue.number,
  163. body: body
  164. });
  165. console.log(`Updated existing issue #${existingIssue.number}`);
  166. } else {
  167. await github.rest.issues.create({
  168. owner: context.repo.owner,
  169. repo: context.repo.repo,
  170. title: title,
  171. body: body,
  172. labels: ['security', 'automated', 'dependencies']
  173. });
  174. console.log('Created new security issue');
  175. }
  176. frontend-audit:
  177. name: Frontend Security Audit
  178. runs-on: ubuntu-latest
  179. permissions:
  180. contents: read
  181. issues: write
  182. steps:
  183. - uses: actions/checkout@v4
  184. - name: Set up Node.js
  185. uses: actions/setup-node@v4
  186. with:
  187. node-version: ${{ env.NODE_VERSION }}
  188. cache: 'npm'
  189. cache-dependency-path: frontend/package-lock.json
  190. - name: Install dependencies
  191. working-directory: frontend
  192. run: npm ci
  193. - name: Run npm audit
  194. id: npm-audit
  195. working-directory: frontend
  196. run: |
  197. npm audit --json > npm-audit-results.json || echo "vulnerabilities_found=true" >> $GITHUB_OUTPUT
  198. npm audit --audit-level=high || true
  199. - name: Upload audit results
  200. if: always()
  201. uses: actions/upload-artifact@v4
  202. with:
  203. name: npm-audit-results
  204. path: frontend/npm-audit-results.json
  205. retention-days: 30
  206. - name: Create issue on vulnerability
  207. if: steps.npm-audit.outputs.vulnerabilities_found == 'true'
  208. uses: actions/github-script@v7
  209. with:
  210. script: |
  211. const fs = require('fs');
  212. const results = JSON.parse(fs.readFileSync('frontend/npm-audit-results.json', 'utf8'));
  213. const vulns = results.vulnerabilities || {};
  214. const vulnCount = Object.keys(vulns).length;
  215. if (vulnCount === 0) {
  216. console.log('No vulnerabilities to report');
  217. return;
  218. }
  219. // Build vulnerability table
  220. let table = '| Package | Severity | Via | Fix |\n';
  221. table += '|---------|----------|-----|-----|\n';
  222. for (const [name, info] of Object.entries(vulns)) {
  223. const via = Array.isArray(info.via) ? info.via.map(v => typeof v === 'string' ? v : v.name).join(', ') : info.via;
  224. table += `| ${name} | ${info.severity} | ${via} | ${info.fixAvailable ? 'Yes' : 'No'} |\n`;
  225. }
  226. const title = `Security Alert: ${vulnCount} npm vulnerabilities found`;
  227. // Check for existing open issue
  228. const existingIssues = await github.rest.issues.listForRepo({
  229. owner: context.repo.owner,
  230. repo: context.repo.repo,
  231. state: 'open',
  232. labels: 'security,automated'
  233. });
  234. const existingIssue = existingIssues.data.find(i => i.title.startsWith('Security Alert:') && i.title.includes('npm'));
  235. const body = `## Automated Security Audit Results
  236. The weekly security audit found vulnerabilities in npm dependencies.
  237. ${table}
  238. ### Recommended Actions
  239. 1. Review each vulnerability: \`npm audit\`
  240. 2. Auto-fix if possible: \`npm audit fix\`
  241. 3. Manual fix for breaking changes: \`npm audit fix --force\` (review changes!)
  242. 4. Close this issue when resolved
  243. ---
  244. *This issue was automatically created by the security audit workflow.*`;
  245. if (existingIssue) {
  246. await github.rest.issues.update({
  247. owner: context.repo.owner,
  248. repo: context.repo.repo,
  249. issue_number: existingIssue.number,
  250. body: body
  251. });
  252. console.log(`Updated existing issue #${existingIssue.number}`);
  253. } else {
  254. await github.rest.issues.create({
  255. owner: context.repo.owner,
  256. repo: context.repo.repo,
  257. title: title,
  258. body: body,
  259. labels: ['security', 'automated', 'dependencies']
  260. });
  261. console.log('Created new security issue');
  262. }