Skip to content

fix(auth): @moq/auth refuses the keys moq-auth refuses - #8804

Merged
kixelated merged 3 commits into
mainfrom
quest/m1/auth-key-strictness
Nov 15, 2026
Merged

kixelated merged 3 commits into
mainfrom
quest/m1/auth-key-strictness

Conversation

@kixelated

@kixelated kixelated commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Finishes quest/m1/auth-key-strictness.md (deleted here).

Problem

Fuzzing the key parsers (moq-dev/moq#8707) found JWKs that only @moq/auth accepts. JSON.parse keeps the last copy of a repeated member, and decodeBase64Flexible accepted whitespace, the standard alphabet, nonzero trailing bits, and a padded or standard-base64 encoded key. A random sweep found more: nesting past serde_json's 127 limit, lone surrogates, numbers out of range, and String.trim stripping a BOM that Rust's str::trim keeps.

Rust had its own gap too: a repeated RSA private member (d, p, q, dp, dq, qi) went through a serde_json::Map and kept the last copy, while every other field refused the repeat. And KeySet skipped an unread top-level member through serde_json's ignore_value, which checks only syntax, so a set accepted 1e400, a lone surrogate, or nesting past 127 there, which a key refuses in its own members.

Approach

  • js/auth/src/json.ts: a small internal JSON parser that refuses what serde_json refuses. A repeated member parses as a sentinel symbol. Every schema refuses it where it reads that member and strips it where it doesn't. serde does the same thing: it refuses a repeated field it reads and ignores repeated unknown members.
  • Key.parse and KeySet.parse use it. Key.parse trims the way Rust's str::trim does (\p{White_Space}) and decodes UTF-8 strictly, keeping a BOM. KeySet.parse no longer trims, matching KeySet::from_str.
  • decodeBase64url mirrors moq-auth's decode_base64url: unpadded, or with canonical padding, and refuses whitespace, +//, and nonzero trailing bits. An encoded key is unpadded only, as Key::from_str reads it.
  • moq-auth: deserialize_rsa_private refuses a repeated private member.
  • moq-auth: KeySet's RawKeySet gets a flattened IgnoredAny catch-all, so serde buffers every top-level member through deserialize_any and refuses what it refuses in a key. A repeated keys is still refused and a repeated unread member still accepted.
  • Corpus: a jwk/jwks case given as a JSON string is now its raw text, so it can spell a repeated member. There are 15 new jwk cases and 6 new sets cases, which both languages replay, and the fuzz seeds use the raw text.

Impact

  • Public API: none. Wire: none.
  • Behavior: @moq/auth stops loading the keys above, including a key encoded in standard (+//, padded) base64, which an old test had pinned as accepted. moq-auth stops loading an RSA key that repeats a private member, and a key set whose unread top-level member is out of range, holds a lone surrogate, or nests past 127. A key written by either CLI never does any of these.

Decisions (taken while AFK; a review can undo each)

  • Repeated unread members ({"note":1,"note":2,...}) stay accepted in both, because serde ignores them. The quest's plan said to refuse a repeat at any depth. That would have made JS stricter than Rust and needed a raw-JSON pre-scan in Rust. Exact agreement is the Goal, so I followed serde.

  • Fixed the Rust RSA private-member leniency here, not as a follow-up, since the Goal is exact agreement.

  • Also matched serde_json's recursion limit, lone-surrogate, and number-range refusals, plus the trim and BOM differences, which the random sweep found beyond the quest's list.

  • Replaced the parse - accepts a JWK in standard padded base64 test with one that refuses it. That test came from a dependency swap (moq-dev/moq#6053) that kept the old behavior, not from a requirement.

  • JWKS unread top-level members: ✅ make moq-auth refuse them as @moq/auth now does (rejected: give the JS parser a lax mode for unread members). Pullfrog found the disagreement (r4578188565). Strict follows the fail-loudly rule and matches how a key's own members already parse in both.

Alternatives

  • Loosening Rust to match JS. The quest rejected this, since failing loudly on a malformed key is the rule.
  • A reviver on JSON.parse. It can't see repeated members.

Validation

  • just check passes (the Rust and JS tests include the corpus replay). cargo test -p moq-auth --all-features gives 255 passed, and bun test in js/auth gives 200 passed.
  • Differential sweep: a scratch script generated 12,000 random keys and key sets from the fuzz fixed keys. The mutations were repeated members (read, unread, in a scope), base64 field mutations, Unicode and JSON whitespace, deep nesting, odd numbers and escapes, and padded, BOM-prefixed, and encoded forms. It ran each through Key::from_str/KeySet::from_str and Key.parse/KeySet.parse. Result: 0 disagreements, with about 35% of the inputs accepted. The pre-change Key.parse disagreed with Rust on 3,317 of 8,000.

Caused by

  • moq-dev/moq#745: added JSON.parse and the lenient standard-base64 fallback to Key.parse (found by check: fuzzing).
  • moq-dev/moq#6898: read RSA private members through a serde_json::Map, which keeps the last of a repeat (found by check: differential sweep).

Follow-ups

None.

Models

Size: S
Implementer: Claude Code, claude-opus-5-5, unknown effort (arm A)
Implementer: Claude Code, claude-opus-5-5, medium effort (iterate, S, arm A)

Review

Rewritten by quest review after each round.

  • Head: e265fae

  • Base: origin/main at 527e301

  • Risk: high, The change tightens authentication key parsing, a security boundary relied on by applications and relays, so rejected key sets could prevent authentication until configuration is corrected or the change is reverted.

  • Classified: medium by default

  • Reviewer: Codex, gpt-6.1-sol, medium effort

  • Session: 01a1def8-7ef8-7200-a354-cbbc336a417c, round 2

  • Score: 9/10, No actionable defects were found in the incremental changes, which align Rust's handling of unread JWKS members with JavaScript; builds and tests were not run as instructed.

  • Raised the risk to high: The change tightens authentication key parsing, a security boundary relied on by applications and relays, so rejected key sets could prevent authentication until configuration is corrected or the change is reverted.

  • Rounds: 2; first round 0; findings 0: fixed 0, refuted 0, open 0

  • Findings: none

kixelated and others added 2 commits November 15, 2026 03:02
@moq/auth parses keys and key sets with a JSON parser that refuses what serde_json
does (a repeated member it reads, nesting past 127, a lone surrogate, a number out
of range) and decodes key fields as moq-auth's strict base64url decoder does.
moq-auth now refuses a repeated RSA private member too, which it kept the last of.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title js/auth refuses the keys moq-auth refuses fix(auth): @moq/auth refuses the keys moq-auth refuses Nov 15, 2026
@kixelated
kixelated marked this pull request as ready for review November 15, 2026 11:30
@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

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 20 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 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: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1228e239-0341-41c4-8133-bf20aaa22a35
Commits

Reviewing files that changed from the base of the PR and between 52732a0 and e265fae.

Files selected for processing: 11

  • js/auth/src/corpus.test.ts
  • js/auth/src/json.ts
  • js/auth/src/key.test.ts
  • js/auth/src/key.ts
  • js/auth/src/set.ts
  • quest/m1/auth-key-strictness.md
  • rs/moq-auth/src/corpus.rs
  • rs/moq-auth/src/fuzz.rs
  • rs/moq-auth/src/key.rs
  • rs/moq-auth/src/set.rs
  • rs/moq-auth/tests/corpus.json
  • 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

@pullfrog pullfrog Bot 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.

ℹ️ One parity gap left. It's in KeySet, not Key.

Reviewed changes

This review covers the full PR: a strict JSON parser and base64url decoder for @moq/auth, the Rust fix for repeated RSA private members, and the corpus cases that pin both.

  • json.ts parser: refuses what serde_json refuses: nesting deeper than 127, lone surrogates, and non-finite numbers. A repeated member parses as REPEATED, which zod then refuses or strips.
  • Key.parse / KeySet.parse: trim only where Rust trims, decode the encoded form unpadded with strict UTF-8, and parse through Json.parse.
  • decodeBase64url: matches decode_base64url (URL_SAFE_NO_PAD, or canonical URL_SAFE, with no trailing bits). This replaces decodeBase64Flexible.
  • deserialize_rsa_private: a map visitor that refuses a repeated d/p/q/dp/dq/qi.
  • Corpus: a string case is now raw text. This adds 15 jwk cases and 2 sets cases, and the fuzz seeds use the raw text too.

I checked the base64 edge cases (AA=, A===, ====, padding with nonzero trailing bits) against the base64 crate's URL_SAFE and URL_SAFE_NO_PAD engines, and they agree. The trim and BOM handling also match str::trim and String::from_utf8. bun test js/auth/src passes all 196 tests.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using claude-opus-5-5 | 𝕏

Comment thread js/auth/src/set.ts Resolved
…fuse in a key

KeySet read a plain RawKeySet, so serde_json skipped an unknown top-level
member through ignore_value, checking only its syntax: 1e400, a lone
surrogate, or nesting past the recursion limit got through, while
@moq/auth's KeySet.parse refuses them. A flattened catch-all buffers every
member as a key's own members are, so both refuse them. A repeated unread
member stays accepted in both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@pullfrog pullfrog Bot 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.

✅ No new issues found.

Reviewed changes

This review covers the one commit added since the prior Pullfrog review (9c0dcbb..e265fae).

  • RawKeySet catch-all: rs/moq-auth/src/set.rs flattens a HashMap<String, IgnoredAny>, so serde buffers unread top-level members through deserialize_any. KeySet::from_str now refuses an out-of-range number, a lone surrogate, or nesting past 127 there, as KeySet.parse does.
  • Corpus: four new sets cases pin a repeated unread member (accepted), an out-of-range unread member, a lone-surrogate unread member, and an over-deep unread member (all refused).

The earlier KeySet parity thread is resolved.

Pullfrog  | View workflow run | Using claude-sonnet-5-5 | 𝕏

@kixelated
kixelated added this pull request to the merge queue Nov 15, 2026
Merged via the queue into main with commit d3e9935 Nov 15, 2026
5 checks passed
@kixelated
kixelated deleted the quest/m1/auth-key-strictness branch November 15, 2026 19:04
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