Skip to content

fix(runtime): base64url-encode the forwarded identity header (non-Latin-1 claims break every RPC) - #262

Merged
prisis merged 2 commits into
alphafrom
advisor/213-identity-header-encoding
Aug 1, 2026
Merged

prisis merged 2 commits into
alphafrom
advisor/213-identity-header-encoding

Conversation

@prisis

@prisis prisis commented Aug 1, 2026

Copy link
Copy Markdown
Member

The worker forwards verified identity to the shard as HTTP headers (x-lunora-userid, x-lunora-identity = raw JSON.stringify(claims)). HTTP header values are WebIDL ByteStrings — workerd/undici throw TypeError for any code unit > 255 — so a resolveIdentity returning any claim with a non-Latin-1 char (a CJK/Cyrillic/Arabic name, an emoji, an IDN) makes new Request(...) throw before the shard is reached, becoming an opaque INTERNAL 500 for every RPC/batch/fan-out/REST/dispatch for that user (Latin-1 accents survive, which is why local testing misses it).

Adds a zero-dep shared/identity-header.ts (UTF-8 → base64url, with a sniffing decoder for order-independent rollout) and routes all four producer sites (create-worker, cross-shard-relations, dispatch-runner, and shard-client's .as() path — the last two found via repo-wide grep, not in the original inventory) plus every consumer (parseIdentityHeader, voice-turn's parseIdentity, and the raw userId reads in shard-do/voice-do) through it. Legacy raw-JSON headers still decode during a mixed-version deploy.

Verified: runtime 814, dispatch 23, do 503, agent 288; a real createWorker→ShardDO integration test with CJK/emoji claims + a non-Latin-1 userId (reproduces the ByteString TypeError pre-fix).

⚠ Touches runtime/create-worker.ts + do/shard-do.ts — merge-order vs #248 (226 create-worker), #243 (206-207 shard-do).

🤖 Generated with Claude Code

HTTP header values are WebIDL ByteStrings, but the worker forwarded
verified caller identity to the shard as raw x-lunora-userid /
x-lunora-identity (raw JSON.stringify(claims)) header values. Any
non-Latin-1 code unit in a claim or userId — a CJK/Cyrillic/Arabic
name, an emoji, an IDN — made new Request(...) throw before the shard
was ever reached, turning into an opaque INTERNAL 500 for every
RPC/batch/fan-out/REST/dispatch call for that user. Latin-1-only text
(incl. accented Latin letters) survived unencoded, which is why local
testing with ASCII-ish names missed this.

Add shared/identity-header.ts: encodeIdentityHeader/encodeUserIdHeader
UTF-8-encode then base64url-encode so the header value is always
ASCII; decodeIdentityHeader/decodeUserIdHeader invert this, sniffing
a legacy unencoded value so an old producer and a new consumer (or
vice versa) both keep working during rollout. Every decode fails soft
to undefined on malformed input, matching the prior parseIdentityHeader
posture.

Switch every producer (createWorker's resolveForwardContext,
cross-shard-relations' fan-out headers, the dispatch runner, and
createShardClient's .as() identity — the last found via a repo-wide
grep once the first three were fixed) to the codec, and every consumer
(ShardDO's request/WS-upgrade identity capture via parseIdentityHeader,
the voice DO's parseIdentity) to decode it back.

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

coderabbitai Bot commented Aug 1, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

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

Next review available in: 31 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 5a9ad9bf-2bdf-460c-9988-563ffd901597

📥 Commits

Reviewing files that changed from the base of the PR and between 36421ad and f3ed90a.

⛔ Files ignored due to path filters (11)
  • packages/agent/__tests__/voice-turn.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
  • packages/dispatch/__tests__/dispatch-runner-behavior.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
  • packages/do/__tests__/shard-do.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
  • packages/runtime/__tests__/create-worker.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
  • packages/runtime/__tests__/identity-header.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
  • packages/runtime/__tests__/identity-integration.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
  • packages/runtime/__tests__/identity-layer.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
  • packages/runtime/__tests__/server-query.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
  • packages/runtime/__tests__/shard-client.test.ts is excluded by !**/__tests__/**, !**/*.test.ts and included by packages/**
  • shared/base64.ts is excluded by none and included by none
  • shared/identity-header.ts is excluded by none and included by none
📒 Files selected for processing (9)
  • packages/agent/src/voice-do.ts
  • packages/agent/src/voice-turn.ts
  • packages/dispatch/src/create-dispatch-runner.ts
  • packages/dispatch/tsconfig.json
  • packages/do/src/admin-rpc-args.ts
  • packages/do/src/shard-do.ts
  • packages/runtime/src/create-worker.ts
  • packages/runtime/src/cross-shard-relations.ts
  • packages/runtime/src/shard-client.ts

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.

@github-actions

github-actions Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Thank you for following the naming conventions! 🙏

@github-actions

github-actions Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Thank you for confirming the Contributor License Agreement! 🙏

@netlify

netlify Bot commented Aug 1, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for lunorash ready!

Name Link
🔨 Latest commit f3ed90a
🔍 Latest deploy log https://app.netlify.com/projects/lunorash/deploys/6a6db34542ec600008a933a6
😎 Deploy Preview https://deploy-preview-262--lunorash.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@codspeed

codspeed Bot commented Aug 1, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 17.53%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 1 regressed benchmark
✅ 250 untouched benchmarks
⏩ 10 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ + resolveIdentity (userId + claims → x-lunora-identity) 1.6 ms 2 ms -17.53%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing advisor/213-identity-header-encoding (f3ed90a) with alpha (a540991)2

Open in CodSpeed

Footnotes

  1. 10 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on alpha (d9791ea) during the generation of this report, so a540991 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

Move toBase64Url/fromBase64Url from shared/identity-header.ts into
shared/base64.ts alongside their standard-base64 siblings, and export
identity-header's isByteStringSafe instead of leaving it private -
three test files were re-implementing it inline, one with a subtly
different (codePointAt-in-a-length-indexed-loop) iteration than the
real charCodeAt version. Also documents the deploy-ordering
constraint on decodeUserIdHeader's `=` sentinel for mixed-rollout
safety.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@prisis
prisis merged commit 7382b7b into alpha Aug 1, 2026
43 of 44 checks passed
@prisis
prisis deleted the advisor/213-identity-header-encoding branch August 1, 2026 10:56
@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using our GitHub Discussions tab for questions.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Sep 8, 2026

This branch was previously deployed

1 inactive deployment
benchmarks — f3ed90a8 Deployed Aug 1, 2026 by prisis via Benchmarks #1306
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant