diff --git a/.github/workflows/security-posture.yml b/.github/workflows/security-posture.yml index 0f69603..e3e05b8 100644 --- a/.github/workflows/security-posture.yml +++ b/.github/workflows/security-posture.yml @@ -108,8 +108,19 @@ jobs: ALERT_REF: ${{ github.ref }} run: | echo "Reading the alerts of ${ALERT_REF}" - if gh api "repos/${GITHUB_REPOSITORY}/code-scanning/alerts?state=open&per_page=100&ref=${ALERT_REF}" \ - > alerts.json 2>api-error.txt; then + # Every page. The API returns at most a hundred alerts per call, and this + # image carries more: on 2026-11-13 cookieradar #68 had 123 open on its ref + # and main 142. Reading one page, three exceptions whose findings sat further + # down read as matching nothing and failed the gate, and an unexplained alert + # there would not have been seen at all. No pipe: the step runs without + # pipefail, and a failed read has to fail here, so that an unreadable list + # is not taken for an empty one. + alerts() { + gh api --paginate --slurp "$1" > "$2.pages" || return 1 + python -c 'import json, sys; json.dump([a for page in json.load(open(sys.argv[1])) for a in page], open(sys.argv[2], "w"))' "$2.pages" "$2" + } + if alerts "repos/${GITHUB_REPOSITORY}/code-scanning/alerts?state=open&per_page=100&ref=${ALERT_REF}" \ + alerts.json 2>api-error.txt; then echo "count=$(python -c 'import json;print(len(json.load(open("alerts.json"))))')" >> "$GITHUB_OUTPUT" else echo "readable=false" >> "$GITHUB_OUTPUT" @@ -126,17 +137,17 @@ jobs: # exception main holds as settled would read as unused there and fail # every pull request. Measured on all six Radar on 2026-10-11 with the # real tool: this union gives the verdict main gives. - gh api "repos/${GITHUB_REPOSITORY}/code-scanning/alerts?state=closed&per_page=100&ref=${ALERT_REF}" \ - > closed-ref.json 2>/dev/null || echo "[]" > closed-ref.json - gh api "repos/${GITHUB_REPOSITORY}/code-scanning/alerts?state=closed&per_page=100&ref=refs/heads/main" \ - > closed-main.json 2>/dev/null || echo "[]" > closed-main.json + alerts "repos/${GITHUB_REPOSITORY}/code-scanning/alerts?state=closed&per_page=100&ref=${ALERT_REF}" \ + closed-ref.json 2>/dev/null || echo "[]" > closed-ref.json + alerts "repos/${GITHUB_REPOSITORY}/code-scanning/alerts?state=closed&per_page=100&ref=refs/heads/main" \ + closed-main.json 2>/dev/null || echo "[]" > closed-main.json # And main's open alerts: on a pull request ref CodeQL reports only what # the change introduces, so an exception for a finding still open on main # in an untouched file read as unused as soon as CodeQL reported anything # else (cookieradar #37, 2026-10-12). They are history the entry may match, # not alerts of this ref: blocking is decided on alerts.json alone. - gh api "repos/${GITHUB_REPOSITORY}/code-scanning/alerts?state=open&per_page=100&ref=refs/heads/main" \ - > open-main.json 2>/dev/null || echo "[]" > open-main.json + alerts "repos/${GITHUB_REPOSITORY}/code-scanning/alerts?state=open&per_page=100&ref=refs/heads/main" \ + open-main.json 2>/dev/null || echo "[]" > open-main.json python -c 'import json, sys; json.dump([a for f in sys.argv[1:-1] for a in json.load(open(f))], open(sys.argv[-1], "w"))' \ closed-ref.json closed-main.json open-main.json closed.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e89b69..8145c7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,17 @@ what has already shipped, and for nothing else. See `RELEASING.md`. ### Fixed +- **Security Posture reads every page of alerts.** The code scanning API returns at most a hundred + alerts per call, and the gate read one page of each list. Found in cookieradar on 2026-11-13 + (cookieradar #70): a pull request with 123 open alerts and main with 142, three exceptions whose + findings sat past the hundredth read as matching nothing, and the gate failed on a product whose every + alert was explained; read the other way, an unexplained alert past the hundredth would not have been + seen at all. This repository had 49 open on main on 2026-11-15, so its gate had not met the limit yet. + Each of the four reads now goes through `gh api --paginate --slurp` and joins the pages; a failed read + still fails, so an unreadable list is not taken for an empty one. + `tests/test_security_posture_workflow.py` runs the step's own function against a stand-in `gh` that + answers in two pages. + - **Docker Scout scans the image as soon as it is published, and Security Posture reads that scan.** The scan reads `latest` from Docker Hub but ran only on a push to main, weekly or by hand: after the certifi release the gate stayed red on the replaced image until both were dispatched by hand. diff --git a/tests/test_security_posture_workflow.py b/tests/test_security_posture_workflow.py index d595216..88f8a91 100644 --- a/tests/test_security_posture_workflow.py +++ b/tests/test_security_posture_workflow.py @@ -98,3 +98,88 @@ def test_main_s_open_alerts_count_as_known_on_a_pull_request(collect): run = collect["run"] assert "alerts?state=open&per_page=100&ref=refs/heads/main" in run assert "open-main.json" in run + + +# ── Every page of alerts ───────────────────────────────────────────────────── +# The code scanning API returns at most a hundred alerts per call. On +# 2026-11-13 cookieradar #68 had 123 open alerts on its ref and main had 142: +# the gate read the first hundred, three exceptions whose findings sat further +# down read as matching nothing, and the build failed on a product whose every +# alert was explained. Read the other way, an unexplained alert past the +# hundredth would not have been seen at all. + +ALERTS_URL = "/code-scanning/alerts?" + + +def _alerts_function(run: str) -> str: + """The shell function the step reads alerts with, as written in the step.""" + lines = run.splitlines() + start = next(i for i, line in enumerate(lines) if line.strip().startswith("alerts() {")) + end = next(i for i in range(start, len(lines)) if lines[i].strip() == "}") + return "\n".join(lines[start:end + 1]) + + +def test_every_read_of_the_alerts_goes_through_every_page(collect): + run = collect["run"] + function = _alerts_function(run) + assert "gh api --paginate --slurp" in function + reads = [line.strip() for line in run.splitlines() if ALERTS_URL in line] + assert len(reads) == 4, reads + for line in reads: + assert line.startswith(("alerts ", "if alerts ")), f"read without paging: {line}" + + +def _posix(path: Path) -> str: + """A path for bash's PATH: Git Bash on Windows splits C:/... at the colon.""" + import re + + text = path.as_posix() + drive = re.match(r"([A-Za-z]):/(.*)", text) + return f"/{drive.group(1).lower()}/{drive.group(2)}" if drive else text + + +def _run_alerts(tmp_path, collect, pages_json: str, gh_exit: int = 0): + import shutil + import subprocess + import sys + + bash = shutil.which("bash") + if bash is None: + pytest.skip("no bash to run the step's function with") + fake = tmp_path / "bin" + fake.mkdir() + (fake / "pages.json").write_text(pages_json, encoding="utf-8") + (fake / "gh").write_text( + "#!/bin/sh\n" + f'cat "$(dirname "$0")/pages.json"\n' + f"exit {gh_exit}\n", + encoding="utf-8", newline="\n", + ) + (fake / "gh").chmod(0o755) + python_dir = _posix(Path(sys.executable).parent) + script = ( + f'export PATH="{_posix(fake)}:{python_dir}:$PATH"\n' + # Never the real gh: a test that reached GitHub would pass on its answer. + f'[ "$(command -v gh)" = "{_posix(fake)}/gh" ] || {{ echo "the fake gh is not first on PATH" >&2; exit 99; }}\n' + "GITHUB_REPOSITORY=owner/repo\n" + + _alerts_function(collect["run"]) + + '\nalerts "repos/${GITHUB_REPOSITORY}/code-scanning/alerts?state=open&per_page=100&ref=x" out.json\n' + ) + return subprocess.run([bash, "-e", "-c", script], cwd=tmp_path, capture_output=True, text=True, + encoding="utf-8") + + +def test_the_pages_are_joined_into_one_list(tmp_path, collect): + import json + + result = _run_alerts(tmp_path, collect, '[[{"number": 1}, {"number": 2}], [{"number": 3}]]') + assert result.returncode == 0, result.stderr + alerts = json.loads((tmp_path / "out.json").read_text(encoding="utf-8")) + assert [a["number"] for a in alerts] == [1, 2, 3] + + +def test_an_api_error_still_reads_as_an_error(tmp_path, collect): + """The step tells an unreadable list apart from an empty one by the exit + status of the read: a fork's token gets an error, not a clean product.""" + result = _run_alerts(tmp_path, collect, "[]", gh_exit=1) + assert result.returncode not in (0, 99), result.stderr