Skip to content

Limit implicit keys to 1024 characters - #708

Merged
frenck merged 3 commits into
mainfrom
frenck/simple-key-cap
Dec 4, 2026
Merged

frenck merged 3 commits into
mainfrom
frenck/simple-key-cap

Conversation

@frenck

@frenck frenck commented Dec 4, 2026

Copy link
Copy Markdown
Owner

Breaking change

An implicit mapping key longer than 1024 characters no longer loads. Before, there was no limit; now a document like this is rejected on every load path:

yamlrocks.loads(("k" * 1025 + ": value\n").encode())
# before: {'kkk...': 'value'}
# after:  YAMLRocksParseError: an implicit mapping key cannot be longer than 1024 characters (write a longer key as an explicit '? ' key) at line 1, column 1

This is what the YAML spec requires, and PyYAML (C and pure Python) rejects the same documents. To adapt, write a longer key in the explicit form, which has no limit:

? kkkk...
: value

dumps and the round-trip emitter now do that on their own. Nothing in the real-world corpus is affected.

Proposed change

YAML 1.2.2 (sections 7.4.2 and 8.2.2) limits an implicit key, the everyday key: value form, to a single line of at most 1024 characters. YAMLRocks had no limit, so a 1025 character plain key, a 2000 character quoted key and a long flow collection used as a key all loaded where PyYAML rejects them. Decided in the audit plan as a compliance fix.

The rule, as libyaml applies it:

  • The count is in characters, not bytes, from the key's first character (an anchor or tag in front of it included) up to the :, so spaces before the : count too.
  • It covers plain, single-quoted, double-quoted and flow collection keys, in block mappings, flow mappings and single pairs in flow sequences.
  • An explicit ? key has no limit, in block and in flow.
  • A short flow mapping key that spans lines ({ a\n : b }) still loads, as the spec and the YAML test suite require; only the length counts there.

The check lives in the scanner (MAX_IMPLICIT_KEY_CHARS in src/scanner/mod.rs). Every reader uses the one parser, so loads, OPT_ROUND_TRIP, OPT_ANNOTATED and !include give the same message, pointing at where the key starts. The verdict matches PyYAML's C and pure Python loaders on every boundary case checked (1024 against 1025 characters over 32 key shapes, including multibyte keys, properties, a BOM, comments and nested flow collections).

Emitters: a key that would come out longer than the limit is written in the explicit form, so output always loads back. Block: ? key then : value on the next line. Flow: {? key: v}. That holds for dumps, represent and the round-trip emitter, counted with one helper in src/emit_util.rs. A sweep of 12,960 dumps (nine key kinds, six value kinds, eight option sets, three widths, with and without represent) all reloaded to the same data, and libyaml loads the explicit output too. Unmodified documents re-emit byte for byte as before: any key that loaded is within the limit.

Speed: a pending key in the scanner is now dropped once it runs past the limit, so the tokens held back for it are released instead of waiting for the end of the line. On 1 MB of minified JSON, loads is about 1.25x faster with less than half the extra memory; round-trip about 1.08x. Pretty JSON, block documents and a minified value under a key are unchanged within noise.

Tests: new tests/core/test_long_keys.py (19 key shapes at the boundary on three load paths with exact messages and positions, !include, explicit keys of 5000 characters, dumps reloading across options, represent parity, round-trip re-emit), Rust boundary tests in the scanner (including one that the token queue stays bounded) and the encoder. Four existing fixtures used implicit keys over 1024 characters and now use the explicit form. The loading and dumping guides got a "Long keys" section.

Real-world corpus and YAML test suite: no file newly fails, and no pinned suite error message changed.

Type of change

  • Dependency or tooling upgrade
  • Bugfix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Deprecation (replaces or removes a feature, with a migration path)
  • Breaking change (a fix or feature that changes existing behavior)
  • Code quality, refactor, or test-only change
  • Documentation only

Additional information

