Skip to content

Commit ed919f3

Browse files
authored
cherry-pick(#27098): fix(har): handle invalid Expires/Max-Age (#27123)
Fixes #27073.
1 parent 476b74f commit ed919f3

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

packages/playwright-core/src/server/har/harTracer.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ export class HarTracer {
223223
harEntry.response.cookies = this._options.omitCookies ? [] : event.cookies.map(c => {
224224
return {
225225
...c,
226-
expires: c.expires === -1 ? undefined : new Date(c.expires).toISOString()
226+
expires: c.expires === -1 ? undefined : safeDateToISOString(c.expires)
227227
};
228228
});
229229

@@ -658,11 +658,11 @@ function parseCookie(c: string): har.Cookie {
658658
if (name === 'Domain')
659659
cookie.domain = value;
660660
if (name === 'Expires')
661-
cookie.expires = new Date(value).toISOString();
661+
cookie.expires = safeDateToISOString(value);
662662
if (name === 'HttpOnly')
663663
cookie.httpOnly = true;
664664
if (name === 'Max-Age')
665-
cookie.expires = new Date(Date.now() + (+value) * 1000).toISOString();
665+
cookie.expires = safeDateToISOString(Date.now() + (+value) * 1000);
666666
if (name === 'Path')
667667
cookie.path = value;
668668
if (name === 'SameSite')
@@ -673,4 +673,11 @@ function parseCookie(c: string): har.Cookie {
673673
return cookie;
674674
}
675675

676+
function safeDateToISOString(value: string | number) {
677+
try {
678+
return new Date(value).toISOString();
679+
} catch (e) {
680+
}
681+
}
682+
676683
const startedDateSymbol = Symbol('startedDate');

tests/library/har.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,20 @@ it('should include set-cookies', async ({ contextFactory, server }, testInfo) =>
226226
expect(new Date(cookies[2].expires).valueOf()).toBeGreaterThan(Date.now());
227227
});
228228

229+
it('should skip invalid Expires', async ({ contextFactory, server }, testInfo) => {
230+
const { page, getLog } = await pageWithHar(contextFactory, testInfo);
231+
server.setRoute('/empty.html', (req, res) => {
232+
res.setHeader('Set-Cookie', [
233+
'name=value;Expires=Sat Sep 14 01:02:27 CET 2024',
234+
]);
235+
res.end();
236+
});
237+
await page.goto(server.EMPTY_PAGE);
238+
const log = await getLog();
239+
const cookies = log.entries[0].response.cookies;
240+
expect(cookies[0]).toEqual({ name: 'name', value: 'value' });
241+
});
242+
229243
it('should include set-cookies with comma', async ({ contextFactory, server, browserName }, testInfo) => {
230244
it.fixme(browserName === 'webkit', 'We get "name1=val, ue1, name2=val, ue2" as a header value');
231245
const { page, getLog } = await pageWithHar(contextFactory, testInfo);

0 commit comments

Comments
 (0)