-
Notifications
You must be signed in to change notification settings - Fork 21
Chunk the Nuxt session cookie instead of trimming its payload #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
janithjay
wants to merge
1
commit into
thunder-id:main
Choose a base branch
from
janithjay:fix-nuxt-quicksample-cookie-chunking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
packages/nuxt/src/runtime/server/utils/chunkedCookie.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // Copyright 2025 The ThunderID Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import {deleteCookie, getCookie, parseCookies, setCookie} from 'h3'; | ||
| import type {H3Event} from 'h3'; | ||
|
|
||
| // Mirrors next-auth's session cookie chunking constants | ||
| // (packages/core/src/lib/utils/cookie.ts): browsers reject a `Set-Cookie` | ||
| // once the full `name=value; attributes` line exceeds ~4096 bytes, so the | ||
| // payload budget per chunk reserves headroom for the cookie's own name and | ||
| // attributes (Path, HttpOnly, SameSite, Max-Age, ...). | ||
| const ALLOWED_COOKIE_SIZE = 4096; | ||
| const ESTIMATED_EMPTY_COOKIE_SIZE = 160; | ||
| const CHUNK_SIZE = ALLOWED_COOKIE_SIZE - ESTIMATED_EMPTY_COOKIE_SIZE; | ||
|
|
||
| interface ChunkedCookieOptions { | ||
| httpOnly: boolean; | ||
| maxAge: number; | ||
| path: string; | ||
| sameSite: 'lax'; | ||
| secure: boolean; | ||
| } | ||
|
|
||
| function chunkName(name: string, index: number): string { | ||
| return `${name}.${index}`; | ||
| } | ||
|
|
||
| /** | ||
| * Every cookie name in the current request belonging to `name` — the | ||
| * unchunked base cookie and/or any numbered `${name}.0`, `${name}.1`, ... | ||
| * chunks left over from a previous write. | ||
| */ | ||
| function findExistingCookieNames(event: H3Event, name: string): string[] { | ||
| const all: Record<string, string> = parseCookies(event); | ||
| const prefix = `${name}.`; | ||
| return Object.keys(all).filter((key: string) => key === name || key.startsWith(prefix)); | ||
| } | ||
|
|
||
| /** | ||
| * Read a cookie that may have been split across `${name}.0`, `${name}.1`, | ||
| * ... chunks, reassembling it into the original value. Falls back to the | ||
| * unchunked `name` cookie when the value fit in a single cookie. | ||
| */ | ||
| export function getChunkedCookie(event: H3Event, name: string): string | undefined { | ||
| const unchunked: string | undefined = getCookie(event, name); | ||
| if (unchunked !== undefined) return unchunked; | ||
|
|
||
| const chunks: string[] = []; | ||
| for (let i = 0; ; i += 1) { | ||
| const chunk: string | undefined = getCookie(event, chunkName(name, i)); | ||
| if (chunk === undefined) break; | ||
| chunks.push(chunk); | ||
| } | ||
|
|
||
| return chunks.length > 0 ? chunks.join('') : undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Write a cookie value, splitting it across numbered `${name}.0`, | ||
| * `${name}.1`, ... chunks once it would exceed the ~4KB per-cookie limit | ||
| * browsers enforce, and reassembling transparently via {@link getChunkedCookie}. | ||
| * Mirrors next-auth's session cookie chunking. | ||
| * | ||
| * Clears any cookie names the previous value needed but the new one doesn't | ||
| * (e.g. a smaller re-issued session that now fits in fewer chunks, or in a | ||
| * single unchunked cookie). | ||
| */ | ||
| export function setChunkedCookie(event: H3Event, name: string, value: string, options: ChunkedCookieOptions): void { | ||
| const existing: string[] = findExistingCookieNames(event, name); | ||
| const chunkCount: number = Math.max(1, Math.ceil(value.length / CHUNK_SIZE)); | ||
| const newNames: Set<string> = | ||
| chunkCount === 1 | ||
| ? new Set([name]) | ||
| : new Set(Array.from({length: chunkCount}, (_, i: number) => chunkName(name, i))); | ||
|
|
||
| for (const existingName of existing) { | ||
| if (!newNames.has(existingName)) { | ||
| deleteCookie(event, existingName, options); | ||
| } | ||
| } | ||
|
|
||
| if (chunkCount === 1) { | ||
| setCookie(event, name, value, options); | ||
| return; | ||
| } | ||
|
|
||
| for (let i = 0; i < chunkCount; i += 1) { | ||
| setCookie(event, chunkName(name, i), value.slice(i * CHUNK_SIZE, (i + 1) * CHUNK_SIZE), options); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Delete a cookie that may have been chunked — clears the base cookie name | ||
| * and every numbered chunk present in the current request. | ||
| */ | ||
| export function deleteChunkedCookie(event: H3Event, name: string, options: ChunkedCookieOptions): void { | ||
| const existing: string[] = findExistingCookieNames(event, name); | ||
|
|
||
| if (existing.length === 0) { | ||
| deleteCookie(event, name, options); | ||
| return; | ||
| } | ||
|
|
||
| for (const existingName of existing) { | ||
| deleteCookie(event, existingName, options); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.