security.yml 9.7 KB

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