Skip to content

Chunk the Nuxt session cookie instead of trimming its payload - #74

Merged
brionmario merged 1 commit into
thunder-id:mainfrom
janithjay:fix-nuxt-quicksample-cookie-chunking
Aug 17, 2026
Merged

Chunk the Nuxt session cookie instead of trimming its payload#74
brionmario merged 1 commit into
thunder-id:mainfrom
janithjay:fix-nuxt-quicksample-cookie-chunking

Conversation

@janithjay

@janithjay janithjay commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Purpose

The Nuxt quickstart's redirect-flow sign-in silently fails: the OAuth exchange completes successfully end-to-end (confirmed via backend logs - authorize, credential submission, code exchange, and JWKS fetch all return 200/302 as expected), but the user is bounced back to a signed-out state instead of landing on the authenticated page.

Root cause: createSessionToken (packages/nuxt/src/runtime/server/utils/session.ts) embedded the full access token, ID token, and refresh token - all JWTs themselves - as fields inside the session cookie's own JWT payload. With ThunderID's token claim set, the resulting cookie value routinely exceeds the browser's hard ~4096-byte per-cookie limit. The browser silently drops any Set-Cookie over that limit (confirmed in Chrome DevTools under "Malformed Response Cookies"), so the session is never actually established despite the server doing everything correctly.

Approach

Rather than trimming the payload, this changes splits the session cookie across numbered name.0, name.1, ... chunk cookies once it would exceed the per-cookie limit, and reassembles them transparently on read. The approach suggested in review #69 (comment), modeled on next-auth's own session cookie chunking.

idToken is not removed - unlike the alternative fix in #69, this branch keeps the full session payload (accessToken + idToken + refreshToken) intact.

Chunking absorbs the size instead of reducing it, so useServerSession()'s session.idToken keeps working exactly as it did before the bug was ever introduced.

The chunk name/size math itself (split/join/filterChunkNames) is framework-agnostic, so it now lives in @thunderid/node as a new CookieChunking utility rather than inside the Nuxt package.

packages/nuxt/src/runtime/server/utils/chunkedCookie.ts is now a thin h3 adapter over it (getChunkedCookie / setChunkedCookie / deleteChunkedCookie), wired into all 5 session-cookie touchpoints.

  • Issuance (session.ts)
  • Token refresh (token-refresh.ts)
  • Two session reads (serverSession.ts)
  • Sign-out (signout.post.ts).

@thunderid/nextjs builds its session cookie the same way and hits the same 4KB ceiling. it can adopt CookieChunking the same way in a follow-up.

The temp session cookie is untouched, it's always small and never needs chunking.

After the fix how __thunderid__session cookie was accepted

image

Related Issues

Related PRs

Checklist

  • Followed the contribution guidelines.
  • Manual test round performed and verified.
  • Documentation provided. (Add links if there are any)
  • Tests provided. (Add links if there are any)
    • Unit Tests
    • Integration Tests
  • Breaking changes. (Fill if applicable)
    • Breaking changes section filled.
    • breaking change label added.

Security checks

  • Followed secure coding standards.
  • Confirmed that this PR doesn't commit any keys, passwords, tokens, usernames, or other secrets.

Summary by CodeRabbit

  • New Features

    • Added support for storing and retrieving large session cookies by safely splitting them into multiple browser cookies.
    • Session issuance and token refresh now support oversized session data without changing the authentication flow.
  • Bug Fixes

    • Improved session sign-out and cookie replacement cleanup, preventing stale session cookie fragments from remaining in the browser.
  • Tests

    • Added coverage for large cookies, transitions between storage formats, cleanup, deletion, and token refresh behavior.

Copilot AI lite review requested due to automatic review settings August 12, 2026 19:52

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review available on request

  • 🔍 Trigger review

Reviews should be triggered manually for repositories with fewer than 10 stars. Select Trigger review above or comment @coderabbitai review to review the latest changes. For a full review, comment @coderabbitai full review.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: eefc9ad0-5b74-4d72-9cff-0fb12c719a74

📝 Walkthrough

Walkthrough

Nuxt now stores oversized session tokens across numbered cookies. Session reads, refreshes, and sign-out operations use chunked-cookie utilities. Unit tests cover writing, reading, stale-cookie cleanup, deletion, and token refresh behavior.

