diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f4de9fc..8c300fd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -69,6 +69,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + # uses: ./ runs with no action ref, so the lookup for a SHA or major-tag pin + # is checked here against a fixed tag listing. + - name: action-release.sh finds the release for a pin + run: sh scripts/test-action-release.sh - id: el uses: ./ with: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b53f408..bbbbb01 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,15 +1,22 @@ name: Release on: push: - tags: ['v*'] -permissions: - contents: write # create the release - packages: write # push the container image - id-token: write # keyless signing + # Release tags only (v0.7.0). The major tags (v0, v1) move with each release + # and are never releases themselves, so pushing one must not start this. + tags: ['v[0-9]+.[0-9]+.[0-9]+'] +permissions: {} jobs: release: runs-on: ubuntu-latest + permissions: + contents: write # create the release and move the major tag + packages: write # push the container image and its attestation + id-token: write # keyless signing and the provenance attestations + attestations: write # store the provenance attestations steps: + - name: Check the tag is a release version + run: | + [[ "$GITHUB_REF_NAME" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || { echo "::error::$GITHUB_REF_NAME is not a release tag like v0.7.0"; exit 1; } - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 - uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5.6.0 with: @@ -38,15 +45,13 @@ jobs: - uses: sigstore/cosign-installer@398d4b0eeef1380460a10c8013a76f728fb906ac # v3.9.1 - name: Sign checksums run: cosign sign-blob --yes --bundle dist/checksums.txt.sigstore.json dist/checksums.txt - - name: Publish release - env: - GH_TOKEN: ${{ github.token }} - run: gh release create "$GITHUB_REF_NAME" dist/* --title "$GITHUB_REF_NAME" --generate-notes - - name: Move the major tag the action is pinned to (v0, v1, ...) - run: | - major="${GITHUB_REF_NAME%%.*}" - git tag -f "$major" "$GITHUB_SHA" - git push -f origin "refs/tags/$major" + # One build provenance attestation covering every file the release attaches. + - name: Attest release assets + uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4.2.2 + with: + subject-path: dist/* + # The image is pushed and attested before the release is published, so a + # failure here leaves no published release without its image. - uses: docker/setup-qemu-action@c7c53464625b32c7a7e944ae62b3e17d2b600130 # v3.7.0 - uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0 - uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0 @@ -54,13 +59,44 @@ jobs: registry: ghcr.io username: ${{ github.actor }} password: ${{ github.token }} + # Pushed by digest only: no tag names the image until it is attested. - name: Build and push image + id: image uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6.19.2 with: context: . platforms: linux/amd64,linux/arm64 - push: true build-args: VERSION=${{ github.ref_name }} - tags: | - ghcr.io/defectdojo/earlylight:${{ github.ref_name }} - ghcr.io/defectdojo/earlylight:latest + outputs: type=image,name=ghcr.io/defectdojo/earlylight,push-by-digest=true,name-canonical=true,push=true + - name: Attest image + uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4.2.2 + with: + subject-name: ghcr.io/defectdojo/earlylight + subject-digest: ${{ steps.image.outputs.digest }} + push-to-registry: true + # A storage record needs artifact-metadata: write; this job does not ask for it. + create-storage-record: false + - name: Tag image + env: + IMAGE: ghcr.io/defectdojo/earlylight + DIGEST: ${{ steps.image.outputs.digest }} + run: | + docker buildx imagetools create --tag "$IMAGE:$GITHUB_REF_NAME" --tag "$IMAGE:latest" "$IMAGE@$DIGEST" + # The tags must name the attested image itself, not a copy with a new digest. + for tag in "$GITHUB_REF_NAME" latest; do + got=$(docker buildx imagetools inspect --format '{{json .Manifest}}' "$IMAGE:$tag" | jq -r .digest) + [ "$got" = "$DIGEST" ] || { echo "::error::$IMAGE:$tag is $got, not the attested $DIGEST"; exit 1; } + done + # gh uploads the assets to a draft and publishes it once they are all there, + # which is what a repository with immutable releases requires. + - name: Publish release + env: + GH_TOKEN: ${{ github.token }} + run: gh release create "$GITHUB_REF_NAME" dist/* --title "$GITHUB_REF_NAME" --generate-notes + # The major tag stays a plain tag with no release, so it can keep moving + # when releases are immutable. + - name: Move the major tag the action is pinned to (v0, v1, ...) + run: | + major="${GITHUB_REF_NAME%%.*}" + git tag -f "$major" "$GITHUB_SHA" + git push -f origin "refs/tags/$major" diff --git a/README.md b/README.md index f015cec..1bd046f 100644 --- a/README.md +++ b/README.md @@ -23,20 +23,37 @@ The detectors ported from standalone scanners also build as standalone binaries ## Install ```bash -curl -fsSL https://raw.githubusercontent.com/DefectDojo/earlylight/main/install.sh | sh +v=v0.6.0 # a release from https://github.com/DefectDojo/earlylight/releases +curl -fsSL "https://raw.githubusercontent.com/DefectDojo/earlylight/$v/install.sh" | EARLYLIGHT_VERSION=$v sh brew install defectdojo/tap/earlylight -go install github.com/DefectDojo/earlylight/cmd/earlylight@latest -go install github.com/DefectDojo/earlylight/cmd/emudhra-scan@latest # just the eMudhra detector -go install github.com/DefectDojo/earlylight/cmd/php2shell-scan@latest # just the PHP2Shell detector -docker run --rm ghcr.io/defectdojo/earlylight scan image python:3.13-slim +go install github.com/DefectDojo/earlylight/cmd/earlylight@$v +go install github.com/DefectDojo/earlylight/cmd/emudhra-scan@$v # just the eMudhra detector +go install github.com/DefectDojo/earlylight/cmd/php2shell-scan@$v # just the PHP2Shell detector +docker run --rm ghcr.io/defectdojo/earlylight:$v scan image python:3.13-slim ``` +Each command names a release tag rather than a branch: `main` changes with every merge, a release tag stays on the commit it was released from. Without `EARLYLIGHT_VERSION`, `install.sh` installs the latest release. + `install.sh` checks each binary against the release's `checksums.txt`, and when `cosign` is installed it also verifies that file's Sigstore signature. Set `EARLYLIGHT_VERSION`, `EARLYLIGHT_BIN_DIR` or `EARLYLIGHT_BINARIES="earlylight php2shell-scan"` to change what it installs. -The static binary is the main way to run Early Light: it scans the machine it runs on, works the same on Linux, macOS and Windows, and needs nothing else. The container image is for CI systems that run steps in containers and for Kubernetes jobs; to scan a host from it, mount the host read-only and use `--as-root`: `docker run --rm -v /:/host:ro ghcr.io/defectdojo/earlylight scan fs --as-root /host`. +The static binary is the main way to run Early Light: it scans the machine it runs on, works the same on Linux, macOS and Windows, and needs nothing else. The container image is for CI systems that run steps in containers and for Kubernetes jobs; to scan a host from it, mount the host read-only and use `--as-root`: `docker run --rm -v /:/host:ro ghcr.io/defectdojo/earlylight:$v scan fs --as-root /host`. Release binaries for Linux, macOS and Windows are on the [releases page](https://github.com/DefectDojo/earlylight/releases), with `checksums.txt` signed by Sigstore (`cosign verify-blob --bundle checksums.txt.sigstore.json --certificate-identity-regexp 'https://github.com/DefectDojo/earlylight/' --certificate-oidc-issuer https://token.actions.githubusercontent.com checksums.txt`). +### Verify a release + +Releases after v0.6.0 carry a build provenance attestation for every file attached to the release and for the container image. It records the repository, the workflow, the tag and the commit that built them. Check one with the [GitHub CLI](https://cli.github.com/), logged in with `gh auth login`: + +```bash +v=v0.7.0 # any release after v0.6.0 +gh attestation verify "earlylight_${v}_linux_amd64" --repo DefectDojo/earlylight \ + --signer-workflow DefectDojo/earlylight/.github/workflows/release.yml --source-ref "refs/tags/$v" +gh attestation verify "oci://ghcr.io/defectdojo/earlylight:$v" --repo DefectDojo/earlylight \ + --signer-workflow DefectDojo/earlylight/.github/workflows/release.yml --source-ref "refs/tags/$v" +``` + +`--repo` alone checks that a workflow in this repository built the file; `--signer-workflow` and `--source-ref` also require the release workflow and that tag. The attestations are listed on the repository's [attestations page](https://github.com/DefectDojo/earlylight/attestations). + ## Use ```bash @@ -85,8 +102,8 @@ jobs: earlylight: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: DefectDojo/earlylight@v0 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - uses: DefectDojo/earlylight@v0.6.0 with: mode: repo # fs, image, repo or host target: . @@ -94,6 +111,8 @@ jobs: fail-on: vulnerable # vulnerable, unknown or never ``` +Pin the action to a release tag, as above, or to the commit SHA a release tag points to (`DefectDojo/earlylight@ # v0.7.0`, for releases after v0.6.0); Dependabot and Renovate keep either up to date. Never pin it to a branch. `@v0` moves to each new v0 release. For a commit SHA or `@v0`, the action lists the repository's release tags to find the release; if it cannot, the step fails (set `version` to skip the lookup). + The action installs the release it is pinned to (checksum-verified), writes SARIF to `earlylight.sarif` for DefectDojo or code scanning, and puts a findings table in the job summary. Outputs: `sarif`, `exit-code`. ## Verdicts @@ -183,7 +202,7 @@ It only makes network connections when a scan needs them: | What | Connects to | | --- | --- | -| Installing (`install.sh` and the GitHub Action) | GitHub, to download the release and its `checksums.txt`. When `cosign` is installed, it also fetches Sigstore's public trust root to check the signature | +| Installing (`install.sh` and the GitHub Action) | GitHub, to download the release and its `checksums.txt`, and for an action pinned to `v0` or a commit SHA, to list the release tags. When `cosign` is installed, it also fetches Sigstore's public trust root to check the signature | | `scan image` | The registry that hosts the image you name | | `scan repo` and `fix` | The registries that host the images your source tree references (for `sshole`, the base images whose Alpine release a Dockerfile does not name). `--offline` turns this off, and `--public-registries-only` refuses private and internal addresses | | `scan host` | The hosts you name: a TLS handshake, and for `sshole` an SSH handshake that asks which login methods the server offers (as user `earlylight-probe`) and stops before sending any password. The server logs it as a connection that did not authenticate | diff --git a/action.yml b/action.yml index 4503cd4..cdb7ec0 100644 --- a/action.yml +++ b/action.yml @@ -28,7 +28,7 @@ inputs: description: "When to fail the step: vulnerable (exit 1), unknown (exit 1 or 3), or never." default: vulnerable version: - description: "Early Light release to run, for example v0.2.0. Empty uses the release this action is pinned to, or the latest release." + description: "Early Light release to run, for example v0.2.0. Empty uses the release this action is pinned to (a release tag, a major tag such as v0, or a release's commit SHA), or the latest release." default: "" outputs: @@ -52,6 +52,21 @@ runs: set -euo pipefail if [ -z "$EARLYLIGHT_VERSION" ] && [[ "$ACTION_REF" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then export EARLYLIGHT_VERSION="$ACTION_REF" + elif [ -z "$EARLYLIGHT_VERSION" ] && [[ "$ACTION_REF" =~ ^(v[0-9]+|[0-9a-f]{40})$ ]]; then + # Pinned to a major tag (v0) or a commit SHA: install the release at that commit. + # A pin that cannot be checked fails rather than running some other release. + if ! tags=$(git ls-remote --tags https://github.com/DefectDojo/earlylight); then + echo "::error::Could not list Early Light's release tags to find the release for action ref $ACTION_REF. Set the version input to skip the lookup." + exit 2 + fi + EARLYLIGHT_VERSION=$(printf '%s\n' "$tags" | sh "$GITHUB_ACTION_PATH/scripts/action-release.sh" "$ACTION_REF") + if [ -n "$EARLYLIGHT_VERSION" ]; then + export EARLYLIGHT_VERSION + else + echo "::warning::No Early Light release is tagged at action ref $ACTION_REF; installing the latest release. Pin the action to a release tag or a release's commit SHA, or set version." + fi + elif [ -z "$EARLYLIGHT_VERSION" ] && [ -n "$ACTION_REF" ]; then + echo "::warning::Early Light action ref $ACTION_REF is not a release; installing the latest release. Pin the action to a release tag or a release's commit SHA, or set version." fi if [ "$RUNNER_OS" = "Windows" ]; then echo "::error::The Early Light action runs on Linux and macOS runners." && exit 2 diff --git a/install.sh b/install.sh index 0af54d3..7accc7a 100755 --- a/install.sh +++ b/install.sh @@ -1,7 +1,7 @@ #!/bin/sh # Install Early Light from a GitHub release, verifying the release checksum. # -# curl -fsSL https://raw.githubusercontent.com/DefectDojo/earlylight/main/install.sh | sh +# curl -fsSL https://raw.githubusercontent.com/DefectDojo/earlylight//install.sh | sh # # Environment: # EARLYLIGHT_VERSION release tag to install (default: the latest release) diff --git a/scripts/action-release.sh b/scripts/action-release.sh new file mode 100755 index 0000000..d7540d0 --- /dev/null +++ b/scripts/action-release.sh @@ -0,0 +1,39 @@ +#!/bin/sh +# Print the release tag (v1.2.3) that a GitHub Action ref stands for, reading +# `git ls-remote --tags` output on stdin: +# +# git ls-remote --tags https://github.com/DefectDojo/earlylight | sh action-release.sh v0 +# +# REF is a major tag (v0: the highest v0.x.y release at the commit v0 points +# to) or a commit SHA (the highest release tagged at that commit). Prints +# nothing when no release matches, for example for a branch name. +set -eu + +[ $# -eq 1 ] || { echo "usage: action-release.sh REF < ls-remote-output" >&2; exit 2; } + +awk -v ref="$1" ' + { + name = $2 + sub(/^refs\/tags\//, "", name) + # An annotated tag is listed twice; the ^{} line holds the commit it points to. + if (sub(/\^\{\}$/, "", name) || !(name in commit)) commit[name] = $1 + } + function newer(a, b, x, y, i) { + split(substr(a, 2), x, "."); split(substr(b, 2), y, ".") + for (i = 1; i <= 3; i++) if (x[i] + 0 != y[i] + 0) return x[i] + 0 > y[i] + 0 + return 0 + } + END { + if (ref ~ /^v[0-9]+$/) { + if (!(ref in commit)) exit + target = commit[ref]; prefix = ref "." + } else if (length(ref) == 40 && ref ~ /^[0-9a-f]+$/) { + target = ref; prefix = "v" + } else exit + best = "" + for (n in commit) + if (n ~ /^v[0-9]+\.[0-9]+\.[0-9]+$/ && index(n, prefix) == 1 && commit[n] == target) + if (best == "" || newer(n, best)) best = n + if (best != "") print best + } +' diff --git a/scripts/test-action-release.sh b/scripts/test-action-release.sh new file mode 100755 index 0000000..51ae70b --- /dev/null +++ b/scripts/test-action-release.sh @@ -0,0 +1,62 @@ +#!/bin/sh +# Check scripts/action-release.sh against a fixed `git ls-remote --tags` +# listing, under every awk on PATH (awk, gawk, mawk, busybox awk). +set -eu + +dir=$(cd "$(dirname "$0")" && pwd) +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT INT TERM + +C=cccccccccccccccccccccccccccccccccccccccc # commit of v0, v0.9.0 and v0.10.0 +D=dddddddddddddddddddddddddddddddddddddddd # commit of v0.8.0, and of v2 (no v2 release) +E=eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee # commit of v1 and v1.0.0-rc1 only +T=1111111111111111111111111111111111111111 # the v0.10.0 tag object, not a commit +cat > "$tmp/refs" </dev/null 2>&1 || continue + shim=$tmp/$name + mkdir -p "$shim" + printf '#!/bin/sh\nexec %s%s "$@"\n' "$bin" "$sub" > "$shim/awk" + chmod +x "$shim/awk" + while read -r ref want; do + [ "$want" = - ] && want="" + got=$(PATH="$shim:$PATH" sh "$dir/action-release.sh" "$ref" < "$tmp/refs") + runs=$((runs + 1)) + if [ "$got" != "$want" ]; then + echo "FAIL ($impl) $ref: got '$got', want '$want'" + fails=$((fails + 1)) + fi + done <