diff --git a/.github/scripts/verify_contact_small_gap_trust.py b/.github/scripts/verify_contact_small_gap_trust.py new file mode 100644 index 00000000..2c0ba9b2 --- /dev/null +++ b/.github/scripts/verify_contact_small_gap_trust.py @@ -0,0 +1,419 @@ +"""Read-only source-identity check for the exact Math-#547 candidate. + +This verifier runs only from trusted base-branch code. It never checks out, +imports, executes, downloads or interprets candidate files. A PASS establishes +only the pinned PR/path/tree identity described below; it is not a review, +security certification, proof result or scientific acceptance. +""" +from __future__ import annotations + +import json +import math +import os +import re +import sys +import urllib.error +import urllib.request + +REPOSITORY = "d6g8k5htny-coder/Math-" +EXPECTED_REPOSITORY_ID = 1385716596 +PR_NUMBER = 547 +HEAD_REF = "dcl/contact-small-gap-mutant-identity-20261030" +EXPECTED_MERGE_BASE = "19a55750451b8016b50d1d20be6a18c021807ecb" +EXPECTED_HEAD = "aba43cce0fad06c8ed8a6f6f8e5dfc1b495ffa8b" +HEX40 = re.compile(r"[0-9a-f]{40}\Z") +MAX_RESPONSE_BYTES = 8 * 1024 * 1024 +MAX_TREE_ENTRIES = 100_000 +MAX_FILE_PAGES = 30 +PAGE_SIZE = 100 + +# These are the exact base/head Git blob identities observed for PR #547. +# Base identities must remain present in the live base tree; head identities +# must remain present in the candidate tree. No candidate bytes are fetched. +EXPECTED_FILES = { + ".github/workflows/contact-small-gap.yml": { + "base_blob": "86cadd90ac8c68ded41e462d8c5f249780886dda", + "base_size": 4164, + "head_blob": "10e958e0026b16d84155fc2f38f6539bbf54aed1", + "head_size": 7170, + }, + "reviews/contact_kernel_tail_20260925/SOURCE_FILES.json": { + "base_blob": "0f73583343b98ec78cd20ad90b2a4935ea2b34fe", + "base_size": 1338, + "head_blob": "a096b6bc5643b983ade7c9f3d02b6490e932ebab", + "head_size": 1338, + }, + "reviews/contact_kernel_tail_20260925/run_validation.py": { + "base_blob": "97dbbe04417655b527db287fd12e97a41cc6db35", + "base_size": 3914, + "head_blob": "c60624104972b5f619dfcf7c63429e573297125f", + "head_size": 7061, + }, +} + + +class VerificationError(RuntimeError): + pass + + +def require(condition: bool, message: str) -> None: + if not condition: + raise VerificationError(message) + + +def unique_object(pairs): + result = {} + for key, value in pairs: + require(key not in result, "duplicate JSON object key") + result[key] = value + return result + + +def reject_constant(_token): + raise VerificationError("non-finite JSON constant") + + +def finite_float(token: str) -> float: + value = float(token) + require(math.isfinite(value), "non-finite JSON number") + return value + + +def strict_json(raw: bytes): + require(isinstance(raw, bytes), "JSON input must be bytes") + try: + text = raw.decode("utf-8", errors="strict") + return json.loads( + text, + object_pairs_hook=unique_object, + parse_constant=reject_constant, + parse_float=finite_float, + ) + except VerificationError: + raise + except (UnicodeDecodeError, json.JSONDecodeError, ValueError, TypeError) as exc: + raise VerificationError("malformed JSON") from exc + + +def sha40(value, message: str) -> str: + require(isinstance(value, str) and HEX40.fullmatch(value) is not None, message) + return value + + +def exact_int(value, message: str) -> int: + require(type(value) is int and value >= 0, message) + return value + + +class NoRedirect(urllib.request.HTTPRedirectHandler): + def redirect_request(self, req, fp, code, msg, headers, newurl): + raise VerificationError("API redirect refused") + + +class GitHubAPI: + """GET-only GitHub REST client. Redirects and oversized responses fail closed.""" + + def __init__(self, token: str, opener=None): + require(isinstance(token, str) and bool(token), "read-only GitHub token missing") + self._token = token + self._opener = opener if opener is not None else urllib.request.build_opener(NoRedirect()) + + def get(self, route: str): + path = route.split("?", 1)[0] if isinstance(route, str) else "" + segments = path.split("/") + require( + isinstance(route, str) + and route.startswith(f"/repos/{REPOSITORY}/") + and not any(ch in route for ch in ("\r", "\n", "#", "\\", " ")) + and all(segment not in (".", "..") for segment in segments), + "invalid fixed-repository API route", + ) + request = urllib.request.Request( + "https://api.github.com" + route, + headers={ + "Authorization": "Bearer " + self._token, + "Accept": "application/vnd.github+json", + "X-GitHub-Api-Version": "2022-11-28", + "User-Agent": "contact-small-gap-trust-read-only", + }, + method="GET", + ) + try: + with self._opener.open(request, timeout=20) as response: + require(getattr(response, "status", 200) == 200, "unexpected API status") + raw = response.read(MAX_RESPONSE_BYTES + 1) + except VerificationError: + raise + except (urllib.error.HTTPError, urllib.error.URLError, TimeoutError, OSError) as exc: + raise VerificationError("GitHub API request failed") from exc + require(len(raw) <= MAX_RESPONSE_BYTES, "API response exceeds size limit") + return strict_json(raw) + + +def repo_identity(value, label: str) -> int: + require(isinstance(value, dict), f"{label} repository missing") + require(value.get("full_name") == REPOSITORY, f"{label} repository mismatch") + repo_id = value.get("id") + require(type(repo_id) is int and repo_id == EXPECTED_REPOSITORY_ID, + f"{label} canonical repository ID mismatch") + require(value.get("fork") is False, f"{label} fork repository refused") + return repo_id + + +def event_snapshot(event_name: str, event: dict, env: dict) -> tuple[int, str | None, int]: + require(event_name in ("pull_request_target", "workflow_dispatch"), "unsupported event") + require(env.get("GITHUB_REPOSITORY") == REPOSITORY, "workflow repository mismatch") + require(env.get("GITHUB_REF") == "refs/heads/main", "workflow must run from trusted main") + trusted_sha = sha40(env.get("GITHUB_SHA"), "trusted workflow SHA missing") + repo = event.get("repository") if isinstance(event, dict) else None + event_repo_id = repo_identity(repo, "event") + require(repo.get("default_branch") == "main", "event default branch mismatch") + + try: + supplied_number = int(str(env.get("PR_NUMBER", "")), 10) + except (TypeError, ValueError) as exc: + raise VerificationError("PR number input invalid") from exc + require(supplied_number == PR_NUMBER, "unexpected PR number input") + + if event_name == "pull_request_target": + snapshot = event.get("pull_request") + require(isinstance(snapshot, dict), "pull_request event snapshot missing") + require(snapshot.get("number") == PR_NUMBER, "event PR number mismatch") + require(snapshot.get("state") == "open" and snapshot.get("merged") is False, + "event PR state is not open") + require(exact_int(snapshot.get("changed_files"), "event changed_files invalid") == len(EXPECTED_FILES), + "event changed-file count mismatch") + base = snapshot.get("base") + head = snapshot.get("head") + require(isinstance(base, dict) and isinstance(head, dict), "event base/head missing") + require(base.get("ref") == "main", "event base ref mismatch") + require(head.get("ref") == HEAD_REF, "event head ref mismatch") + event_base_sha = sha40(base.get("sha"), "event base SHA invalid") + event_head_sha = sha40(head.get("sha"), "event head SHA invalid") + require(event_base_sha == trusted_sha, "stale event base SHA") + require(event_head_sha == EXPECTED_HEAD, "stale or unexpected event head SHA") + base_repo_id = repo_identity(base.get("repo"), "event base") + head_repo_id = repo_identity(head.get("repo"), "event head") + require(base_repo_id == head_repo_id == event_repo_id, "event head is not same-repository") + return supplied_number, event_head_sha, event_repo_id + + inputs = event.get("inputs") + require(isinstance(inputs, dict), "manual dispatch inputs missing") + try: + input_number = int(str(inputs.get("pr_number", "")), 10) + except (TypeError, ValueError) as exc: + raise VerificationError("manual dispatch PR number invalid") from exc + require(input_number == PR_NUMBER == supplied_number, "manual dispatch PR mismatch") + return supplied_number, None, event_repo_id + + +def pr_snapshot(pr: dict) -> tuple: + require(isinstance(pr, dict), "live PR response missing") + require(pr.get("number") == PR_NUMBER, "live PR number mismatch") + require(pr.get("state") == "open" and pr.get("merged") is False, "target PR is not open") + base = pr.get("base") + head = pr.get("head") + require(isinstance(base, dict) and isinstance(head, dict), "live PR base/head missing") + require(base.get("ref") == "main", "live PR base ref mismatch") + require(head.get("ref") == HEAD_REF, "live PR head ref mismatch") + base_sha = sha40(base.get("sha"), "live PR base SHA invalid") + head_sha = sha40(head.get("sha"), "live PR head SHA invalid") + require(head_sha == EXPECTED_HEAD, "live PR head drift") + base_repo_id = repo_identity(base.get("repo"), "live PR base") + head_repo_id = repo_identity(head.get("repo"), "live PR head") + require(base_repo_id == head_repo_id, "live PR fork/head repository refused") + return ( + pr.get("number"), pr.get("state"), pr.get("merged"), + base.get("ref"), base_sha, base_repo_id, + head.get("ref"), head_sha, head_repo_id, + exact_int(pr.get("changed_files"), "live PR changed_files invalid"), + ) + + +def require_event_pr_matches_live(event_pr: dict, pr: dict) -> None: + require(isinstance(event_pr, dict), "event PR snapshot missing") + require(event_pr.get("number") == pr.get("number") == PR_NUMBER, + "event/live PR number mismatch") + require(event_pr.get("state") == pr.get("state"), "event/live PR state mismatch") + require(event_pr.get("merged") == pr.get("merged"), "event/live merged state mismatch") + require(event_pr.get("changed_files") == pr.get("changed_files"), + "event/live changed-file count mismatch") + event_base = event_pr.get("base") + event_head = event_pr.get("head") + live_base = pr.get("base") + live_head = pr.get("head") + require(all(isinstance(x, dict) for x in (event_base, event_head, live_base, live_head)), + "event/live PR base/head missing") + require(event_base.get("ref") == live_base.get("ref"), "event/live base ref mismatch") + require(event_base.get("sha") == live_base.get("sha"), "event/live base SHA mismatch") + require(event_head.get("ref") == live_head.get("ref"), "event/live head ref mismatch") + require(event_head.get("sha") == live_head.get("sha"), "event/live head SHA mismatch") + require(repo_identity(event_base.get("repo"), "event base") == + repo_identity(live_base.get("repo"), "live base"), + "event/live base repository mismatch") + require(repo_identity(event_head.get("repo"), "event head") == + repo_identity(live_head.get("repo"), "live head"), + "event/live head repository mismatch") + + +def compare_merge_base(api: GitHubAPI, base_sha: str, head_sha: str, *, require_base_ancestor=False) -> None: + route = f"/repos/{REPOSITORY}/compare/{base_sha}...{head_sha}?per_page=1" + data = api.get(route) + require(isinstance(data, dict), "compare response missing") + merge = data.get("merge_base_commit") + require(isinstance(merge, dict), "merge base missing") + require(sha40(merge.get("sha"), "merge-base SHA invalid") == EXPECTED_MERGE_BASE, + "unexpected merge base") + if require_base_ancestor: + require(exact_int(data.get("behind_by"), "compare behind_by invalid") == 0, + "expected base is not an ancestor") + require(data.get("status") in ("ahead", "identical"), "base ancestry status invalid") + + +def changed_files(api: GitHubAPI, pr: dict) -> dict: + count = exact_int(pr.get("changed_files"), "changed file count invalid") + require(count == len(EXPECTED_FILES), "unexpected changed file count") + rows = [] + for page in range(1, MAX_FILE_PAGES + 1): + batch = api.get(f"/repos/{REPOSITORY}/pulls/{PR_NUMBER}/files?per_page={PAGE_SIZE}&page={page}") + require(isinstance(batch, list), "changed-file page is not a list") + rows.extend(batch) + if len(batch) < PAGE_SIZE: + break + else: + raise VerificationError("changed-file pagination limit reached") + require(len(rows) == count, "changed-file pagination incomplete or truncated") + + observed = {} + for row in rows: + require(isinstance(row, dict), "changed-file entry malformed") + path = row.get("filename") + require(isinstance(path, str) and path not in observed, "missing or duplicate changed path") + require(row.get("status") == "modified", "changed path is not a modification") + require("previous_filename" not in row, "renamed path refused") + observed[path] = sha40(row.get("sha"), "changed-file blob SHA invalid") + require(set(observed) == set(EXPECTED_FILES), "changed path set mismatch") + for path, identity in EXPECTED_FILES.items(): + require(observed[path] == identity["head_blob"], f"changed-file blob mismatch: {path}") + return observed + + +def commit_tree(api: GitHubAPI, commit_sha: str) -> str: + data = api.get(f"/repos/{REPOSITORY}/commits/{commit_sha}") + require(isinstance(data, dict) and sha40(data.get("sha"), "commit response SHA invalid") == commit_sha, + "commit identity mismatch") + commit = data.get("commit") + require(isinstance(commit, dict) and isinstance(commit.get("tree"), dict), "commit tree missing") + return sha40(commit["tree"].get("sha"), "commit tree SHA invalid") + + +def tree_entries(api: GitHubAPI, tree_sha: str, label: str) -> dict: + data = api.get(f"/repos/{REPOSITORY}/git/trees/{tree_sha}?recursive=1") + require(isinstance(data, dict), f"{label} tree response missing") + require(sha40(data.get("sha"), f"{label} tree SHA invalid") == tree_sha, f"{label} tree identity mismatch") + require(data.get("truncated") is False, f"{label} tree is truncated") + entries = data.get("tree") + require(isinstance(entries, list) and len(entries) <= MAX_TREE_ENTRIES, f"{label} tree entries unavailable") + result = {} + for row in entries: + require(isinstance(row, dict), f"{label} tree entry malformed") + path = row.get("path") + require(isinstance(path, str) and bool(path) and path not in result, f"{label} tree path missing or duplicate") + result[path] = row + return result + + +def verify_tree_path(entries: dict, path: str, expected_blob: str, expected_size: int, label: str) -> None: + parts = path.split("/") + for index in range(1, len(parts)): + parent = "/".join(parts[:index]) + row = entries.get(parent) + require(isinstance(row, dict), f"{label} parent path missing: {parent}") + require(row.get("type") == "tree" and row.get("mode") == "040000", + f"{label} parent is not a real tree: {parent}") + row = entries.get(path) + require(isinstance(row, dict), f"{label} path missing: {path}") + require(row.get("type") == "blob" and row.get("mode") == "100644", + f"{label} path is not a regular non-executable blob: {path}") + require(sha40(row.get("sha"), f"{label} blob SHA invalid: {path}") == expected_blob, + f"{label} blob identity mismatch: {path}") + require(exact_int(row.get("size"), f"{label} blob size invalid: {path}") == expected_size, + f"{label} blob size mismatch: {path}") + + +def verify(event_name: str, event: dict, env: dict, api: GitHubAPI) -> dict: + number, event_head_sha, event_repo_id = event_snapshot(event_name, event, env) + pr = api.get(f"/repos/{REPOSITORY}/pulls/{number}") + snapshot = pr_snapshot(pr) + live_base_sha = snapshot[4] + live_head_sha = snapshot[7] + require(snapshot[5] == snapshot[8] == event_repo_id, "live PR repository differs from event repository") + require(live_base_sha == sha40(env.get("GITHUB_SHA"), "trusted workflow SHA missing"), + "live PR base differs from trusted workflow commit") + if event_head_sha is not None: + require(event_head_sha == live_head_sha, "stale event/live PR head mismatch") + require_event_pr_matches_live(event["pull_request"], pr) + + # The trusted base may advance when this verifier is integrated. It must + # remain descended from the original merge base, and the three affected + # base-tree entries must remain byte-identical to their recorded base blobs. + compare_merge_base(api, EXPECTED_MERGE_BASE, live_base_sha, require_base_ancestor=True) + compare_merge_base(api, EXPECTED_MERGE_BASE, live_head_sha, require_base_ancestor=True) + compare_merge_base(api, live_base_sha, live_head_sha) + + changed = changed_files(api, pr) + base_tree_sha = commit_tree(api, live_base_sha) + head_tree_sha = commit_tree(api, live_head_sha) + base_tree = tree_entries(api, base_tree_sha, "base") + head_tree = tree_entries(api, head_tree_sha, "head") + for path, identity in EXPECTED_FILES.items(): + verify_tree_path(base_tree, path, identity["base_blob"], identity["base_size"], "base") + verify_tree_path(head_tree, path, identity["head_blob"], identity["head_size"], "head") + + # Re-read the PR only after every compare, file-list and tree request. A + # mid-run change to the PR snapshot must not leave a stale PASS behind. + final_pr = api.get(f"/repos/{REPOSITORY}/pulls/{number}") + final_snapshot = pr_snapshot(final_pr) + require(final_snapshot == snapshot, "live PR snapshot changed during verification") + require(final_snapshot[5] == final_snapshot[8] == event_repo_id, + "final PR repository differs from event repository") + require(final_snapshot[4] == sha40(env.get("GITHUB_SHA"), "trusted workflow SHA missing"), + "final PR base differs from trusted workflow commit") + if event_head_sha is not None: + require(event_head_sha == final_snapshot[7], "final event/live PR head mismatch") + require_event_pr_matches_live(event["pull_request"], final_pr) + + return { + "disposition": "PASS_SCOPED_SOURCE_IDENTITY", + "repository": REPOSITORY, + "pull_request": PR_NUMBER, + "event": event_name, + "trusted_base_sha": final_snapshot[4], + "expected_merge_base": EXPECTED_MERGE_BASE, + "candidate_head_sha": final_snapshot[7], + "changed_paths": sorted(changed), + "scope": "exact source identity only; no review, security certification, proof or scientific acceptance", + } + + +def main() -> int: + try: + event_path = os.environ.get("GITHUB_EVENT_PATH", "") + require(bool(event_path), "event payload path missing") + with open(event_path, "rb") as stream: + event_raw = stream.read(MAX_RESPONSE_BYTES + 1) + require(len(event_raw) <= MAX_RESPONSE_BYTES, "event payload exceeds size limit") + event = strict_json(event_raw) + event_name = os.environ.get("GITHUB_EVENT_NAME", "") + api = GitHubAPI(os.environ.get("GH_TOKEN", "")) + result = verify(event_name, event, os.environ, api) + print(json.dumps(result, sort_keys=True)) + return 0 + except Exception as exc: + # The token and response bodies are never included in diagnostics. + print(f"FAIL: {type(exc).__name__}: {exc}", file=sys.stderr) + return 1 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/.github/workflows/contact-small-gap-trust.yml b/.github/workflows/contact-small-gap-trust.yml new file mode 100644 index 00000000..3b3fbebc --- /dev/null +++ b/.github/workflows/contact-small-gap-trust.yml @@ -0,0 +1,44 @@ +name: Contact small-gap candidate trust check +'on': + pull_request_target: + branches: [main] + paths: + - '.github/workflows/contact-small-gap.yml' + - 'reviews/contact_kernel_tail_20260925/SOURCE_FILES.json' + - 'reviews/contact_kernel_tail_20260925/run_validation.py' + types: [opened, reopened, synchronize, edited, ready_for_review] + workflow_dispatch: + inputs: + pr_number: + description: Exact candidate PR number (only 547 is admitted) + required: true + default: 547 + type: number +permissions: + contents: read + pull-requests: read +jobs: + contact-small-gap-trust: + name: contact-small-gap-trust + runs-on: ubuntu-24.04 + timeout-minutes: 5 + permissions: + contents: read + pull-requests: read + steps: + - name: Check out only the trusted base-branch verifier + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 + with: + # pull_request_target GITHUB_SHA is the trusted base-branch commit. + # Never check out or execute the submitted PR head. + ref: ${{ github.sha }} + persist-credentials: false + sparse-checkout: .github/scripts/verify_contact_small_gap_trust.py + sparse-checkout-cone-mode: false + - name: Verify the exact PR and Git tree identities through read-only API calls + env: + GH_TOKEN: ${{ github.token }} + PR_NUMBER: ${{ github.event.pull_request.number || inputs.pr_number }} + run: | + set -euo pipefail + python3 -B -I .github/scripts/verify_contact_small_gap_trust.py diff --git a/tests/test_contact_small_gap_trust.py b/tests/test_contact_small_gap_trust.py new file mode 100644 index 00000000..8a5c7805 --- /dev/null +++ b/tests/test_contact_small_gap_trust.py @@ -0,0 +1,552 @@ +"""No-network adverse controls for the trusted Math-#547 source verifier.""" +from __future__ import annotations + +import copy +import importlib.util +import json +from pathlib import Path +import sys +import unittest +import urllib.request + +ROOT = Path(__file__).resolve().parents[1] +SCRIPT = ROOT / ".github" / "scripts" / "verify_contact_small_gap_trust.py" +spec = importlib.util.spec_from_file_location("contact_small_gap_trust", SCRIPT) +trust = importlib.util.module_from_spec(spec) +spec.loader.exec_module(trust) + +REPO = trust.REPOSITORY +BASE = trust.EXPECTED_MERGE_BASE +HEAD = trust.EXPECTED_HEAD +BASE_TREE = "1" * 40 +HEAD_TREE = "2" * 40 + + +def repo_object(): + return {"full_name": REPO, "id": trust.EXPECTED_REPOSITORY_ID, "fork": False, "default_branch": "main"} + + +def tree_rows(side): + rows = {} + for path, identity in trust.EXPECTED_FILES.items(): + parts = path.split("/") + for index in range(1, len(parts)): + parent = "/".join(parts[:index]) + rows[parent] = {"path": parent, "type": "tree", "mode": "040000", "sha": "a" * 40} + blob = identity["base_blob"] if side == "base" else identity["head_blob"] + size = identity["base_size"] if side == "base" else identity["head_size"] + rows[path] = {"path": path, "type": "blob", "mode": "100644", "sha": blob, "size": size} + return list(rows.values()) + + +def make_pr(base_sha=BASE): + repo = repo_object() + return { + "number": trust.PR_NUMBER, + "state": "open", + "merged": False, + "changed_files": len(trust.EXPECTED_FILES), + "base": {"ref": "main", "sha": base_sha, "repo": copy.deepcopy(repo)}, + "head": {"ref": trust.HEAD_REF, "sha": HEAD, "repo": copy.deepcopy(repo)}, + } + + +def make_event(event_name="pull_request_target", base_sha=BASE): + event = {"repository": repo_object()} + if event_name == "pull_request_target": + event["pull_request"] = make_pr(base_sha) + else: + event["inputs"] = {"pr_number": str(trust.PR_NUMBER)} + return event + + +def make_env(event_name="pull_request_target", base_sha=BASE): + return { + "GITHUB_REPOSITORY": REPO, + "GITHUB_REF": "refs/heads/main", + "GITHUB_SHA": base_sha, + "PR_NUMBER": str(trust.PR_NUMBER), + } + + +class FakeAPI: + def __init__(self, base_sha=BASE): + self.pr = make_pr(base_sha) + self.files = [ + {"filename": path, "status": "modified", "sha": identity["head_blob"]} + for path, identity in trust.EXPECTED_FILES.items() + ] + self.base_tree = tree_rows("base") + self.head_tree = tree_rows("head") + self.base_truncated = False + self.head_truncated = False + self.routes = [] + self.error_route = None + self.pr_reads = 0 + self.final_pr_mutator = None + # Model a small commit DAG: BASE is the common ancestor of the pinned + # candidate HEAD and any explicitly supplied unrelated base advance. + self.descendant_shas = {BASE, HEAD, base_sha} + self.compare_overrides = {} + self.compare_routes = [] + self.compare_results = [] + + def compare_response(self, base_sha, head_sha, route): + if route in self.compare_overrides: + return copy.deepcopy(self.compare_overrides[route]) + if base_sha == head_sha: + return { + "status": "identical", + "ahead_by": 0, + "behind_by": 0, + "merge_base_commit": {"sha": base_sha}, + } + if base_sha == BASE and head_sha in self.descendant_shas: + return { + "status": "ahead", + "ahead_by": 1, + "behind_by": 0, + "merge_base_commit": {"sha": BASE}, + } + if base_sha in self.descendant_shas and head_sha in self.descendant_shas: + # The live base and candidate may be sibling descendants. Their + # pair can be diverged while retaining the frozen common ancestor. + return { + "status": "diverged", + "ahead_by": 1, + "behind_by": 1, + "merge_base_commit": {"sha": BASE}, + } + return { + "status": "diverged", + "ahead_by": 0, + "behind_by": 1, + "merge_base_commit": {"sha": "c" * 40}, + } + + def get(self, route): + self.routes.append(route) + if route == self.error_route: + raise trust.VerificationError("synthetic API error") + if route == f"/repos/{REPO}/pulls/{trust.PR_NUMBER}": + self.pr_reads += 1 + if self.pr_reads == 2 and self.final_pr_mutator is not None: + self.final_pr_mutator(self.pr) + return copy.deepcopy(self.pr) + if route.startswith(f"/repos/{REPO}/pulls/{trust.PR_NUMBER}/files?"): + if "page=1" in route: + return copy.deepcopy(self.files) + return [] + if route == f"/repos/{REPO}/commits/{self.pr['base']['sha']}": + return {"sha": self.pr["base"]["sha"], "commit": {"tree": {"sha": BASE_TREE}}} + if route == f"/repos/{REPO}/commits/{HEAD}": + return {"sha": HEAD, "commit": {"tree": {"sha": HEAD_TREE}}} + if route == f"/repos/{REPO}/git/trees/{BASE_TREE}?recursive=1": + return {"sha": BASE_TREE, "truncated": self.base_truncated, "tree": copy.deepcopy(self.base_tree)} + if route == f"/repos/{REPO}/git/trees/{HEAD_TREE}?recursive=1": + return {"sha": HEAD_TREE, "truncated": self.head_truncated, "tree": copy.deepcopy(self.head_tree)} + if "/compare/" in route: + pair = route.split("/compare/", 1)[1].split("?", 1)[0] + base_sha, separator, head_sha = pair.partition("...") + if not separator: + raise AssertionError("malformed compare route: " + route) + self.compare_routes.append((base_sha, head_sha)) + response = self.compare_response(base_sha, head_sha, route) + self.compare_results.append((base_sha, head_sha, copy.deepcopy(response))) + return response + raise AssertionError("unexpected API route: " + route) + + +class VerifierTests(unittest.TestCase): + def run_verify(self, event_name="pull_request_target", event=None, env=None, api=None): + api = api or FakeAPI() + event = event if event is not None else make_event(event_name) + env = env if env is not None else make_env(event_name) + return trust.verify(event_name, event, env, api) + + def test_pull_request_target_positive(self): + result = self.run_verify() + self.assertEqual(result["disposition"], "PASS_SCOPED_SOURCE_IDENTITY") + self.assertEqual(result["candidate_head_sha"], HEAD) + self.assertEqual(set(result["changed_paths"]), set(trust.EXPECTED_FILES)) + + def test_manual_dispatch_positive_after_unrelated_base_advance(self): + advanced = "b" * 40 + api = FakeAPI(base_sha=advanced) + result = self.run_verify( + "workflow_dispatch", make_event("workflow_dispatch", advanced), make_env("workflow_dispatch", advanced), api + ) + self.assertEqual(result["trusted_base_sha"], advanced) + self.assertEqual(api.compare_routes, [(BASE, advanced), (BASE, HEAD), (advanced, HEAD)]) + self.assertEqual( + [row[2]["merge_base_commit"]["sha"] for row in api.compare_results], + [BASE, BASE, BASE], + ) + self.assertEqual([row[2]["behind_by"] for row in api.compare_results[:2]], [0, 0]) + self.assertEqual([row[2]["status"] for row in api.compare_results[:2]], ["ahead", "ahead"]) + self.assertEqual(api.compare_results[2][2]["status"], "diverged") + + def test_wrong_merge_base_on_live_pair_rejected(self): + advanced = "b" * 40 + api = FakeAPI(base_sha=advanced) + route = f"/repos/{REPO}/compare/{advanced}...{HEAD}?per_page=1" + api.compare_overrides[route] = { + "status": "diverged", + "ahead_by": 1, + "behind_by": 1, + "merge_base_commit": {"sha": "d" * 40}, + } + with self.assertRaises(trust.VerificationError): + self.run_verify( + "workflow_dispatch", + make_event("workflow_dispatch", advanced), + make_env("workflow_dispatch", advanced), + api, + ) + + def test_non_descendant_live_base_rejected(self): + advanced = "b" * 40 + api = FakeAPI(base_sha=advanced) + route = f"/repos/{REPO}/compare/{BASE}...{advanced}?per_page=1" + api.compare_overrides[route] = { + "status": "diverged", + "ahead_by": 0, + "behind_by": 1, + "merge_base_commit": {"sha": "d" * 40}, + } + with self.assertRaises(trust.VerificationError): + self.run_verify( + "workflow_dispatch", + make_event("workflow_dispatch", advanced), + make_env("workflow_dispatch", advanced), + api, + ) + + def test_non_descendant_candidate_head_rejected(self): + api = FakeAPI() + route = f"/repos/{REPO}/compare/{BASE}...{HEAD}?per_page=1" + api.compare_overrides[route] = { + "status": "diverged", + "ahead_by": 0, + "behind_by": 1, + "merge_base_commit": {"sha": "d" * 40}, + } + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_positive_behind_by_rejected(self): + api = FakeAPI() + route = f"/repos/{REPO}/compare/{BASE}...{HEAD}?per_page=1" + api.compare_overrides[route] = { + "status": "ahead", + "ahead_by": 1, + "behind_by": 1, + "merge_base_commit": {"sha": BASE}, + } + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_diverged_ancestry_status_rejected(self): + api = FakeAPI() + route = f"/repos/{REPO}/compare/{BASE}...{HEAD}?per_page=1" + api.compare_overrides[route] = { + "status": "diverged", + "ahead_by": 1, + "behind_by": 0, + "merge_base_commit": {"sha": BASE}, + } + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_missing_or_malformed_compare_responses_rejected(self): + route = f"/repos/{REPO}/compare/{BASE}...{HEAD}?per_page=1" + cases = [ + None, + [], + {"status": "ahead", "ahead_by": 1, "behind_by": 0}, + {"status": "ahead", "behind_by": 0, "merge_base_commit": {"sha": "bad"}}, + # Keep the expected merge base so these cases isolate required-field + # validation rather than failing at the merge-base check first. + {"status": "ahead", "ahead_by": 1, "merge_base_commit": {"sha": BASE}}, + {"status": "ahead", "ahead_by": 1, "behind_by": "0", "merge_base_commit": {"sha": BASE}}, + {"status": "ahead", "ahead_by": 1, "behind_by": True, "merge_base_commit": {"sha": BASE}}, + {"ahead_by": 1, "behind_by": 0, "merge_base_commit": {"sha": BASE}}, + ] + for response in cases: + with self.subTest(response=response): + api = FakeAPI() + api.compare_overrides[route] = response + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_stale_event_head_rejected(self): + event = make_event() + event["pull_request"]["head"]["sha"] = "c" * 40 + with self.assertRaises(trust.VerificationError): + self.run_verify(event=event) + + def test_live_head_drift_rejected(self): + api = FakeAPI() + api.pr["head"]["sha"] = "c" * 40 + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_wrong_event_pr_rejected(self): + event = make_event() + event["pull_request"]["number"] = 548 + with self.assertRaises(trust.VerificationError): + self.run_verify(event=event) + + def test_wrong_live_pr_rejected(self): + api = FakeAPI() + api.pr["number"] = 548 + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_wrong_event_repository_rejected(self): + event = make_event() + event["repository"]["full_name"] = "attacker/Math-" + with self.assertRaises(trust.VerificationError): + self.run_verify(event=event) + + def test_wrong_base_ref_rejected(self): + api = FakeAPI() + api.pr["base"]["ref"] = "release" + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_wrong_head_ref_rejected(self): + api = FakeAPI() + api.pr["head"]["ref"] = "attacker-branch" + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_fork_head_rejected(self): + api = FakeAPI() + api.pr["head"]["repo"]["full_name"] = "attacker/Math-" + api.pr["head"]["repo"]["id"] = 1 + api.pr["head"]["repo"]["fork"] = True + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_wrong_trusted_ref_rejected(self): + env = make_env() + env["GITHUB_REF"] = "refs/heads/feature" + with self.assertRaises(trust.VerificationError): + self.run_verify(env=env) + + def test_extra_changed_path_rejected(self): + api = FakeAPI() + api.files.append({"filename": "extra.txt", "status": "modified", "sha": "d" * 40}) + api.pr["changed_files"] += 1 + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_missing_changed_path_rejected(self): + api = FakeAPI() + api.files.pop() + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_duplicate_changed_path_rejected(self): + api = FakeAPI() + api.files.append(copy.deepcopy(api.files[0])) + api.pr["changed_files"] += 1 + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_renamed_changed_path_rejected(self): + api = FakeAPI() + api.files[0]["previous_filename"] = "old-name.yml" + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_truncated_base_tree_rejected(self): + api = FakeAPI() + api.base_truncated = True + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_truncated_head_tree_rejected(self): + api = FakeAPI() + api.head_truncated = True + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_missing_tree_path_rejected(self): + api = FakeAPI() + api.head_tree = [row for row in api.head_tree if row.get("path") != ".github/workflows/contact-small-gap.yml"] + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_symlink_mode_rejected(self): + api = FakeAPI() + for row in api.head_tree: + if row.get("path") == ".github/workflows/contact-small-gap.yml": + row["mode"] = "120000" + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_tree_mode_at_file_path_rejected(self): + api = FakeAPI() + for row in api.head_tree: + if row.get("path") == ".github/workflows/contact-small-gap.yml": + row["type"] = "tree" + row["mode"] = "040000" + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_symlink_parent_rejected(self): + api = FakeAPI() + for row in api.head_tree: + if row.get("path") == ".github/workflows": + row["type"] = "blob" + row["mode"] = "120000" + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_wrong_candidate_blob_rejected(self): + api = FakeAPI() + for row in api.head_tree: + if row.get("path") == ".github/workflows/contact-small-gap.yml": + row["sha"] = "d" * 40 + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_wrong_candidate_size_rejected(self): + api = FakeAPI() + for row in api.head_tree: + if row.get("path") == ".github/workflows/contact-small-gap.yml": + row["size"] += 1 + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_wrong_base_blob_rejected(self): + api = FakeAPI() + for row in api.base_tree: + if row.get("path") == ".github/workflows/contact-small-gap.yml": + row["sha"] = "d" * 40 + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_api_error_rejected(self): + api = FakeAPI() + api.error_route = f"/repos/{REPO}/pulls/{trust.PR_NUMBER}" + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_manual_dispatch_wrong_pr_rejected(self): + event = make_event("workflow_dispatch") + event["inputs"]["pr_number"] = "548" + with self.assertRaises(trust.VerificationError): + self.run_verify("workflow_dispatch", event, make_env("workflow_dispatch"), FakeAPI()) + + def test_event_live_changed_file_count_mismatch_rejected(self): + event = make_event() + event["pull_request"]["changed_files"] = 4 + with self.assertRaises(trust.VerificationError): + self.run_verify(event=event) + + def test_event_live_state_mismatch_rejected(self): + event = make_event() + event["pull_request"]["state"] = "closed" + with self.assertRaises(trust.VerificationError): + self.run_verify(event=event) + + def test_base_source_drift_after_base_advance_rejected(self): + api = FakeAPI(base_sha="b" * 40) + for row in api.base_tree: + if row.get("path") == ".github/workflows/contact-small-gap.yml": + row["sha"] = "d" * 40 + with self.assertRaises(trust.VerificationError): + self.run_verify("workflow_dispatch", make_event("workflow_dispatch", "b" * 40), make_env("workflow_dispatch", "b" * 40), api) + + def test_changed_file_api_blob_mismatch_rejected(self): + api = FakeAPI() + api.files[0]["sha"] = "d" * 40 + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + + def test_duplicate_json_key_rejected(self): + with self.assertRaises(trust.VerificationError): + trust.strict_json(b'{"a":1,"a":2}') + + def test_nonfinite_json_rejected(self): + with self.assertRaises(trust.VerificationError): + trust.strict_json(b'{"a":NaN}') + + def test_malformed_json_rejected(self): + with self.assertRaises(trust.VerificationError): + trust.strict_json(b'{') + + def test_no_redirect_handler_rejects_redirect(self): + handler = trust.NoRedirect() + request = urllib.request.Request("https://api.github.com/repos/example/repo") + with self.assertRaises(trust.VerificationError): + handler.redirect_request(request, None, 302, "Found", {}, "https://example.org") + + def test_wrong_repository_id_rejected_at_all_event_and_live_surfaces(self): + cases = { + "event": lambda event, api: event["repository"].update(id=123456789), + "event base": lambda event, api: event["pull_request"]["base"]["repo"].update(id=123456789), + "event head": lambda event, api: event["pull_request"]["head"]["repo"].update(id=123456789), + "live base": lambda event, api: api.pr["base"]["repo"].update(id=123456789), + "live head": lambda event, api: api.pr["head"]["repo"].update(id=123456789), + } + for label, mutate in cases.items(): + with self.subTest(surface=label): + event = make_event() + api = FakeAPI() + mutate(event, api) + with self.assertRaises(trust.VerificationError): + self.run_verify(event=event, api=api) + + def assert_final_pr_mutation_rejected(self, mutate): + api = FakeAPI() + api.final_pr_mutator = mutate + with self.assertRaises(trust.VerificationError): + self.run_verify(api=api) + self.assertEqual(api.pr_reads, 2) + self.assertEqual(api.routes[-1], f"/repos/{REPO}/pulls/{trust.PR_NUMBER}") + + def test_final_pr_head_change_rejected(self): + self.assert_final_pr_mutation_rejected(lambda pr: pr["head"].update(sha="c" * 40)) + + def test_final_pr_base_change_rejected(self): + self.assert_final_pr_mutation_rejected(lambda pr: pr["base"].update(sha="b" * 40)) + + def test_final_pr_state_change_rejected(self): + self.assert_final_pr_mutation_rejected(lambda pr: pr.update(state="closed")) + + def test_final_pr_merged_change_rejected(self): + self.assert_final_pr_mutation_rejected(lambda pr: pr.update(merged=True)) + + def test_final_pr_changed_file_count_change_rejected(self): + self.assert_final_pr_mutation_rejected(lambda pr: pr.update(changed_files=4)) + + def test_final_pr_snapshot_read_is_last(self): + api = FakeAPI() + self.run_verify(api=api) + self.assertEqual(api.pr_reads, 2) + self.assertEqual(api.routes[-1], f"/repos/{REPO}/pulls/{trust.PR_NUMBER}") + + def test_api_requests_are_get_only(self): + class Response: + status = 200 + def __enter__(self): return self + def __exit__(self, *args): return False + def read(self, size): return b'{"ok":true}' + class Opener: + def __init__(self): self.methods = [] + def open(self, request, timeout): + self.methods.append(request.get_method()) + return Response() + opener = Opener() + api = trust.GitHubAPI("test-token", opener=opener) + self.assertEqual(api.get(f"/repos/{REPO}/pulls/547"), {"ok": True}) + self.assertEqual(api.get(f"/repos/{REPO}/compare/{BASE}...{HEAD}?per_page=1"), {"ok": True}) + self.assertEqual(opener.methods, ["GET", "GET"]) + + +if __name__ == "__main__": + unittest.main(verbosity=2)