Checklist

  • I have read the AI Policy, and this pull request was not created by an autonomous agent.
  • I fully understand the code in this pull request and can explain every line, including any AI-assisted changes.
  • The change is covered by tests, and uv run pytest passes locally. A pull request cannot be merged unless CI is green.
  • uv run ruff check . and uv run ruff format --check . pass.
  • cargo fmt --check and cargo clippy --all-targets -- -D warnings pass.
  • Round-trip fidelity is preserved: an unmodified document still re-emits byte-for-byte.
  • No commented-out or dead code is left in the pull request.

If the change is user-facing:

  • Documentation under docs/ is added or updated, and docs/verify_examples.py still passes.

YAML 1.2.2 sections 7.4.2 and 8.2.2 limit an implicit key to one line of
at most 1024 characters. There was no limit at all, so keys PyYAML
rejects loaded here. The scanner now counts characters from the key's
first character, properties included, up to its `:`, as libyaml does,
for plain, quoted and flow collection keys. An explicit `? key` has no
limit.

Both emitters write a longer key in the explicit form, so their output
always loads back. A pending key that grows past the limit is dropped
early, which releases the tokens held for it: minified JSON loads about
1.25x faster.
@frenck frenck added bugfix Inconsistencies or issues which will cause a problem for users or implementers. rust labels Dec 4, 2026
Copilot AI balanced review requested due to automatic review settings December 4, 2026 00:43
@coderabbitai

coderabbitai Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Explain Change →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 38 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d4518613-bd29-462f-ba16-8b8665cda5d4
Commits

Reviewing files that changed from the base of the PR and between ca5f7f4 and aad8c8c.

Files selected for processing: 3

  • src/emit_util.rs
  • src/scanner/mod.rs
  • tests/core/test_long_keys.py

No actionable comments were generated in the recent review.

Walkthrough

The scanner now rejects implicit mapping keys longer than 1,024 characters and reports the key’s starting location. Explicit keys remain unlimited. Block and flow emitters use explicit-key syntax for over-limit keys. Tests and guides cover the limit, emitted form, and round-trip behavior.

Possibly related PRs

  • frenck/YAMLRocks#638: Updates scanner handling of mapping-key properties and round-trip emission of explicit keys, overlapping with the scanner and round-trip emitter changes here.
Merge Risk: Minimal · previous review ca5f7

The change adds a 1024-character limit for implicit mapping keys. Longer keys now fail to load unless written as explicit keys, and the emitters write them explicitly so their output still loads. This is documented, tests cover it, and no merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "Limit implicit keys to 1024 characters" names the main change: implicit mapping keys longer than 1024 characters are now limited and rejected on load, with explicit-key output on emit. The …
Description check Passed The description covers the behavior change, the YAML 1.2.2 basis, the emitter adjustments, the implementation location, tests, and the checklist. It is directly related to the changeset.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "Limit implicit keys to 1024 characters" names the main change: implicit mapping keys longer than 1024 characters are now limited and rejected on load, with explicit-key output on emit. The changeset (scanner check, emitters, tests, guides) matches this title.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: cb3c3106-fa4a-4e18-83da-c3f20e2f417f
Commits

Reviewing files that changed from the base of the PR and between 8d92f79 and ca5f7f4.

Files selected for processing: 3

  • docs/src/content/docs/guides/dumping.md
  • docs/src/content/docs/guides/loading.md
  • src/scanner/mod.rs
Files skipped from review as they are similar to previous changes: 2

  • docs/src/content/docs/guides/dumping.md
  • docs/src/content/docs/guides/loading.md

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments

Minor 1
  1. Restore the enclosing flow entry's explicit-key state.
Additional comments: 1

docs/src/content/docs/guides/loading.md-285-286: 1

285-286: Minor · Functional Correctness · Quick win

Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.

Clarify the round-trip form for flow keys.

An unmodified flow mapping such as {? <long key>: value} keeps its colon on the same line. The round-trip emitter preserves its bytes rather than moving the colon to the next line. Qualify the description for flow mappings.

As per coding guidelines, “an unmodified document must re-emit byte-for-byte.”

Source: Coding guidelines

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b6d3498f-5609-40aa-adff-07e862185350
Commits

Reviewing files that changed from the base of the PR and between 2935faf and 8d92f79.

