|
| 1 | +# Container image vulnerability scan (advisory) |
| 2 | +# |
| 3 | +# Trivy builds the application image and scans it for known-vulnerable OS and |
| 4 | +# Python packages. Advisory only -- it reports findings to the repo's Security |
| 5 | +# tab without blocking a merge, because the image inevitably contains |
| 6 | +# already-known CVEs in upstream packages that are not this project's bug. |
| 7 | +# |
| 8 | +# Split from the Dockerfile lint (container-scan.yml) for two reasons: |
| 9 | +# |
| 10 | +# - Least privilege. The image build runs Dockerfile instructions, which on a |
| 11 | +# pull request are attacker-influenceable. That path (the `scan` job) is |
| 12 | +# held to a read-only token and never publishes results. Only `publish`, |
| 13 | +# which runs on push to main (curated, fast-forwarded from reviewed dev), |
| 14 | +# gets security-events:write to upload SARIF. |
| 15 | +# - Cost. Docs-only changes do not rebuild the image (paths-ignore below), |
| 16 | +# matching docker-publish.yml. hadolint stays on the broad trigger in |
| 17 | +# container-scan.yml so the blocking gate always reports. |
| 18 | + |
| 19 | +name: Container scan (Trivy) |
| 20 | + |
| 21 | +on: |
| 22 | + pull_request: |
| 23 | + paths-ignore: |
| 24 | + - '**.md' |
| 25 | + - 'docs/**' |
| 26 | + - '.github/ISSUE_TEMPLATE/**' |
| 27 | + push: |
| 28 | + branches: [main] |
| 29 | + paths-ignore: |
| 30 | + - '**.md' |
| 31 | + - 'docs/**' |
| 32 | + - '.github/ISSUE_TEMPLATE/**' |
| 33 | + workflow_dispatch: |
| 34 | + |
| 35 | +permissions: {} |
| 36 | + |
| 37 | +concurrency: |
| 38 | + group: container-trivy-${{ github.workflow }}-${{ github.ref }} |
| 39 | + cancel-in-progress: true |
| 40 | + |
| 41 | +jobs: |
| 42 | + # Pull requests and manual runs: build and scan under a read-only token. |
| 43 | + # The build executes PR-supplied Dockerfile instructions, so this job must |
| 44 | + # not hold any write scope, and it does not upload to the Security tab. |
| 45 | + scan: |
| 46 | + name: Trivy (image scan, advisory) |
| 47 | + if: github.event_name != 'push' |
| 48 | + runs-on: ubuntu-latest |
| 49 | + # Advisory: a CVE in an upstream package must not block a PR. |
| 50 | + continue-on-error: true |
| 51 | + permissions: |
| 52 | + contents: read |
| 53 | + steps: |
| 54 | + - name: Checkout repository |
| 55 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 56 | + with: |
| 57 | + persist-credentials: false |
| 58 | + |
| 59 | + - name: Set up Buildx |
| 60 | + uses: docker/setup-buildx-action@d7f5e7f509e45cec5c76c4d5afdd7de93d0b3df5 # v4.1.0 |
| 61 | + |
| 62 | + # Build without pushing so a broken Dockerfile is caught here, and the |
| 63 | + # exact image we ship is what gets scanned. |
| 64 | + - name: Build image |
| 65 | + uses: docker/build-push-action@f9f3042f7e2789586610d6e8b85c8f03e5195baf # v7.2.0 |
| 66 | + with: |
| 67 | + context: . |
| 68 | + push: false |
| 69 | + load: true |
| 70 | + tags: odysseus:ci |
| 71 | + |
| 72 | + - name: Scan image with Trivy |
| 73 | + uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0 |
| 74 | + with: |
| 75 | + image-ref: odysseus:ci |
| 76 | + format: table |
| 77 | + ignore-unfixed: true |
| 78 | + env: |
| 79 | + # Pin the vuln DB source to GHCR to avoid rate-limited Docker Hub |
| 80 | + # mirrors that flake on shared runners. |
| 81 | + TRIVY_DB_REPOSITORY: ghcr.io/aquasecurity/trivy-db:2 |
| 82 | + |
| 83 | + # Push to main only: build, scan, and publish SARIF to the Security tab. |
| 84 | + # This is the only path that runs trusted code, so it is the only one granted |
| 85 | + # security-events:write. |
| 86 | + publish: |
| 87 | + name: Trivy (image scan + SARIF upload) |
| 88 | + if: github.event_name == 'push' |
| 89 | + runs-on: ubuntu-latest |
| 90 | + continue-on-error: true |
| 91 | + permissions: |
| 92 | + contents: read |
| 93 | + security-events: write # upload SARIF to the Security tab |
| 94 | + steps: |
| 95 | + - name: Checkout repository |
| 96 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 97 | + with: |
| 98 | + persist-credentials: false |
| 99 | + |
| 100 | + - name: Set up Buildx |
| 101 | + uses: docker/setup-buildx-action@d7f5e7f509e45cec5c76c4d5afdd7de93d0b3df5 # v4.1.0 |
| 102 | + |
| 103 | + - name: Build image |
| 104 | + uses: docker/build-push-action@f9f3042f7e2789586610d6e8b85c8f03e5195baf # v7.2.0 |
| 105 | + with: |
| 106 | + context: . |
| 107 | + push: false |
| 108 | + load: true |
| 109 | + tags: odysseus:ci |
| 110 | + |
| 111 | + - name: Scan image with Trivy |
| 112 | + uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0 |
| 113 | + with: |
| 114 | + image-ref: odysseus:ci |
| 115 | + format: sarif |
| 116 | + output: trivy-results.sarif |
| 117 | + ignore-unfixed: true |
| 118 | + env: |
| 119 | + TRIVY_DB_REPOSITORY: ghcr.io/aquasecurity/trivy-db:2 |
| 120 | + |
| 121 | + - name: Upload Trivy results |
| 122 | + uses: github/codeql-action/upload-sarif@03e4368ac7daa2bd82b3e85262f3bf87ee112f57 # v3.36.0 |
| 123 | + with: |
| 124 | + sarif_file: trivy-results.sarif |
| 125 | + category: trivy-image |
0 commit comments