Skip to content

Commit 951224d

Browse files
authored
fix: warn on invalid cookie name characters (#12806)
1 parent 557897a commit 951224d

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

.changeset/old-points-tell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: warn on invalid cookie name characters

packages/kit/src/runtime/server/cookie.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { parse, serialize } from 'cookie';
22
import { add_data_suffix, normalize_path, resolve } from '../../utils/url.js';
33

4+
// eslint-disable-next-line no-control-regex -- control characters are invalid in cookie names
5+
const INVALID_COOKIE_CHARACTER_REGEX = /[\x00-\x1F\x7F()<>@,;:"/[\]?={} \t]/;
6+
47
/**
58
* Tracks all cookies set during dev mode so we can emit warnings
69
* when we detect that there's likely cookie misusage due to wrong paths
@@ -113,6 +116,14 @@ export function get_cookies(request, url, trailing_slash) {
113116
* @param {import('./page/types.js').Cookie['options']} options
114117
*/
115118
set(name, value, options) {
119+
// TODO: remove this check in 3.0
120+
const illegal_characters = name.match(INVALID_COOKIE_CHARACTER_REGEX);
121+
if (illegal_characters) {
122+
console.warn(
123+
`The cookie name "${name}" will be invalid in SvelteKit 3.0 as it contains ${illegal_characters.join(' and ')}. See RFC 2616 for more details https://datatracker.ietf.org/doc/html/rfc2616#section-2.2`
124+
);
125+
}
126+
116127
validate_options(options);
117128
set_internal(name, value, { ...defaults, ...options });
118129
},

0 commit comments

Comments
 (0)