Skip to content

Commit 6dae379

Browse files
fix: [#1162] Allow to use secure flag for cookies on "http://localhost"
Co-authored-by: David Ortner <david@ortner.se>
1 parent 0d1cbe0 commit 6dae379

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

packages/happy-dom/src/cookie/urilities/CookieURLUtility.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ export default class CookieURLUtility {
1414
* @returns "true" if cookie matches URL.
1515
*/
1616
public static cookieMatchesURL(cookie: ICookie, url: URL): boolean {
17+
const isLocalhost = url.hostname === 'localhost' || url.hostname.endsWith('.localhost');
1718
return (
18-
(!cookie.secure || url.protocol === 'https:') &&
19+
(!cookie.secure || url.protocol === 'https:' || isLocalhost) &&
1920
(!cookie.domain || url.hostname.endsWith(cookie.domain)) &&
2021
(!cookie.path || url.pathname.startsWith(cookie.path)) &&
2122
// @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#samesitesamesite-value

packages/happy-dom/test/cookie/CookieContainer.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,21 @@ describe('CookieContainer', () => {
163163
).toBe('__secure-key=value');
164164
});
165165

166+
it('Validates secure cookie keys for localhost', () => {
167+
const originURL = new URL('http://localhost');
168+
const targetURL = new URL('http://localhost');
169+
170+
expect(CookieStringUtility.stringToCookie(originURL, `__secure-key=value`)).toBe(null);
171+
172+
cookieContainer.addCookies([
173+
<ICookie>CookieStringUtility.stringToCookie(originURL, `__secure-key=value; Secure;`)
174+
]);
175+
176+
expect(
177+
CookieStringUtility.cookiesToString(cookieContainer.getCookies(targetURL, false))
178+
).toBe('__secure-key=value');
179+
});
180+
166181
it('Validates host cookie keys.', () => {
167182
const originURL = new URL('https://example.com/path/to/page/');
168183
const targetURL = new URL('https://example.com/path/to/page/');

0 commit comments

Comments
 (0)