Skip to content

Add cookie chunking support for sessions exceeding 4KB limit - #937

Open
sefasenturk95 wants to merge 2 commits into
vvo:mainfrom
sefasenturk95:main
Open

Add cookie chunking support for sessions exceeding 4KB limit#937
sefasenturk95 wants to merge 2 commits into
vvo:mainfrom
sefasenturk95:main

Conversation

@sefasenturk95

@sefasenturk95 sefasenturk95 commented Dec 1, 2025

Copy link
Copy Markdown

This PR adds optional cookie chunking to handle session data that exceeds browser cookie size limits (~4KB). When enabled, large cookies are automatically split into multiple smaller cookies and reconstructed transparently when reading.

Multiple users have reported hitting the 4096-byte cookie size limit with iron-session:

Currently, users hitting this limit must either:

  1. Remove data from their session (not always feasible)
  2. Switch to a different library with external storage (like next-session with Redis)

This PR provides a third option: automatic cookie chunking for moderate-sized sessions that don't warrant the complexity of external storage.

Changes:

  • Added chunking configuration option to SessionOptions interface
  • Implemented splitCookieIntoChunks() for splitting large cookies
  • Implemented reconstructCookie() and reconstructCookieWithStore() for transparent reassembly
  • Updated save() method to handle chunking for both Node.js and CookieStore patterns
  • Updated destroy() method to clean up all chunks

Documentation:

  • Added comprehensive README section explaining the feature
  • Added usage examples for both patterns (req/res and CookieStore)
  • Added FAQ entry: "What if my session data exceeds cookie size limits?"

Testing:

  • Added 6 test cases covering all chunking scenarios

Note: This PR addresses a common pain point while maintaining iron-session's stateless, cookie-based architecture. It's a middle-ground solution between "reduce your data" and "switch to Redis."

This adds optional cookie chunking to handle session data that exceeds
browser cookie size limits (~4KB). When enabled, large cookies are
automatically split into multiple smaller cookies and reconstructed
transparently when reading.

Features:
- Opt-in via chunking config: { enabled: true, chunkSize: 3500 }
- Automatic chunking when session exceeds chunk size
- Transparent reconstruction from chunks on read
- Automatic cleanup of old chunks on save/destroy
- Works with both Node.js req/res and CookieStore patterns
- Backward compatible with existing non-chunked sessions

Chunk naming pattern: {cookieName}.0, {cookieName}.1, etc.

Includes comprehensive test coverage with 6 new test cases.
@vercel

vercel Bot commented Dec 1, 2025

Copy link
Copy Markdown

@sefasenturk95 is attempting to deploy a commit to the Codeagain Team on Vercel.

A member of the Team first needs to authorize it.

- Add type assertions (as any) to collectAllCookies calls to fix mock type mismatch
- Add non-null assertions for array access in test assertions
- Fixes prepare script errors during npm install
@sefasenturk95

Copy link
Copy Markdown
Author

@vvo could you take a look at this please? :) Thanks in advance!

@sefasenturk95

Copy link
Copy Markdown
Author

@vvo hello? Anyone there? 🙈

@coderabbitai coderabbitai Bot mentioned this pull request May 10, 2026
1 task
ThomasGross added a commit to danskernesdigitalebibliotek/dpl-web that referenced this pull request Jun 10, 2026
The Unilogin session (access + refresh + id token) overflows the 4096
byte browser cookie limit, breaking sign-in. Wrap iron-session's
exported sealData/unsealData and split the seal across go-session.0,
go-session.1, ... when it would otherwise exceed the limit. Mirrors the
approach in the unmerged upstream PR vvo/iron-session#937 so we can drop
the wrapper once that lands.
xendk pushed a commit to danskernesdigitalebibliotek/dpl-web that referenced this pull request Jun 10, 2026
The go-session cookie holds two JWTs (access + refresh) plus PKCE and
profile fields, and we are hitting the 4 KB per-cookie browser limit for
some users. Iron-session 8.0.4 has no built-in workaround and the
upstream fix is unreviewed (vvo/iron-session#937).

Apply that PR as a patch-package patch against an exact-pinned
iron-session@8.0.4 so chunking is available without forking, and enable
it in getSessionOptions. With chunking on, iron-session keeps a single
go-session cookie when the sealed value fits, and otherwise splits it
into go-session.0, go-session.1, ... transparently on read.

Letting CI fail on a future Dependabot bump is the intended signal to
refresh or drop the patch when upstream merges.

To reproduce the patch file (iron-session ships only compiled dist/*,
so we cannot patch the source files referenced by PR #937 directly):

    # 1. Get the iron-session source at v8.0.4 with PR #937 applied.
    git clone https://github.com/vvo/iron-session.git
    cd iron-session
    git fetch origin pull/937/head:pr-937
    git diff v8.0.4..pr-937 -- src/core.ts > /tmp/pr-937-core.diff
    git checkout v8.0.4
    git apply /tmp/pr-937-core.diff

    # 2. Build dist/* with the upstream toolchain.
    npm install --ignore-scripts
    npx tsup

    # 3. Copy the built dist into this project's node_modules and let
    #    patch-package capture the diff against the published 8.0.4.
    cp dist/* <dpl-web>/go/node_modules/iron-session/dist/
    cd <dpl-web>/go
    npx patch-package iron-session

This keeps the committed patch bit-for-bit faithful to what upstream
would publish if PR #937 merged, rather than hand-translating
TypeScript changes into compiled output.

Assisted-by: Claude <noreply@anthropic.com>
xendk pushed a commit to danskernesdigitalebibliotek/dpl-web that referenced this pull request Jun 10, 2026
The go-session cookie holds two JWTs (access + refresh) plus PKCE and
profile fields, and we are hitting the 4 KB per-cookie browser limit for
some users. Iron-session 8.0.4 has no built-in workaround and the
upstream fix is unreviewed (vvo/iron-session#937).

Apply that PR as a patch-package patch against an exact-pinned
iron-session@8.0.4 so chunking is available without forking, and enable
it in getSessionOptions. With chunking on, iron-session keeps a single
go-session cookie when the sealed value fits, and otherwise splits it
into go-session.0, go-session.1, ... transparently on read.

Letting CI fail on a future Dependabot bump is the intended signal to
refresh or drop the patch when upstream merges.

To reproduce the patch file (iron-session ships only compiled dist/*,
so we cannot patch the source files referenced by PR #937 directly):

    # 1. Get the iron-session source at v8.0.4 with PR #937 applied.
    git clone https://github.com/vvo/iron-session.git
    cd iron-session
    git fetch origin pull/937/head:pr-937
    git diff v8.0.4..pr-937 -- src/core.ts > /tmp/pr-937-core.diff
    git checkout v8.0.4
    git apply /tmp/pr-937-core.diff

    # 2. Build dist/* with the upstream toolchain.
    npm install --ignore-scripts
    npx tsup

    # 3. Copy the built dist into this project's node_modules and let
    #    patch-package capture the diff against the published 8.0.4.
    cp dist/* <dpl-web>/go/node_modules/iron-session/dist/
    cd <dpl-web>/go
    npx patch-package iron-session

This keeps the committed patch bit-for-bit faithful to what upstream
would publish if PR #937 merged, rather than hand-translating
TypeScript changes into compiled output.

Assisted-by: Claude <noreply@anthropic.com>
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.

1 participant