|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +from dataclasses import dataclass |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +import political_event_tracking_research.trusted_workflow_identity_raw as module |
| 9 | +from political_event_tracking_research.trusted_workflow_identity_raw import ( |
| 10 | + IDENTITY_VERSION, |
| 11 | + MAX_IDENTITY_BYTES, |
| 12 | + TRUSTED_REPOSITORY, |
| 13 | + TRUSTED_WORKFLOW_PATH, |
| 14 | + TRUSTED_WORKFLOW_REF, |
| 15 | + TrustedWorkflowIdentityError, |
| 16 | + build_trusted_workflow_identity_bytes, |
| 17 | + serialize_trusted_workflow_identity, |
| 18 | + validate_trusted_workflow_identity_bytes, |
| 19 | + validate_trusted_workflow_ref, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +SHA = "a" * 40 |
| 24 | + |
| 25 | + |
| 26 | +def wire() -> dict[str, str]: |
| 27 | + return { |
| 28 | + "identity_version": IDENTITY_VERSION, |
| 29 | + "repository": TRUSTED_REPOSITORY, |
| 30 | + "workflow_path": TRUSTED_WORKFLOW_PATH, |
| 31 | + "workflow_ref": TRUSTED_WORKFLOW_REF, |
| 32 | + "reviewed_workflow_sha": SHA, |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | +def test_fixed_identity_bytes_are_deterministic_and_revalidated() -> None: |
| 37 | + raw = build_trusted_workflow_identity_bytes(SHA) |
| 38 | + assert validate_trusted_workflow_identity_bytes(raw) == SHA |
| 39 | + assert serialize_trusted_workflow_identity(dict(reversed(list(wire().items())))) == raw |
| 40 | + assert validate_trusted_workflow_ref(TRUSTED_WORKFLOW_REF) == TRUSTED_WORKFLOW_REF |
| 41 | + |
| 42 | + |
| 43 | +def test_returned_plain_values_are_not_trust_objects() -> None: |
| 44 | + value = module.parse_trusted_workflow_identity_bytes(build_trusted_workflow_identity_bytes(SHA)) |
| 45 | + value["repository"] = "QuantStrategyLab/Other" |
| 46 | + with pytest.raises(TrustedWorkflowIdentityError, match="identity_mismatch"): |
| 47 | + serialize_trusted_workflow_identity(value) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize("field", ["repository", "workflow_path", "workflow_ref"]) |
| 51 | +def test_each_identity_field_is_rechecked(field: str) -> None: |
| 52 | + value = wire() |
| 53 | + value[field] = "wrong" |
| 54 | + with pytest.raises(TrustedWorkflowIdentityError, match="identity_mismatch"): |
| 55 | + serialize_trusted_workflow_identity(value) |
| 56 | + |
| 57 | + |
| 58 | +def test_reviewed_sha_is_explicit_runtime_evidence() -> None: |
| 59 | + value = wire() |
| 60 | + value["reviewed_workflow_sha"] = "b" * 40 |
| 61 | + assert serialize_trusted_workflow_identity(value) != serialize_trusted_workflow_identity(wire()) |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize("mutation", ["unknown", "missing", "duplicate", "noncanonical", "wrong_version"]) |
| 65 | +def test_wire_shape_and_canonical_bytes_fail_closed(mutation: str) -> None: |
| 66 | + value = wire() |
| 67 | + if mutation == "unknown": |
| 68 | + value["debug"] = "unexpected" # type: ignore[typeddict-unknown-key] |
| 69 | + raw = json.dumps(value, sort_keys=True, separators=(",", ":")).encode() |
| 70 | + elif mutation == "missing": |
| 71 | + del value["workflow_path"] |
| 72 | + raw = json.dumps(value, sort_keys=True, separators=(",", ":")).encode() |
| 73 | + elif mutation == "duplicate": |
| 74 | + raw = json.dumps(value, sort_keys=True, separators=(",", ":")).replace( |
| 75 | + '"workflow_path":"' + TRUSTED_WORKFLOW_PATH + '"', |
| 76 | + '"workflow_path":"' + TRUSTED_WORKFLOW_PATH + '","workflow_path":"' + TRUSTED_WORKFLOW_PATH + '"', |
| 77 | + ).encode() |
| 78 | + elif mutation == "noncanonical": |
| 79 | + raw = json.dumps(value).encode() |
| 80 | + else: |
| 81 | + value["identity_version"] = "future" |
| 82 | + raw = json.dumps(value, sort_keys=True, separators=(",", ":")).encode() |
| 83 | + with pytest.raises(TrustedWorkflowIdentityError): |
| 84 | + validate_trusted_workflow_identity_bytes(raw) |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.parametrize( |
| 88 | + "workflow_ref", |
| 89 | + [ |
| 90 | + "QuantStrategyLab/Other/.github/workflows/pert_weekly_period_lock_harness.yml@refs/heads/main", |
| 91 | + TRUSTED_WORKFLOW_REF.replace(".github/workflows/", ".github//workflows/"), |
| 92 | + TRUSTED_WORKFLOW_REF.replace("refs/heads/main", "refs/tags/main"), |
| 93 | + TRUSTED_WORKFLOW_REF + "\n", |
| 94 | + "QuantStrategyLab/PoliticalEventTrackingResearch/.github/workflows/other.yml@refs/heads/main", |
| 95 | + ], |
| 96 | +) |
| 97 | +def test_wrong_workflow_refs_are_sanitized(workflow_ref: str) -> None: |
| 98 | + with pytest.raises(TrustedWorkflowIdentityError, match="identity_mismatch"): |
| 99 | + validate_trusted_workflow_ref(workflow_ref) |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.parametrize("sha", ["a" * 39, "A" * 40, "g" * 40, True, 1, "refs/heads/main"]) |
| 103 | +def test_reviewed_sha_requires_lowercase_full_hex(sha: object) -> None: |
| 104 | + with pytest.raises(TrustedWorkflowIdentityError, match="reviewed_workflow_sha_invalid"): |
| 105 | + build_trusted_workflow_identity_bytes(sha) |
| 106 | + |
| 107 | + |
| 108 | +def test_oversized_and_non_ascii_wire_fail_closed() -> None: |
| 109 | + with pytest.raises(TrustedWorkflowIdentityError, match="identity_wire_oversized"): |
| 110 | + validate_trusted_workflow_identity_bytes(b"{" + b"a" * MAX_IDENTITY_BYTES) |
| 111 | + value = wire() |
| 112 | + value["repository"] = "QuantStrategyLab/PoliticalEventTrackingResearch\N{SNOWMAN}" |
| 113 | + with pytest.raises(TrustedWorkflowIdentityError, match="identity_mismatch"): |
| 114 | + serialize_trusted_workflow_identity(value) |
| 115 | + |
| 116 | + |
| 117 | +@dataclass |
| 118 | +class ForgedObject: |
| 119 | + repository: str = TRUSTED_REPOSITORY |
| 120 | + workflow_path: str = TRUSTED_WORKFLOW_PATH |
| 121 | + workflow_ref: str = TRUSTED_WORKFLOW_REF |
| 122 | + reviewed_workflow_sha: str = SHA |
| 123 | + |
| 124 | + |
| 125 | +def test_forged_objects_and_attribute_failures_are_sanitized() -> None: |
| 126 | + with pytest.raises(TrustedWorkflowIdentityError, match="identity_wire_invalid"): |
| 127 | + serialize_trusted_workflow_identity(ForgedObject()) |
| 128 | + |
| 129 | + class BrokenMapping(dict[str, str]): |
| 130 | + def items(self): # type: ignore[no-untyped-def] |
| 131 | + raise AttributeError("untrusted attribute") |
| 132 | + |
| 133 | + with pytest.raises(TrustedWorkflowIdentityError, match="identity_wire_invalid"): |
| 134 | + serialize_trusted_workflow_identity(BrokenMapping()) |
| 135 | + |
| 136 | + |
| 137 | +def test_unexpected_runtime_error_is_not_broad_caught(monkeypatch: pytest.MonkeyPatch) -> None: |
| 138 | + class BrokenMapping(dict[str, str]): |
| 139 | + def items(self): # type: ignore[no-untyped-def] |
| 140 | + raise RuntimeError("programming failure") |
| 141 | + |
| 142 | + with pytest.raises(RuntimeError, match="programming failure"): |
| 143 | + serialize_trusted_workflow_identity(BrokenMapping()) |
| 144 | + def fail_dumps(*_: object, **__: object) -> str: |
| 145 | + raise RuntimeError("programming failure") |
| 146 | + |
| 147 | + monkeypatch.setattr(module.json, "dumps", fail_dumps) |
| 148 | + with pytest.raises(RuntimeError, match="programming failure"): |
| 149 | + serialize_trusted_workflow_identity(wire()) |
0 commit comments