Skip to content

Commit

Permalink
fix: [#1693] Ignore invalid cookies in CookieContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
capricorn86 committed Jan 22, 2025
1 parent 02130fb commit 52cd54f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 28 deletions.
24 changes: 10 additions & 14 deletions packages/happy-dom/src/cookie/CookieContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/happy-dom/src/cookie/ICookieContainer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import URL from '../url/URL.js';
import ICookie from './ICookie.js';
import IOptionalCookie from './IOptionalCookie.js';

/**
* Cookie Container.
Expand All @@ -13,7 +14,7 @@ export default interface ICookieContainer {
*
* @param cookies Cookies.
*/
addCookies(cookies: ICookie[]): void;
addCookies(cookies: IOptionalCookie[]): void;

/**
* Returns cookies.
Expand Down
20 changes: 7 additions & 13 deletions packages/happy-dom/test/cookie/CookieContainer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([<ICookie>{ originURL }]);
}).toThrowError(
"Failed to execute 'addCookies' on 'CookieContainer': The properties 'key' and 'originURL' are required."
);
});
cookieContainer.addCookies([
<ICookie>{ originURL },
<ICookie>{ key: 'key' },
<ICookie>(<unknown>null)
]);

it('Throws an error if the cookie "originURL" is missing.', () => {
expect(() => {
cookieContainer.addCookies([<ICookie>{ key: 'key' }]);
}).toThrowError(
"Failed to execute 'addCookies' on 'CookieContainer': The properties 'key' and 'originURL' are required."
);
expect(cookieContainer.getCookies(originURL)).toEqual([]);
});
});

Expand Down

0 comments on commit 52cd54f

Please sign in to comment.