Skip to content

fix: validate frame Dictionary_ID against the supplied dictionary - #58

Merged
jamesarich merged 1 commit into
mainfrom
fix/dictionary-id-validation
Aug 17, 2026
Merged

fix: validate frame Dictionary_ID against the supplied dictionary#58
jamesarich merged 1 commit into
mainfrom
fix/dictionary-id-validation

Conversation

@jamesarich

Copy link
Copy Markdown
Collaborator

Closes an RFC 8878 conformance gap found in a parity audit: the decoder read a frame's Dictionary_ID field (§3.1.1.3) but never validated it.

Real libzstd sets a real Dictionary_ID by default when compressing with a proper Zstandard-format dictionary (zstd -D dictfile) and checks it on decode. kzstd previously decoded with whatever ZstdDictionary the caller passed regardless of what the frame declared, producing a generic corruption error deep in entropy decoding on a wrong-dictionary frame instead of a clear one.

What changed

  • ParsedDictionary now captures a trained dictionary's own embedded Dictionary_ID (0 for raw-content dictionaries, matching the wire's "no ID" sentinel).
  • The decoder compares the frame's declared ID against the supplied dictionary's ID when both are non-zero, throwing ZstdException with a message that names it as a dictionary-ID mismatch (not generic corruption).
  • When either side is 0 — no field present, or a raw content dictionary — validation is skipped entirely. Fully backward compatible: kzstd's own encoder never sets a frame Dictionary_ID (confirmed spec-legal, unchanged by this PR), and raw content dictionaries keep working exactly as before.

Testing

  • DictionaryIdValidationTest: pure parsing checks on every target (trained dict captures its embedded ID, raw-content/empty dicts read as ID 0, kzstd's own zero-ID frames still decode against a dictionary with a different ID).
  • KzstdLibzstdInteropTest (zstd-jni oracle, real libzstd): compresses with real libzstd + the existing trained-dict fixture (already has a non-zero embedded ID), decodes with the correct dictionary (succeeds) and a different one (clear ZstdException).

No public API change — no apiDump needed.

./gradlew build passes on every target except native test-binary linking, which hits a pre-existing, unrelated crash on this dev host (reproduced on unmodified main, tracked in #56).

…ionary

Real libzstd sets a non-zero Dictionary_ID by default when compressing with
a proper Zstandard-format dictionary (RFC 8878 §5), and decoders are
expected to validate it (RFC 8878 §3.1.1.3). kzstd read the field but
discarded it, so decoding with the wrong dictionary surfaced as a confusing
generic corruption error instead of a clear mismatch.

ParsedDictionary now captures a trained dictionary's own embedded
Dictionary_ID (0 for raw content dictionaries, matching the wire's own "not
specified" sentinel). The decoder compares it against a frame's declared
Dictionary_ID and throws a ZstdException naming the mismatch when both are
present and differ. Frames with no declared ID (kzstd's own encoder never
sets one) or dictionaries with no embedded ID skip validation entirely, so
existing callers are unaffected.

Signed-off-by: James Rich <james.a.rich@gmail.com>
@coderabbitai

coderabbitai Bot commented Aug 17, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@jamesarich, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 59 minutes

Limit details: You’ve used all 1 included review currently available under your plan.

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

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: eeaa02b0-f3ca-49d7-b106-0dac952670d6

📥 Commits

Reviewing files that changed from the base of the PR and between fba1059 and 960eb0e.

📒 Files selected for processing (6)
  • CHANGELOG.md
  • src/commonMain/kotlin/org/meshtastic/kzstd/internal/ParsedDictionary.kt
  • src/commonMain/kotlin/org/meshtastic/kzstd/internal/ZstdDecoder.kt
  • src/commonTest/kotlin/org/meshtastic/kzstd/DictionaryIdValidationTest.kt
  • src/commonTest/kotlin/org/meshtastic/kzstd/TestVectors.kt
  • src/jvmTest/kotlin/org/meshtastic/kzstd/KzstdLibzstdInteropTest.kt

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

Comment @coderabbitai help to get the list of available commands.

@jamesarich
jamesarich added this pull request to the merge queue Aug 17, 2026
Merged via the queue into main with commit 6823efb Aug 17, 2026
6 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.

1 participant