Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions aggregator/_mmr_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ def __init__(
signer: "Ed25519CheckpointSigner | None" = None,
replay_entries: list[dict] | None = None,
) -> None:
import json

self._root_dir = root_dir
self._log_id = log_id

Expand Down Expand Up @@ -183,7 +181,12 @@ def __init__(
latest_path = root_dir / self._LATEST_FILENAME
self._latest: CheckpointRecord | None = None
if latest_path.exists():
self._latest = CheckpointRecord.from_dict(json.loads(latest_path.read_text()))
from trace_verify._verify import loads_unique

loaded = loads_unique(latest_path.read_text(encoding="utf-8"))
if not isinstance(loaded, dict):
raise ValueError(f"{latest_path} is not a JSON object")
self._latest = CheckpointRecord.from_dict(loaded)
if self._latest.log_id != log_id:
raise ValueError(
f"{latest_path} belongs to log_id={self._latest.log_id!r}, "
Expand Down
42 changes: 32 additions & 10 deletions aggregator/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,37 @@ def _read_json_body(self) -> object:
# Checked before any read. The length used to go straight to
# rfile.read: no cap, and a missing or negative one blocked the
# handler thread until the client closed the connection.
raw_length = self.headers.get("Content-Length")
if raw_length is None:
raw_lengths = (
(self.headers.get_all("Content-Length") or [])
if hasattr(self.headers, "get_all")
else [self.headers.get("Content-Length")]
)
raw_lengths = [v for v in raw_lengths if v is not None]
if not raw_lengths:
raise _BodyError(400, "Content-Length is required")
try:
length = int(raw_length)
except ValueError:
raise _BodyError(400, "Content-Length is not an integer") from None
if length < 0:
raise _BodyError(400, "Content-Length is negative")
if len(set(raw_lengths)) > 1:
raise _BodyError(400, "conflicting Content-Length headers")
raw_length = raw_lengths[0]
if not raw_length.isascii() or not raw_length.isdecimal():
raise _BodyError(400, "Content-Length is not a non-negative integer")
# Leading zeros are valid ("002") and are dropped before measuring, so
# a long run of zeros is the number it denotes. The digit count is
# checked before int(), which refuses past Python's integer-string
# limit and would otherwise turn "0" * 5000 into a 413.
digits = raw_length.lstrip("0") or "0"
if len(digits) > len(str(MAX_BODY_BYTES)):
raise _BodyError(413, f"body exceeds {MAX_BODY_BYTES} bytes")
length = int(digits)
if length > MAX_BODY_BYTES:
raise _BodyError(413, f"body exceeds {MAX_BODY_BYTES} bytes")
if length == 0:
return None
return loads_unique(self.rfile.read(length).decode("utf-8"))
raw_body = self.rfile.read(length)
# read(n) returns fewer bytes when the client closes early, and a valid
# JSON prefix would otherwise be accepted as the whole request.
if len(raw_body) != length:
raise _BodyError(400, "incomplete request body")
return loads_unique(raw_body.decode("utf-8"))

def do_POST(self):
if self.path != "/batch":
Expand All @@ -90,7 +107,12 @@ def do_POST(self):
raw_claims = body.get("claims")
if not isinstance(raw_claims, list) or not raw_claims:
raise ValueError("'claims' must be a non-empty list")
producer = body.get("producer")
if "producer" in body:
producer = body["producer"]
if not isinstance(producer, str) or not producer:
raise ValueError("'producer' must be a non-empty string when present")
else:
producer = None

if canonicalization_id == "as-transmitted":
# Each element is the producer's exact signed claim text --
Expand Down
7 changes: 4 additions & 3 deletions src/trace_verify/_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,14 @@ def _has_surrogate(text: str) -> bool:


def loads_unique(raw: bytes | str) -> Any:
"""``json.loads`` that refuses duplicate member names and non-finite constants.
"""``json.loads`` that refuses duplicate member names and non-finite numbers.

Plain ``json.loads`` keeps the last value for a repeated name, so a claim
carrying ``"hash"`` twice was signature-checked over one value while its
transmitted bytes also carried the other, and a first-wins parser reads
the one the signature never covered. Raises ValueError on a duplicate or
non-finite constant.
the one the signature never covered. It also turns ``1e999`` into
``inf``. Raises ValueError on a duplicate, a non-finite constant
(``NaN``, ``Infinity``) or a number that overflows to infinity.
"""
def _unique(pairs: list[tuple[str, object]]) -> dict:
result: dict = {}
Expand Down
Loading