Skip to content

fix(relay): measure a subscription id in UTF-16 code units, as the capture does - #1400

Merged
cameri merged 1 commit into
rewrite/mainfrom
fix/subscription-id-utf16
Dec 5, 2026
Merged

cameri merged 1 commit into
rewrite/mainfrom
fix/subscription-id-utf16

Conversation

@chappie-daemon

@chappie-daemon chappie-daemon commented Dec 5, 2026 •

Copy link
Copy Markdown
Collaborator

What

REQ and COUNT subscription ids are now measured in UTF-16 code units everywhere, which is what the capture does.

The port measured the same id three different ways: decode_request counted Unicode scalars (chars().count()), and both handlers counted UTF-8 bytes (.len()), against the same 256 bound. The capture counts JavaScript string length — UTF-16 code units — at all three sites (src/schemas/message-schema.ts:11, :34; src/handlers/subscribe-message-handler.ts:297; src/handlers/count-message-handler.ts:69). So an id of 100 CJK characters (300 bytes) was refused by both handlers although the capture accepts it, and an id of 200 emoji (400 units) passed the decoder although the capture refuses it.

Provenance

Cherry-picked with -x from saket0187#9 (0f5a2d8b), which is the fix proposed on #954. Diff read in full before it entered the tree; no workflow, dependency or install-time change. The prose in the commit and the ADR addition screened clear.

Verification

  • cargo test -p nostream-core --test wire_codec: 28 passed.
  • cargo test -p nostream-relay --test handlers: 71 passed.
  • The four new tests fail at the base, which is what makes them evidence rather than decoration: reverting the three source lines only turns decode_bounds_the_subscription_id_in_utf16_code_units red (1 failed) and all three handler tests red (the_subscription_id_limit_counts_utf16_code_units, the_query_id_limit_counts_utf16_code_units, an_id_is_bounded_in_utf16_code_units_at_the_shipped_limit).
  • cargo fmt --check clean.

Kept deliberately the capture's behaviour

