security.yml 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. pull_request:
  15. paths:
  16. - 'backend/**'
  17. - 'frontend/**'
  18. - 'Dockerfile'
  19. - 'docker-compose*.yml'
  20. - 'requirements.txt'
  21. - 'frontend/package*.json'
  22. workflow_dispatch:
  23. # Allow manual trigger
  24. env:
  25. PYTHON_VERSION: '3.11'
  26. NODE_VERSION: '20'
  27. # Default permissions for all jobs
  28. permissions:
  29. contents: read
  30. jobs:
  31. bandit:
  32. name: Python Security Analysis (Bandit)
  33. runs-on: ubuntu-latest
  34. permissions:
  35. contents: read
  36. security-events: write
  37. steps:
  38. - uses: actions/checkout@v4
  39. - name: Set up Python
  40. uses: actions/setup-python@v5
  41. with:
  42. python-version: ${{ env.PYTHON_VERSION }}
  43. - name: Install Bandit
  44. run: pip install bandit[sarif]
  45. - name: Run Bandit
  46. run: |
  47. bandit -r backend/ -f sarif -o bandit-results.sarif --severity-level medium || true
  48. - name: Upload Bandit results to GitHub Security
  49. uses: github/codeql-action/upload-sarif@v4
  50. if: always()
  51. with:
  52. sarif_file: bandit-results.sarif
  53. category: bandit
  54. trivy:
  55. name: Container Security Scan (Trivy)
  56. runs-on: ubuntu-latest
  57. permissions:
  58. contents: read
  59. security-events: write
  60. steps:
  61. - uses: actions/checkout@v4
  62. - name: Build Docker image
  63. run: docker build -t bambuddy:security-scan .
  64. - name: Run Trivy vulnerability scanner
  65. uses: aquasecurity/trivy-action@0.34.0
  66. with:
  67. image-ref: 'bambuddy:security-scan'
  68. format: 'sarif'
  69. output: 'trivy-results.sarif'
  70. severity: 'CRITICAL,HIGH,MEDIUM'
  71. version: 'v0.69.1'
  72. - name: Upload Trivy results to GitHub Security
  73. uses: github/codeql-action/upload-sarif@v4
  74. if: always()
  75. with:
  76. sarif_file: trivy-results.sarif
  77. category: trivy
  78. - name: Run Trivy for Dockerfile/IaC
  79. uses: aquasecurity/trivy-action@0.34.0
  80. with:
  81. scan-type: 'config'
  82. scan-ref: '.'
  83. format: 'sarif'
  84. output: 'trivy-config-results.sarif'
  85. severity: 'CRITICAL,HIGH,MEDIUM'
  86. version: 'v0.69.1'
  87. - name: Upload Trivy config results
  88. uses: github/codeql-action/upload-sarif@v4
  89. if: always()
  90. with:
  91. sarif_file: trivy-config-results.sarif
  92. category: trivy-config
  93. backend-audit:
  94. name: Backend Security Audit
  95. runs-on: ubuntu-latest
  96. permissions:
  97. contents: read
  98. issues: write
  99. steps:
  100. - uses: actions/checkout@v4
  101. - name: Set up Python
  102. uses: actions/setup-python@v5
  103. with:
  104. python-version: ${{ env.PYTHON_VERSION }}
  105. - name: Install dependencies
  106. run: |
  107. python -m pip install --upgrade pip
  108. pip install -r requirements.txt
  109. pip install pip-audit
  110. - name: Run pip-audit
  111. id: pip-audit
  112. run: |
  113. pip-audit --desc on --format json --output pip-audit-results.json || echo "vulnerabilities_found=true" >> $GITHUB_OUTPUT
  114. pip-audit --desc on || true
  115. - name: Upload audit results
  116. if: always()
  117. uses: actions/upload-artifact@v4
  118. with:
  119. name: pip-audit-results
  120. path: pip-audit-results.json
  121. retention-days: 30
  122. - name: Create issue on vulnerability
  123. if: steps.pip-audit.outputs.vulnerabilities_found == 'true'
  124. uses: actions/github-script@v7
  125. with:
  126. script: |
  127. const fs = require('fs');
  128. const results = JSON.parse(fs.readFileSync('pip-audit-results.json', 'utf8'));
  129. // Build vulnerability table
  130. let table = '| Package | Version | Vulnerability | Fix Version |\n';
  131. table += '|---------|---------|---------------|-------------|\n';
  132. for (const vuln of results.dependencies || []) {
  133. for (const v of vuln.vulns || []) {
  134. table += `| ${vuln.name} | ${vuln.version} | ${v.id} | ${v.fix_versions?.join(', ') || 'N/A'} |\n`;
  135. }
  136. }
  137. const title = `Security Alert: ${results.dependencies?.reduce((acc, d) => acc + (d.vulns?.length || 0), 0) || 0} Python vulnerabilities found`;
  138. // Check for existing open issue
  139. const existingIssues = await github.rest.issues.listForRepo({
  140. owner: context.repo.owner,
  141. repo: context.repo.repo,
  142. state: 'open',
  143. labels: 'security,automated'
  144. });
  145. const existingIssue = existingIssues.data.find(i => i.title.startsWith('Security Alert:') && i.title.includes('Python'));
  146. const body = `## Automated Security Audit Results
  147. The weekly security audit found vulnerabilities in Python dependencies.
  148. ${table}
  149. ### Recommended Actions
  150. 1. Review each vulnerability
  151. 2. Update affected packages: \`pip install --upgrade <package>\`
  152. 3. Run \`pip-audit\` locally to verify fixes
  153. 4. Close this issue when resolved
  154. ---
  155. *This issue was automatically created by the security audit workflow.*`;
  156. if (existingIssue) {
  157. await github.rest.issues.update({
  158. owner: context.repo.owner,
  159. repo: context.repo.repo,
  160. issue_number: existingIssue.number,
  161. body: body
  162. });
  163. console.log(`Updated existing issue #${existingIssue.number}`);
  164. } else {
  165. await github.rest.issues.create({
  166. owner: context.repo.owner,
  167. repo: context.repo.repo,
  168. title: title,
  169. body: body,
  170. labels: ['security', 'automated', 'dependencies']
  171. });
  172. console.log('Created new security issue');
  173. }
  174. frontend-audit:
  175. name: Frontend Security Audit
  176. runs-on: ubuntu-latest
  177. permissions:
  178. contents: read
  179. issues: write
  180. steps:
  181. - uses: actions/checkout@v4
  182. - name: Set up Node.js
  183. uses: actions/setup-node@v4
  184. with:
  185. node-version: ${{ env.NODE_VERSION }}
  186. cache: 'npm'
  187. cache-dependency-path: frontend/package-lock.json
  188. - name: Install dependencies
  189. working-directory: frontend
  190. run: npm ci
  191. - name: Run npm audit
  192. id: npm-audit
  193. working-directory: frontend
  194. run: |
  195. npm audit --json > npm-audit-results.json || echo "vulnerabilities_found=true" >> $GITHUB_OUTPUT
  196. npm audit --audit-level=high || true
  197. - name: Upload audit results
  198. if: always()
  199. uses: actions/upload-artifact@v4
  200. with:
  201. name: npm-audit-results
  202. path: frontend/npm-audit-results.json
  203. retention-days: 30
  204. - name: Create issue on vulnerability
  205. if: steps.npm-audit.outputs.vulnerabilities_found == 'true'
  206. uses: actions/github-script@v7
  207. with:
  208. script: |
  209. const fs = require('fs');
  210. const results = JSON.parse(fs.readFileSync('frontend/npm-audit-results.json', 'utf8'));
  211. const vulns = results.vulnerabilities || {};
  212. const vulnCount = Object.keys(vulns).length;
  213. if (vulnCount === 0) {
  214. console.log('No vulnerabilities to report');
  215. return;
  216. }
  217. // Build vulnerability table
  218. let table = '| Package | Severity | Via | Fix |\n';
  219. table += '|---------|----------|-----|-----|\n';
  220. for (const [name, info] of Object.entries(vulns)) {
  221. const via = Array.isArray(info.via) ? info.via.map(v => typeof v === 'string' ? v : v.name).join(', ') : info.via;
  222. table += `| ${name} | ${info.severity} | ${via} | ${info.fixAvailable ? 'Yes' : 'No'} |\n`;
  223. }
  224. const title = `Security Alert: ${vulnCount} npm vulnerabilities found`;
  225. // Check for existing open issue
  226. const existingIssues = await github.rest.issues.listForRepo({
  227. owner: context.repo.owner,
  228. repo: context.repo.repo,
  229. state: 'open',
  230. labels: 'security,automated'
  231. });
  232. const existingIssue = existingIssues.data.find(i => i.title.startsWith('Security Alert:') && i.title.includes('npm'));
  233. const body = `## Automated Security Audit Results
  234. The weekly security audit found vulnerabilities in npm dependencies.
  235. ${table}
  236. ### Recommended Actions
  237. 1. Review each vulnerability: \`npm audit\`
  238. 2. Auto-fix if possible: \`npm audit fix\`
  239. 3. Manual fix for breaking changes: \`npm audit fix --force\` (review changes!)
  240. 4. Close this issue when resolved
  241. ---
  242. *This issue was automatically created by the security audit workflow.*`;
  243. if (existingIssue) {
  244. await github.rest.issues.update({
  245. owner: context.repo.owner,
  246. repo: context.repo.repo,
  247. issue_number: existingIssue.number,
  248. body: body
  249. });
  250. console.log(`Updated existing issue #${existingIssue.number}`);
  251. } else {
  252. await github.rest.issues.create({
  253. owner: context.repo.owner,
  254. repo: context.repo.repo,
  255. title: title,
  256. body: body,
  257. labels: ['security', 'automated', 'dependencies']
  258. });
  259. console.log('Created new security issue');
  260. }