fix(net): cap random origin id at 53 bits in JS client#1785
Conversation
The JS client generated a 62-bit random origin id, but older `@moq/lite` JS clients decode `AnnounceInterest.exclude_hop` as a u53 (number) and throw on anything > 2^53-1. Cap the generated id at 53 bits to match `Origin::random` in rs/moq-net, keeping older deployed bundles alive against fresh peers. The wire format and `OriginSchema` still permit 62 bits; only the generated value is capped. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
WalkthroughIn 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches✨ Simplify code
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
js/net/src/lite/origin.ts (1)
34-34: ⚡ Quick winExtract the bitmask to a named constant.
The bitmask value
0x1f_ffff_ffff_ffffnshould be extracted to a named constant to follow coding guidelines. While the inline comment explains the value, a constant would make the intent clearer, simplify the future reversion to 62 bits, and be self-documenting.As per coding guidelines, avoid using magic numbers; use named constants instead.
♻️ Proposed refactor
+/** Temporary 53-bit mask for compatibility with older `@moq/lite` clients. */ +const ORIGIN_BITMASK_53 = 0x1f_ffff_ffff_ffffn; + export function randomOrigin(): Origin { const buf = new BigUint64Array(1); crypto.getRandomValues(buf); - // Mask to 53 bits. - const raw = buf[0] & 0x1f_ffff_ffff_ffffn; + const raw = buf[0] & ORIGIN_BITMASK_53; // Guard against the (astronomically unlikely) zero draw. return OriginSchema.parse(raw === 0n ? 1n : raw); }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@js/net/src/lite/origin.ts` at line 34, The bitmask value 0x1f_ffff_ffff_ffffn is hardcoded inline in the bitwise AND operation with buf[0], violating the principle of avoiding magic numbers. Extract this bitmask to a named constant at the module level with a descriptive name that reflects its purpose (such as indicating it represents a mask for extractable bits), then replace the inline bitmask in the expression with this constant reference.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@js/net/src/lite/origin.ts`:
- Line 34: The bitmask value 0x1f_ffff_ffff_ffffn is hardcoded inline in the
bitwise AND operation with buf[0], violating the principle of avoiding magic
numbers. Extract this bitmask to a named constant at the module level with a
descriptive name that reflects its purpose (such as indicating it represents a
mask for extractable bits), then replace the inline bitmask in the expression
with this constant reference.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: f87f0fc3-229e-4374-a679-e4b391585aa4
📒 Files selected for processing (1)
js/net/src/lite/origin.ts
Summary
The JS client (
@moq/net) generated a 62-bit random origin id, while the Rust client (rs/moq-net) already caps its generated id at 53 bits. This brings the two into sync.Older
@moq/liteJS clients decodeAnnounceInterest.exclude_hopas au53(a JSnumber) and throw on any value> 2^53-1. A 62-bit origin generated by a fresh peer would crash those deployed bundles. Capping the generated value at 53 bits keeps them alive against fresh peers.This is the JS half of the same TEMPORARY workaround already present in
Origin::random(rs/moq-net). The wire format andOriginSchemastill permit the full 62 bits. Only the value we generate is capped. Both should be restored to 62 bits once the u62 fix has propagated to deployed bundles.Changes
js/net/src/lite/origin.ts: maskrandomOrigin()to 53 bits (0x1f_ffff_ffff_ffffn) instead of 62 bits, with a comment mirroring the Rust rationale.Test plan
biome checkpasses on the changed fileOriginSchemavalidation unchanged(Written by Claude)