Files selected for processing: 10

  • docs/src/content/docs/guides/dumping.md
  • docs/src/content/docs/guides/loading.md
  • src/emit_util.rs
  • src/encode/mod.rs
  • src/key_text.rs
  • src/roundtrip/emit.rs
  • src/scanner/mod.rs
  • tests/core/test_long_keys.py
  • tests/features/test_options.py
  • tests/robustness/test_scaling.py

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

Comment thread src/scanner/mod.rs Resolved
Opening a flow collection reset the explicit key flag and closing it
never put it back. A collection holding an open `?` entry, used as an
implicit key (`{{? long}: v}`), then skipped the length check. The flag
is now saved with the entry's key start and restored when the
collection closes.

The guides also say what the explicit form looks like in a flow
mapping.
@frenck

frenck commented Dec 4, 2026

Copy link
Copy Markdown
Owner Author

On the docs note from the review (no inline thread, so answering here): fair, both guides described the explicit form as ": on the next line", which only holds in a block mapping. Since ca5f7f4 they spell out both forms: ? key with : value on the next line in a block mapping, {? key: value} in a flow mapping.

../Frenck

                       

Blogging my personal ramblings at frenck.dev
Or subscribe to my free bi-weekly newsletter: frenck.dev/newsletter

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

4 open issues
What changed in this PR

Implements YAML-spec compliance by enforcing a 1024-character maximum for implicit mapping keys during scanning, and updates emitters to automatically switch to explicit ? key form when a key would exceed the limit.

Changes:

  • Add scanner enforcement for the 1024-character implicit-key limit (block + flow contexts) with consistent error reporting.
  • Update both the encoder emitter and round-trip emitter to emit overlong keys using explicit key syntax so output reloads successfully.
  • Add targeted regression tests and update docs/guides to document the limit and the explicit-key workaround.
File Description
src/​scanner/​mod.rs Enforces implicit-key length limit in the scanner; adds error construction and scanner boundary tests.
src/​emit_util.rs Adds shared helper to detect whether emitted key text exceeds the implicit-key character limit.
src/​encode/​mod.rs Makes the encoder emitter write overlong keys using explicit ? key form (block + flow).
src/​roundtrip/​emit.rs Makes the round-trip emitter switch to explicit keys when an inline key exceeds the implicit limit.
tests/​core/​test_long_keys.py Adds comprehensive tests for boundary behavior across load paths and emitters.
tests/​features/​test_options.py Updates duplicate-key error test fixture to avoid now-invalid overlong implicit keys.
tests/​robustness/​test_scaling.py Updates scaling fixture to use explicit keys where needed under the new rule.
src/​key_text.rs Updates key-text rendering tests to use explicit keys for very long keys/collections.
docs/​src/​content/​docs/​guides/​loading.md Documents the implicit-key length limit and explicit-key workaround for loading.
docs/​src/​content/​docs/​guides/​dumping.md Documents that dumping emits long keys explicitly to preserve reloadability.

Review effort: Lite

Comment thread src/emit_util.rs Resolved
Comment thread src/scanner/mod.rs Resolved
Comment thread src/emit_util.rs Resolved
Comment thread tests/core/test_long_keys.py Resolved
Copilot AI balanced review requested due to automatic review settings December 4, 2026 01:01
Both length checks only ask whether a key has more than 1024
characters, so they now stop there instead of counting the whole key.
Also fixes a doc comment that said the measured text includes the `:`,
and builds the test message from the limit.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

1 open issue
4 resolved since last review

Review effort: Lite

Comment thread src/scanner/mod.rs Resolved
Copilot AI balanced review requested due to automatic review settings December 4, 2026 01:19
@codspeed

codspeed Bot commented Dec 4, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 16 untouched benchmarks


Comparing frenck/simple-key-cap (aad8c8c) with main (2935faf)

Open in CodSpeed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No actionable issues were found in the reviewed changes.

1 open issue

Review effort: Balanced

@frenck
frenck merged commit 687799e into main Dec 4, 2026
51 checks passed
@frenck
frenck deleted the frenck/simple-key-cap branch December 4, 2026 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Inconsistencies or issues which will cause a problem for users or implementers. rust

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants