-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Cookies set on server are improperly escaped #11687
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
Comments
The server behavior sounds correct to me. The default behavior in the underlying What are you doing on the browser so that it's set differently? And what value does it end up being? |
When I force it to run in the browser (by moving |
|
Ah, my bad. The code I ran with the universal |
If it's an external API, that's going to have its own decisions that have been made about how/whether to encode cookie values. In any case, I suspect the solution for you is to pass an identity |
Here's a better reproduction. I have an API endpoint that sets a cookie:
and a universal
When I use But when I use You can find the full code here Also see #1198 for a similar bug (except in that issue, the "bad" case wouldn't set a cookie at all) |
Use the cookies arg of the load function or if you really want to set it manually use |
@gterras the |
From the link above :
Just use the |
This issue happens when fetching any URL returning a
|
Sorry for the delay - yep, this indeed looks like a bug in how cookies are (re-)serialized when server-side rendering is collecting together the cookies getting sent by the various API calls made with kit/packages/kit/src/runtime/server/fetch.js Lines 131 to 132 in c4efc26
kit/packages/kit/src/runtime/server/cookie.js Line 186 in a2fe7ab
As an aside, in your repro you should be |
My (untested) guess for a fix would be something like this: diff --git a/packages/kit/src/runtime/server/fetch.js b/packages/kit/src/runtime/server/fetch.js
index e59112092..dcd1c240b 100644
--- a/packages/kit/src/runtime/server/fetch.js
+++ b/packages/kit/src/runtime/server/fetch.js
@@ -132,14 +132,17 @@ export function create_fetch({ event, options, manifest, state, get_cookie_heade
const set_cookie = response.headers.get('set-cookie');
if (set_cookie) {
for (const str of set_cookie_parser.splitCookiesString(set_cookie)) {
- const { name, value, ...options } = set_cookie_parser.parseString(str);
+ const { name, value, ...options } = set_cookie_parser.parseString(str, {
+ decodeValues: false
+ });
const path = options.path ?? (url.pathname.split('/').slice(0, -1).join('/') || '/');
// options.sameSite is string, something more specific is required - type cast is safe
set_internal(name, value, {
path,
- .../** @type {import('cookie').CookieSerializeOptions} */ (options)
+ .../** @type {import('cookie').CookieSerializeOptions} */ (options),
+ encode: (value) => value
});
}
} We need to tell the |
Describe the bug
This
load
function in+page.server.ts
sets a cookie with value"a/b"
:But the value that's actually returned in the
Set-Cookie
header and saved in the browser is%22a%2Fb%22
. This bug only happens whenload
is run on the server, not in the browser.Reproduction
Code: https://codesandbox.io/p/devbox/sveltekit-cookie-issue-l3c2zc?file=%2Fsrc%2Froutes%2F%2Bpage.server.ts%3A4%2C2
Website: https://l3c2zc-5173.csb.app/
(The code is deployed on the website above, and visiting the website should set the incorrect cookie
%22a%2Fb%22
in your browser)Logs
No response
System Info
Severity
serious, but I can work around it
Additional Information
No response
The text was updated successfully, but these errors were encountered: