From 07571a92db05394acdbc8fde77baed05ae335af7 Mon Sep 17 00:00:00 2001 From: luckyPipewrench Date: Sun, 31 May 2026 22:58:13 -0400 Subject: [PATCH 1/2] fix: align v1 canonicalization with Go and reject duplicate keys The v1 canonical field list was missing parent_action_id, the taint and contract blocks, severity, redaction, and shield, and it neither reordered nested object keys nor sorted free-form map keys. A receipt carrying any of those fields recomputed a different signing hash and failed verification here while passing in Go. Mirror the full Go ActionRecord field set, declaration order, and omitempty semantics; recurse into nested redaction/shield/taint objects; and sort map keys (by_class) to match json.Marshal. Reject duplicate object keys on the verify path (last-wins parsing is a smuggling vector that lets a first-occurrence display layer disagree with the signed value), and reject input nested beyond the shared cross-language depth cap so deeply nested input is rejected rather than crashing. Add regression tests covering the full field set, nested ordering, map-key sorting, duplicate-key rejection (including unicode-escaped keys), and depth. --- pipelock_verify/_canonical.py | 216 ++++++++++++++++++++++++---------- pipelock_verify/_common.py | 84 +++++++++++++ pipelock_verify/_verify.py | 5 +- tests/test_field_drift.py | 197 +++++++++++++++++++++++++++++++ 4 files changed, 439 insertions(+), 63 deletions(-) create mode 100644 tests/test_field_drift.py diff --git a/pipelock_verify/_canonical.py b/pipelock_verify/_canonical.py index 1b848cd..a94c9d2 100644 --- a/pipelock_verify/_canonical.py +++ b/pipelock_verify/_canonical.py @@ -26,49 +26,128 @@ import json from typing import Any -# ActionRecord fields in Go struct-tag order. Each tuple is -# (json_name, has_omitempty). Source of truth: +# 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. +# +# ActionRecord fields in Go struct-tag order. Source of truth: # https://github.com/luckyPipewrench/pipelock/blob/main/internal/receipt/action.go -_ACTION_RECORD_FIELDS: list[tuple[str, bool]] = [ - ("version", False), - ("action_id", False), - ("action_type", False), - ("timestamp", False), - ("principal", False), - ("actor", False), - ("delegation_chain", False), - ("target", False), - ("intent", True), - ("data_classes_in", True), - ("data_classes_out", True), - ("side_effect_class", False), - ("reversibility", False), - ("policy_hash", False), - ("verdict", False), - ("transport", False), - ("method", True), - ("layer", True), - ("pattern", True), - ("request_id", True), - ("chain_prev_hash", False), - ("chain_seq", False), - ("venue", True), - ("jurisdiction", True), - ("rulebook_id", True), - ("remedy_class", True), - ("contestation_window", True), - ("precedent_refs", True), +# 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 +# signature verification for receipts that carry it. +_ACTION_RECORD_FIELDS: 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), + ("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), + ("venue", True, None), + ("jurisdiction", True, None), + ("rulebook_id", True, None), + ("remedy_class", True, None), + ("contestation_window", True, None), + ("precedent_refs", True, None), ] # Receipt envelope fields in Go struct-tag order. Source of truth: # https://github.com/luckyPipewrench/pipelock/blob/main/internal/receipt/receipt.go -_RECEIPT_FIELDS: list[tuple[str, bool]] = [ - ("version", False), - ("action_record", False), - ("signature", False), - ("signer_key", False), +_RECEIPT_FIELDS: list[tuple[str, bool, str | None]] = [ + ("version", False, None), + ("action_record", False, "action_record"), + ("signature", False, None), + ("signer_key", False, None), ] +# RedactionSummary fields, Go struct order (receipt.RedactionSummary). +_REDACTION_FIELDS: list[tuple[str, bool, str | None]] = [ + ("profile", True, None), + ("provider", True, None), + ("parser", True, None), + ("total_redactions", True, None), + ("by_class", True, None), + ("cache_boundary_kept", True, None), +] + +# ShieldSummary fields, Go struct order (receipt.ShieldSummary). +_SHIELD_FIELDS: list[tuple[str, bool, str | None]] = [ + ("pipeline", True, None), + ("total_rewrites", True, None), + ("extension_probes", True, None), + ("tracking_beacons", True, None), + ("agent_traps", True, None), + ("fingerprint_shim_injected", True, None), + ("svg_foreign_objects", True, None), + ("svg_event_handlers", True, None), + ("svg_external_references", True, None), + ("svg_hidden_text", True, None), + ("svg_animation_injections", True, None), + ("body_bytes", True, None), + ("scanned_bytes", True, None), + ("partial", True, None), + ("adaptive_signals_recorded", True, None), + ("adaptive_signal_max_per_body", True, None), +] + +# TaintSourceRef fields, Go struct order (session.TaintSourceRef). +_TAINT_SOURCE_FIELDS: list[tuple[str, bool, str | None]] = [ + ("url", False, None), + ("kind", False, None), + ("level", False, None), + ("timestamp", False, None), + ("receipt_id", True, None), + ("match_reason", True, 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, +} + def _is_go_zero(value: Any) -> bool: """Return True if ``value`` matches Go's ``omitempty`` zero value. @@ -97,40 +176,55 @@ def _is_go_zero(value: Any) -> bool: return False -def _order_action_record(ar: dict[str, Any]) -> dict[str, Any]: - """Return a new dict with ActionRecord fields in Go's canonical order. +def _normalize_maps(value: Any) -> Any: + """Recursively sort the keys of any plain map, matching Go's json.Marshal. + + Go marshals a ``map[string]T`` with keys in sorted order. Struct fields are + emitted in declaration order (handled by the field-list orderers), but a + value that is a free-form map — the only one in the schema is + ``redaction.by_class`` — must have its keys sorted here or it canonicalizes + differently than Go whenever the input keys are not already sorted. Mirrors + the TypeScript ``normalizeMaps`` and Rust ``normalize_maps`` helpers. + """ + if isinstance(value, list): + return [_normalize_maps(item) for item in value] + if isinstance(value, dict): + return {key: _normalize_maps(value[key]) for key in sorted(value)} + return value + + +def _order_object( + obj: dict[str, Any], + fields: list[tuple[str, bool, str | None]], +) -> 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. - """ - ordered: dict[str, Any] = {} - for name, omitempty in _ACTION_RECORD_FIELDS: - if name not in ar: - continue - value = ar[name] - if omitempty and _is_go_zero(value): - continue - ordered[name] = value - return ordered - -def _order_receipt(receipt: dict[str, Any]) -> dict[str, Any]: - """Return a new dict with Receipt fields in Go's canonical order. + Nested fields recurse so nested object keys are reordered to Go order too: - The nested ``action_record`` is also reordered, so this function alone - is enough to produce canonical bytes for the full envelope (used when - computing chain prev_hash linkage). + * ``"action_record"``/``"redaction"``/``"shield"`` reorder an object value. + * ``"taint_source"`` reorders each object element of an array value. """ ordered: dict[str, Any] = {} - for name, omitempty in _RECEIPT_FIELDS: - if name not in receipt: + for name, omitempty, nested in fields: + if name not in obj: continue - value = receipt[name] + value = obj[name] if omitempty and _is_go_zero(value): continue - if name == "action_record" and isinstance(value, dict): - value = _order_action_record(value) + if nested == "taint_source" and isinstance(value, list): + value = [ + _order_object(item, _TAINT_SOURCE_FIELDS) if isinstance(item, dict) else item + for item in value + ] + elif nested in _NESTED_OBJECT_FIELDS and isinstance(value, dict): + value = _order_object(value, _NESTED_OBJECT_FIELDS[nested]) + else: + # Non-struct value: sort any free-form map keys to match Go. + value = _normalize_maps(value) ordered[name] = value return ordered @@ -164,7 +258,7 @@ def canonicalize_action_record(ar: dict[str, Any]) -> bytes: The returned bytes are what Go's ``ActionRecord.Canonical()`` produces and what gets SHA-256-hashed before Ed25519 signing. """ - return _to_canonical_bytes(_order_action_record(ar)) + return _to_canonical_bytes(_order_object(ar, _ACTION_RECORD_FIELDS)) def canonicalize_receipt(receipt: dict[str, Any]) -> bytes: @@ -173,4 +267,4 @@ def canonicalize_receipt(receipt: dict[str, Any]) -> bytes: The returned bytes are what Go's ``json.Marshal(receipt)`` produces and what gets SHA-256-hashed to form the next receipt's ``chain_prev_hash``. """ - return _to_canonical_bytes(_order_receipt(receipt)) + return _to_canonical_bytes(_order_object(receipt, _RECEIPT_FIELDS)) diff --git a/pipelock_verify/_common.py b/pipelock_verify/_common.py index 6aeaa23..b32bf02 100644 --- a/pipelock_verify/_common.py +++ b/pipelock_verify/_common.py @@ -8,6 +8,7 @@ from __future__ import annotations +import json import re from datetime import datetime from typing import Any @@ -64,3 +65,86 @@ def _is_valid_rfc3339(value: Any) -> bool: class InvalidReceiptError(Exception): """Raised when a receipt cannot be parsed as JSON at all.""" + + +class DuplicateKeyError(InvalidReceiptError): + """Raised when a receipt JSON object contains a duplicate key. + + ``json.loads`` silently keeps the last value for a duplicate key, so + ``{"verdict": "allow", "verdict": "block"}`` would decode as ``"block"`` + with no error. That is a parser-differential smuggling vector: a display or + log layer reading the first occurrence sees a value different from the one + the signature was verified against. The verify path rejects such input + before signature verification. + """ + + +def _reject_duplicate_pairs(pairs: list[tuple[str, Any]]) -> dict[str, Any]: + """``object_pairs_hook`` that rejects any duplicate key in an object. + + ``json.loads`` invokes this for every object at every nesting depth with + the full ordered list of (key, value) pairs *before* it collapses + duplicates, so a duplicate at any depth is caught. + """ + seen: set[str] = set() + for key, _ in pairs: + if key in seen: + raise DuplicateKeyError(f"duplicate object key: {key!r}") + seen.add(key) + return dict(pairs) + + +# Shared cross-language receipt-nesting cap. The Go and TypeScript verifiers +# enforce the same value and Rust inherits it from serde_json's default +# recursion limit, so all four reject input nested beyond this depth. Receipts +# nest ~4 levels, so honest input is never affected. +_MAX_NESTING_DEPTH = 128 + + +def _exceeds_max_depth(text: str) -> bool: + """Return True if text nests objects/arrays beyond _MAX_NESTING_DEPTH. + + A cheap string-aware scan (string contents are skipped) run before + json.loads so deeply nested ARRAYS — which never reach object_pairs_hook and + would otherwise raise CPython's RecursionError — are rejected at the same + depth as the other verifiers, rather than at the interpreter's stack limit. + """ + depth = 0 + in_string = False + escaped = False + for ch in text: + if in_string: + if escaped: + escaped = False + elif ch == "\\": + escaped = True + elif ch == '"': + in_string = False + continue + if ch == '"': + in_string = True + elif ch in "[{": + depth += 1 + if depth > _MAX_NESTING_DEPTH: + return True + elif ch in "]}": + depth -= 1 + return False + + +def loads_no_duplicate_keys(text: str | bytes) -> Any: + """``json.loads`` that rejects duplicate object keys at any nesting depth. + + Also rejects input nested beyond the shared cross-language depth cap. That + bound is enforced before json.loads so deeply nested arrays cannot make + CPython raise ``RecursionError`` (which is not a ``JSONDecodeError`` and + would otherwise escape the verify path's handling and crash the caller); a + ``RecursionError`` backstop covers any residual case. + """ + scan_text = text.decode("utf-8") if isinstance(text, bytes) else text + if _exceeds_max_depth(scan_text): + raise InvalidReceiptError("JSON nesting too deep") + try: + return json.loads(text, object_pairs_hook=_reject_duplicate_pairs) + except RecursionError as exc: + raise InvalidReceiptError("JSON nesting too deep") from exc diff --git a/pipelock_verify/_verify.py b/pipelock_verify/_verify.py index a63402e..d924a1c 100644 --- a/pipelock_verify/_verify.py +++ b/pipelock_verify/_verify.py @@ -14,6 +14,7 @@ from ._canonical import 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 # Wire format constants — keep in sync with internal/receipt/receipt.go. _RECEIPT_VERSION = 1 @@ -199,7 +200,7 @@ def _parse_receipt(source: str | bytes | dict[str, Any]) -> dict[str, Any]: raise InvalidReceiptError( f"unsupported source type {type(source).__name__}; expected str, bytes, or dict" ) - parsed = json.loads(source) + parsed = loads_no_duplicate_keys(source) if not isinstance(parsed, dict): raise InvalidReceiptError("receipt must be a JSON object") return parsed @@ -255,7 +256,7 @@ def _read_jsonl(path: Path) -> list[dict[str, Any]]: if not line: continue try: - parsed = json.loads(line) + parsed = loads_no_duplicate_keys(line) except json.JSONDecodeError as exc: raise json.JSONDecodeError(f"line {lineno}: {exc.msg}", exc.doc, exc.pos) from exc if not isinstance(parsed, dict): diff --git a/tests/test_field_drift.py b/tests/test_field_drift.py new file mode 100644 index 0000000..116e4c7 --- /dev/null +++ b/tests/test_field_drift.py @@ -0,0 +1,197 @@ +"""Regression tests for cross-language canonicalization drift and duplicate-key +rejection. + +These cover the two fixes that closed real cross-language verification +differentials: + +1. The v1 canonical field list was missing parent_action_id, the taint block, + the contract block, severity, redaction, and shield — so any receipt carrying + those fields recomputed a different signing hash and failed verification here + while passing in Go. The field list now mirrors the full Go ActionRecord. +2. ``json.loads`` silently keeps the last value for a duplicate object key, so a + receipt with a smuggled duplicate verdict could verify. The verify path now + rejects duplicate keys at any nesting depth. + +The tests are self-contained (no fixture files); the shared agent-egress-bench +corpus is exercised against this verifier by the cross-language gate in the +pipelock repository. +""" + +from __future__ import annotations + +import pytest + +from pipelock_verify._canonical import canonicalize_action_record +from pipelock_verify._common import ( + DuplicateKeyError, + InvalidReceiptError, + loads_no_duplicate_keys, +) +from pipelock_verify._verify import verify + + +def _full_field_record() -> dict: + """An action record populating one field from every block that was missing + from the canonical list before the drift fix.""" + return { + "version": 1, + "action_id": "drift-0001", + "parent_action_id": "drift-parent-0001", + "action_type": "write", + "timestamp": "2026-04-15T12:00:00Z", + "principal": "org:test", + "actor": "agent:test", + "delegation_chain": ["grant"], + "target": "https://example.com/spawn", + "side_effect_class": "external_write", + "reversibility": "compensatable", + "policy_hash": "sha256:abc", + "verdict": "allow", + "session_taint_level": "suspected", + "session_contaminated": True, + "recent_taint_sources": [ + { + "url": "https://src.example/a", + "kind": "response_injection", + "level": 4, + "timestamp": "2026-04-15T12:00:00Z", + "match_reason": "ignore_previous_instructions", + } + ], + "session_task_id": "task-1", + "authority_kind": "delegated", + "contract_winning_source": "operator_policy", + "contract_hash": "sha256:def", + "contract_generation": 4, + "transport": "https", + "severity": "medium", + "redaction": { + "profile": "strict", + "provider": "provider.example", + "total_redactions": 1, + "by_class": {"api_key": 1}, + }, + "shield": { + "pipeline": "browser_shield_v1", + "total_rewrites": 1, + "agent_traps": 1, + }, + "request_id": "req-1", + "chain_prev_hash": "genesis", + "chain_seq": 0, + } + + +def test_parent_action_id_ordered_after_action_id(): + canonical = canonicalize_action_record(_full_field_record()).decode() + assert ( + '"action_id":"drift-0001","parent_action_id":"drift-parent-0001","action_type"' in canonical + ) + + +def test_shield_ordered_after_redaction_before_request_id(): + canonical = canonicalize_action_record(_full_field_record()).decode() + # redaction object, then shield object, then request_id. + assert '"shield":{' in canonical + assert ( + canonical.index('"redaction":') + < canonical.index('"shield":') + < canonical.index('"request_id"') + ) + + +def test_nested_shield_keys_in_go_struct_order(): + canonical = canonicalize_action_record(_full_field_record()).decode() + # pipeline, then total_rewrites, then agent_traps (declaration order). + shield = canonical[canonical.index('"shield":') :] + assert ( + shield.index('"pipeline"') + < shield.index('"total_rewrites"') + < shield.index('"agent_traps"') + ) + + +def test_taint_block_present_in_canonical(): + canonical = canonicalize_action_record(_full_field_record()).decode() + for field in ( + "session_taint_level", + "session_contaminated", + "recent_taint_sources", + "authority_kind", + "contract_hash", + "contract_generation", + "severity", + ): + assert f'"{field}"' in canonical, f"{field} missing from canonical output" + + +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. + assert '"level":4' in canonical + + +def test_redaction_by_class_map_keys_sorted_like_go(): + # Go's json.Marshal sorts map keys; a verifier that preserves input order + # diverges whenever by_class keys are not already sorted. + record = _full_field_record() + record["redaction"]["by_class"] = {"email": 2, "api_key": 1} + canonical = canonicalize_action_record(record).decode() + assert '"by_class":{"api_key":1,"email":2}' in canonical + + +# ---- duplicate-key rejection ---- + + +def test_loads_rejects_top_level_duplicate(): + with pytest.raises(DuplicateKeyError): + loads_no_duplicate_keys('{"a":1,"a":2}') + + +def test_loads_rejects_nested_duplicate(): + with pytest.raises(DuplicateKeyError): + loads_no_duplicate_keys('{"x":{"a":1,"a":2}}') + + +def test_loads_rejects_duplicate_in_array_element(): + with pytest.raises(DuplicateKeyError): + loads_no_duplicate_keys('{"arr":[{"a":1},{"a":1,"a":2}]}') + + +def test_loads_rejects_unicode_escaped_duplicate(): + # "a" decodes to "a"; must be caught (cross-language smuggling vector). + with pytest.raises(DuplicateKeyError): + loads_no_duplicate_keys('{"a":1,"\\u0061":2}') + + +def test_loads_rejects_over_deep_nesting(): + deep = "[" * 200 + "1" + "]" * 200 + with pytest.raises(InvalidReceiptError): + loads_no_duplicate_keys(deep) + + +def test_loads_accepts_modest_nesting(): + modest = "[" * 100 + "1" + "]" * 100 + assert loads_no_duplicate_keys(modest) is not None + + +def test_loads_accepts_clean_nested_json(): + assert loads_no_duplicate_keys('{"a":1,"b":{"c":2},"d":[{"e":3},{"e":4}]}') == { + "a": 1, + "b": {"c": 2}, + "d": [{"e": 3}, {"e": 4}], + } + + +def test_verify_rejects_duplicate_verdict_key(): + # A duplicate verdict key must be rejected at parse, before signature checks. + receipt = ( + '{"version":1,"action_record":{"version":1,"action_id":"x",' + '"action_type":"write","timestamp":"2026-04-15T12:00:00Z",' + '"verdict":"allow","verdict":"block","target":"https://e.example",' + '"transport":"https","chain_prev_hash":"genesis","chain_seq":0},' + '"signature":"ed25519:00","signer_key":"00"}' + ) + result = verify(receipt) + assert not result.valid + assert "duplicate object key" in (result.error or "") From 2fc73e50e263ab4ab16073cbd09cf6059447b978 Mon Sep 17 00:00:00 2001 From: luckyPipewrench Date: Mon, 1 Jun 2026 09:36:13 -0400 Subject: [PATCH 2/2] fix: reject duplicate keys in string receipt details --- pipelock_verify/_common.py | 7 +++---- pipelock_verify/_verify.py | 2 +- tests/test_field_drift.py | 22 ++++++++++++++++++---- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/pipelock_verify/_common.py b/pipelock_verify/_common.py index b32bf02..3bad63d 100644 --- a/pipelock_verify/_common.py +++ b/pipelock_verify/_common.py @@ -94,10 +94,9 @@ def _reject_duplicate_pairs(pairs: list[tuple[str, Any]]) -> dict[str, Any]: return dict(pairs) -# Shared cross-language receipt-nesting cap. The Go and TypeScript verifiers -# enforce the same value and Rust inherits it from serde_json's default -# recursion limit, so all four reject input nested beyond this depth. Receipts -# nest ~4 levels, so honest input is never affected. +# Shared cross-language receipt-nesting cap. All four reference verifiers +# enforce the same value explicitly and reject input nested beyond this depth. +# Receipts nest ~4 levels, so honest input is never affected. _MAX_NESTING_DEPTH = 128 diff --git a/pipelock_verify/_verify.py b/pipelock_verify/_verify.py index d924a1c..513fba8 100644 --- a/pipelock_verify/_verify.py +++ b/pipelock_verify/_verify.py @@ -229,7 +229,7 @@ def _extract_receipt(parsed: dict[str, Any]) -> dict[str, Any] | None: if isinstance(detail, dict): return detail if isinstance(detail, (str, bytes)): - decoded = json.loads(detail) + decoded = loads_no_duplicate_keys(detail) if not isinstance(decoded, dict): raise InvalidReceiptError("flight-recorder detail JSON did not decode to an object") return decoded diff --git a/tests/test_field_drift.py b/tests/test_field_drift.py index 116e4c7..9d9b8fd 100644 --- a/tests/test_field_drift.py +++ b/tests/test_field_drift.py @@ -165,14 +165,14 @@ def test_loads_rejects_unicode_escaped_duplicate(): def test_loads_rejects_over_deep_nesting(): - deep = "[" * 200 + "1" + "]" * 200 + deep = "[" * 129 + "1" + "]" * 129 with pytest.raises(InvalidReceiptError): loads_no_duplicate_keys(deep) -def test_loads_accepts_modest_nesting(): - modest = "[" * 100 + "1" + "]" * 100 - assert loads_no_duplicate_keys(modest) is not None +def test_loads_accepts_exact_max_nesting(): + max_depth = "[" * 128 + "1" + "]" * 128 + assert loads_no_duplicate_keys(max_depth) is not None def test_loads_accepts_clean_nested_json(): @@ -195,3 +195,17 @@ def test_verify_rejects_duplicate_verdict_key(): result = verify(receipt) assert not result.valid assert "duplicate object key" in (result.error or "") + + +def test_verify_rejects_duplicate_key_inside_string_detail(): + # Flight-recorder entries may carry detail as a JSON string. That inner JSON + # must use the same duplicate-key rejecting loader as top-level receipts. + entry = ( + '{"type":"action_receipt","detail":"{\\"version\\":1,' + '\\"action_record\\":{\\"version\\":1,\\"verdict\\":\\"allow\\",' + '\\"verdict\\":\\"block\\"},\\"signature\\":\\"ed25519:00\\",' + '\\"signer_key\\":\\"00\\"}"}' + ) + result = verify(entry) + assert not result.valid + assert "duplicate object key" in (result.error or "")