diff --git a/docs/anchor-format.md b/docs/anchor-format.md index 4541990..a96f396 100644 --- a/docs/anchor-format.md +++ b/docs/anchor-format.md @@ -87,3 +87,16 @@ record. A failed legacy proof reports no established subject. Verifiers never use timestamps to infer a subject, extract an alternate object on failure, or try both objects. An explicit invalid marker or mismatch with the supplied file refuses. Checkpoint/batch-of-batches entries do not use this batch-only field. + +## Proposed portable inclusion-count bound + +The draft implementation follow-up to trace-spec#630 limits `leaf_count` to +1 through 9007199254740991 and `leaf_index` to 0 through `leaf_count - 1`. +This keeps the schema, installed verifier, standalone verifier and CLI within +the same interoperable integer range. The schema does not describe proof +indices; the verifier enforces their relation to the declared count. + +This is a proposed stricter acceptance rule pending the TRACE owner decision +in https://github.com/agentrust-io/trace-spec/pull/630. Existing oversized entries +would be refused by this candidate. It does not change leaf hashing, attest a +registry entry, or allocate or reconstruct a tree of the declared size. diff --git a/schema/registry-entry.schema.json b/schema/registry-entry.schema.json index cb4a4d1..297dc1a 100644 --- a/schema/registry-entry.schema.json +++ b/schema/registry-entry.schema.json @@ -20,6 +20,7 @@ "leaf_count": { "type": "integer", "minimum": 1, + "maximum": 9007199254740991, "description": "Total number of claims (leaves) covered by this entry" }, "producer": { diff --git a/src/trace_verify/__main__.py b/src/trace_verify/__main__.py index 9e58246..3e0911d 100644 --- a/src/trace_verify/__main__.py +++ b/src/trace_verify/__main__.py @@ -326,11 +326,11 @@ def _main_inclusion(argv: list[str] | None) -> int: if not isinstance(raw_path, list): raise ValueError("proof.audit_path must be a list") leaf_index = proof.get("leaf_index") - if type(leaf_index) is not int or leaf_index < 0: - raise ValueError("proof.leaf_index must be a non-negative integer") + if type(leaf_index) is not int or not 0 <= leaf_index < 2**53 - 1: + raise ValueError("proof.leaf_index must be an integer in 0..9007199254740990") leaf_count = entry.get("leaf_count") - if type(leaf_count) is not int or leaf_count < 1: - raise ValueError("entry.leaf_count must be a positive integer") + if type(leaf_count) is not int or not 1 <= leaf_count <= 2**53 - 1: + raise ValueError("entry.leaf_count must be an integer in 1..9007199254740991") audit_path = [decode_hash(h) for h in raw_path] merkle_root = decode_hash(entry.get("merkle_root")) ok = verify_inclusion( diff --git a/src/trace_verify/_verify.py b/src/trace_verify/_verify.py index 8883f2e..12b44a7 100644 --- a/src/trace_verify/_verify.py +++ b/src/trace_verify/_verify.py @@ -292,7 +292,7 @@ def verify_inclusion( return False if not isinstance(leaf_count, int) or isinstance(leaf_count, bool): return False - if leaf_index < 0 or leaf_count < 1 or leaf_index >= leaf_count: + if leaf_index < 0 or leaf_count < 1 or leaf_index >= leaf_count or leaf_count > 2**53 - 1: return False r = hashlib.sha256( diff --git a/tests/test_inclusion_safe_integer_bounds.py b/tests/test_inclusion_safe_integer_bounds.py new file mode 100644 index 0000000..a0bc125 --- /dev/null +++ b/tests/test_inclusion_safe_integer_bounds.py @@ -0,0 +1,78 @@ +"""Proposed interoperability bound from trace-spec#630; no large tree allocation.""" +import hashlib +import importlib.util +import json +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + +from jsonschema import Draft202012Validator +from trace_verify._verify import canonical_claim_bytes, verify_inclusion + +ROOT = Path(__file__).resolve().parents[1] +MAX = 2**53 - 1 +spec = importlib.util.spec_from_file_location('standalone_bounds', ROOT / 'tools/verify_inclusion.py') +standalone = importlib.util.module_from_spec(spec) +spec.loader.exec_module(standalone) + + +def case(count, index=0): + claim = json.loads((ROOT / 'samples/example-trust-record.json').read_text()) + leaf = hashlib.sha256(b'\x00' + canonical_claim_bytes(claim)).digest() + sibling = bytes(32) + if index == 0: + path = [sibling] * 53 + root = leaf + for node in path: + root = hashlib.sha256(b'\x01' + root + node).digest() + else: + # Rightmost leaf of a 2**53 + 1-leaf tree needs just its left subtree. + path = [sibling] + root = hashlib.sha256(b'\x01' + sibling + leaf).digest() + entry = {'ts': '2026-06-12T18:00:00Z', 'producer': 'test/1.0.0', + 'batch_id': 'bounds', 'leaf_count': count, 'leaf_subject': 'record', + 'merkle_root': 'sha256:' + root.hex()} + return claim, path, root, entry + + +class SafeIntegerBoundsTests(unittest.TestCase): + def test_library_and_standalone_bounds(self): + for verifier in (verify_inclusion, standalone.verify_inclusion): + for count, index, expected in ((MAX, 0, True), (MAX + 1, 0, False), + (MAX + 2, MAX + 1, False)): + with self.subTest(verifier=verifier.__module__, count=count, index=index): + claim, path, root, _ = case(count, index) + self.assertEqual(verifier(claim, index, path, count, root), expected) + + def test_registry_schema_bounds(self): + validator = Draft202012Validator(json.loads( + (ROOT / 'schema/registry-entry.schema.json').read_text())) + for count, expected in ((MAX, True), (MAX + 1, False)): + with self.subTest(count=count): + self.assertEqual(validator.is_valid(case(count)[3]), expected) + + def test_both_clis_accept_maximum_and_refuse_oversize(self): + with tempfile.TemporaryDirectory() as directory: + tmp = Path(directory) + for count, index, expected in ((MAX, 0, True), (MAX + 1, 0, False), + (MAX + 2, MAX + 1, False)): + claim, path, _, entry = case(count, index) + for name, value in [('claim', claim), ('entry', entry), + ('proof', {'leaf_index': index, 'audit_path': + ['sha256:' + p.hex() for p in path]})]: + (tmp / name).write_text(json.dumps(value)) + for command in ([sys.executable, '-m', 'trace_verify', '--no-verify-signature'], + [sys.executable, str(ROOT / 'tools/verify_inclusion.py')]): + with self.subTest(command=command, count=count, index=index): + result = subprocess.run(command + ['--claim', str(tmp / 'claim'), + '--entry', str(tmp / 'entry'), '--proof', str(tmp / 'proof')], + capture_output=True, text=True, cwd=ROOT, timeout=10) + self.assertEqual(result.returncode == 0, expected, + result.stdout + result.stderr) + + if '-m' in command and not expected: + self.assertEqual(result.returncode, 1) + field = 'proof.leaf_index' if index else 'entry.leaf_count' + self.assertIn(field, result.stderr) diff --git a/tools/verify_inclusion.py b/tools/verify_inclusion.py index 4298a69..36d69b0 100644 --- a/tools/verify_inclusion.py +++ b/tools/verify_inclusion.py @@ -286,7 +286,7 @@ def verify_inclusion( return False if not isinstance(leaf_count, int) or isinstance(leaf_count, bool): return False - if leaf_index < 0 or leaf_count < 1 or leaf_index >= leaf_count: + if leaf_index < 0 or leaf_count < 1 or leaf_index >= leaf_count or leaf_count > 2**53 - 1: return False r = hashlib.sha256(