Changes

Chunked session cookies

Layer / File(s) Summary
Chunked-cookie utility implementation
packages/nuxt/src/runtime/server/utils/chunkedCookie.ts
Added utilities to read, write, split, reassemble, clean up, and delete base or numbered cookies.
Session lifecycle integration
packages/nuxt/src/runtime/server/utils/session.ts, packages/nuxt/src/runtime/server/utils/serverSession.ts, packages/nuxt/src/runtime/server/utils/token-refresh.ts, packages/nuxt/src/runtime/server/routes/auth/session/signout.post.ts
Session issuance, retrieval, token refresh, and sign-out now use chunked-cookie handling.
Chunked-cookie and refresh validation
packages/nuxt/tests/unit/chunked-cookie.test.ts, packages/nuxt/tests/unit/token-refresh.test.ts
Added coverage for chunked-cookie round trips, stale-cookie cleanup, deletion, and token-refresh behavior.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant issueSessionCookie
  participant setChunkedCookie
  participant BrowserCookies
  participant getChunkedCookie
  participant useServerSession
  issueSessionCookie->>setChunkedCookie: Write session token
  setChunkedCookie->>BrowserCookies: Set base or numbered cookies
  BrowserCookies->>getChunkedCookie: Provide request cookies
  getChunkedCookie->>useServerSession: Return session token
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes directly address issue #4881 by chunking, reading, refreshing, and deleting oversized session cookies while preserving the full session payload.
Out of Scope Changes check ✅ Passed All production and test changes support the linked issue and the stated objective of reliable oversized session-cookie handling.
Title check ✅ Passed The title clearly and concisely describes the main change: chunking oversized Nuxt session cookies instead of trimming their payload.
Description check ✅ Passed The description explains the problem, root cause, implementation, affected flows, testing, related work, and security checks; optional documentation and integration tests remain unchecked.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 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.

Inline comments:
In `@packages/nuxt/src/runtime/server/utils/chunkedCookie.ts`:
- Around line 33-37: Update findExistingCookieNames to match the base cookie
name and only names with a decimal numeric suffix after `${name}.`; exclude
prefixed cookies such as `${name}.metadata` from the returned cleanup list.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 1fdf493e-1ea9-4904-9393-3e6e5b282198

📥 Commits

Reviewing files that changed from the base of the PR and between 7e1d437 and 4dba7b8.

📒 Files selected for processing (7)
  • packages/nuxt/src/runtime/server/routes/auth/session/signout.post.ts
  • packages/nuxt/src/runtime/server/utils/chunkedCookie.ts
  • packages/nuxt/src/runtime/server/utils/serverSession.ts
  • packages/nuxt/src/runtime/server/utils/session.ts
  • packages/nuxt/src/runtime/server/utils/token-refresh.ts
  • packages/nuxt/tests/unit/chunked-cookie.test.ts
  • packages/nuxt/tests/unit/token-refresh.test.ts

Comment thread packages/nuxt/src/runtime/server/utils/chunkedCookie.ts Outdated
@janithjay
janithjay force-pushed the fix-nuxt-quicksample-cookie-chunking branch from 4dba7b8 to 03a99bf Compare August 12, 2026 20:05
@janithjay
janithjay marked this pull request as draft August 14, 2026 03:50
@janithjay
janithjay force-pushed the fix-nuxt-quicksample-cookie-chunking branch from 03a99bf to ac47919 Compare August 16, 2026 04:30
@janithjay
janithjay marked this pull request as ready for review August 16, 2026 04:41
@janithjay
janithjay force-pushed the fix-nuxt-quicksample-cookie-chunking branch from ac47919 to bd6cb03 Compare August 16, 2026 04:46
…ding 4KB browser limits

Signed-off-by: janithjay <janithjayashan018@gmail.com>
@janithjay
janithjay force-pushed the fix-nuxt-quicksample-cookie-chunking branch from bd6cb03 to 9c8688c Compare August 17, 2026 04:27
@brionmario
brionmario merged commit 4f67e81 into thunder-id:main Aug 17, 2026
6 checks passed
@janithjay
janithjay deleted the fix-nuxt-quicksample-cookie-chunking branch August 17, 2026 05:54
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