Skip to content
Merged
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
6 changes: 3 additions & 3 deletions .github/workflows/ci-linux-images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ jobs:

mirrors:
needs: [tests, docker-conformance]
if: github.event_name == 'workflow_dispatch' && github.repository == 'emulebb/emulebb-build' && github.ref == 'refs/heads/main'
if: (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.repository == 'emulebb/emulebb-build' && github.ref == 'refs/heads/main'
runs-on: ubuntu-24.04
timeout-minutes: 45
permissions:
Expand Down Expand Up @@ -161,7 +161,7 @@ jobs:

native:
needs: mirrors
if: github.event_name == 'workflow_dispatch' && github.repository == 'emulebb/emulebb-build' && github.ref == 'refs/heads/main'
if: (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.repository == 'emulebb/emulebb-build' && github.ref == 'refs/heads/main'
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -215,7 +215,7 @@ jobs:

verify:
needs: [mirrors, native]
if: github.event_name == 'workflow_dispatch' && github.repository == 'emulebb/emulebb-build' && github.ref == 'refs/heads/main'
if: (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.repository == 'emulebb/emulebb-build' && github.ref == 'refs/heads/main'
runs-on: ubuntu-24.04
timeout-minutes: 15
permissions:
Expand Down
2 changes: 1 addition & 1 deletion emule_workspace/ci_linux_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def context() -> dict:
sha = os.environ.get("GITHUB_SHA", "")
if (os.environ.get("GITHUB_ACTIONS") != "true" or os.environ.get("GITHUB_REPOSITORY") != "emulebb/emulebb-build"
or os.environ.get("GITHUB_REF") != "refs/heads/main"
or os.environ.get("GITHUB_EVENT_NAME") != "workflow_dispatch"
or os.environ.get("GITHUB_EVENT_NAME") not in {"push", "workflow_dispatch"}
or not re.fullmatch(r"[0-9a-f]{40}", sha)
or os.environ.get("GITHUB_WORKFLOW_SHA") != sha):
raise RuntimeError("CI images require the trusted main workflow and exact Build source")
Expand Down
37 changes: 33 additions & 4 deletions tests/test_ci_linux_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,44 @@ def test_ghcr_rust_cross_is_not_misrepresented_as_a_hub_mirror():
assert not any("rust-cross" in source for source in images.SOURCES.values())


def test_trusted_context_rejects_pr_before_any_git_or_registry_operation(monkeypatch):
@pytest.mark.parametrize(("event", "ref", "repository"), [
("pull_request", "refs/pull/123/merge", "emulebb/emulebb-build"),
("pull_request_target", "refs/heads/main", "emulebb/emulebb-build"),
("push", "refs/heads/topic", "emulebb/emulebb-build"),
("push", "refs/tags/main", "emulebb/emulebb-build"),
("push", "refs/heads/main", "fork/emulebb-build"),
])
def test_untrusted_context_fails_before_any_git_or_registry_operation(monkeypatch, event, ref, repository):
monkeypatch.setenv("GITHUB_ACTIONS", "true")
monkeypatch.setenv("GITHUB_REPOSITORY", "emulebb/emulebb-build")
monkeypatch.setenv("GITHUB_REF", "refs/pull/123/merge")
monkeypatch.setenv("GITHUB_REPOSITORY", repository)
monkeypatch.setenv("GITHUB_REF", ref)
monkeypatch.setenv("GITHUB_EVENT_NAME", event)
monkeypatch.setenv("GITHUB_SHA", "a" * 40)
monkeypatch.setenv("GITHUB_WORKFLOW_SHA", "a" * 40)
monkeypatch.setattr(images.subprocess, "check_output", lambda *a, **k: pytest.fail("Untrusted source ran a command"))
with pytest.raises(RuntimeError, match="trusted main"):
images.context()


@pytest.mark.parametrize("event", ["push", "workflow_dispatch"])
def test_trusted_context_binds_clean_main_workflow_source(monkeypatch, event):
from emule_workspace import python_runtime

sha = "a" * 40
for name, value in {
"GITHUB_ACTIONS": "true", "GITHUB_REPOSITORY": "emulebb/emulebb-build",
"GITHUB_REF": "refs/heads/main", "GITHUB_EVENT_NAME": event,
"GITHUB_SHA": sha, "GITHUB_WORKFLOW_SHA": sha, "GITHUB_RUN_ID": "42", "GITHUB_RUN_ATTEMPT": "1",
}.items():
monkeypatch.setenv(name, value)
monkeypatch.setattr(python_runtime, "python_version", lambda: "3.15.2")
monkeypatch.setattr(images.subprocess, "check_output", lambda command, **_: sha if command[-1] == "HEAD" else "")
assert images.context()["sourceSha"] == sha
monkeypatch.setenv("GITHUB_WORKFLOW_SHA", "b" * 40)
with pytest.raises(RuntimeError, match="exact Build source"):
images.context()


def test_private_package_is_actionable_failure_without_credential_changes(monkeypatch):
def unavailable(*args, **kwargs):
raise urllib.error.HTTPError("https://ghcr.io/token", 403, "synthetic anonymous denial", {}, None)
Expand Down Expand Up @@ -352,7 +381,7 @@ def test_producer_permissions_and_native_matrix_have_no_untrusted_publication_pa
artifact = next(step for step in job["steps"] if step.get("uses", "").startswith("actions/upload-artifact@"))
assert artifact["if"] == "always()"
continue
assert "github.event_name == 'workflow_dispatch'" in job["if"]
assert "(github.event_name == 'push' || github.event_name == 'workflow_dispatch')" in job["if"]
assert "github.ref == 'refs/heads/main'" in job["if"]
assert "github.repository == 'emulebb/emulebb-build'" in job["if"]
assert job["permissions"]["packages"] == "write"
Expand Down