Skip to content

Bound the canonical text, not only the input, by max_bytes - #59

Merged
zy4 merged 1 commit into
mainfrom
fix/canonical-size-bound
Oct 31, 2026
Merged

zy4 merged 1 commit into
mainfrom
fix/canonical-size-bound

Conversation

@SrishtiCode

Copy link
Copy Markdown
Collaborator

Port of the fix from dekrops/counterplay#2, the first correctness fix under the verified-cli freeze (dekrops/counterplay#5). Limits::max_bytes is documented to bound both input and canonical output, but parse() only checked the input and the exponent expansion separately, so [1e9] with max_bytes 10 was accepted although its canonical text is 12 bytes, and raw C1 and DEL characters grow when escaped. parse() now measures the exact canonical length (new Value::canonical_len(), computed without writing the text) and refuses anything over max_bytes. Adds regression tests for the exponent, C1, object-key and DEL cases, and a property test that canonical_len() equals the length of to_canonical_string(). All workspace tests pass, including the wvp corpus (641 cases). Resource limits may be tuned under the compatibility policy, so this is not a breaking change.

Signed-off-by: Srishti <rathisrishti@gmail.com>
@claude

claude Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Claude finished @SrishtiCode's task in 2m 22s —— View job


Claude review

PR summary. parse() already bounded the input bytes and a running count of exponent-expansion zeros, but neither bounded the canonical text that escapes raw C0/C1/DEL characters or that writes exponents in full. This PR adds Value::canonical_len(), measured without serializing, and refuses input whose canonical text exceeds limits.max_bytes. The property test ties canonical_len() to to_canonical_string().len() across generated values; targeted tests cover the exponent, raw C1, object-key and DEL cases.

Key changes reviewed

  • encode::canonical_len and string_len (crates/verified-canonical/src/encode.rs:41-77): branch-by-branch agreement with write_value/write_string, including the 2/6-byte escape split, non-escaped /, non-escaped U+00A0, and the N=0/N≥1 bracket+comma+colon arithmetic. Saturating adds keep the recursion safe without changing the computed bound at realistic sizes.
  • parse::parse post-check (crates/verified-canonical/src/parse.rs:187-194): placed after trailing-data; returns the same LimitExceeded::Bytes code as the pre-existing input and expansion checks.
  • Limits::max_bytes documentation (crates/verified-canonical/src/parse.rs:16): now consistent with the enforced invariant ("The largest input, and the largest canonical text, in bytes.").
  • Public Value::canonical_len surface (crates/verified-canonical/src/value.rs:40-43).
  • Regression and property coverage: tests/encoding.rs:216-244, tests/properties.rs:113-116.

Areas reviewed

  • canonical_len byte-for-byte correctness vs. write_value/write_string.
  • Interaction with the pre-existing input-size and exponent-expansion checks, and with parse_canonical.
  • Refusal-code stability: the new path still reports ParseError::Limit(LimitExceeded::Bytes { limit }), so no refusal-code change reaches callers.
  • Compatibility impact: docs/protocol/compatibility.md allows tuning resource limits; the stricter upper bound aligns with the documented max_bytes meaning, so this is not a protocol-level change.
  • Threat model fit (docs/security/threat-model.md): escapes and exponent expansion are now bounded on hostile input, matching "the bound is enforced while reading."
  • Depth and recursion: canonical_len recurses only after parse() has enforced max_depth, so stack use is bounded.

Areas skipped

  • Downstream crates were not re-read in depth: they only consume parse() via Limits::DEFAULT and the same ParseError, which this change preserves.

Findings

  • Severity high / medium / low: 0 / 0 / 0. No inline comments posted.

Residual risks

  • None identified in the changed lines. A future escape rule change in write_string must update string_len in lockstep; the property test enforces that, but a reviewer modifying one should remember to re-run it.

Claude review: no actionable issues found.

  • Read CLAUDE.md, CONTRIBUTING.md, overview, threat model, compatibility
  • Read the changed files
  • Analyze the correctness of Value::canonical_len
  • Check the parse() ordering / refusal code
  • Verify tests cover the stated regressions
  • Post inline findings (if any) and the summary
    · branch fix/canonical-size-bound

@zy4 zy4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked canonical_len against write_value and write_string, including the 2- and 6-byte escapes; the property test ties them together. Approving.

@zy4
zy4 merged commit 16cea17 into main Oct 31, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants