Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/reusable-build-smoke-gate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ jobs:

- name: Generate trust pack artifact
if: ${{ inputs.generate_trust_pack }}
env:
# Checkout of a PR may be a synthetic merge; preserve the actual source head.
WINEBOT_SOURCE_HEAD_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
run: scripts/ci/generate-trust-pack.sh "${{ inputs.trust_pack_dir }}"

- name: Upload trust pack
Expand Down
21 changes: 16 additions & 5 deletions scripts/ci/generate-trust-pack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@
set -euo pipefail

OUT_DIR="${1:-artifacts/trust-pack}"
mkdir -p "$OUT_DIR"

timestamp="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
sha="$(git rev-parse --short HEAD 2>/dev/null || echo unknown)"
# The checkout can be a synthetic PR merge commit rather than the source head.
# Record both IDs with unambiguous names; refuse provenance-free evidence.
checkout_sha="$(git rev-parse HEAD 2>/dev/null || true)"
source_sha="${WINEBOT_SOURCE_HEAD_SHA:-$checkout_sha}"
for candidate in "$checkout_sha" "$source_sha"; do
if [[ ! "$candidate" =~ ^[0-9a-f]{40}$ ]]; then
echo "Trust pack requires valid full checkout/source Git SHAs" >&2
exit 1
fi
done
branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo unknown)"
timestamp="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
mkdir -p "$OUT_DIR"

cat > "${OUT_DIR}/summary.md" <<EOF
# WineBot Trust Pack

- Generated: ${timestamp}
- Branch: ${branch}
- Commit: ${sha}
- Checkout ref: ${branch}
- Tested checkout commit: ${checkout_sha}
- Source head commit: ${source_sha}
- Note: These may differ when GitHub Actions checks out a pull-request merge candidate.

## Included Artifacts

Expand Down
3 changes: 2 additions & 1 deletion scripts/ci/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ pytest \
tests/test_diag_bundle.py \
tests/test_auto_view.py \
tests/test_ui_dashboard.py \
tests/test_ui_accessibility.py
tests/test_ui_accessibility.py \
tests/test_ci_trust_pack_provenance.py
93 changes: 93 additions & 0 deletions tests/test_ci_trust_pack_provenance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""Guard exact Git source-versus-checkout provenance in public trust packs."""

from __future__ import annotations

import os
import subprocess
from pathlib import Path

import pytest

GENERATOR = Path(__file__).resolve().parents[1] / "scripts/ci/generate-trust-pack.sh"
WORKFLOW = (
Path(__file__).resolve().parents[1] / ".github/workflows/reusable-build-smoke-gate.yml"
)
CHECKOUT = "b" * 40
SOURCE = "a" * 40


@pytest.fixture
def workspace(tmp_path: Path) -> Path:
bin_dir = tmp_path / "bin"
bin_dir.mkdir()
fake_git = bin_dir / "git"
fake_git.write_text(
"#!/bin/sh\n"
'if [ "$1" = "rev-parse" ] && [ "$2" = "HEAD" ]; then\n'
' printf "%s\\n" "$FAKE_CHECKOUT_SHA"\n'
'elif [ "$1" = "rev-parse" ] && [ "$2" = "--abbrev-ref" ]; then\n'
' printf "HEAD\\n"\n'
"else\n"
" exit 7\n"
"fi\n",
encoding="utf-8",
)
fake_git.chmod(0o755)
return tmp_path


def generate(workspace: Path, source: str | None = SOURCE, checkout: str = CHECKOUT):
env = {
**os.environ,
"PATH": f"{workspace / 'bin'}:{os.environ.get('PATH', '')}",
"FAKE_CHECKOUT_SHA": checkout,
}
if source is None:
env.pop("WINEBOT_SOURCE_HEAD_SHA", None)
else:
env["WINEBOT_SOURCE_HEAD_SHA"] = source
output = workspace / "pack"
result = subprocess.run(
["bash", str(GENERATOR), str(output)],
cwd=workspace,
env=env,
text=True,
capture_output=True,
check=False,
timeout=30,
)
return result, output


def test_merged_checkout_not_misrepresented_as_source(workspace: Path) -> None:
result, out = generate(workspace)
assert result.returncode == 0, result.stderr
summary = (out / "summary.md").read_text(encoding="utf-8")
assert f"- Tested checkout commit: {CHECKOUT}" in summary
assert f"- Source head commit: {SOURCE}" in summary
assert "- Commit: " not in summary
assert (out / "inventory.json").is_file()


def test_direct_checkout_defaults_source_to_tested_commit(workspace: Path) -> None:
result, out = generate(workspace, source=None)
assert result.returncode == 0, result.stderr
summary = (out / "summary.md").read_text(encoding="utf-8")
assert f"- Source head commit: {CHECKOUT}" in summary


@pytest.mark.parametrize(
("source", "checkout"),
[("not-a-commit", CHECKOUT), (SOURCE, "invalid-checkout")],
)
def test_invalid_identity_fails_before_publishing(
workspace: Path, source: str, checkout: str
) -> None:
result, out = generate(workspace, source=source, checkout=checkout)
assert result.returncode != 0
assert not (out / "summary.md").exists()


def test_reusable_workflow_supplies_pr_source_head() -> None:
workflow = WORKFLOW.read_text(encoding="utf-8")
assert "WINEBOT_SOURCE_HEAD_SHA: ${{ github.event.pull_request.head.sha || github.sha }}" in workflow
Loading