The bounds and refusals stay the capture's: 256 UTF-16 units at the frame schema (not NIP-01's 64-character maximum), a NOTICE on REQ and a CLOSED on COUNT for a lower configured maxSubscriptionIdLength (not NIP-01/NIP-45's CLOSED). An id carrying an unpaired surrogate escape is still refused at parse, because a Rust String cannot hold one. ADR 0002 records the unit and the difference; the fuzz target and docs/rewrite/fuzz.md now state the invariant in the same unit.

Closes #954

After the merge

rewrite/main is not the default branch, so GitHub will not close #954 when this lands in it — it gets closed by hand with the merge sha, the same way #1324 was.

@changeset-bot

changeset-bot Bot commented Dec 5, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: cbcad87

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 0 packages

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Subscription ID length checks in the decoder and REQ and COUNT handlers now count UTF-16 code units. Tests cover the 256-unit frame limit and configured handler limits. Documentation and the changeset describe the limits and rejection responses.

Changes: Subscription ID Length

Layer / File(s) Summary
Decoder limit and validation
crates/nostream-core/src/messages.rs, crates/nostream-core/tests/wire_codec.rs, docs/rewrite/fuzz.md, fuzz/fuzz_targets/client_frame.rs
The decoder and fuzz target count subscription IDs in UTF-16 code units. Tests verify acceptance at 256 units and rejection above the limit.
REQ and COUNT handler limits
crates/nostream-relay/src/handlers/*, crates/nostream-relay/tests/handlers.rs, docs/rewrite/decisions/0002-core-crypto-and-negentropy.md, .changeset/subscription-id-utf16-length.md
The handlers compare ID lengths to the configured maximum in UTF-16 code units. Tests cover configured limits and the shipped 256-unit limit. The ADR and changeset describe the limits and rejection responses.

Possibly related issues

Suggested reviewers: greptile-apps, cameri

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Minimal · up to cbcad

Subscription ID length checks now count the same unit in the decoder and in the REQ and COUNT handlers, so non-ASCII IDs that the decoder accepts are no longer refused by the handlers. Limits and refusal responses are unchanged. I found no merge-blocking risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: subscription-ID length is now measured in UTF-16 code units, matching the capture. The changeset covers this in the decoder (messages.rs), both handlers (subscribe.rs,…
Linked Issues check Passed Directly linked issue #954 requires UTF-16 code-unit counting in decoding and in the REQ/COUNT handlers, plus non-ASCII tests. The PR summary and PR description state that all three sites now use UTF-…
Out of Scope Changes check Passed The changes fall into three groups, all tied to the #954 objective. (1) Source: the one-line length changes in messages.rs, subscribe.rs, and count.rs. (2) Tests: the new cases in wire_codec.rs and ha…
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title names the main change: subscription-ID length is now measured in UTF-16 code units, matching the capture. The changeset covers this in the decoder (messages.rs), both handlers (subscribe.rs, count.rs), tests, ADR 0002, and the fuzz target. The title is specific and accurate.

Full details: Linked Issues check

Explanation

Directly linked issue #954 requires UTF-16 code-unit counting in decoding and in the REQ/COUNT handlers, plus non-ASCII tests. The PR summary and PR description state that all three sites now use UTF-16 units: decode_request in crates/nostream-core/src/messages.rs (chars().count() replaced), subscribe.rs and count.rs (.len() replaced). The non-ASCII tests exist: decode_bounds_the_subscription_id_in_utf16_code_units in wire_codec.rs uses 128 astral characters (256 units) and rejects one more unit; the handler tests in handlers.rs accept 100 BMP CJK characters (300 UTF-8 bytes) and reject 257 UTF-16 units. The issue's example (100 Chinese characters) is covered. The PR keeps the existing 256 bound and refusal behavior, as the issue does not ask for a change there. Limit: the repository diff tool returned no text, so the source hunks were not read directly. This verdict relies on the PR summary and description, which are specific per file, and on the stated test results.

Full details: Out of Scope Changes check

Explanation

The changes fall into three groups, all tied to the #954 objective. (1) Source: the one-line length changes in messages.rs, subscribe.rs, and count.rs. (2) Tests: the new cases in wire_codec.rs and handlers.rs. (3) Documentation and fuzz support: the changeset note, ADR 0002 entry, docs/rewrite/fuzz.md wording, and the fuzz target check in fuzz/fuzz_targets/client_frame.rs, which now validate the same UTF-16 unit. These record and enforce the same invariant, so they support the objective and are not unrelated. The PR description's provenance note (cherry-pick from another fork) is not a code change in scope. No unrelated change was identified in the summary. Limit: the source diff could not be read directly, so this relies on the summary.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • 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

Commits

Reviewing files that changed from the base of the PR and between 4c4f6a6 and cbcad87.

Files selected for processing: 9

  • .changeset/subscription-id-utf16-length.md
  • crates/nostream-core/src/messages.rs
  • crates/nostream-core/tests/wire_codec.rs
  • crates/nostream-relay/src/handlers/count.rs
  • crates/nostream-relay/src/handlers/subscribe.rs
  • crates/nostream-relay/tests/handlers.rs
  • docs/rewrite/decisions/0002-core-crypto-and-negentropy.md
  • docs/rewrite/fuzz.md
  • fuzz/fuzz_targets/client_frame.rs

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

@greptile-apps

greptile-apps Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] This PR appears safe to merge.

Summary

Measures REQ and COUNT subscription IDs in UTF-16 code units throughout the decoder and both handlers.

  • REQ and COUNT IDs now use UTF-16 units for their length limits.

Reviews (1) · Last reviewed commit: "fix(relay): measure a subscription id in..." · Reviewed by Greptile

@cameri
cameri merged commit f0bb502 into rewrite/main Dec 5, 2026
21 checks passed
@cameri
cameri deleted the fix/subscription-id-utf16 branch December 5, 2026 14:23
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.

3 participants