Repository navigation
feat: add serializer option for session data - #308
Open
Lalithkishore1224 wants to merge 1 commit into
Open
Lalithkishore1224 wants to merge 1 commit into
Lalithkishore1224 wants to merge 1 commit into
Conversation
Session data is always encoded with JSON.stringify and decoded with
JSON.parse, so storing a BigInt (or anything else JSON cannot represent)
makes encodeSecureSession throw and the request fails with a 500.
Add an opt-in `serializer` option ({ stringify, parse }) per session so
users can choose how data is encoded before encryption. The default is
still JSON, so existing cookies and behavior are unchanged.
Fixes fastify#278
This branch has not been deployed
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.
Fixes #278
Session data goes through a hardcoded
JSON.stringifyinencodeSecureSessionandJSON.parseindecodeSecureSession(index.js). Putting a BigInt in the session makesJSON.stringifythrowTypeError: Do not know how to serialize a BigInt, and the request ends in a 500. There was no way to change how the data is encoded.I didn't change the default encoding. Turning BigInts into strings by default (as #307 tried) would mean they come back as strings, and that changes what's stored for everyone. Instead this adds an opt-in
serializeroption:Changes:
index.js: each session's options takeserializer(defaultJSON). It's stored with the session's key and expiry and used byencodeSecureSession/decodeSecureSession. Registration fails withserializer must have stringify and parse functionsif either function is missing.parsegets the decrypted message as a string.types/index.d.ts: newSessionSerializerinterface and an optionalserializerfield onSecureSessionPluginOptions.README.md: a "Custom serializer" section with the BigInt example and two caveats:__tshas to round-trip as a number, and if you switch serializers,parsestill has to read old cookies.test/serializer.test.js: tests a BigInt round trip through real requests (top-level and nested), checks that the encode/decode decorators call the serializer, checks per-session serializers with multiple sessions, and checks the validation error. Type tests are intypes/index.tst.ts.Verification. Repro script: a POST route sets
9007199254740993n, and a GET route reads it back from the cookie.Before (main) and after with no
serializer, which is unchanged:After, with the serializer above:
With index.js reverted, all 4 new tests fail. With types/index.d.ts reverted, the type tests fail.
npm testpasses: 54 unit tests with 100% coverage, 18 tstyche assertions. Lint is clean.Left out on purpose: there's no built-in BigInt encoding, and I didn't catch errors thrown by a custom
parse. Those errors propagate the same way aJSON.parsefailure would today.