|
| 1 | +name: Security Checks |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main, develop] |
| 6 | + pull_request: |
| 7 | + schedule: |
| 8 | + - cron: '0 0 * * 0' # Weekly vulnerability scan |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + security-events: write |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | +jobs: |
| 16 | + npm-audit: |
| 17 | + name: NPM Dependency Audit |
| 18 | + runs-on: ubuntu-latest |
| 19 | + steps: |
| 20 | + - name: Checkout code |
| 21 | + uses: actions/checkout@v4 |
| 22 | + |
| 23 | + - name: Setup Node.js |
| 24 | + uses: actions/setup-node@v4 |
| 25 | + with: |
| 26 | + node-version: '22' |
| 27 | + cache: 'npm' |
| 28 | + |
| 29 | + - name: Install dependencies |
| 30 | + run: npm ci |
| 31 | + |
| 32 | + - name: Run npm audit |
| 33 | + run: npm audit --audit-level=moderate |
| 34 | + continue-on-error: true |
| 35 | + |
| 36 | + - name: Check for fixable vulnerabilities |
| 37 | + run: npm audit fix --dry-run |
| 38 | + continue-on-error: true |
| 39 | + |
| 40 | + snyk-scan: |
| 41 | + name: Snyk Vulnerability Scan |
| 42 | + runs-on: ubuntu-latest |
| 43 | + steps: |
| 44 | + - name: Checkout code |
| 45 | + uses: actions/checkout@v4 |
| 46 | + |
| 47 | + - name: Run Snyk scan |
| 48 | + uses: snyk/actions/node@master |
| 49 | + env: |
| 50 | + SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }} |
| 51 | + continue-on-error: true |
| 52 | + |
| 53 | + container-scan: |
| 54 | + name: Container Image Security Scan |
| 55 | + runs-on: ubuntu-latest |
| 56 | + steps: |
| 57 | + - name: Checkout code |
| 58 | + uses: actions/checkout@v4 |
| 59 | + |
| 60 | + - name: Set up Docker Buildx |
| 61 | + uses: docker/setup-buildx-action@v3 |
| 62 | + |
| 63 | + - name: Build Docker image |
| 64 | + uses: docker/build-push-action@v5 |
| 65 | + with: |
| 66 | + context: . |
| 67 | + file: ./Dockerfile |
| 68 | + tags: budget-api:scan |
| 69 | + load: true |
| 70 | + cache-from: type=gha |
| 71 | + cache-to: type=gha,mode=max |
| 72 | + |
| 73 | + - name: Run Trivy vulnerability scan |
| 74 | + uses: aquasecurity/trivy-action@master |
| 75 | + with: |
| 76 | + image-ref: budget-api:scan |
| 77 | + format: 'sarif' |
| 78 | + output: 'trivy-results.sarif' |
| 79 | + |
| 80 | + - name: Upload Trivy results to GitHub Security |
| 81 | + uses: github/codeql-action/upload-sarif@v3 |
| 82 | + if: always() |
| 83 | + with: |
| 84 | + sarif_file: 'trivy-results.sarif' |
| 85 | + category: 'trivy' |
| 86 | + |
| 87 | + secret-scan: |
| 88 | + name: Secret & Credential Detection |
| 89 | + runs-on: ubuntu-latest |
| 90 | + steps: |
| 91 | + - name: Checkout code |
| 92 | + uses: actions/checkout@v4 |
| 93 | + with: |
| 94 | + fetch-depth: 0 |
| 95 | + |
| 96 | + - name: Run gitleaks scan |
| 97 | + uses: gitleaks/gitleaks-action@v2 |
| 98 | + env: |
| 99 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 100 | + |
| 101 | + eslint: |
| 102 | + name: ESLint Code Quality |
| 103 | + runs-on: ubuntu-latest |
| 104 | + steps: |
| 105 | + - name: Checkout code |
| 106 | + uses: actions/checkout@v4 |
| 107 | + |
| 108 | + - name: Setup Node.js |
| 109 | + uses: actions/setup-node@v4 |
| 110 | + with: |
| 111 | + node-version: '22' |
| 112 | + cache: 'npm' |
| 113 | + |
| 114 | + - name: Install dependencies |
| 115 | + run: npm ci |
| 116 | + |
| 117 | + - name: Run ESLint |
| 118 | + run: npx eslint src --max-warnings 0 --format json --output-file eslint-report.json || true |
| 119 | + continue-on-error: true |
| 120 | + |
| 121 | + - name: Comment on PR with ESLint results |
| 122 | + if: github.event_name == 'pull_request' |
| 123 | + uses: actions/github-script@v7 |
| 124 | + with: |
| 125 | + script: | |
| 126 | + const fs = require('fs'); |
| 127 | + const report = JSON.parse(fs.readFileSync('eslint-report.json', 'utf8')); |
| 128 | + const issues = report.filter(f => f.messages.length > 0); |
| 129 | + if (issues.length > 0) { |
| 130 | + let comment = '## ESLint Issues\n\n'; |
| 131 | + issues.forEach(file => { |
| 132 | + comment += `### ${file.filePath}\n`; |
| 133 | + file.messages.forEach(msg => { |
| 134 | + comment += `- Line ${msg.line}: ${msg.message} (${msg.severity})\n`; |
| 135 | + }); |
| 136 | + }); |
| 137 | + github.rest.issues.createComment({ |
| 138 | + issue_number: context.issue.number, |
| 139 | + owner: context.repo.owner, |
| 140 | + repo: context.repo.repo, |
| 141 | + body: comment |
| 142 | + }); |
| 143 | + } |
| 144 | +
|
| 145 | + docker-build: |
| 146 | + name: Docker Build Test |
| 147 | + runs-on: ubuntu-latest |
| 148 | + steps: |
| 149 | + - name: Checkout code |
| 150 | + uses: actions/checkout@v4 |
| 151 | + |
| 152 | + - name: Set up Docker Buildx |
| 153 | + uses: docker/setup-buildx-action@v3 |
| 154 | + |
| 155 | + - name: Build Docker image |
| 156 | + uses: docker/build-push-action@v5 |
| 157 | + with: |
| 158 | + context: . |
| 159 | + file: ./Dockerfile |
| 160 | + tags: budget-api:test |
| 161 | + cache-from: type=gha |
| 162 | + cache-to: type=gha,mode=max |
| 163 | + |
| 164 | + dependency-check: |
| 165 | + name: OWASP Dependency-Check |
| 166 | + runs-on: ubuntu-latest |
| 167 | + steps: |
| 168 | + - name: Checkout code |
| 169 | + uses: actions/checkout@v4 |
| 170 | + |
| 171 | + - name: Run OWASP Dependency-Check |
| 172 | + uses: dependency-check/Dependency-Check_Action@main |
| 173 | + with: |
| 174 | + project: 'budget-automation' |
| 175 | + path: '.' |
| 176 | + format: 'JSON' |
| 177 | + args: > |
| 178 | + --enable-retired |
| 179 | +
|
| 180 | + - name: Upload Dependency-Check results |
| 181 | + uses: github/codeql-action/upload-sarif@v3 |
| 182 | + if: always() |
| 183 | + with: |
| 184 | + sarif_file: 'dependency-check-report.sarif' |
| 185 | + category: 'dependency-check' |
| 186 | + |
| 187 | + license-check: |
| 188 | + name: License Compliance |
| 189 | + runs-on: ubuntu-latest |
| 190 | + steps: |
| 191 | + - name: Checkout code |
| 192 | + uses: actions/checkout@v4 |
| 193 | + |
| 194 | + - name: Setup Node.js |
| 195 | + uses: actions/setup-node@v4 |
| 196 | + with: |
| 197 | + node-version: '22' |
| 198 | + cache: 'npm' |
| 199 | + |
| 200 | + - name: Install dependencies |
| 201 | + run: npm ci |
| 202 | + |
| 203 | + - name: Check licenses |
| 204 | + run: npx license-checker --onlyunknown |
| 205 | + continue-on-error: true |
| 206 | + |
| 207 | + summary: |
| 208 | + name: Security Summary |
| 209 | + runs-on: ubuntu-latest |
| 210 | + needs: [npm-audit, snyk-scan, container-scan, secret-scan, eslint, docker-build] |
| 211 | + if: always() |
| 212 | + steps: |
| 213 | + - name: Check security status |
| 214 | + run: | |
| 215 | + echo "Security checks completed!" |
| 216 | + echo "Review the results above for any issues." |
0 commit comments