From 52cd54f398bc069ebb7c2071646059c8cbabbf7a Mon Sep 17 00:00:00 2001 From: David Ortner Date: Wed, 22 Jan 2025 02:29:34 +0100 Subject: [PATCH] fix: [#1693] Ignore invalid cookies in CookieContainer --- .../happy-dom/src/cookie/CookieContainer.ts | 24 ++++++++----------- .../happy-dom/src/cookie/ICookieContainer.ts | 3 ++- .../test/cookie/CookieContainer.test.ts | 20 ++++++---------- 3 files changed, 19 insertions(+), 28 deletions(-) diff --git a/packages/happy-dom/src/cookie/CookieContainer.ts b/packages/happy-dom/src/cookie/CookieContainer.ts index 12d37040..c01fcdac 100644 --- a/packages/happy-dom/src/cookie/CookieContainer.ts +++ b/packages/happy-dom/src/cookie/CookieContainer.ts @@ -33,22 +33,18 @@ export default class CookieContainer implements ICookieContainer { for (const cookie of cookies) { const newCookie = Object.assign({}, DefaultCookie, cookie); - if (!newCookie || !newCookie.key || !newCookie.originURL) { - throw new Error( - "Failed to execute 'addCookies' on 'CookieContainer': The properties 'key' and 'originURL' are required." - ); - } - - // Remove existing cookie with same name, domain and path. - const index = indexMap[getKey(newCookie)]; + if (newCookie && newCookie.key && newCookie.originURL) { + // Remove existing cookie with same name, domain and path. + const index = indexMap[getKey(newCookie)]; - if (index !== undefined) { - this.#cookies.splice(index, 1); - } + if (index !== undefined) { + this.#cookies.splice(index, 1); + } - if (!CookieExpireUtility.hasExpired(newCookie)) { - indexMap[getKey(newCookie)] = this.#cookies.length; - this.#cookies.push(newCookie); + if (!CookieExpireUtility.hasExpired(newCookie)) { + indexMap[getKey(newCookie)] = this.#cookies.length; + this.#cookies.push(newCookie); + } } } } diff --git a/packages/happy-dom/src/cookie/ICookieContainer.ts b/packages/happy-dom/src/cookie/ICookieContainer.ts index f757e0c4..85b68387 100644 --- a/packages/happy-dom/src/cookie/ICookieContainer.ts +++ b/packages/happy-dom/src/cookie/ICookieContainer.ts @@ -1,5 +1,6 @@ import URL from '../url/URL.js'; import ICookie from './ICookie.js'; +import IOptionalCookie from './IOptionalCookie.js'; /** * Cookie Container. @@ -13,7 +14,7 @@ export default interface ICookieContainer { * * @param cookies Cookies. */ - addCookies(cookies: ICookie[]): void; + addCookies(cookies: IOptionalCookie[]): void; /** * Returns cookies. diff --git a/packages/happy-dom/test/cookie/CookieContainer.test.ts b/packages/happy-dom/test/cookie/CookieContainer.test.ts index 6c800c2f..747ef648 100644 --- a/packages/happy-dom/test/cookie/CookieContainer.test.ts +++ b/packages/happy-dom/test/cookie/CookieContainer.test.ts @@ -194,21 +194,15 @@ describe('CookieContainer', () => { ).toBe('__host-key=value'); }); - it('Throws an error if the cookie "key" is missing.', () => { + it('Ignores invalid cookies.', () => { const originURL = new URL('https://example.com/path/to/page/'); - expect(() => { - cookieContainer.addCookies([{ originURL }]); - }).toThrowError( - "Failed to execute 'addCookies' on 'CookieContainer': The properties 'key' and 'originURL' are required." - ); - }); + cookieContainer.addCookies([ + { originURL }, + { key: 'key' }, + (null) + ]); - it('Throws an error if the cookie "originURL" is missing.', () => { - expect(() => { - cookieContainer.addCookies([{ key: 'key' }]); - }).toThrowError( - "Failed to execute 'addCookies' on 'CookieContainer': The properties 'key' and 'originURL' are required." - ); + expect(cookieContainer.getCookies(originURL)).toEqual([]); }); });