Repository navigation
fix: forward custom encode option to cookie serializer - #377
Open
ezefernandezyf wants to merge 1 commit into
Open
ezefernandezyf wants to merge 1 commit into
ezefernandezyf wants to merge 1 commit into
Conversation
cookie v2 split the stringify API into two arguments:
stringifySetCookie(cookie: SetCookie, options?: StringifyOptions)
`encode` belongs to `StringifyOptions`, while the cookie attributes stay in
the first argument. Since the cookie v2.x migration (fastify#361) every option was
merged into the first argument, so a custom `encode` was silently dropped and
the default encoder was used instead, double encoding the value: `%2F`
became `%252F`.
Split `encode` out of the attributes and pass it as the second argument.
`options || {}` is required because `fastify.serializeCookie(name, value)` is
a public call path with an optional options object, and a bare destructuring
would throw a TypeError there.
Refs: fastify#376
Author
|
Found #374 after opening this one, which covers the same fix. I am leaving this open in case the maintainers prefer the stricter shape: here only It also adds coverage for the other two paths through the same helper: This closes #376, which was filed after #374 and is not referenced there. Happy to close this if #374 is the preferred path. |
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.
What this fixes
Fixes #376
reply.setCookie(),fastify.serializeCookie()and the exportedserialize()stoppedhonoring the documented
encodeoption after the cookie v2.x migration.The caller's encoder is ignored and the default
encodeURIComponentis used instead, so analready escaped value is escaped a second time (
%2Fbecomes%252F). The header also grows(4091 to 4103 bytes in the report), which can push a cookie past the 4096 byte limit browsers
commonly enforce, causing the client to drop the cookie entirely.
Root cause
cookie v2 split its stringify API into two arguments:
encodebelongs toStringifyOptions, while the cookie attributes (name,value,sameSite,...) stay in the first argument. Since the migration (#361) the plugin merged every option into the
first argument, so
encodewas silently ignored: JavaScript does not complain about a key thecalled function never reads.
What to review first
index.js,serialize():encodeis destructured out of the attributes and passed as thesecond argument. That is the whole fix.
options || {}is load bearing, not defensive noise.fastify.serializeCookie(name, value)isa public call path with an optional options object and
types/index.tst.tsexercises it with twoarguments. The previous code tolerated
undefinedbecauseObject.assign({ name, value }, undefined)is legal, and a bare destructuring would throw aTypeErrorthere. There is a testfor this path.
mainand pass with the fix.Verification
npm testgreen: 56 unit tests,c8 --100coverage intact, 46 tstyche assertions.npm run lintclean.encodeis supplied the output is byte identical to before, because cookie v2 resolvesthe encoder as
encode || defaultEncode.Out of scope
encodewas already declared inSerializeOptions.Fixes #376