Security Scan #25
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Security Scan | |
| on: | |
| push: | |
| branches: [ main, develop ] | |
| pull_request: | |
| branches: [ main ] | |
| schedule: | |
| # Run weekly on Sunday at midnight | |
| - cron: '0 0 * * 0' | |
| workflow_dispatch: | |
| permissions: | |
| contents: read | |
| security-events: write | |
| jobs: | |
| dependency-scan: | |
| name: Dependency Vulnerability Scan | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.11' | |
| - name: Install security tools | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install safety pip-audit | |
| - name: Run Safety check | |
| working-directory: ./backend | |
| run: | | |
| safety check -r requirements.txt --json > safety-report.json || true | |
| safety check -r requirements.txt || true | |
| continue-on-error: true | |
| - name: Run pip-audit | |
| working-directory: ./backend | |
| run: | | |
| pip-audit -r requirements.txt --format json > pip-audit-report.json || true | |
| pip-audit -r requirements.txt || true | |
| continue-on-error: true | |
| - name: Upload dependency reports | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: dependency-scan-reports | |
| path: | | |
| backend/safety-report.json | |
| backend/pip-audit-report.json | |
| code-scan: | |
| name: Static Code Security Scan | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.11' | |
| - name: Install Bandit | |
| run: pip install bandit[toml] | |
| - name: Run Bandit | |
| working-directory: ./backend | |
| run: | | |
| bandit -r app -f sarif -o bandit-results.sarif --severity-level medium || true | |
| bandit -r app -f json -o bandit-results.json --severity-level medium || true | |
| - name: Upload Bandit SARIF | |
| uses: github/codeql-action/upload-sarif@v3 | |
| if: always() | |
| with: | |
| sarif_file: backend/bandit-results.sarif | |
| continue-on-error: true | |
| - name: Upload Bandit report | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: bandit-report | |
| path: backend/bandit-results.json | |
| semgrep: | |
| name: Semgrep Security Scan | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Run Semgrep | |
| uses: returntocorp/semgrep-action@v1 | |
| with: | |
| config: >- | |
| p/security-audit | |
| p/python | |
| p/owasp-top-ten | |
| p/secrets | |
| generateSarif: true | |
| continue-on-error: true | |
| - name: Upload Semgrep SARIF | |
| uses: github/codeql-action/upload-sarif@v3 | |
| if: always() | |
| with: | |
| sarif_file: semgrep.sarif | |
| continue-on-error: true | |
| docker-scan: | |
| name: Docker Image Security Scan | |
| runs-on: ubuntu-latest | |
| needs: [code-scan] | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Build backend image | |
| run: | | |
| docker build -t dmarc-backend:scan ./backend --target production | |
| - name: Run Trivy vulnerability scanner | |
| uses: aquasecurity/trivy-action@master | |
| with: | |
| image-ref: 'dmarc-backend:scan' | |
| format: 'sarif' | |
| output: 'trivy-results.sarif' | |
| severity: 'CRITICAL,HIGH,MEDIUM' | |
| continue-on-error: true | |
| - name: Upload Trivy scan results | |
| uses: github/codeql-action/upload-sarif@v3 | |
| if: always() | |
| with: | |
| sarif_file: 'trivy-results.sarif' | |
| continue-on-error: true | |
| secrets-scan: | |
| name: Secrets Detection | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Run Gitleaks | |
| uses: gitleaks/gitleaks-action@v2 | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| continue-on-error: true | |
| owasp-zap: | |
| name: OWASP ZAP Scan | |
| runs-on: ubuntu-latest | |
| if: github.event_name == 'workflow_dispatch' || github.event_name == 'schedule' | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Start application | |
| run: | | |
| docker compose up -d | |
| sleep 60 | |
| - name: Run OWASP ZAP Baseline Scan | |
| uses: zaproxy/action-baseline@v0.10.0 | |
| with: | |
| target: 'http://localhost:8000' | |
| rules_file_name: '.zap/rules.tsv' | |
| cmd_options: '-a' | |
| continue-on-error: true | |
| - name: Stop application | |
| if: always() | |
| run: docker compose down | |
| security-summary: | |
| name: Security Summary | |
| runs-on: ubuntu-latest | |
| needs: [dependency-scan, code-scan, semgrep, docker-scan, secrets-scan] | |
| if: always() | |
| steps: | |
| - name: Download all artifacts | |
| uses: actions/download-artifact@v4 | |
| continue-on-error: true | |
| - name: Generate summary | |
| run: | | |
| echo "## Security Scan Summary" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "| Check | Status |" >> $GITHUB_STEP_SUMMARY | |
| echo "|-------|--------|" >> $GITHUB_STEP_SUMMARY | |
| echo "| Dependency Scan | ${{ needs.dependency-scan.result }} |" >> $GITHUB_STEP_SUMMARY | |
| echo "| Code Scan | ${{ needs.code-scan.result }} |" >> $GITHUB_STEP_SUMMARY | |
| echo "| Semgrep | ${{ needs.semgrep.result }} |" >> $GITHUB_STEP_SUMMARY | |
| echo "| Docker Scan | ${{ needs.docker-scan.result }} |" >> $GITHUB_STEP_SUMMARY | |
| echo "| Secrets Scan | ${{ needs.secrets-scan.result }} |" >> $GITHUB_STEP_SUMMARY |