From 68fdd69dcdd8fba217d1ba984325a6a8642c8939 Mon Sep 17 00:00:00 2001 From: luckyPipewrench Date: Fri, 24 Jul 2026 19:47:47 -0400 Subject: [PATCH 1/3] fix: restore v1 signing-projection parity with the Go emitter The v1 canonical projection omitted nine fields the emitter signs, so every receipt produced by a current Pipelock failed signature verification. decision_phase is emitted on ordinary proxy decisions, which made the break total rather than partial. Adds decision_phase, defer_id, resolution_policy, resolution_source, session_id, session_id_original, run_nonce, key_transition and session_control in the emitter's declaration order, with the nested ordering and omitempty semantics the signing projection uses. Ports the bound session-open genesis validation so chains anchored with the g1 prefix verify, and tightens v1 shape checking: malformed session_control, out-of-range chain_seq, unknown signed fields, malformed nested objects, misplaced key_transition markers, and restart opens not bound to the prior tail were all accepted with a valid signature and are now rejected. Adds a captured chain from a real 3.2.0 binary as a regression fixture covering session open, heartbeat, close, evidence receipts, transcript root and checkpoint records. Parity is now proven against genuine emitter output rather than hand-written data that can drift alongside the code. The projection's source-of-truth comment pointed at the wrong file, which is the likely reason this drift went unnoticed. It now names the frozen signing projection. --- pipelock_verify/_canonical.py | 159 ++++++- pipelock_verify/_verify.py | 609 ++++++++++++++++++++++++- tests/fixtures/v3_2_0_live_chain.jsonl | 19 + tests/fixtures/v3_2_0_live_chain.pub | 1 + tests/test_conformance.py | 22 + tests/test_field_drift.py | 299 +++++++++++- tests/test_regressions.py | 432 +++++++++++++++++- 7 files changed, 1521 insertions(+), 20 deletions(-) create mode 100644 tests/fixtures/v3_2_0_live_chain.jsonl create mode 100644 tests/fixtures/v3_2_0_live_chain.pub diff --git a/pipelock_verify/_canonical.py b/pipelock_verify/_canonical.py index a94c9d2..8c149d0 100644 --- a/pipelock_verify/_canonical.py +++ b/pipelock_verify/_canonical.py @@ -27,15 +27,16 @@ from typing import Any # Field specs are (json_name, has_omitempty, nested_kind). nested_kind is one -# of None, "action_record", "redaction", "shield", or "taint_source" and tells -# the orderer to recurse into a nested object (or, for "taint_source", into -# each element of a nested array) so the nested object's keys are reordered to -# the Go struct order too. Go re-marshals nested structs in declaration order, -# so a verifier that leaves nested object keys in input order recomputes a -# different signing hash whenever the input keys are not already Go-ordered. +# of None, "action_record", "redaction", "shield", "taint_source", +# "key_transition", "session_control", or a session-control payload kind and +# tells the orderer to recurse into a nested object (or, for "taint_source", +# into each element of a nested array) so the nested object's keys are reordered +# to the Go struct order too. Go re-marshals nested structs in declaration +# order, so a verifier that leaves nested object keys in input order recomputes +# a different signing hash whenever the input keys are not already Go-ordered. # # ActionRecord fields in Go struct-tag order. Source of truth: -# https://github.com/luckyPipewrench/pipelock/blob/main/internal/receipt/action.go +# https://github.com/luckyPipewrench/pipelock/blob/main/internal/receipt/canonical.go # The list MUST match that struct's field set, declaration order, and omitempty # semantics EXACTLY — including parent_action_id, the taint block, the contract # block, severity, redaction, and shield. Any omitted or reordered field breaks @@ -57,6 +58,12 @@ ("reversibility", False, None), ("policy_hash", False, None), ("verdict", False, None), + ("decision_phase", True, None), + ("defer_id", True, None), + ("resolution_policy", True, None), + ("resolution_source", True, None), + ("session_id", True, None), + ("session_id_original", True, None), ("session_taint_level", True, None), ("session_contaminated", True, None), ("recent_taint_sources", True, "taint_source"), @@ -84,6 +91,9 @@ ("request_id", True, None), ("chain_prev_hash", False, None), ("chain_seq", False, None), + ("run_nonce", True, None), + ("key_transition", True, "key_transition"), + ("session_control", True, "session_control"), ("venue", True, None), ("jurisdiction", True, None), ("rulebook_id", True, None), @@ -141,11 +151,115 @@ ("match_reason", True, None), ] +# KeyTransition fields, Go struct order (receipt.canonicalKeyTransitionV1). +_KEY_TRANSITION_FIELDS: list[tuple[str, bool, str | None]] = [ + ("prior_signer_key", False, None), + ("prior_chain_seq", False, None), + ("prior_chain_hash", False, None), +] + +# SessionControl fields, Go struct order (receipt.canonicalSessionControlV1). +_SESSION_CONTROL_FIELDS: list[tuple[str, bool, str | None]] = [ + ("kind", False, None), + ("open", True, "session_open"), + ("heartbeat", True, "session_heartbeat"), + ("close", True, "session_close"), +] + +# SessionOpen fields, Go struct order (receipt.sessionOpenCanonicalV1). +_SESSION_OPEN_FIELDS: list[tuple[str, bool, str | None]] = [ + ("run_nonce", False, None), + ("open_nonce", False, None), + ("recorder_session", False, None), + ("policy_hash", False, None), + ("signer_key_epoch", False, None), + ("heartbeat_seconds", False, None), + ("chain_open_seq", False, None), + ("prior_chain_head", True, None), + ("prior_chain_seq", True, None), + ("genesis_hash", True, None), + ("genesis_anchor_head", True, None), + ("genesis_anchor_log", True, None), + ("posture_capsule_sha256", True, None), + ("posture_signer_key_id", True, None), + ("containment_nonce", True, None), + ("contained_uid", True, None), +] + +# SessionHeartbeat fields, Go struct order (receipt.sessionHeartbeatCanonicalV1). +_SESSION_HEARTBEAT_FIELDS: list[tuple[str, bool, str | None]] = [ + ("run_nonce", False, None), + ("open_nonce", False, None), + ("beat", False, None), + ("chain_head", False, None), + ("chain_seq_head", False, None), + ("heartbeat_time", False, None), + ("fsync_errors_gated", False, None), + ("durability_blocks", False, None), +] + +# SessionClose fields, Go struct order (receipt.sessionCloseCanonicalV1). +_SESSION_CLOSE_FIELDS: list[tuple[str, bool, str | None]] = [ + ("run_nonce", False, None), + ("open_nonce", False, None), + ("final_seq", False, None), + ("root_hash", False, None), + ("receipt_count", False, None), + ("close_reason", False, None), + ("fsync_errors_gated", False, None), + ("durability_blocks", False, None), +] + # Maps a nested_kind tag to its field list (object-valued nests only). _NESTED_OBJECT_FIELDS: dict[str, list[tuple[str, bool, str | None]]] = { "action_record": _ACTION_RECORD_FIELDS, "redaction": _REDACTION_FIELDS, "shield": _SHIELD_FIELDS, + "key_transition": _KEY_TRANSITION_FIELDS, + "session_control": _SESSION_CONTROL_FIELDS, + "session_open": _SESSION_OPEN_FIELDS, + "session_heartbeat": _SESSION_HEARTBEAT_FIELDS, + "session_close": _SESSION_CLOSE_FIELDS, +} + +_NESTED_REQUIRED_ZERO_VALUES: dict[str, dict[str, Any]] = { + "key_transition": { + "prior_signer_key": "", + "prior_chain_seq": 0, + "prior_chain_hash": "", + }, + "session_control": { + "kind": "", + }, + "session_open": { + "run_nonce": "", + "open_nonce": "", + "recorder_session": "", + "policy_hash": "", + "signer_key_epoch": "", + "heartbeat_seconds": 0, + "chain_open_seq": 0, + }, + "session_heartbeat": { + "run_nonce": "", + "open_nonce": "", + "beat": 0, + "chain_head": "", + "chain_seq_head": 0, + "heartbeat_time": "", + "fsync_errors_gated": 0, + "durability_blocks": 0, + }, + "session_close": { + "run_nonce": "", + "open_nonce": "", + "final_seq": 0, + "root_hash": "", + "receipt_count": 0, + "close_reason": "", + "fsync_errors_gated": 0, + "durability_blocks": 0, + }, } @@ -196,12 +310,17 @@ def _normalize_maps(value: Any) -> Any: def _order_object( obj: dict[str, Any], fields: list[tuple[str, bool, str | None]], + *, + nested_kind: str | None = None, + emit_missing_required: bool = False, ) -> dict[str, Any]: """Return a new dict with ``obj``'s keys in Go's canonical struct order. - Missing fields are skipped. ``omitempty`` fields with zero values are - dropped. Unknown fields are ignored: Go's ``json.Unmarshal`` would drop - them, and the re-serialized canonical form should not include them. + Missing fields are skipped, except for non-omitempty fields inside a + present pointer-like nested struct: Go unmarshals that as a non-nil struct + pointer and re-marshals its zero fields. ``omitempty`` fields with zero + values are dropped. Unknown fields are ignored by this low-level + canonicalizer; verification rejects them before signature checks. Nested fields recurse so nested object keys are reordered to Go order too: @@ -211,9 +330,16 @@ def _order_object( ordered: dict[str, Any] = {} for name, omitempty, nested in fields: if name not in obj: - continue - value = obj[name] - if omitempty and _is_go_zero(value): + if not emit_missing_required or omitempty or nested_kind is None: + continue + zero_values = _NESTED_REQUIRED_ZERO_VALUES.get(nested_kind, {}) + if name not in zero_values: + continue + value = zero_values[name] + else: + value = obj[name] + pointer_like_struct = nested in _NESTED_OBJECT_FIELDS and isinstance(value, dict) + if omitempty and not pointer_like_struct and _is_go_zero(value): continue if nested == "taint_source" and isinstance(value, list): value = [ @@ -221,7 +347,12 @@ def _order_object( for item in value ] elif nested in _NESTED_OBJECT_FIELDS and isinstance(value, dict): - value = _order_object(value, _NESTED_OBJECT_FIELDS[nested]) + value = _order_object( + value, + _NESTED_OBJECT_FIELDS[nested], + nested_kind=nested, + emit_missing_required=True, + ) else: # Non-struct value: sort any free-form map keys to match Go. value = _normalize_maps(value) diff --git a/pipelock_verify/_verify.py b/pipelock_verify/_verify.py index 513fba8..01bc277 100644 --- a/pipelock_verify/_verify.py +++ b/pipelock_verify/_verify.py @@ -11,7 +11,20 @@ from cryptography.exceptions import InvalidSignature from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey -from ._canonical import canonicalize_action_record, canonicalize_receipt +from ._canonical import ( + _ACTION_RECORD_FIELDS, + _KEY_TRANSITION_FIELDS, + _RECEIPT_FIELDS, + _REDACTION_FIELDS, + _SESSION_CLOSE_FIELDS, + _SESSION_CONTROL_FIELDS, + _SESSION_HEARTBEAT_FIELDS, + _SESSION_OPEN_FIELDS, + _SHIELD_FIELDS, + _TAINT_SOURCE_FIELDS, + canonicalize_action_record, + canonicalize_receipt, +) from ._common import InvalidReceiptError as InvalidReceiptError from ._common import _is_valid_rfc3339 as _is_valid_rfc3339 from ._common import loads_no_duplicate_keys @@ -29,6 +42,10 @@ # Flight-recorder entry type for receipts. Matches # internal/receipt/emitter.go recorderEntryType. _RECORDER_ENTRY_TYPE = "action_receipt" +_SESSION_OPEN_GENESIS_PREFIX = "g1:" +_SESSION_OPEN_GENESIS_LABEL = "pipelock.receipt.session_open.v1" +_UINT64_MAX = 2**64 - 1 +_INT64_MAX = 2**63 - 1 # Valid action_type enum values. Matches the allActionTypes map in # internal/receipt/action.go. A verifier that does not enforce this set @@ -48,6 +65,17 @@ } ) +_V1_RECEIPT_FIELDS = frozenset(name for name, _, _ in _RECEIPT_FIELDS) | {"ext"} +_ACTION_RECORD_FIELD_NAMES = frozenset(name for name, _, _ in _ACTION_RECORD_FIELDS) +_REDACTION_FIELD_NAMES = frozenset(name for name, _, _ in _REDACTION_FIELDS) +_SHIELD_FIELD_NAMES = frozenset(name for name, _, _ in _SHIELD_FIELDS) +_TAINT_SOURCE_FIELD_NAMES = frozenset(name for name, _, _ in _TAINT_SOURCE_FIELDS) +_KEY_TRANSITION_FIELD_NAMES = frozenset(name for name, _, _ in _KEY_TRANSITION_FIELDS) +_SESSION_CONTROL_FIELD_NAMES = frozenset(name for name, _, _ in _SESSION_CONTROL_FIELDS) +_SESSION_OPEN_FIELD_NAMES = frozenset(name for name, _, _ in _SESSION_OPEN_FIELDS) +_SESSION_HEARTBEAT_FIELD_NAMES = frozenset(name for name, _, _ in _SESSION_HEARTBEAT_FIELDS) +_SESSION_CLOSE_FIELD_NAMES = frozenset(name for name, _, _ in _SESSION_CLOSE_FIELDS) + @dataclass class VerifyResult: @@ -270,6 +298,270 @@ def _read_jsonl(path: Path) -> list[dict[str, Any]]: return receipts +def _reject_unknown_fields( + obj: dict[str, Any], + allowed: frozenset[str], + where: str, +) -> str | None: + unknown = sorted(set(obj) - allowed) + if unknown: + return f"{where} carries unknown signed field(s): {unknown}" + return None + + +def _require_string(obj: dict[str, Any], name: str, where: str) -> str | None: + if name in obj and not isinstance(obj[name], str): + return f"{where} {name} must be a string" + return None + + +def _require_uint64(obj: dict[str, Any], name: str, where: str) -> str | None: + if name not in obj: + return None + value = obj[name] + if not isinstance(value, int) or isinstance(value, bool) or value < 0 or value > _UINT64_MAX: + return f"{where} {name} must be a uint64" + return None + + +def _require_int(obj: dict[str, Any], name: str, where: str) -> str | None: + if name in obj and (not isinstance(obj[name], int) or isinstance(obj[name], bool)): + return f"{where} {name} must be an integer" + return None + + +def _require_bool(obj: dict[str, Any], name: str, where: str) -> str | None: + if name in obj and not isinstance(obj[name], bool): + return f"{where} {name} must be a boolean" + return None + + +def _require_int64(obj: dict[str, Any], name: str, where: str) -> str | None: + if name not in obj: + return None + value = obj[name] + if ( + not isinstance(value, int) + or isinstance(value, bool) + or value < -_INT64_MAX - 1 + or value > _INT64_MAX + ): + return f"{where} {name} must be an integer" + return None + + +def _validate_redaction_shape(action_record: dict[str, Any]) -> str | None: + if "redaction" not in action_record or action_record.get("redaction") is None: + return None + redaction = action_record["redaction"] + if not isinstance(redaction, dict): + return "redaction must be an object" + if err := _reject_unknown_fields(redaction, _REDACTION_FIELD_NAMES, "redaction"): + return err + for name in ("profile", "provider", "parser"): + if err := _require_string(redaction, name, "redaction"): + return err + if err := _require_int(redaction, "total_redactions", "redaction"): + return err + if "by_class" in redaction and redaction["by_class"] is not None: + by_class = redaction["by_class"] + if not isinstance(by_class, dict): + return "redaction by_class must be an object" + for key, value in by_class.items(): + if not isinstance(key, str): + return "redaction by_class keys must be strings" + if not isinstance(value, int) or isinstance(value, bool): + return "redaction by_class values must be integers" + return _require_bool(redaction, "cache_boundary_kept", "redaction") + + +def _validate_shield_shape(action_record: dict[str, Any]) -> str | None: + if "shield" not in action_record or action_record.get("shield") is None: + return None + shield = action_record["shield"] + if not isinstance(shield, dict): + return "shield must be an object" + if err := _reject_unknown_fields(shield, _SHIELD_FIELD_NAMES, "shield"): + return err + if err := _require_string(shield, "pipeline", "shield"): + return err + for name in ( + "total_rewrites", + "extension_probes", + "tracking_beacons", + "agent_traps", + "svg_foreign_objects", + "svg_event_handlers", + "svg_external_references", + "svg_hidden_text", + "svg_animation_injections", + "body_bytes", + "scanned_bytes", + "adaptive_signals_recorded", + "adaptive_signal_max_per_body", + ): + if err := _require_int(shield, name, "shield"): + return err + for name in ("fingerprint_shim_injected", "partial"): + if err := _require_bool(shield, name, "shield"): + return err + return None + + +def _validate_taint_sources_shape(action_record: dict[str, Any]) -> str | None: + if ( + "recent_taint_sources" not in action_record + or action_record.get("recent_taint_sources") is None + ): + return None + sources = action_record["recent_taint_sources"] + if not isinstance(sources, list): + return "recent_taint_sources must be a list" + for index, source in enumerate(sources): + where = f"recent_taint_sources[{index}]" + if not isinstance(source, dict): + return f"{where} must be an object" + if err := _reject_unknown_fields(source, _TAINT_SOURCE_FIELD_NAMES, where): + return err + for name in ("url", "kind", "timestamp", "receipt_id", "match_reason"): + if err := _require_string(source, name, where): + return err + if "level" in source: + level = source["level"] + if not isinstance(level, int) or isinstance(level, bool) or level < 0 or level > 255: + return f"{where} level must be a uint8" + return None + + +def _validate_key_transition_shape(action_record: dict[str, Any]) -> str | None: + if "key_transition" not in action_record or action_record.get("key_transition") is None: + return None + marker = action_record["key_transition"] + if not isinstance(marker, dict): + return "key_transition must be an object" + if err := _reject_unknown_fields(marker, _KEY_TRANSITION_FIELD_NAMES, "key_transition"): + return err + for name in ("prior_signer_key", "prior_chain_hash"): + if err := _require_string(marker, name, "key_transition"): + return err + return _require_uint64(marker, "prior_chain_seq", "key_transition") + + +def _validate_session_payload_shape( + payload: Any, + allowed_fields: frozenset[str], + string_fields: tuple[str, ...], + uint64_fields: tuple[str, ...], + where: str, + *, + int64_fields: tuple[str, ...] = (), +) -> str | None: + if not isinstance(payload, dict): + return f"{where} must be an object" + if err := _reject_unknown_fields(payload, allowed_fields, where): + return err + for name in string_fields: + if err := _require_string(payload, name, where): + return err + for name in uint64_fields: + if err := _require_uint64(payload, name, where): + return err + for name in int64_fields: + if err := _require_int64(payload, name, where): + return err + return None + + +def _validate_session_control_shape(action_record: dict[str, Any]) -> str | None: + if "session_control" not in action_record or action_record.get("session_control") is None: + return None + ctrl = action_record["session_control"] + if not isinstance(ctrl, dict): + return "session_control must be an object" + if err := _reject_unknown_fields(ctrl, _SESSION_CONTROL_FIELD_NAMES, "session_control"): + return err + + kind = ctrl.get("kind") + if not isinstance(kind, str): + return "session_control kind must be a string" + + payload_names = ("open", "heartbeat", "close") + present_payloads = [name for name in payload_names if ctrl.get(name) is not None] + if len(present_payloads) != 1: + return "session_control must carry exactly one payload" + + if kind == "session_open": + if present_payloads != ["open"]: + return "session_open kind missing open payload" + return _validate_session_payload_shape( + ctrl["open"], + _SESSION_OPEN_FIELD_NAMES, + ( + "run_nonce", + "open_nonce", + "recorder_session", + "policy_hash", + "signer_key_epoch", + "prior_chain_head", + "genesis_hash", + "genesis_anchor_head", + "genesis_anchor_log", + "posture_capsule_sha256", + "posture_signer_key_id", + "containment_nonce", + "contained_uid", + ), + ("chain_open_seq", "prior_chain_seq"), + "session_open", + int64_fields=("heartbeat_seconds",), + ) + if kind == "heartbeat": + if present_payloads != ["heartbeat"]: + return "heartbeat kind missing heartbeat payload" + return _validate_session_payload_shape( + ctrl["heartbeat"], + _SESSION_HEARTBEAT_FIELD_NAMES, + ("run_nonce", "open_nonce", "chain_head", "heartbeat_time"), + ("beat", "chain_seq_head", "fsync_errors_gated", "durability_blocks"), + "session_heartbeat", + ) + if kind == "session_close": + if present_payloads != ["close"]: + return "session_close kind missing close payload" + return _validate_session_payload_shape( + ctrl["close"], + _SESSION_CLOSE_FIELD_NAMES, + ("run_nonce", "open_nonce", "root_hash", "close_reason"), + ("final_seq", "receipt_count", "fsync_errors_gated", "durability_blocks"), + "session_close", + ) + return "unknown session_control kind" + + +def _validate_v1_receipt_shape( + receipt: dict[str, Any], action_record: dict[str, Any] +) -> str | None: + if err := _reject_unknown_fields(receipt, _V1_RECEIPT_FIELDS, "receipt"): + return err + if err := _reject_unknown_fields(action_record, _ACTION_RECORD_FIELD_NAMES, "action_record"): + return err + if err := _require_string(action_record, "chain_prev_hash", "action_record"): + return err + if err := _require_uint64(action_record, "chain_seq", "action_record"): + return err + if err := _require_string(action_record, "run_nonce", "action_record"): + return err + if err := _validate_redaction_shape(action_record): + return err + if err := _validate_shield_shape(action_record): + return err + if err := _validate_taint_sources_shape(action_record): + return err + if err := _validate_key_transition_shape(action_record): + return err + return _validate_session_control_shape(action_record) + + def _verify_receipt_dict( receipt: dict[str, Any], expected_key_hex: str | None, @@ -285,6 +577,9 @@ def _verify_receipt_dict( if not isinstance(action_record, dict): return VerifyResult(valid=False, error="missing or invalid action_record") + if shape_error := _validate_v1_receipt_shape(receipt, action_record): + return VerifyResult(valid=False, error=f"unmarshal receipt: {shape_error}") + ar_version = action_record.get("version") if ar_version != _ACTION_RECORD_VERSION: return VerifyResult( @@ -413,6 +708,230 @@ def _compute_receipt_hash(receipt: dict[str, Any]) -> str: return hashlib.sha256(canonical).hexdigest() +def _compute_session_open_genesis(open_payload: dict[str, Any]) -> str: + """Return Go's bound session-open genesis hash for a SessionOpen payload.""" + + h = hashlib.sha256() + + def frame(data: bytes) -> None: + h.update(len(data).to_bytes(8, "big")) + h.update(data) + + def string_field(name: str) -> bytes: + value = open_payload.get(name, "") + if not isinstance(value, str): + raise InvalidReceiptError(f"session_open {name} must be a string") + return value.encode("utf-8") + + frame(_SESSION_OPEN_GENESIS_LABEL.encode("utf-8")) + frame(string_field("run_nonce")) + frame(string_field("open_nonce")) + frame(string_field("recorder_session")) + frame(string_field("policy_hash")) + frame(string_field("signer_key_epoch")) + + heartbeat_seconds = open_payload.get("heartbeat_seconds", 0) + if not isinstance(heartbeat_seconds, int) or isinstance(heartbeat_seconds, bool): + raise InvalidReceiptError("session_open heartbeat_seconds must be an integer") + if heartbeat_seconds > _INT64_MAX: + raise InvalidReceiptError("session_open heartbeat_seconds must be an integer") + if heartbeat_seconds < 0: + heartbeat_seconds = 0 + frame(heartbeat_seconds.to_bytes(8, "big")) + + frame(string_field("genesis_anchor_head")) + frame(string_field("genesis_anchor_log")) + frame(string_field("posture_capsule_sha256")) + frame(string_field("containment_nonce")) + frame(string_field("contained_uid")) + + return _SESSION_OPEN_GENESIS_PREFIX + h.hexdigest() + + +def _session_control_open(action_record: dict[str, Any]) -> dict[str, Any] | None: + return _session_control_payload(action_record, "session_open", "open") + + +def _session_control_heartbeat(action_record: dict[str, Any]) -> dict[str, Any] | None: + return _session_control_payload(action_record, "heartbeat", "heartbeat") + + +def _session_control_close(action_record: dict[str, Any]) -> dict[str, Any] | None: + return _session_control_payload(action_record, "session_close", "close") + + +def _session_control_payload( + action_record: dict[str, Any], + kind: str, + payload_name: str, +) -> dict[str, Any] | None: + ctrl = action_record.get("session_control") + if not isinstance(ctrl, dict) or ctrl.get("kind") != kind: + return None + payload = ctrl.get(payload_name) + if isinstance(payload, dict): + return payload + return None + + +def _validate_first_prev_hash(action_record: dict[str, Any], actual_prev: str) -> str | None: + if not actual_prev.startswith(_SESSION_OPEN_GENESIS_PREFIX): + if actual_prev == _GENESIS_HASH and _session_control_open(action_record) is not None: + return "session_open on legacy genesis must use bound g1 chain_prev_hash" + if actual_prev == _GENESIS_HASH and action_record.get("run_nonce"): + return "run_nonce first receipt is not a matching session_open" + return None + + open_payload = _session_control_open(action_record) + if open_payload is None: + return "g1 chain_prev_hash requires SessionControl.Open" + + seq = action_record.get("chain_seq") + if seq != 0: + return "bound session_open genesis must be chain_seq 0" + + try: + computed = _compute_session_open_genesis(open_payload) + except InvalidReceiptError as exc: + return str(exc) + + if actual_prev != computed: + return "session_open genesis hash mismatch" + if open_payload.get("genesis_hash") != computed: + return "session_open genesis_hash mismatch" + if open_payload.get("chain_open_seq") != seq: + return "session_open chain_open_seq does not match receipt chain_seq" + if open_payload.get("prior_chain_head", "") or open_payload.get("prior_chain_seq", 0) != 0: + return "bound genesis session_open must not carry prior chain tail" + + return None + + +def _validate_restart_open( + action_record: dict[str, Any], + open_payload: dict[str, Any], + prior_head: str, + prior_seq: int, +) -> str | None: + actual_prev = action_record.get("chain_prev_hash", "") + if actual_prev.startswith(_SESSION_OPEN_GENESIS_PREFIX): + return "restart session_open must not use g1 chain_prev_hash" + if open_payload.get("genesis_hash", ""): + return "restart session_open must not carry genesis_hash" + if open_payload.get("chain_open_seq", 0) != action_record.get("chain_seq"): + return "session_open chain_open_seq does not match receipt chain_seq" + if open_payload.get("prior_chain_head", "") != prior_head: + return "session_open prior_chain_head does not match prior tail hash" + if open_payload.get("prior_chain_seq", 0) != prior_seq: + return "session_open prior_chain_seq does not match prior tail seq" + return None + + +def _validate_session_control_state( + action_record: dict[str, Any], + run_nonces: dict[str, str], + closed_runs: dict[str, bool], + active_run: str, + active_open: str, + prev_hash: str, + segment_receipt_count: int, +) -> tuple[str | None, str, str]: + ctrl = action_record.get("session_control") + open_payload = _session_control_open(action_record) + heartbeat = _session_control_heartbeat(action_record) + close = _session_control_close(action_record) + run_nonce = action_record.get("run_nonce", "") + + if not run_nonce: + if ctrl is not None: + return "session_control receipt missing run_nonce", active_run, active_open + return None, active_run, active_open + + if open_payload is None: + open_nonce = run_nonces.get(run_nonce) + if open_nonce is None: + return "run_nonce first receipt is not a matching session_open", active_run, active_open + if closed_runs.get(run_nonce, False): + return "record observed after session_close", active_run, active_open + + if heartbeat is not None: + if heartbeat.get("run_nonce", "") != run_nonce: + return ( + "heartbeat run_nonce does not match receipt run_nonce", + active_run, + active_open, + ) + if active_run == "" or active_open == "": + return "heartbeat has no active session_open", active_run, active_open + if heartbeat.get("run_nonce", "") != active_run: + return ( + "heartbeat run_nonce does not match active session_open", + active_run, + active_open, + ) + if heartbeat.get("open_nonce", "") != open_nonce: + return "heartbeat open_nonce does not match session_open", active_run, active_open + if heartbeat.get("open_nonce", "") != active_open: + return ( + "heartbeat open_nonce does not match active session_open", + active_run, + active_open, + ) + if heartbeat.get("chain_head", "") != prev_hash: + return "heartbeat chain_head mismatch", active_run, active_open + if heartbeat.get("chain_seq_head", 0) != action_record.get("chain_seq", 0) - 1: + return "heartbeat chain_seq_head mismatch", active_run, active_open + + if close is not None: + if close.get("run_nonce", "") != run_nonce: + return ( + "session_close run_nonce does not match receipt run_nonce", + active_run, + active_open, + ) + if active_run == "" or active_open == "": + return "session_close has no active session_open", active_run, active_open + if close.get("run_nonce", "") != active_run: + return ( + "session_close run_nonce does not match active session_open", + active_run, + active_open, + ) + if close.get("open_nonce", "") != open_nonce: + return ( + "session_close open_nonce does not match session_open", + active_run, + active_open, + ) + if close.get("open_nonce", "") != active_open: + return ( + "session_close open_nonce does not match active session_open", + active_run, + active_open, + ) + if close.get("root_hash", "") != prev_hash: + return "session_close root_hash mismatch", active_run, active_open + if close.get("final_seq", 0) != action_record.get("chain_seq"): + return "session_close final_seq mismatch", active_run, active_open + if close.get("receipt_count", 0) != segment_receipt_count + 1: + return "session_close receipt_count mismatch", active_run, active_open + closed_runs[run_nonce] = True + return None, "", "" + + return None, active_run, active_open + + if open_payload.get("run_nonce", "") != run_nonce: + return "session_open run_nonce does not match receipt run_nonce", active_run, active_open + open_nonce = open_payload.get("open_nonce", "") + if open_nonce == "": + return "session_open open_nonce is empty", active_run, active_open + if run_nonce in run_nonces: + return "duplicate session_open for run_nonce", active_run, active_open + run_nonces[run_nonce] = open_nonce + closed_runs[run_nonce] = False + return None, open_payload.get("run_nonce", ""), open_nonce + + def _verify_chain_list( receipts: list[dict[str, Any]], public_key_hex: str | None, @@ -447,10 +966,41 @@ def _verify_chain_list( # attacker can't splice receipts from a second signer into the chain. expected_key = public_key_hex or receipts[0].get("signer_key", "") - prev_hash = _GENESIS_HASH + prev_hash: str | None = None + run_nonces: dict[str, str] = {} + closed_runs: dict[str, bool] = {} + active_run = "" + active_open = "" + segment_receipt_count = 0 for i, receipt in enumerate(receipts): ar = receipt.get("action_record") or {} seq = ar.get("chain_seq", i) + marker = ar.get("key_transition") + + if marker is not None: + if i == 0: + return ChainResult( + valid=False, + broken_at_seq=seq, + error=( + "seq " + f"{seq}: chain starts at a key_transition segment without the prior segment" + ), + ) + if seq != 0: + return ChainResult( + valid=False, + broken_at_seq=seq, + error=f"seq {seq}: key_transition marker on a non-genesis receipt (seq != 0)", + ) + return ChainResult( + valid=False, + broken_at_seq=seq, + error=( + f"seq {seq}: key_transition rotation verification is not implemented " + "by this Python verifier" + ), + ) result = _verify_receipt_dict(receipt, expected_key) if not result.valid: @@ -468,14 +1018,67 @@ def _verify_chain_list( ) actual_prev = ar.get("chain_prev_hash", "") - if actual_prev != prev_hash: + if i == 0: + anchor_error = _validate_first_prev_hash(ar, actual_prev) + if anchor_error is not None: + return ChainResult( + valid=False, + broken_at_seq=seq, + error=f"seq {seq}: {anchor_error}", + ) + expected_prev = ( + actual_prev + if actual_prev.startswith(_SESSION_OPEN_GENESIS_PREFIX) + else _GENESIS_HASH + ) + else: + expected_prev = prev_hash + open_payload = _session_control_open(ar) + if open_payload is not None: + if prev_hash is None: + return ChainResult( + valid=False, + broken_at_seq=seq, + error=f"seq {seq}: session_open continuation has no prior segment", + ) + restart_error = _validate_restart_open( + ar, + open_payload, + prev_hash, + receipts[i - 1].get("action_record", {}).get("chain_seq", i - 1), + ) + if restart_error is not None: + return ChainResult( + valid=False, + broken_at_seq=seq, + error=f"seq {seq}: {restart_error}", + ) + + if actual_prev != expected_prev: return ChainResult( valid=False, broken_at_seq=seq, error=f"seq {seq}: chain_prev_hash mismatch", ) + state_error, active_run, active_open = _validate_session_control_state( + ar, + run_nonces, + closed_runs, + active_run, + active_open, + expected_prev, + segment_receipt_count, + ) + if state_error is not None: + return ChainResult( + valid=False, + broken_at_seq=seq, + error=f"seq {seq}: {state_error}", + ) + prev_hash = _compute_receipt_hash(receipt) + segment_receipt_count += 1 first_ar = receipts[0].get("action_record") or {} last_ar = receipts[-1].get("action_record") or {} diff --git a/tests/fixtures/v3_2_0_live_chain.jsonl b/tests/fixtures/v3_2_0_live_chain.jsonl new file mode 100644 index 0000000..c720614 --- /dev/null +++ b/tests/fixtures/v3_2_0_live_chain.jsonl @@ -0,0 +1,19 @@ +{"v":2,"seq":0,"ts":"2026-07-24T22:57:41.868471422Z","session_id":"proxy","type":"action_receipt","event_kind":"unclassified","transport":"receipt_session","summary":"receipt: allow unclassified receipt_session","detail":{"version":1,"action_record":{"version":1,"action_id":"019f9659-49ec-7198-a538-d6776b51f209","action_type":"unclassified","timestamp":"2026-07-24T22:57:41.86819273Z","principal":"local","actor":"pipelock","delegation_chain":null,"target":"pipelock://session/open","side_effect_class":"none","reversibility":"unknown","policy_hash":"9da87919033d0a277a33bf45e0a688ae06e32e92641f4d09c1a7e96f8431d80b","verdict":"allow","transport":"receipt_session","chain_prev_hash":"g1:2451a4fbed9070302bf6a684609e3ef0890f8b882b68629640ee2f5c4c02768f","chain_seq":0,"run_nonce":"4120828d9be9b0f9cfb02359a1d29dad","session_control":{"kind":"session_open","open":{"run_nonce":"4120828d9be9b0f9cfb02359a1d29dad","open_nonce":"52377cda42792526c8c6bd6ec89f6668","recorder_session":"proxy","policy_hash":"9da87919033d0a277a33bf45e0a688ae06e32e92641f4d09c1a7e96f8431d80b","signer_key_epoch":"7b50779c51c9968bd70551d777d3672dedf3d68e008fbffa1c0de18dedb92897","heartbeat_seconds":0,"chain_open_seq":0,"genesis_hash":"g1:2451a4fbed9070302bf6a684609e3ef0890f8b882b68629640ee2f5c4c02768f"}}},"signature":"ed25519:779062485a0190c38fcb25a3d82a4d25c7e160389823f1932afc81865a2f947dd46fb04c0bda9f5f04159c148c99b0ee36eda2ee456527368fc5c6410dee3907","signer_key":"7b50779c51c9968bd70551d777d3672dedf3d68e008fbffa1c0de18dedb92897"},"prev_hash":"genesis","hash":"fd160faefe79ecbc705df75ca62ed7e5face13c63d538b2ada753a6aa3b7a81e"} +{"v":2,"seq":1,"ts":"2026-07-24T22:57:50.704050026Z","session_id":"proxy","type":"action_receipt","event_kind":"read","transport":"fetch","summary":"receipt: allow read fetch","detail":{"version":1,"action_record":{"version":1,"action_id":"019f9659-6c6f-74aa-917c-ad7b41d5700a","action_type":"read","timestamp":"2026-07-24T22:57:50.704002224Z","principal":"local","actor":"anonymous","delegation_chain":null,"target":"https://example.com/","side_effect_class":"external_read","reversibility":"full","policy_hash":"875ec708cb702411dc12bb96f3894c44626ee7ca69de2bd53ea21840264fb305","verdict":"allow","decision_phase":"intent","session_taint_level":"trusted","authority_kind":"user_broad","taint_decision":"allow","taint_decision_reason":"taint_safe_read_only_action","transport":"fetch","method":"GET","request_id":"req-1","chain_prev_hash":"95b0e94559e6c567d833bcdbd0ebef70ed596525c9cdcc3384b58468250bd451","chain_seq":1,"run_nonce":"4120828d9be9b0f9cfb02359a1d29dad"},"signature":"ed25519:cfbf741a1e1b5d48bd30cf6196062be95988c1aab4d75a3745df3ddebccbdca30dc40e4bf733f765bc14b452b0652f7ad47470edb2865323319594143ac16000","signer_key":"7b50779c51c9968bd70551d777d3672dedf3d68e008fbffa1c0de18dedb92897"},"prev_hash":"fd160faefe79ecbc705df75ca62ed7e5face13c63d538b2ada753a6aa3b7a81e","hash":"9918c29a955d1fe79a69d14a756a2d9c7c41b412ab00d10413bfe7f04e63f27a"} +{"v":2,"seq":2,"ts":"2026-07-24T22:57:50.708760151Z","session_id":"proxy","type":"evidence_receipt","event_kind":"proxy_decision","transport":"fetch","summary":"proxy_decision: http_request allow via scanner","detail":{"record_type":"evidence_receipt_v2","receipt_version":2,"payload_kind":"proxy_decision","canonicalization":{"jcs_profile":"pipelock-jcs-rfc8785-nfc-v1","jcs_version":"rfc8785","hash_alg":"sha256","sig_alg":"ed25519","redaction_ruleset_id":"pipelock-transform-v1","redaction_ruleset_version":"1","redaction_ruleset_hash":"sha256:541896788b42651a202448894583a847db9d1aa081c33a7e1f0512303d72527e"},"crit":["canonicalization"],"event_id":"019f9659-6c74-78e4-8d3b-086296c58214","timestamp":"2026-07-24T22:57:50.708583012Z","principal":"local","actor":"pipelock","signature":{"signer_key_id":"7b50779c51c9968bd70551d777d3672dedf3d68e008fbffa1c0de18dedb92897","key_purpose":"receipt-signing","algorithm":"ed25519","signature":"ed25519:a6b8b4f3be6e82d4aae2d5ed1183e9b82074cdfc547f51cf7f7ad930651bec2a340db99a2eb2adb1b8d0361bef0a4921a3b882a95cfc7b39fbd4c42bfde9eb0b"},"chain_seq":0,"chain_prev_hash":"genesis","policy_hash":"sha256:875ec708cb702411dc12bb96f3894c44626ee7ca69de2bd53ea21840264fb305","payload":{"action_type":"http_request","target":"https://example.com/","verdict":"allow","transport":"fetch","policy_sources":["scanner"],"winning_source":"scanner"}},"prev_hash":"9918c29a955d1fe79a69d14a756a2d9c7c41b412ab00d10413bfe7f04e63f27a","hash":"b82d3033fb4b1892c2847db8b135d9bb2c3b4c8eda9508b2cf53bb16a0e09674"} +{"v":2,"seq":3,"ts":"2026-07-24T22:57:50.821130338Z","session_id":"proxy","type":"action_receipt","event_kind":"read","transport":"fetch","summary":"receipt: allow read fetch","detail":{"version":1,"action_record":{"version":1,"action_id":"019f9659-6c6f-74aa-917c-ad7b41d5700a","action_type":"read","timestamp":"2026-07-24T22:57:50.82107391Z","principal":"local","actor":"anonymous","delegation_chain":null,"target":"https://example.com/","side_effect_class":"external_read","reversibility":"full","policy_hash":"875ec708cb702411dc12bb96f3894c44626ee7ca69de2bd53ea21840264fb305","verdict":"allow","decision_phase":"outcome","session_taint_level":"trusted","authority_kind":"user_broad","taint_decision":"allow","taint_decision_reason":"taint_safe_read_only_action","transport":"fetch","method":"GET","layer":"outcome","pattern":"status=200 bytes=559 reason=complete","request_id":"req-1","chain_prev_hash":"d0c5d4740309ba11b21e5f4f6736cc6803c32becb86e856c9cdd252bc98601e8","chain_seq":2,"run_nonce":"4120828d9be9b0f9cfb02359a1d29dad"},"signature":"ed25519:b42798237f723fbb7108fa5bfa48884faf8066efd25e6c6fb0828222a87ef08d03aebd9d0dfdf9a2302a58091c68b923fff4155cd7550365478aca006f7af000","signer_key":"7b50779c51c9968bd70551d777d3672dedf3d68e008fbffa1c0de18dedb92897"},"prev_hash":"b82d3033fb4b1892c2847db8b135d9bb2c3b4c8eda9508b2cf53bb16a0e09674","hash":"29a5cfba37b3f56998ee34351d0f14c07657ec65af1c5e093c2389eb9c135fff"} +{"v":2,"seq":4,"ts":"2026-07-24T22:57:50.827351963Z","session_id":"proxy","type":"evidence_receipt","event_kind":"proxy_decision","transport":"fetch","summary":"proxy_decision: http_request allow via scanner","detail":{"record_type":"evidence_receipt_v2","receipt_version":2,"payload_kind":"proxy_decision","canonicalization":{"jcs_profile":"pipelock-jcs-rfc8785-nfc-v1","jcs_version":"rfc8785","hash_alg":"sha256","sig_alg":"ed25519","redaction_ruleset_id":"pipelock-transform-v1","redaction_ruleset_version":"1","redaction_ruleset_hash":"sha256:541896788b42651a202448894583a847db9d1aa081c33a7e1f0512303d72527e"},"crit":["canonicalization"],"event_id":"019f9659-6ceb-7349-a97e-c1115afa57fa","timestamp":"2026-07-24T22:57:50.827215733Z","principal":"local","actor":"pipelock","signature":{"signer_key_id":"7b50779c51c9968bd70551d777d3672dedf3d68e008fbffa1c0de18dedb92897","key_purpose":"receipt-signing","algorithm":"ed25519","signature":"ed25519:70c3f63cf8d60fc1cafc1571f4583fd318c5a6f8381080699395bf1f3f2576c80accf9569b64b47cb04356aa4762faaf64ce954dca520d832e14081282a95206"},"chain_seq":1,"chain_prev_hash":"f608a467efcf9cbea6a79a049039adfdca4ad4213a0156003020181674619d10","policy_hash":"sha256:875ec708cb702411dc12bb96f3894c44626ee7ca69de2bd53ea21840264fb305","payload":{"action_type":"http_request","target":"https://example.com/","verdict":"allow","transport":"fetch","policy_sources":["scanner"],"winning_source":"scanner","rule_id":"status=200 bytes=559 reason=complete"}},"prev_hash":"29a5cfba37b3f56998ee34351d0f14c07657ec65af1c5e093c2389eb9c135fff","hash":"f79a056b53cb9c8279b6cf59a7cdeaf72744b363ec3c449fa0b906b58b2a53df"} +{"v":2,"seq":5,"ts":"2026-07-24T22:57:50.840148629Z","session_id":"proxy","type":"action_receipt","event_kind":"read","transport":"fetch","summary":"receipt: allow read fetch","detail":{"version":1,"action_record":{"version":1,"action_id":"019f9659-6cf7-743d-a943-7c38f80a1aef","action_type":"read","timestamp":"2026-07-24T22:57:50.840099675Z","principal":"local","actor":"anonymous","delegation_chain":null,"target":"https://example.com/about","side_effect_class":"external_read","reversibility":"full","policy_hash":"875ec708cb702411dc12bb96f3894c44626ee7ca69de2bd53ea21840264fb305","verdict":"allow","decision_phase":"intent","session_taint_level":"trusted","authority_kind":"user_broad","taint_decision":"allow","taint_decision_reason":"taint_safe_read_only_action","transport":"fetch","method":"GET","request_id":"req-2","chain_prev_hash":"143340ffceb811a7f7e2863be67756ee55d175fcceb64671e74b39e99c84b242","chain_seq":3,"run_nonce":"4120828d9be9b0f9cfb02359a1d29dad"},"signature":"ed25519:f66905efd3b042f6bb4efcb75f6bd407d2a5c7343a8c08ac0e415dd17beacc7090b974d17bb888d032dc9f18ca4aeb992966385387586c504452f8b3371d6a09","signer_key":"7b50779c51c9968bd70551d777d3672dedf3d68e008fbffa1c0de18dedb92897"},"prev_hash":"f79a056b53cb9c8279b6cf59a7cdeaf72744b363ec3c449fa0b906b58b2a53df","hash":"0e7a86bf0f44993e65c20ffb917c16393958d81120d6e861f9b09c455d632c76"} +{"v":2,"seq":6,"ts":"2026-07-24T22:57:50.845101257Z","session_id":"proxy","type":"evidence_receipt","event_kind":"proxy_decision","transport":"fetch","summary":"proxy_decision: http_request allow via scanner","detail":{"record_type":"evidence_receipt_v2","receipt_version":2,"payload_kind":"proxy_decision","canonicalization":{"jcs_profile":"pipelock-jcs-rfc8785-nfc-v1","jcs_version":"rfc8785","hash_alg":"sha256","sig_alg":"ed25519","redaction_ruleset_id":"pipelock-transform-v1","redaction_ruleset_version":"1","redaction_ruleset_hash":"sha256:541896788b42651a202448894583a847db9d1aa081c33a7e1f0512303d72527e"},"crit":["canonicalization"],"event_id":"019f9659-6cfc-7ef3-9742-147f6b160386","timestamp":"2026-07-24T22:57:50.844979996Z","principal":"local","actor":"pipelock","signature":{"signer_key_id":"7b50779c51c9968bd70551d777d3672dedf3d68e008fbffa1c0de18dedb92897","key_purpose":"receipt-signing","algorithm":"ed25519","signature":"ed25519:3257b7dec5826405d764ae6afac23bcecfa28bf3c2108d451374d4a35bc74c97d00211fd61a1c3dbbf5cd11ab6276fe83becaabd87f4426acc12db26b099030a"},"chain_seq":2,"chain_prev_hash":"5919cf9d44ce89fc013a325e1a3721712a6750b180ee58f5920ec936d4217fed","policy_hash":"sha256:875ec708cb702411dc12bb96f3894c44626ee7ca69de2bd53ea21840264fb305","payload":{"action_type":"http_request","target":"https://example.com/about","verdict":"allow","transport":"fetch","policy_sources":["scanner"],"winning_source":"scanner"}},"prev_hash":"0e7a86bf0f44993e65c20ffb917c16393958d81120d6e861f9b09c455d632c76","hash":"45d57a53ec98c297241607f657d71253c4b9de96b8f965f8b29c612faf630f4e"} +{"v":2,"seq":7,"ts":"2026-07-24T22:57:50.880055556Z","session_id":"proxy","type":"action_receipt","event_kind":"read","transport":"fetch","summary":"receipt: allow read fetch","detail":{"version":1,"action_record":{"version":1,"action_id":"019f9659-6cf7-743d-a943-7c38f80a1aef","action_type":"read","timestamp":"2026-07-24T22:57:50.88001572Z","principal":"local","actor":"anonymous","delegation_chain":null,"target":"https://example.com/about","side_effect_class":"external_read","reversibility":"full","policy_hash":"875ec708cb702411dc12bb96f3894c44626ee7ca69de2bd53ea21840264fb305","verdict":"allow","decision_phase":"outcome","session_taint_level":"trusted","authority_kind":"user_broad","taint_decision":"allow","taint_decision_reason":"taint_safe_read_only_action","transport":"fetch","method":"GET","layer":"outcome","pattern":"status=404 bytes=559 reason=complete","request_id":"req-2","chain_prev_hash":"78f72ee3587aaa47e9c51d02acbe332387664a920261ddaad3b592f002dc0f00","chain_seq":4,"run_nonce":"4120828d9be9b0f9cfb02359a1d29dad"},"signature":"ed25519:75022d56303ec1c754f1df028ffbccf6d10986377423d45a29672a04bb4bb0c1d2514d1e3de4e3f3188a555c4ed1d2bbdc657380ee4492189131c966b6229401","signer_key":"7b50779c51c9968bd70551d777d3672dedf3d68e008fbffa1c0de18dedb92897"},"prev_hash":"45d57a53ec98c297241607f657d71253c4b9de96b8f965f8b29c612faf630f4e","hash":"4eb33aa73f334b6d3df29f9eae09e0ce638bca3b3adac91dee7e43ae19d30365"} +{"v":2,"seq":8,"ts":"2026-07-24T22:57:50.886682225Z","session_id":"proxy","type":"evidence_receipt","event_kind":"proxy_decision","transport":"fetch","summary":"proxy_decision: http_request allow via scanner","detail":{"record_type":"evidence_receipt_v2","receipt_version":2,"payload_kind":"proxy_decision","canonicalization":{"jcs_profile":"pipelock-jcs-rfc8785-nfc-v1","jcs_version":"rfc8785","hash_alg":"sha256","sig_alg":"ed25519","redaction_ruleset_id":"pipelock-transform-v1","redaction_ruleset_version":"1","redaction_ruleset_hash":"sha256:541896788b42651a202448894583a847db9d1aa081c33a7e1f0512303d72527e"},"crit":["canonicalization"],"event_id":"019f9659-6d26-7866-bdcb-faa1822b2b8e","timestamp":"2026-07-24T22:57:50.886550884Z","principal":"local","actor":"pipelock","signature":{"signer_key_id":"7b50779c51c9968bd70551d777d3672dedf3d68e008fbffa1c0de18dedb92897","key_purpose":"receipt-signing","algorithm":"ed25519","signature":"ed25519:517309433199641864ab1a4acd1e9b62b887636868bc95d03016efe5950a1c63df9eb17332058f5d47262ce613bdd289d606ea2733b4eec01f6917b733be9b05"},"chain_seq":3,"chain_prev_hash":"d8cd6bbcd4bbf82386e4524b2a8c54b3163ec81ab14385cce705927cf1a25f03","policy_hash":"sha256:875ec708cb702411dc12bb96f3894c44626ee7ca69de2bd53ea21840264fb305","payload":{"action_type":"http_request","target":"https://example.com/about","verdict":"allow","transport":"fetch","policy_sources":["scanner"],"winning_source":"scanner","rule_id":"status=404 bytes=559 reason=complete"}},"prev_hash":"4eb33aa73f334b6d3df29f9eae09e0ce638bca3b3adac91dee7e43ae19d30365","hash":"3970dd8385b0b7370c794f8a48ade54e187b9f2b611909328b9d51d9848c813a"} +{"v":2,"seq":9,"ts":"2026-07-24T22:57:50.899010214Z","session_id":"proxy","type":"action_receipt","event_kind":"read","transport":"fetch","summary":"receipt: allow read fetch","detail":{"version":1,"action_record":{"version":1,"action_id":"019f9659-6d32-7485-9d81-0d98a8956881","action_type":"read","timestamp":"2026-07-24T22:57:50.898969757Z","principal":"local","actor":"anonymous","delegation_chain":null,"target":"https://example.org/","side_effect_class":"external_read","reversibility":"full","policy_hash":"875ec708cb702411dc12bb96f3894c44626ee7ca69de2bd53ea21840264fb305","verdict":"allow","decision_phase":"intent","session_taint_level":"trusted","authority_kind":"user_broad","taint_decision":"allow","taint_decision_reason":"taint_safe_read_only_action","transport":"fetch","method":"GET","request_id":"req-3","chain_prev_hash":"fcfaeea2fa26401c5e52d0af10e327e0f5ef10e586d6c1672962950876a1831e","chain_seq":5,"run_nonce":"4120828d9be9b0f9cfb02359a1d29dad"},"signature":"ed25519:e4c59977b6d780426f445069e3cccde2a3147842097dbbbf36691949b05a7892350ab62b480fa446bba01cba0117b18357d132945b525337a239481cd319a609","signer_key":"7b50779c51c9968bd70551d777d3672dedf3d68e008fbffa1c0de18dedb92897"},"prev_hash":"3970dd8385b0b7370c794f8a48ade54e187b9f2b611909328b9d51d9848c813a","hash":"ffc2f8ab8864f352c2eb9d41422f2b919c3c14939b16462c356d1cd507b5ce8a"} +{"v":2,"seq":10,"ts":"2026-07-24T22:57:50.904064868Z","session_id":"proxy","type":"evidence_receipt","event_kind":"proxy_decision","transport":"fetch","summary":"proxy_decision: http_request allow via scanner","detail":{"record_type":"evidence_receipt_v2","receipt_version":2,"payload_kind":"proxy_decision","canonicalization":{"jcs_profile":"pipelock-jcs-rfc8785-nfc-v1","jcs_version":"rfc8785","hash_alg":"sha256","sig_alg":"ed25519","redaction_ruleset_id":"pipelock-transform-v1","redaction_ruleset_version":"1","redaction_ruleset_hash":"sha256:541896788b42651a202448894583a847db9d1aa081c33a7e1f0512303d72527e"},"crit":["canonicalization"],"event_id":"019f9659-6d37-7e44-bf3d-9d12b210af35","timestamp":"2026-07-24T22:57:50.903935391Z","principal":"local","actor":"pipelock","signature":{"signer_key_id":"7b50779c51c9968bd70551d777d3672dedf3d68e008fbffa1c0de18dedb92897","key_purpose":"receipt-signing","algorithm":"ed25519","signature":"ed25519:416449bd045d9370a32c93f6bb1f9c4ee5abdfb251991444db1fa5eb9fdafb1b2e178a730dd5c0e12982e5df25ab55797fdc879b5fb9eaf0a4590fa06a536b0a"},"chain_seq":4,"chain_prev_hash":"62d70ddff62ae37ed78363dfb998c6d37899c74a94e6eb81bb045a0916efe02c","policy_hash":"sha256:875ec708cb702411dc12bb96f3894c44626ee7ca69de2bd53ea21840264fb305","payload":{"action_type":"http_request","target":"https://example.org/","verdict":"allow","transport":"fetch","policy_sources":["scanner"],"winning_source":"scanner"}},"prev_hash":"ffc2f8ab8864f352c2eb9d41422f2b919c3c14939b16462c356d1cd507b5ce8a","hash":"a0b32e5ad391c694ccb5b0b10aa1dc555e75d6169c57f2b8b331daee338f01cf"} +{"v":2,"seq":11,"ts":"2026-07-24T22:57:51.008382971Z","session_id":"proxy","type":"action_receipt","event_kind":"read","transport":"fetch","summary":"receipt: allow read fetch","detail":{"version":1,"action_record":{"version":1,"action_id":"019f9659-6d32-7485-9d81-0d98a8956881","action_type":"read","timestamp":"2026-07-24T22:57:51.008332244Z","principal":"local","actor":"anonymous","delegation_chain":null,"target":"https://example.org/","side_effect_class":"external_read","reversibility":"full","policy_hash":"875ec708cb702411dc12bb96f3894c44626ee7ca69de2bd53ea21840264fb305","verdict":"allow","decision_phase":"outcome","session_taint_level":"trusted","authority_kind":"user_broad","taint_decision":"allow","taint_decision_reason":"taint_safe_read_only_action","transport":"fetch","method":"GET","layer":"outcome","pattern":"status=200 bytes=559 reason=complete","request_id":"req-3","chain_prev_hash":"df0825bdded1c4c441e0cb07df9153391049e7523d265b77d1c41c3a0dec689c","chain_seq":6,"run_nonce":"4120828d9be9b0f9cfb02359a1d29dad"},"signature":"ed25519:01284ab624a85e0314584a263562625ec413025f57db483043d48b5048880b51deb89d4f0b1429efbb3f5db1708175be88c9f36a415ffc07c2740fbffaebe30f","signer_key":"7b50779c51c9968bd70551d777d3672dedf3d68e008fbffa1c0de18dedb92897"},"prev_hash":"a0b32e5ad391c694ccb5b0b10aa1dc555e75d6169c57f2b8b331daee338f01cf","hash":"3b98f038af6f4a960a8e33258c5c3d6658c0bfe29ddcced4450024da92dda803"} +{"v":2,"seq":12,"ts":"2026-07-24T22:57:51.01504585Z","session_id":"proxy","type":"evidence_receipt","event_kind":"proxy_decision","transport":"fetch","summary":"proxy_decision: http_request allow via scanner","detail":{"record_type":"evidence_receipt_v2","receipt_version":2,"payload_kind":"proxy_decision","canonicalization":{"jcs_profile":"pipelock-jcs-rfc8785-nfc-v1","jcs_version":"rfc8785","hash_alg":"sha256","sig_alg":"ed25519","redaction_ruleset_id":"pipelock-transform-v1","redaction_ruleset_version":"1","redaction_ruleset_hash":"sha256:541896788b42651a202448894583a847db9d1aa081c33a7e1f0512303d72527e"},"crit":["canonicalization"],"event_id":"019f9659-6da6-7de1-9239-70d0aeefa1f3","timestamp":"2026-07-24T22:57:51.01491005Z","principal":"local","actor":"pipelock","signature":{"signer_key_id":"7b50779c51c9968bd70551d777d3672dedf3d68e008fbffa1c0de18dedb92897","key_purpose":"receipt-signing","algorithm":"ed25519","signature":"ed25519:4db4d1e80d2800c2dde3a470b7fa922d049292872bb561bcc80040a9704a5215307687793ff82dc5b8be2ede9904063f815cc11c5371076bf337ea2672c9f60d"},"chain_seq":5,"chain_prev_hash":"ad9025ae832a38b7cf165755cd2b61b7896c2b99e8eac74c2d7449b0cd180a91","policy_hash":"sha256:875ec708cb702411dc12bb96f3894c44626ee7ca69de2bd53ea21840264fb305","payload":{"action_type":"http_request","target":"https://example.org/","verdict":"allow","transport":"fetch","policy_sources":["scanner"],"winning_source":"scanner","rule_id":"status=200 bytes=559 reason=complete"}},"prev_hash":"3b98f038af6f4a960a8e33258c5c3d6658c0bfe29ddcced4450024da92dda803","hash":"5e2f0e39f22cfefb4878657b714fc4749ddba40bb90f62c47d368a60022cd9de"} +{"v":2,"seq":13,"ts":"2026-07-24T22:58:41.87587915Z","session_id":"proxy","type":"action_receipt","event_kind":"unclassified","transport":"receipt_session","summary":"receipt: allow unclassified receipt_session","detail":{"version":1,"action_record":{"version":1,"action_id":"019f965a-3453-7991-b54b-2b353567fcb8","action_type":"unclassified","timestamp":"2026-07-24T22:58:41.875799668Z","principal":"local","actor":"pipelock","delegation_chain":null,"target":"pipelock://session/heartbeat","side_effect_class":"none","reversibility":"unknown","policy_hash":"9da87919033d0a277a33bf45e0a688ae06e32e92641f4d09c1a7e96f8431d80b","verdict":"allow","transport":"receipt_session","chain_prev_hash":"5413b151b545878a664db5f1efafc6095ec68993439d8e92146f3757e08d7040","chain_seq":7,"run_nonce":"4120828d9be9b0f9cfb02359a1d29dad","session_control":{"kind":"heartbeat","heartbeat":{"run_nonce":"4120828d9be9b0f9cfb02359a1d29dad","open_nonce":"52377cda42792526c8c6bd6ec89f6668","beat":1,"chain_head":"5413b151b545878a664db5f1efafc6095ec68993439d8e92146f3757e08d7040","chain_seq_head":6,"heartbeat_time":"2026-07-24T22:58:41.87563882Z","fsync_errors_gated":0,"durability_blocks":0}}},"signature":"ed25519:03c1ca9488ca85a78f2af1bbc5060b409d286e623a367d4a2873ab39a8d2cdad9439b37e43afd178dc28f764ff813315eb749c40b27679a7c82099c47964e900","signer_key":"7b50779c51c9968bd70551d777d3672dedf3d68e008fbffa1c0de18dedb92897"},"prev_hash":"5e2f0e39f22cfefb4878657b714fc4749ddba40bb90f62c47d368a60022cd9de","hash":"30e618e0cb11299994f829253c79c30706cc14b35ae73f2647599c2d954b8ed0"} +{"v":2,"seq":14,"ts":"2026-07-24T22:59:41.876804118Z","session_id":"proxy","type":"action_receipt","event_kind":"unclassified","transport":"receipt_session","summary":"receipt: allow unclassified receipt_session","detail":{"version":1,"action_record":{"version":1,"action_id":"019f965b-1eb4-78f4-8311-63f2716bada2","action_type":"unclassified","timestamp":"2026-07-24T22:59:41.876740206Z","principal":"local","actor":"pipelock","delegation_chain":null,"target":"pipelock://session/heartbeat","side_effect_class":"none","reversibility":"unknown","policy_hash":"9da87919033d0a277a33bf45e0a688ae06e32e92641f4d09c1a7e96f8431d80b","verdict":"allow","transport":"receipt_session","chain_prev_hash":"fcd3e03f511ac0b786d319a55db09cf7a67f179667d8662d56313bca510560ec","chain_seq":8,"run_nonce":"4120828d9be9b0f9cfb02359a1d29dad","session_control":{"kind":"heartbeat","heartbeat":{"run_nonce":"4120828d9be9b0f9cfb02359a1d29dad","open_nonce":"52377cda42792526c8c6bd6ec89f6668","beat":2,"chain_head":"fcd3e03f511ac0b786d319a55db09cf7a67f179667d8662d56313bca510560ec","chain_seq_head":7,"heartbeat_time":"2026-07-24T22:59:41.876590059Z","fsync_errors_gated":0,"durability_blocks":0}}},"signature":"ed25519:d704c0a6785951b6ced5cea1cc8623bf58d905d5a638fda62d976d94a21c0a0df1d49c438a5a2bf9ea1a9834b92ef06c6402a70c42df2c0d27c3d048c7f4ef09","signer_key":"7b50779c51c9968bd70551d777d3672dedf3d68e008fbffa1c0de18dedb92897"},"prev_hash":"30e618e0cb11299994f829253c79c30706cc14b35ae73f2647599c2d954b8ed0","hash":"491fb2873452c67a96ddbad4dd88484f04d062e4e3444439baec38c50430482f"} +{"v":2,"seq":15,"ts":"2026-07-24T23:00:41.876746706Z","session_id":"proxy","type":"action_receipt","event_kind":"unclassified","transport":"receipt_session","summary":"receipt: allow unclassified receipt_session","detail":{"version":1,"action_record":{"version":1,"action_id":"019f965c-0914-7869-8416-4663ffb13e18","action_type":"unclassified","timestamp":"2026-07-24T23:00:41.876685439Z","principal":"local","actor":"pipelock","delegation_chain":null,"target":"pipelock://session/heartbeat","side_effect_class":"none","reversibility":"unknown","policy_hash":"9da87919033d0a277a33bf45e0a688ae06e32e92641f4d09c1a7e96f8431d80b","verdict":"allow","transport":"receipt_session","chain_prev_hash":"16dfefca864219df263c645dff88751781f66d5ac15ef9dd9e71f42c64574a44","chain_seq":9,"run_nonce":"4120828d9be9b0f9cfb02359a1d29dad","session_control":{"kind":"heartbeat","heartbeat":{"run_nonce":"4120828d9be9b0f9cfb02359a1d29dad","open_nonce":"52377cda42792526c8c6bd6ec89f6668","beat":3,"chain_head":"16dfefca864219df263c645dff88751781f66d5ac15ef9dd9e71f42c64574a44","chain_seq_head":8,"heartbeat_time":"2026-07-24T23:00:41.876554428Z","fsync_errors_gated":0,"durability_blocks":0}}},"signature":"ed25519:a6b967487fdc96b5ee53b3a8b90f238943d823adf7ab6dce5c32e1517a36c17b1dee05837d7af3513b367454335459b4735c604209727df2a14fc0548a958201","signer_key":"7b50779c51c9968bd70551d777d3672dedf3d68e008fbffa1c0de18dedb92897"},"prev_hash":"491fb2873452c67a96ddbad4dd88484f04d062e4e3444439baec38c50430482f","hash":"63aa385ce0a6cfbe181b360c7ba012173c0492967c41e2d5a50c14fa041e7463"} +{"v":2,"seq":16,"ts":"2026-07-24T23:00:44.292088359Z","session_id":"proxy","type":"action_receipt","event_kind":"unclassified","transport":"receipt_session","summary":"receipt: allow unclassified receipt_session","detail":{"version":1,"action_record":{"version":1,"action_id":"019f965c-1283-7db6-9896-c030199575e6","action_type":"unclassified","timestamp":"2026-07-24T23:00:44.292030929Z","principal":"local","actor":"pipelock","delegation_chain":null,"target":"pipelock://session/close","side_effect_class":"none","reversibility":"unknown","policy_hash":"9da87919033d0a277a33bf45e0a688ae06e32e92641f4d09c1a7e96f8431d80b","verdict":"allow","transport":"receipt_session","chain_prev_hash":"4a679ca6883d22d5b5efab6b6e42fa54b17ae1fb82d1cd19b43a5b0bfbddd6c3","chain_seq":10,"run_nonce":"4120828d9be9b0f9cfb02359a1d29dad","session_control":{"kind":"session_close","close":{"run_nonce":"4120828d9be9b0f9cfb02359a1d29dad","open_nonce":"52377cda42792526c8c6bd6ec89f6668","final_seq":10,"root_hash":"4a679ca6883d22d5b5efab6b6e42fa54b17ae1fb82d1cd19b43a5b0bfbddd6c3","receipt_count":11,"close_reason":"graceful_shutdown","fsync_errors_gated":0,"durability_blocks":0}}},"signature":"ed25519:b98ef6a0105e0dd61ac7eff92ca80a80f957edf783307ff166eb418943c171f0bca7c9f5a8a415f5f587fae90e17824972815f43345085e1f7817db4a7494601","signer_key":"7b50779c51c9968bd70551d777d3672dedf3d68e008fbffa1c0de18dedb92897"},"prev_hash":"63aa385ce0a6cfbe181b360c7ba012173c0492967c41e2d5a50c14fa041e7463","hash":"c5f3ffa68a562a1dd2f05137316c8b878d655ca6232de99b208eb374d544e0fb"} +{"v":2,"seq":17,"ts":"2026-07-24T23:00:44.297192908Z","session_id":"proxy","type":"transcript_root","event_kind":"transcript_root","transport":"","summary":"transcript_root: 11 receipts, root=623d13a756891c52","detail":{"session_id":"proxy","final_seq":10,"root_hash":"623d13a756891c52b6a4b3d400a95c3324da7b5c3b11c95446fb11aee2ea3063","receipt_count":11,"start_time":"2026-07-24T22:57:41.86819273Z","end_time":"2026-07-24T23:00:44.292030929Z"},"prev_hash":"c5f3ffa68a562a1dd2f05137316c8b878d655ca6232de99b208eb374d544e0fb","hash":"9cb5badc37664b9abc3f17a8ea44303fe01f7a93fc6d992231c890c4543a9cb3"} +{"v":2,"seq":18,"ts":"2026-07-24T23:00:44.298045559Z","session_id":"proxy","type":"checkpoint","event_kind":"checkpoint","transport":"","summary":"checkpoint: 18 entries [seq 0-17]","detail":{"entry_count":18,"first_seq":0,"last_seq":17,"signature":"7cbb62621173a7e4fd92768110e8af0b7677ad65f5452cf270f5c4aa2786fa85765bba7b3c5e9fb1b465d202fc51d27e1ed48c0d6cf9942b5b2909dd22ef120e"},"prev_hash":"9cb5badc37664b9abc3f17a8ea44303fe01f7a93fc6d992231c890c4543a9cb3","hash":"ee71497dc7a88152dfa27273127f4742b5c633ffa7f805ac53d0828ec5c122c1"} diff --git a/tests/fixtures/v3_2_0_live_chain.pub b/tests/fixtures/v3_2_0_live_chain.pub new file mode 100644 index 0000000..c42f667 --- /dev/null +++ b/tests/fixtures/v3_2_0_live_chain.pub @@ -0,0 +1 @@ +7b50779c51c9968bd70551d777d3672dedf3d68e008fbffa1c0de18dedb92897 diff --git a/tests/test_conformance.py b/tests/test_conformance.py index 5140899..4db39e3 100644 --- a/tests/test_conformance.py +++ b/tests/test_conformance.py @@ -16,6 +16,7 @@ import pipelock_verify CONFORMANCE_DIR = Path(__file__).parent / "conformance" +FIXTURE_DIR = Path(__file__).parent / "fixtures" @pytest.fixture(scope="module") @@ -129,3 +130,24 @@ def test_broken_chain_individual_signatures_valid(test_key_hex): for i, line in enumerate(raw.strip().split("\n")): result = pipelock_verify.verify(line, public_key_hex=test_key_hex) assert result.valid, f"receipt {i} sig invalid: {result.error}" + + +def test_v3_2_0_live_recorder_chain_verifies_with_pinned_key(): + """A real v3.2.0 recorder log must verify end-to-end. + + This fixture carries action_receipt entries with decision_phase, + run_nonce, session_open/heartbeat/session_close controls, interleaved with + evidence_receipt, checkpoint, and transcript_root entries. It is the + regression gate for drift from Go's frozen v1 canonical projection in + internal/receipt/canonical.go. + """ + public_key = (FIXTURE_DIR / "v3_2_0_live_chain.pub").read_text().strip() + result = pipelock_verify.verify_chain( + FIXTURE_DIR / "v3_2_0_live_chain.jsonl", + public_key_hex=public_key, + ) + assert result.valid, f"live v3.2.0 chain invalid: {result.error}" + assert result.receipt_count == 11 + assert result.final_seq == 10 + assert result.start_time == "2026-07-24T22:57:41.86819273Z" + assert result.end_time == "2026-07-24T23:00:44.292030929Z" diff --git a/tests/test_field_drift.py b/tests/test_field_drift.py index 9d9b8fd..97c1e76 100644 --- a/tests/test_field_drift.py +++ b/tests/test_field_drift.py @@ -21,13 +21,141 @@ import pytest -from pipelock_verify._canonical import canonicalize_action_record +from pipelock_verify._canonical import ( + _ACTION_RECORD_FIELDS, + _KEY_TRANSITION_FIELDS, + _SESSION_CLOSE_FIELDS, + _SESSION_CONTROL_FIELDS, + _SESSION_HEARTBEAT_FIELDS, + _SESSION_OPEN_FIELDS, + canonicalize_action_record, +) from pipelock_verify._common import ( DuplicateKeyError, InvalidReceiptError, loads_no_duplicate_keys, ) -from pipelock_verify._verify import verify +from pipelock_verify._verify import _compute_session_open_genesis, verify + +CANONICAL_GO_MESSAGE = ( + "Checked-in Python mirror of Go internal/receipt/canonical.go " + "actionRecordCanonicalV1 drifted; refresh this list from Go origin/main." +) + +EXPECTED_ACTION_RECORD_CANONICAL_V1: list[tuple[str, bool, str | None]] = [ + ("version", False, None), + ("action_id", False, None), + ("parent_action_id", True, None), + ("action_type", False, None), + ("timestamp", False, None), + ("principal", False, None), + ("actor", False, None), + ("delegation_chain", False, None), + ("target", False, None), + ("intent", True, None), + ("data_classes_in", True, None), + ("data_classes_out", True, None), + ("side_effect_class", False, None), + ("reversibility", False, None), + ("policy_hash", False, None), + ("verdict", False, None), + ("decision_phase", True, None), + ("defer_id", True, None), + ("resolution_policy", True, None), + ("resolution_source", True, None), + ("session_id", True, None), + ("session_id_original", True, None), + ("session_taint_level", True, None), + ("session_contaminated", True, None), + ("recent_taint_sources", True, "taint_source"), + ("session_task_id", True, None), + ("session_task_label", True, None), + ("authority_kind", True, None), + ("taint_decision", True, None), + ("taint_decision_reason", True, None), + ("task_override_applied", True, None), + ("contract_winning_source", True, None), + ("contract_live_verdict", True, None), + ("contract_policy_sources", True, None), + ("contract_rule_id", True, None), + ("active_manifest_hash", True, None), + ("contract_hash", True, None), + ("contract_selector_id", True, None), + ("contract_generation", True, None), + ("transport", False, None), + ("method", True, None), + ("layer", True, None), + ("pattern", True, None), + ("severity", True, None), + ("redaction", True, "redaction"), + ("shield", True, "shield"), + ("request_id", True, None), + ("chain_prev_hash", False, None), + ("chain_seq", False, None), + ("run_nonce", True, None), + ("key_transition", True, "key_transition"), + ("session_control", True, "session_control"), + ("venue", True, None), + ("jurisdiction", True, None), + ("rulebook_id", True, None), + ("remedy_class", True, None), + ("contestation_window", True, None), + ("precedent_refs", True, None), +] + +EXPECTED_KEY_TRANSITION_CANONICAL_V1: list[tuple[str, bool, str | None]] = [ + ("prior_signer_key", False, None), + ("prior_chain_seq", False, None), + ("prior_chain_hash", False, None), +] + +EXPECTED_SESSION_CONTROL_CANONICAL_V1: list[tuple[str, bool, str | None]] = [ + ("kind", False, None), + ("open", True, "session_open"), + ("heartbeat", True, "session_heartbeat"), + ("close", True, "session_close"), +] + +EXPECTED_SESSION_OPEN_CANONICAL_V1: list[tuple[str, bool, str | None]] = [ + ("run_nonce", False, None), + ("open_nonce", False, None), + ("recorder_session", False, None), + ("policy_hash", False, None), + ("signer_key_epoch", False, None), + ("heartbeat_seconds", False, None), + ("chain_open_seq", False, None), + ("prior_chain_head", True, None), + ("prior_chain_seq", True, None), + ("genesis_hash", True, None), + ("genesis_anchor_head", True, None), + ("genesis_anchor_log", True, None), + ("posture_capsule_sha256", True, None), + ("posture_signer_key_id", True, None), + ("containment_nonce", True, None), + ("contained_uid", True, None), +] + +EXPECTED_SESSION_HEARTBEAT_CANONICAL_V1: list[tuple[str, bool, str | None]] = [ + ("run_nonce", False, None), + ("open_nonce", False, None), + ("beat", False, None), + ("chain_head", False, None), + ("chain_seq_head", False, None), + ("heartbeat_time", False, None), + ("fsync_errors_gated", False, None), + ("durability_blocks", False, None), +] + +EXPECTED_SESSION_CLOSE_CANONICAL_V1: list[tuple[str, bool, str | None]] = [ + ("run_nonce", False, None), + ("open_nonce", False, None), + ("final_seq", False, None), + ("root_hash", False, None), + ("receipt_count", False, None), + ("close_reason", False, None), + ("fsync_errors_gated", False, None), + ("durability_blocks", False, None), +] def _full_field_record() -> dict: @@ -47,6 +175,12 @@ def _full_field_record() -> dict: "reversibility": "compensatable", "policy_hash": "sha256:abc", "verdict": "allow", + "decision_phase": "intent", + "defer_id": "defer-1", + "resolution_policy": "operator", + "resolution_source": "scanner", + "session_id": "session-1", + "session_id_original": "session-original-1", "session_taint_level": "suspected", "session_contaminated": True, "recent_taint_sources": [ @@ -79,9 +213,42 @@ def _full_field_record() -> dict: "request_id": "req-1", "chain_prev_hash": "genesis", "chain_seq": 0, + "run_nonce": "run-1", + "key_transition": { + "prior_signer_key": "key-a", + "prior_chain_seq": 6, + "prior_chain_hash": "hash-a", + }, + "session_control": { + "kind": "heartbeat", + "heartbeat": { + "run_nonce": "run-1", + "open_nonce": "open-1", + "beat": 1, + "chain_head": "head-1", + "chain_seq_head": 0, + "heartbeat_time": "2026-04-15T12:00:01Z", + "fsync_errors_gated": 0, + "durability_blocks": 1, + }, + }, } +def test_action_record_field_list_matches_canonical_go_projection(): + assert _ACTION_RECORD_FIELDS == EXPECTED_ACTION_RECORD_CANONICAL_V1, CANONICAL_GO_MESSAGE + + +def test_nested_session_field_lists_match_canonical_go_projection(): + assert _KEY_TRANSITION_FIELDS == EXPECTED_KEY_TRANSITION_CANONICAL_V1, CANONICAL_GO_MESSAGE + assert _SESSION_CONTROL_FIELDS == EXPECTED_SESSION_CONTROL_CANONICAL_V1, CANONICAL_GO_MESSAGE + assert _SESSION_OPEN_FIELDS == EXPECTED_SESSION_OPEN_CANONICAL_V1, CANONICAL_GO_MESSAGE + assert _SESSION_HEARTBEAT_FIELDS == EXPECTED_SESSION_HEARTBEAT_CANONICAL_V1, ( + CANONICAL_GO_MESSAGE + ) + assert _SESSION_CLOSE_FIELDS == EXPECTED_SESSION_CLOSE_CANONICAL_V1, CANONICAL_GO_MESSAGE + + def test_parent_action_id_ordered_after_action_id(): canonical = canonicalize_action_record(_full_field_record()).decode() assert ( @@ -121,10 +288,138 @@ def test_taint_block_present_in_canonical(): "contract_hash", "contract_generation", "severity", + "run_nonce", + "key_transition", + "session_control", ): assert f'"{field}"' in canonical, f"{field} missing from canonical output" +def test_v1_decision_fields_ordered_after_verdict_before_taint_block(): + canonical = canonicalize_action_record(_full_field_record()).decode() + assert ( + canonical.index('"verdict"') + < canonical.index('"decision_phase"') + < canonical.index('"defer_id"') + < canonical.index('"resolution_policy"') + < canonical.index('"resolution_source"') + < canonical.index('"session_id"') + < canonical.index('"session_id_original"') + < canonical.index('"session_taint_level"') + ) + + +def test_session_control_payload_keys_in_go_struct_order(): + canonical = canonicalize_action_record(_full_field_record()).decode() + assert canonical.index('"run_nonce"') < canonical.index('"key_transition"') + assert canonical.index('"key_transition"') < canonical.index('"session_control"') + session_control = canonical[canonical.index('"session_control":') :] + assert ( + session_control.index('"kind"') + < session_control.index('"heartbeat"') + < session_control.index('"beat"') + < session_control.index('"chain_head"') + < session_control.index('"fsync_errors_gated"') + < session_control.index('"durability_blocks"') + ) + + +def test_session_open_genesis_matches_go_golden_with_non_empty_tail_fields(): + # Mirrors internal/receipt/session_control_test.go fixedSessionOpen() and + # goldenSessionOpenGenesis. The non-empty tail fields catch wrong spelling + # or order for the posture/containment binding. + open_payload = { + "run_nonce": "run-nonce-aaaa", + "open_nonce": "open-nonce-bbbb", + "recorder_session": "rec-sess-cccc", + "policy_hash": "policy-hash-dddd", + "signer_key_epoch": "epoch-3", + "heartbeat_seconds": 60, + "chain_open_seq": 0, + "genesis_anchor_head": "anchor-head-eeee", + "genesis_anchor_log": "anchor-log-ffff", + "posture_capsule_sha256": "capsule-sha-0000", + "containment_nonce": "contain-nonce-1111", + "contained_uid": "966", + } + assert ( + _compute_session_open_genesis(open_payload) + == "g1:4e3d35d36683d6d5c65e427b447ada0d2e9098befff7caad954972bdadd405ec" + ) + + +def test_present_empty_key_transition_emits_go_zero_fields(): + record = _full_field_record() + record["key_transition"] = {} + canonical = canonicalize_action_record(record).decode() + assert ( + '"key_transition":{"prior_signer_key":"","prior_chain_seq":0,"prior_chain_hash":""}' + ) in canonical + + +def test_present_empty_session_control_emits_go_zero_kind(): + record = _full_field_record() + record["session_control"] = {} + canonical = canonicalize_action_record(record).decode() + assert '"session_control":{"kind":""}' in canonical + + +def test_session_control_open_payload_keys_in_go_struct_order_with_zero_uints(): + record = _full_field_record() + record["session_control"] = { + "kind": "session_open", + "open": { + "run_nonce": "run-1", + "open_nonce": "open-1", + "recorder_session": "proxy", + "policy_hash": "policy-1", + "signer_key_epoch": "epoch-1", + "heartbeat_seconds": 0, + "chain_open_seq": 0, + "genesis_hash": "g1:abc", + "genesis_anchor_head": "anchor-head", + "genesis_anchor_log": "anchor-log", + "posture_capsule_sha256": "capsule", + "posture_signer_key_id": "posture-key", + "containment_nonce": "containment", + "contained_uid": "966", + }, + } + canonical = canonicalize_action_record(record).decode() + assert ( + '"open":{"run_nonce":"run-1","open_nonce":"open-1",' + '"recorder_session":"proxy","policy_hash":"policy-1",' + '"signer_key_epoch":"epoch-1","heartbeat_seconds":0,' + '"chain_open_seq":0,"genesis_hash":"g1:abc",' + '"genesis_anchor_head":"anchor-head","genesis_anchor_log":"anchor-log",' + '"posture_capsule_sha256":"capsule","posture_signer_key_id":"posture-key",' + '"containment_nonce":"containment","contained_uid":"966"}' + ) in canonical + + +def test_session_control_close_payload_keys_in_go_struct_order_with_zero_uints(): + record = _full_field_record() + record["session_control"] = { + "kind": "session_close", + "close": { + "run_nonce": "run-1", + "open_nonce": "open-1", + "final_seq": 10, + "root_hash": "root-1", + "receipt_count": 11, + "close_reason": "graceful_shutdown", + "fsync_errors_gated": 0, + "durability_blocks": 0, + }, + } + canonical = canonicalize_action_record(record).decode() + assert ( + '"close":{"run_nonce":"run-1","open_nonce":"open-1","final_seq":10,' + '"root_hash":"root-1","receipt_count":11,"close_reason":"graceful_shutdown",' + '"fsync_errors_gated":0,"durability_blocks":0}' + ) in canonical + + def test_nested_taint_source_level_is_numeric(): canonical = canonicalize_action_record(_full_field_record()).decode() # session.TaintLevel is a uint8 -> serializes as a number, not a string. diff --git a/tests/test_regressions.py b/tests/test_regressions.py index 4f5d1fb..29c6729 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -7,6 +7,7 @@ from __future__ import annotations +import hashlib import json from pathlib import Path @@ -15,7 +16,7 @@ import pipelock_verify from pipelock_verify.__main__ import main -from pipelock_verify._canonical import canonicalize_action_record +from pipelock_verify._canonical import canonicalize_action_record, canonicalize_receipt CONFORMANCE_DIR = Path(__file__).parent / "conformance" @@ -361,3 +362,432 @@ def test_v2_in_chain_fallback_to_index_when_chain_seq_not_int(tmp_path): result = pipelock_verify.verify_chain(f) assert not result.valid assert result.broken_at_seq == 0 + + +# --- Finding 7: v1 chain/session-control fail-closed parity --- + + +def _write_signed_chain(tmp_path: Path, *receipts: dict) -> Path: + f = tmp_path / "chain.jsonl" + f.write_text("\n".join(json.dumps(r, separators=(",", ":")) for r in receipts) + "\n") + return f + + +def _receipt_hash(receipt: dict) -> str: + return hashlib.sha256(canonicalize_receipt(receipt)).hexdigest() + + +def test_verify_chain_rejects_bool_chain_seq_even_with_valid_signature(tmp_path): + ar = _valid_action_record() + ar["action_id"] = "bool-chain-seq" + ar["chain_seq"] = False + receipt = _sign_action_record(ar) + + result = pipelock_verify.verify_chain(_write_signed_chain(tmp_path, receipt)) + + assert not result.valid + assert "chain_seq must be a uint64" in (result.error or "") + + +def test_verify_chain_rejects_negative_chain_seq_even_with_valid_signature(tmp_path): + ar = _valid_action_record() + ar["action_id"] = "negative-chain-seq" + ar["chain_seq"] = -1 + receipt = _sign_action_record(ar) + + result = pipelock_verify.verify_chain(_write_signed_chain(tmp_path, receipt)) + + assert not result.valid + assert "chain_seq must be a uint64" in (result.error or "") + + +@pytest.mark.parametrize( + ("session_control", "want"), + [ + ("not-object", "session_control must be an object"), + ({"kind": "session_open"}, "session_control must carry exactly one payload"), + ( + {"kind": "session_open", "heartbeat": {}}, + "session_open kind missing open payload", + ), + ( + {"kind": "heartbeat", "open": {}}, + "heartbeat kind missing heartbeat payload", + ), + ( + {"kind": "session_close", "open": {}}, + "session_close kind missing close payload", + ), + ({"kind": "unknown", "open": {}}, "unknown session_control kind"), + ( + {"kind": "heartbeat", "heartbeat": {"beat": True}}, + "session_heartbeat beat must be a uint64", + ), + ( + {"kind": "session_open", "open": {"heartbeat_seconds": True}}, + "session_open heartbeat_seconds must be an integer", + ), + ( + {"kind": "session_open", "open": {"heartbeat_seconds": 2**63}}, + "session_open heartbeat_seconds must be an integer", + ), + ], +) +def test_verify_chain_rejects_malformed_session_control_even_with_valid_signature( + tmp_path, + session_control, + want, +): + ar = _valid_action_record() + ar["action_id"] = "malformed-session-control" + ar["session_control"] = session_control + receipt = _sign_action_record(ar) + + result = pipelock_verify.verify_chain(_write_signed_chain(tmp_path, receipt)) + + assert not result.valid + assert want in (result.error or "") + + +def test_verify_chain_rejects_first_run_nonce_without_session_open(tmp_path): + ar = _valid_action_record() + ar["action_id"] = "run-nonce-no-open" + ar["run_nonce"] = "run-attack" + receipt = _sign_action_record(ar) + + result = pipelock_verify.verify_chain(_write_signed_chain(tmp_path, receipt)) + + assert not result.valid + assert "run_nonce first receipt is not a matching session_open" in (result.error or "") + + +def test_verify_chain_rejects_unknown_signed_v1_field_even_with_valid_signature(tmp_path): + ar = _valid_action_record() + ar["action_id"] = "unknown-field" + ar["unsigned_but_plausible"] = "display-me" + receipt = _sign_action_record(ar) + + result = pipelock_verify.verify_chain(_write_signed_chain(tmp_path, receipt)) + + assert not result.valid + assert "unknown signed field" in (result.error or "") + + +@pytest.mark.parametrize( + ("field", "value", "want"), + [ + ("redaction", {"unknown": 1}, "redaction carries unknown signed field"), + ("redaction", {"total_redactions": True}, "redaction total_redactions must be an integer"), + ("shield", {"unknown": 1}, "shield carries unknown signed field"), + ("shield", {"total_rewrites": True}, "shield total_rewrites must be an integer"), + ( + "recent_taint_sources", + [{"url": "https://src.example", "kind": "k", "level": True}], + "recent_taint_sources[0] level must be a uint8", + ), + ( + "recent_taint_sources", + [{"url": "https://src.example", "kind": "k", "level": 4, "unknown": "x"}], + "recent_taint_sources[0] carries unknown signed field", + ), + ], +) +def test_verify_chain_rejects_malformed_existing_signed_nested_objects( + tmp_path, + field, + value, + want, +): + ar = _valid_action_record() + ar["action_id"] = "malformed-existing-nested" + ar[field] = value + receipt = _sign_action_record(ar) + + result = pipelock_verify.verify_chain(_write_signed_chain(tmp_path, receipt)) + + assert not result.valid + assert want in (result.error or "") + + +def test_verify_chain_rejects_first_receipt_key_transition_even_with_valid_signature(tmp_path): + ar = _valid_action_record() + ar["action_id"] = "first-key-transition" + ar["key_transition"] = { + "prior_signer_key": "old-key", + "prior_chain_seq": 99, + "prior_chain_hash": "old-hash", + } + receipt = _sign_action_record(ar) + + result = pipelock_verify.verify_chain(_write_signed_chain(tmp_path, receipt)) + + assert not result.valid + assert "chain starts at a key_transition segment" in (result.error or "") + + +def test_verify_chain_rejects_non_genesis_key_transition_even_with_valid_signature(tmp_path): + first = _sign_action_record(_valid_action_record()) + ar = _valid_action_record() + ar["action_id"] = "non-genesis-key-transition" + ar["timestamp"] = "2026-04-09T13:50:01Z" + ar["chain_seq"] = 1 + ar["chain_prev_hash"] = _receipt_hash(first) + ar["key_transition"] = { + "prior_signer_key": first["signer_key"], + "prior_chain_seq": 0, + "prior_chain_hash": ar["chain_prev_hash"], + } + second = _sign_action_record(ar) + + result = pipelock_verify.verify_chain(_write_signed_chain(tmp_path, first, second)) + + assert not result.valid + assert "key_transition marker on a non-genesis receipt" in (result.error or "") + + +def test_verify_chain_rejects_session_control_without_receipt_run_nonce(tmp_path): + ar = _valid_action_record() + ar["action_id"] = "session-control-no-receipt-run" + ar["session_control"] = { + "kind": "heartbeat", + "heartbeat": { + "run_nonce": "run-2", + "open_nonce": "open-2", + "beat": 1, + "chain_head": "genesis", + "chain_seq_head": 0, + "heartbeat_time": "2026-04-09T13:50:01Z", + "fsync_errors_gated": 0, + "durability_blocks": 0, + }, + } + receipt = _sign_action_record(ar) + + result = pipelock_verify.verify_chain(_write_signed_chain(tmp_path, receipt)) + + assert not result.valid + assert "session_control receipt missing run_nonce" in (result.error or "") + + +def test_verify_chain_accepts_restart_session_open_bound_to_prior_tail(tmp_path): + first = _sign_action_record(_valid_action_record()) + prior_hash = _receipt_hash(first) + ar = _valid_action_record() + ar["action_id"] = "good-restart-open" + ar["timestamp"] = "2026-04-09T13:50:01Z" + ar["chain_seq"] = 1 + ar["chain_prev_hash"] = prior_hash + ar["run_nonce"] = "run-2" + ar["session_control"] = { + "kind": "session_open", + "open": { + "run_nonce": "run-2", + "open_nonce": "open-2", + "recorder_session": "proxy", + "policy_hash": "policy", + "signer_key_epoch": first["signer_key"], + "heartbeat_seconds": 60, + "chain_open_seq": 1, + "prior_chain_head": prior_hash, + "prior_chain_seq": 0, + }, + } + second = _sign_action_record(ar) + + result = pipelock_verify.verify_chain(_write_signed_chain(tmp_path, first, second)) + + assert result.valid, result.error + + +@pytest.mark.parametrize( + ("mutate", "want"), + [ + ( + lambda open_payload, prior_hash: open_payload.update( + {"prior_chain_head": "not-the-prior-tail"} + ), + "session_open prior_chain_head does not match prior tail hash", + ), + ( + lambda open_payload, prior_hash: open_payload.update({"prior_chain_seq": 99}), + "session_open prior_chain_seq does not match prior tail seq", + ), + ( + lambda open_payload, prior_hash: open_payload.update({"chain_open_seq": 2}), + "session_open chain_open_seq does not match receipt chain_seq", + ), + ( + lambda open_payload, prior_hash: open_payload.update({"genesis_hash": "g1:bad"}), + "restart session_open must not carry genesis_hash", + ), + ], +) +def test_verify_chain_rejects_malformed_restart_session_open_even_with_valid_signature( + tmp_path, + mutate, + want, +): + first = _sign_action_record(_valid_action_record()) + prior_hash = _receipt_hash(first) + ar = _valid_action_record() + ar["action_id"] = "bad-restart-open" + ar["timestamp"] = "2026-04-09T13:50:01Z" + ar["chain_seq"] = 1 + ar["chain_prev_hash"] = prior_hash + ar["run_nonce"] = "run-2" + open_payload = { + "run_nonce": "run-2", + "open_nonce": "open-2", + "recorder_session": "proxy", + "policy_hash": "policy", + "signer_key_epoch": first["signer_key"], + "heartbeat_seconds": 60, + "chain_open_seq": 1, + "prior_chain_head": prior_hash, + "prior_chain_seq": 0, + } + mutate(open_payload, prior_hash) + ar["session_control"] = {"kind": "session_open", "open": open_payload} + second = _sign_action_record(ar) + + result = pipelock_verify.verify_chain(_write_signed_chain(tmp_path, first, second)) + + assert not result.valid + assert want in (result.error or "") + + +def test_verify_chain_rejects_heartbeat_with_wrong_tail_binding(tmp_path): + first = _sign_action_record(_valid_action_record()) + prior_hash = _receipt_hash(first) + open_ar = _valid_action_record() + open_ar["action_id"] = "restart-open-before-heartbeat" + open_ar["timestamp"] = "2026-04-09T13:50:01Z" + open_ar["chain_seq"] = 1 + open_ar["chain_prev_hash"] = prior_hash + open_ar["run_nonce"] = "run-2" + open_ar["session_control"] = { + "kind": "session_open", + "open": { + "run_nonce": "run-2", + "open_nonce": "open-2", + "recorder_session": "proxy", + "policy_hash": "policy", + "signer_key_epoch": first["signer_key"], + "heartbeat_seconds": 60, + "chain_open_seq": 1, + "prior_chain_head": prior_hash, + "prior_chain_seq": 0, + }, + } + second = _sign_action_record(open_ar) + second_hash = _receipt_hash(second) + + heartbeat_ar = _valid_action_record() + heartbeat_ar["action_id"] = "bad-heartbeat" + heartbeat_ar["timestamp"] = "2026-04-09T13:50:02Z" + heartbeat_ar["chain_seq"] = 2 + heartbeat_ar["chain_prev_hash"] = second_hash + heartbeat_ar["run_nonce"] = "run-2" + heartbeat_ar["session_control"] = { + "kind": "heartbeat", + "heartbeat": { + "run_nonce": "run-2", + "open_nonce": "open-2", + "beat": 1, + "chain_head": "not-the-prior-tail", + "chain_seq_head": 1, + "heartbeat_time": "2026-04-09T13:50:02Z", + "fsync_errors_gated": 0, + "durability_blocks": 0, + }, + } + third = _sign_action_record(heartbeat_ar) + + result = pipelock_verify.verify_chain(_write_signed_chain(tmp_path, first, second, third)) + + assert not result.valid + assert "heartbeat chain_head mismatch" in (result.error or "") + + +def test_verify_chain_rejects_session_close_with_wrong_root_hash(tmp_path): + first = _sign_action_record(_valid_action_record()) + prior_hash = _receipt_hash(first) + open_ar = _valid_action_record() + open_ar["action_id"] = "restart-open-before-close" + open_ar["timestamp"] = "2026-04-09T13:50:01Z" + open_ar["chain_seq"] = 1 + open_ar["chain_prev_hash"] = prior_hash + open_ar["run_nonce"] = "run-2" + open_ar["session_control"] = { + "kind": "session_open", + "open": { + "run_nonce": "run-2", + "open_nonce": "open-2", + "recorder_session": "proxy", + "policy_hash": "policy", + "signer_key_epoch": first["signer_key"], + "heartbeat_seconds": 60, + "chain_open_seq": 1, + "prior_chain_head": prior_hash, + "prior_chain_seq": 0, + }, + } + second = _sign_action_record(open_ar) + second_hash = _receipt_hash(second) + + close_ar = _valid_action_record() + close_ar["action_id"] = "bad-close" + close_ar["timestamp"] = "2026-04-09T13:50:02Z" + close_ar["chain_seq"] = 2 + close_ar["chain_prev_hash"] = second_hash + close_ar["run_nonce"] = "run-2" + close_ar["session_control"] = { + "kind": "session_close", + "close": { + "run_nonce": "run-2", + "open_nonce": "open-2", + "final_seq": 2, + "root_hash": "not-the-prior-tail", + "receipt_count": 3, + "close_reason": "normal", + "fsync_errors_gated": 0, + "durability_blocks": 0, + }, + } + third = _sign_action_record(close_ar) + + result = pipelock_verify.verify_chain(_write_signed_chain(tmp_path, first, second, third)) + + assert not result.valid + assert "session_close root_hash mismatch" in (result.error or "") + + +# The key_transition SHAPE gate is only reachable through the single-receipt +# path: verify_chain() rejects any key_transition marker by POSITION first +# (seq 0 -> "starts at a key_transition segment", seq > 0 -> "non-genesis"), +# so a chain-level test can never exercise shape validation. Without these, +# _validate_key_transition_shape passes every test even when deleted. +@pytest.mark.parametrize( + ("marker", "want"), + [ + ( + {"prior_signer_key": "old", "prior_chain_seq": 0, "prior_chain_hash": "h", "bogus": 1}, + "key_transition carries unknown signed field", + ), + ({"prior_signer_key": 123}, "key_transition prior_signer_key must be a string"), + ({"prior_chain_hash": 123}, "key_transition prior_chain_hash must be a string"), + ({"prior_chain_seq": -1}, "key_transition prior_chain_seq must be a uint64"), + ({"prior_chain_seq": True}, "key_transition prior_chain_seq must be a uint64"), + ("not-an-object", "key_transition must be an object"), + ], +) +def test_verify_rejects_malformed_key_transition_even_with_valid_signature(marker, want): + ar = _valid_action_record() + ar["action_id"] = "malformed-key-transition" + ar["key_transition"] = marker + receipt = _sign_action_record(ar) + + result = pipelock_verify.verify(json.dumps(receipt)) + + assert not result.valid + assert want in (result.error or "") From 904914cb246c8bdc00e7a40d7e696745d39ffe45 Mon Sep 17 00:00:00 2001 From: luckyPipewrench Date: Fri, 24 Jul 2026 20:03:52 -0400 Subject: [PATCH 2/3] fix: cover the unsigned ext bag in the chain hash and bound integer fields The receipt envelope canonicalization omitted ext, but Go's ReceiptHash is a digest over the marshalled receipt and ext is a marshalled field. This verifier therefore agreed with Go only while ext was absent, and ext could be attached or altered on a receipt without changing the chain hash it commits to. ext is now ordered last in the envelope, matching the Go struct, and stays out of the signed action record projection where it does not belong. Routes the redaction and shield counters through the int64 bound rather than accepting arbitrary integers, so a value the emitter could never produce and Go would refuse to decode is rejected here too. Drops the now unused unbounded helper. Pins the live chain regression to the root hash the Go verifier independently computed for that fixture. Two verifiers can agree a chain is well formed while disagreeing on what it commits to, so validity alone did not cover the parity objective. --- pipelock_verify/_canonical.py | 9 ++++++++ pipelock_verify/_verify.py | 19 ++++++++-------- tests/test_conformance.py | 6 +++++ tests/test_regressions.py | 42 +++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 10 deletions(-) diff --git a/pipelock_verify/_canonical.py b/pipelock_verify/_canonical.py index 8c149d0..fba2814 100644 --- a/pipelock_verify/_canonical.py +++ b/pipelock_verify/_canonical.py @@ -109,6 +109,15 @@ ("action_record", False, "action_record"), ("signature", False, None), ("signer_key", False, None), + # Ext is the one tolerated unknown top-level surface on a v1 receipt: an + # UNSIGNED, advisory forward-compat bag that verification never consults. + # It is deliberately absent from the SIGNED action-record projection, but + # Go's ReceiptHash is sha256 over json.Marshal(Receipt), which DOES include + # ext. Omitting it here would make the chain hash agree with Go only while + # ext is absent, and would let ext be added or altered on a receipt without + # changing this verifier's chain hash. Ordered last, omitempty, per the Go + # struct. + ("ext", True, None), ] # RedactionSummary fields, Go struct order (receipt.RedactionSummary). diff --git a/pipelock_verify/_verify.py b/pipelock_verify/_verify.py index 01bc277..ea66839 100644 --- a/pipelock_verify/_verify.py +++ b/pipelock_verify/_verify.py @@ -324,12 +324,6 @@ def _require_uint64(obj: dict[str, Any], name: str, where: str) -> str | None: return None -def _require_int(obj: dict[str, Any], name: str, where: str) -> str | None: - if name in obj and (not isinstance(obj[name], int) or isinstance(obj[name], bool)): - return f"{where} {name} must be an integer" - return None - - def _require_bool(obj: dict[str, Any], name: str, where: str) -> str | None: if name in obj and not isinstance(obj[name], bool): return f"{where} {name} must be a boolean" @@ -361,7 +355,7 @@ def _validate_redaction_shape(action_record: dict[str, Any]) -> str | None: for name in ("profile", "provider", "parser"): if err := _require_string(redaction, name, "redaction"): return err - if err := _require_int(redaction, "total_redactions", "redaction"): + if err := _require_int64(redaction, "total_redactions", "redaction"): return err if "by_class" in redaction and redaction["by_class"] is not None: by_class = redaction["by_class"] @@ -370,8 +364,13 @@ def _validate_redaction_shape(action_record: dict[str, Any]) -> str | None: for key, value in by_class.items(): if not isinstance(key, str): return "redaction by_class keys must be strings" - if not isinstance(value, int) or isinstance(value, bool): - return "redaction by_class values must be integers" + if ( + not isinstance(value, int) + or isinstance(value, bool) + or value < -_INT64_MAX - 1 + or value > _INT64_MAX + ): + return "redaction by_class values must be int64" return _require_bool(redaction, "cache_boundary_kept", "redaction") @@ -400,7 +399,7 @@ def _validate_shield_shape(action_record: dict[str, Any]) -> str | None: "adaptive_signals_recorded", "adaptive_signal_max_per_body", ): - if err := _require_int(shield, name, "shield"): + if err := _require_int64(shield, name, "shield"): return err for name in ("fingerprint_shim_injected", "partial"): if err := _require_bool(shield, name, "shield"): diff --git a/tests/test_conformance.py b/tests/test_conformance.py index 4db39e3..40dc6b6 100644 --- a/tests/test_conformance.py +++ b/tests/test_conformance.py @@ -151,3 +151,9 @@ def test_v3_2_0_live_recorder_chain_verifies_with_pinned_key(): assert result.final_seq == 10 assert result.start_time == "2026-07-24T22:57:41.86819273Z" assert result.end_time == "2026-07-24T23:00:44.292030929Z" + # Pinned to the root hash the Go verifier independently computed for this + # exact chain (pipelock v3.2.0 `verify-receipt --chain`). Validity alone + # would not catch a divergence in the chain-hash construction: both + # verifiers can agree a chain is well formed while disagreeing on what it + # commits to. This is the parity assertion. + assert result.root_hash == "623d13a756891c52b6a4b3d400a95c3324da7b5c3b11c95446fb11aee2ea3063" diff --git a/tests/test_regressions.py b/tests/test_regressions.py index 29c6729..78b4f55 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -791,3 +791,45 @@ def test_verify_rejects_malformed_key_transition_even_with_valid_signature(marke assert not result.valid assert want in (result.error or "") + + +def test_receipt_chain_hash_covers_the_unsigned_ext_bag(): + """`ext` is unsigned but IS part of Go's chain hash. + + Go's ReceiptHash is sha256 over json.Marshal(Receipt), and Receipt.Ext is a + marshalled field. A verifier that drops ext from the receipt envelope agrees + with Go only while ext is absent, and lets ext be attached or altered on a + receipt without changing the chain hash it commits to. ext stays out of the + SIGNED action-record projection; this is the envelope hash only. + """ + base = { + "version": 1, + "action_record": {"version": 1}, + "signature": "ed25519:aa", + "signer_key": "bb", + } + with_ext = dict(base) | {"ext": {"note": "advisory"}} + altered_ext = dict(base) | {"ext": {"note": "tampered"}} + + bare = canonicalize_receipt(base) + tagged = canonicalize_receipt(with_ext) + altered = canonicalize_receipt(altered_ext) + + assert b'"ext"' in tagged + assert bare != tagged, "ext must change the receipt envelope hash" + assert tagged != altered, "altering ext must change the receipt envelope hash" + # ext is ordered last, matching the Go struct. + assert tagged.decode().index('"ext"') > tagged.decode().index('"signer_key"') + + +def test_ext_stays_out_of_the_signed_action_record_projection(): + """ext is advisory and unsigned: it must never enter the signing input.""" + ar = _valid_action_record() + ar["ext"] = {"note": "advisory"} + + # ext is not an action_record field at all, so the shape gate rejects it. + receipt = _sign_action_record(ar) + result = pipelock_verify.verify(json.dumps(receipt)) + + assert not result.valid + assert "unknown signed field" in (result.error or "") From 6e5ed2dc3053ef33b85579180979050ba69c6b06 Mon Sep 17 00:00:00 2001 From: luckyPipewrench Date: Fri, 24 Jul 2026 20:43:36 -0400 Subject: [PATCH 3/3] fix: preserve producer ext bytes so the chain hash matches Go exactly Go emits the ext bag verbatim into the bytes its receipt hash digests, so the chain hash commits to the producer's exact ext bytes. Parsing ext and re-serializing it changed key order, number spelling such as 1e+06, and escaping, which meant this verifier disagreed with Go on any receipt carrying ext even though the previous commit had started including ext in the envelope. The raw source slice is now kept whenever the caller supplies the original JSON, covering both the recorder detail object and the JSON string form. Documents the one case that cannot preserve bytes, an already parsed receipt passed straight to the library, along with why that direction is fail closed rather than exploitable. Joins a wrapped line in a README example so the formatter check passes on it. --- README.md | 4 +- pipelock_verify/_canonical.py | 117 +++++++++++++++++++++++++++++++--- pipelock_verify/_verify.py | 80 +++++++++++++++++++++++ tests/test_regressions.py | 71 +++++++++++++++++++++ 4 files changed, 261 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 5e6e194..3531263 100644 --- a/README.md +++ b/README.md @@ -64,9 +64,7 @@ Pass a flight-recorder JSONL path: chain = pipelock_verify.verify_chain("evidence-proxy-0.jsonl") if not chain.valid: - raise SystemExit( - f"chain broken at seq {chain.broken_at_seq}: {chain.error}" - ) + raise SystemExit(f"chain broken at seq {chain.broken_at_seq}: {chain.error}") print(f"CHAIN VALID: {chain.receipt_count} receipts, root {chain.root_hash}") ``` diff --git a/pipelock_verify/_canonical.py b/pipelock_verify/_canonical.py index fba2814..d4ceee4 100644 --- a/pipelock_verify/_canonical.py +++ b/pipelock_verify/_canonical.py @@ -28,12 +28,16 @@ # Field specs are (json_name, has_omitempty, nested_kind). nested_kind is one # of None, "action_record", "redaction", "shield", "taint_source", -# "key_transition", "session_control", or a session-control payload kind and -# tells the orderer to recurse into a nested object (or, for "taint_source", -# into each element of a nested array) so the nested object's keys are reordered -# to the Go struct order too. Go re-marshals nested structs in declaration -# order, so a verifier that leaves nested object keys in input order recomputes -# a different signing hash whenever the input keys are not already Go-ordered. +# "key_transition", "session_control", a session-control payload kind, or +# "raw_json". Nested struct kinds tell the orderer to recurse into a nested +# object (or, for "taint_source", into each element of a nested array) so the +# nested object's keys are reordered to the Go struct order too. "raw_json" +# models Go's json.RawMessage: it is compacted and HTML-escaped, but its inner +# object key order and number spellings are preserved. +# +# Go re-marshals nested structs in declaration order, so a verifier that leaves +# nested object keys in input order recomputes a different signing hash whenever +# the input keys are not already Go-ordered. # # ActionRecord fields in Go struct-tag order. Source of truth: # https://github.com/luckyPipewrench/pipelock/blob/main/internal/receipt/canonical.go @@ -117,7 +121,7 @@ # ext is absent, and would let ext be added or altered on a receipt without # changing this verifier's chain hash. Ordered last, omitempty, per the Go # struct. - ("ext", True, None), + ("ext", True, "raw_json"), ] # RedactionSummary fields, Go struct order (receipt.RedactionSummary). @@ -272,6 +276,71 @@ } +class _RawJSON: + """Go json.RawMessage bytes after json.Marshal-style compaction.""" + + __slots__ = ("data",) + + def __init__(self, data: bytes) -> None: + self.data = data + + +def raw_json_value(raw: str | bytes) -> _RawJSON: + """Return a RawMessage-compatible value for receipt-envelope canonicalization. + + Go's json.RawMessage is not a decoded map. When a receipt is unmarshalled, + the raw bytes of ``ext`` are retained; when it is marshalled for + ReceiptHash, Go compacts those bytes and applies its default HTML-safe + escaping, but it does not sort keys or normalize number spellings inside + the RawMessage. + """ + + text = raw.decode("utf-8") if isinstance(raw, bytes) else raw + out: list[str] = [] + in_string = False + escaped = False + for ch in text: + if in_string: + if escaped: + out.append(ch) + escaped = False + continue + if ch == "\\": + out.append(ch) + escaped = True + continue + if ch == '"': + out.append(ch) + in_string = False + continue + if ch == "<": + out.append("\\u003c") + continue + if ch == ">": + out.append("\\u003e") + continue + if ch == "&": + out.append("\\u0026") + continue + if ch == "\u2028": + out.append("\\u2028") + continue + if ch == "\u2029": + out.append("\\u2029") + continue + out.append(ch) + continue + + if ch == '"': + out.append(ch) + in_string = True + elif ch in " \t\r\n": + continue + else: + out.append(ch) + return _RawJSON("".join(out).encode("utf-8")) + + def _is_go_zero(value: Any) -> bool: """Return True if ``value`` matches Go's ``omitempty`` zero value. @@ -347,6 +416,12 @@ def _order_object( value = zero_values[name] else: value = obj[name] + if nested == "raw_json": + # RawMessage omitempty only drops nil/empty bytes. JSON input with + # an explicit ext value, including null, false, 0, "", {}, or [], + # unmarshals to non-empty raw bytes and is therefore marshalled. + ordered[name] = value + continue pointer_like_struct = nested in _NESTED_OBJECT_FIELDS and isinstance(value, dict) if omitempty and not pointer_like_struct and _is_go_zero(value): continue @@ -388,7 +463,33 @@ def _go_html_escape(serialized: str) -> str: def _to_canonical_bytes(obj: Any) -> bytes: """Serialize an ordered dict to canonical UTF-8 bytes.""" - raw = json.dumps(obj, separators=(",", ":"), ensure_ascii=False) + return _serialize_go_json(obj) + + +def _serialize_go_json(value: Any) -> bytes: + if isinstance(value, _RawJSON): + return value.data + if isinstance(value, dict): + parts: list[bytes] = [b"{"] + first = True + for key, item in value.items(): + if not first: + parts.append(b",") + first = False + parts.append(_serialize_go_json(str(key))) + parts.append(b":") + parts.append(_serialize_go_json(item)) + parts.append(b"}") + return b"".join(parts) + if isinstance(value, (list, tuple)): + parts = [b"["] + for index, item in enumerate(value): + if index: + parts.append(b",") + parts.append(_serialize_go_json(item)) + parts.append(b"]") + return b"".join(parts) + raw = json.dumps(value, separators=(",", ":"), ensure_ascii=False) return _go_html_escape(raw).encode("utf-8") diff --git a/pipelock_verify/_verify.py b/pipelock_verify/_verify.py index ea66839..c5d487c 100644 --- a/pipelock_verify/_verify.py +++ b/pipelock_verify/_verify.py @@ -24,6 +24,7 @@ _TAINT_SOURCE_FIELDS, canonicalize_action_record, canonicalize_receipt, + raw_json_value, ) from ._common import InvalidReceiptError as InvalidReceiptError from ._common import _is_valid_rfc3339 as _is_valid_rfc3339 @@ -46,6 +47,7 @@ _SESSION_OPEN_GENESIS_LABEL = "pipelock.receipt.session_open.v1" _UINT64_MAX = 2**64 - 1 _INT64_MAX = 2**63 - 1 +_JSON_DECODER = json.JSONDecoder() # Valid action_type enum values. Matches the allActionTypes map in # internal/receipt/action.go. A verifier that does not enforce this set @@ -231,9 +233,84 @@ def _parse_receipt(source: str | bytes | dict[str, Any]) -> dict[str, Any]: parsed = loads_no_duplicate_keys(source) if not isinstance(parsed, dict): raise InvalidReceiptError("receipt must be a JSON object") + _attach_raw_ext(parsed, source) return parsed +def _find_top_level_raw_member(text: str, member: str) -> str | None: + """Return the raw JSON value for a top-level object member, if present.""" + + length = len(text) + index = 0 + while index < length and text[index] in " \t\r\n": + index += 1 + if index >= length or text[index] != "{": + return None + index += 1 + + while True: + while index < length and text[index] in " \t\r\n": + index += 1 + if index >= length or text[index] == "}": + return None + key, index = _JSON_DECODER.raw_decode(text, index) + if not isinstance(key, str): + return None + while index < length and text[index] in " \t\r\n": + index += 1 + if index >= length or text[index] != ":": + return None + index += 1 + while index < length and text[index] in " \t\r\n": + index += 1 + value_start = index + _value, index = _JSON_DECODER.raw_decode(text, index) + if key == member: + return text[value_start:index] + while index < length and text[index] in " \t\r\n": + index += 1 + if index < length and text[index] == ",": + index += 1 + continue + return None + + +def _attach_receipt_raw_ext(receipt: dict[str, Any], receipt_text: str) -> None: + raw_ext = _find_top_level_raw_member(receipt_text, "ext") + if raw_ext is not None: + receipt["ext"] = raw_json_value(raw_ext) + + +def _attach_raw_ext(parsed: dict[str, Any], source_text: str) -> None: + """Preserve Go json.RawMessage bytes for receipt ext when source JSON exists. + + Go emits ``Receipt.Ext`` verbatim (compacted) into the bytes ``ReceiptHash`` + digests, so the chain hash commits to the producer's exact ext bytes: key + order, number spelling such as ``1e+06``, and escaping all survive. Parsing + ext and re-serializing it would silently change those bytes and diverge from + Go on receipts that carry ext, so the raw source slice is kept whenever the + caller gave us the original JSON text. + + A caller that passes an ALREADY-PARSED receipt (the ``dict`` entry points, + rather than a path or a JSON string) has no source bytes left to preserve, + so ext is re-serialized from the parsed value and the chain hash can differ + from Go for exotic-but-equivalent spellings. That direction is fail-closed: + such a chain is reported broken rather than accepted, and an attacker gains + nothing, because altering ext's parsed VALUE changes this verifier's hash + too. Verify from a path or the raw JSON text when ext byte fidelity matters. + """ + + if "action_record" in parsed and "signature" in parsed: + _attach_receipt_raw_ext(parsed, source_text) + return + + if not ("type" in parsed and "detail" in parsed and isinstance(parsed.get("detail"), dict)): + return + raw_detail = _find_top_level_raw_member(source_text, "detail") + if raw_detail is not None: + _attach_receipt_raw_ext(parsed["detail"], raw_detail) + + def _extract_receipt(parsed: dict[str, Any]) -> dict[str, Any] | None: """Pull a receipt dict out of a parsed JSONL line. @@ -260,6 +337,8 @@ def _extract_receipt(parsed: dict[str, Any]) -> dict[str, Any] | None: decoded = loads_no_duplicate_keys(detail) if not isinstance(decoded, dict): raise InvalidReceiptError("flight-recorder detail JSON did not decode to an object") + raw_detail = detail.decode("utf-8") if isinstance(detail, bytes) else detail + _attach_receipt_raw_ext(decoded, raw_detail) return decoded raise InvalidReceiptError( f"flight-recorder detail has unexpected type {type(detail).__name__}" @@ -289,6 +368,7 @@ def _read_jsonl(path: Path) -> list[dict[str, Any]]: raise json.JSONDecodeError(f"line {lineno}: {exc.msg}", exc.doc, exc.pos) from exc if not isinstance(parsed, dict): raise json.JSONDecodeError(f"line {lineno}: JSONL entry must be a JSON object", line, 0) + _attach_raw_ext(parsed, line) receipt = _extract_receipt(parsed) if receipt is None: # Non-receipt recorder entry (checkpoint, other event). Skip diff --git a/tests/test_regressions.py b/tests/test_regressions.py index 78b4f55..4e55213 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -10,6 +10,7 @@ import hashlib import json from pathlib import Path +from typing import Any import pytest from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey @@ -17,6 +18,7 @@ import pipelock_verify from pipelock_verify.__main__ import main from pipelock_verify._canonical import canonicalize_action_record, canonicalize_receipt +from pipelock_verify._verify import _parse_receipt CONFORMANCE_DIR = Path(__file__).parent / "conformance" @@ -822,6 +824,75 @@ def test_receipt_chain_hash_covers_the_unsigned_ext_bag(): assert tagged.decode().index('"ext"') > tagged.decode().index('"signer_key"') +@pytest.mark.parametrize( + ("raw_ext", "want_ext"), + [ + ('{"b":2,"a":1}', '{"b":2,"a":1}'), + ('{"s":"<&\u2028\u2029"}', '{"s":"\\u003c\\u0026\\u2028\\u2029"}'), + ('[ 1, {"z":0, "y":1} ]', '[1,{"z":0,"y":1}]'), + ( + '{"n":1e+06,"big":123456789012345678901234567890}', + '{"n":1e+06,"big":123456789012345678901234567890}', + ), + ("null", "null"), + ("{}", "{}"), + ("[]", "[]"), + ("false", "false"), + ("0", "0"), + ('""', '""'), + ], +) +def test_receipt_ext_matches_go_rawmessage_compaction(raw_ext: str, want_ext: str) -> None: + """ext is RawMessage-like: present JSON zeros stay, inner order is not sorted.""" + + receipt = _sign_action_record(_valid_action_record()) + raw = json.dumps(receipt, separators=(",", ":")) + raw = raw[:-1] + f',"ext":{raw_ext}' + "}" + parsed = _parse_receipt(raw) + + canonical = canonicalize_receipt(parsed).decode() + + assert canonical.endswith(f',"ext":{want_ext}}}') + + +def test_receipt_ext_from_plain_dict_preserves_insertion_order_and_json_zero_values() -> None: + receipt = _sign_action_record(_valid_action_record()) + receipt["ext"] = {"b": 2, "a": 1} + + canonical = canonicalize_receipt(receipt).decode() + + assert canonical.endswith(',"ext":{"b":2,"a":1}}') + + ext_values: tuple[Any, ...] = (None, {}, [], False, 0, "") + for ext in ext_values: + receipt["ext"] = ext + assert ',"ext":' in canonicalize_receipt(receipt).decode() + + +@pytest.mark.parametrize("detail_as_string", [False, True]) +def test_verify_chain_preserves_raw_ext_from_recorder_detail_shapes( + tmp_path: Path, detail_as_string: bool +) -> None: + receipt = _sign_action_record(_valid_action_record()) + raw_receipt = json.dumps(receipt, separators=(",", ":")) + raw_receipt = ( + raw_receipt[:-1] + ',"ext":{"n":1e+06,"nested":{"b":2,"a":1},"zero":0,"empty":{}}' + "}" + ) + expected_root = hashlib.sha256(canonicalize_receipt(_parse_receipt(raw_receipt))).hexdigest() + + if detail_as_string: + detail = json.dumps(raw_receipt, separators=(",", ":")) + else: + detail = raw_receipt + path = tmp_path / "chain.jsonl" + path.write_text(f'{{"type":"action_receipt","detail":{detail}}}\n') + + result = pipelock_verify.verify_chain(path) + + assert result.valid, result.error + assert result.root_hash == expected_root + + def test_ext_stays_out_of_the_signed_action_record_projection(): """ext is advisory and unsigned: it must never enter the signing input.""" ar = _valid_action_record()