fix: validate base64-prefixed chunked cookies decode to valid JSON#210
Merged
Conversation
2 tasks
o-santi
approved these changes
May 6, 2026
mandarini
pushed a commit
that referenced
this pull request
May 7, 2026
🤖 I have created a release *beep* *boop* --- ## [0.10.3](v0.10.2...v0.10.3) (2026-05-07) ### Bug Fixes * allow cookies encode without getAll/setAll on browser client ([#213](#213)) ([89f3f28](89f3f28)), closes [#170](#170) * enable tree-shaking for browser bundles ([#216](#216)) ([f009d71](f009d71)) * **tsconfig:** set explicit rootDir to silence TS6059 in consumer IDEs ([#211](#211)) ([a77ee8a](a77ee8a)), closes [#209](#209) * validate base64-prefixed chunked cookies decode to valid JSON ([#210](#210)) ([302cc0e](302cc0e)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). Co-authored-by: supabase-releaser[bot] <223506987+supabase-releaser[bot]@users.noreply.github.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Adds defensive validation to chunked cookie decoding so corrupted or mid-write cookie state no longer propagates a malformed string into
@supabase/auth-js. When a value is read via the chunked cookie path and carries thebase64-prefix written by this module, the decoded payload is now verified to be valid JSON (which it always must be, sincesetItemAsyncin auth-js writes viaJSON.stringify). On decode or parse failure we log a console warning and returnnull, signalling the entry is absent so the SDK can recover cleanly instead of crashing or re-saving garbage.What changed
decodeChunkedCookieValuehelper insrc/cookies.ts, used by both the browser and servergetItempaths insidecreateStorageFromOptions.base64-prefix (raw cookies and PKCE code verifier paths are unaffected).stringFromBase64URLand warns: "could not base64url-decode chunked cookie value..."nullin either failure case.getItempaths increateStorageFromOptionsnow delegate to this helper.src/cookies.spec.tscovering: invalid JSON in decoded chunks, valid JSON in decoded chunks, undecodable base64url payload, and that raw (non-base64) cookies are passed through without JSON validation.Why
Reported in #169 with a complete root cause analysis from the original reporter. When a session is large enough to span 3+ cookie chunks (
sb-...auth-token.0/.1/.2/...) and a server side refresh writes only some of the new chunks (e.g. response committed before allSet-Cookieheaders go out, partial set/remove during SSR, browser racing concurrent navigations), the browser ends up holding chunks from two different generations.combineChunksjoins them blindly and there are two failure modes downstream:stringFromBase64URLsucceeds, but the decoded result is garbage that failsJSON.parselater in auth-js. Before the auth-js companion fix, this produced aTypeError: Cannot create property 'user' on string ...in_recoverAndRefreshand embedded the access token JSON in the error message (token leaked into application error logs)..from a raw JWT segment that landed in a chunk.stringFromBase64URLthrows synchronously:Invalid Base64-URL character "." at position N. This surfaces as an unhandled rejection in production and was reported separately by another customer hitting the same root cause class.This change catches both failure modes at the source so they never reach auth-js. The corresponding auth-js change (supabase/supabase-js#2312) is the primary fix for the user visible crash from variant 1; this PR is defense in depth and is the actual fix for variant 2.
Notes for reviewers
base64-prefix that this module itself writes. Raw cookies (cookieEncoding: "raw"), and any cookie value that does not carry the prefix, are returned unchanged. This keeps the change safe for users storing non-JSON values via the storage interface and for the PKCE code verifier path.getItem. The server path retained its existingtypeof chunkedCookie !== "string"defensive shortcut for cases wherecombineChunksmight return a non-string at runtime.Tracking