Skip to content
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

fix: [#1693] Ignore invalid cookies in CookieContainer #1701

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading