Skip to content

Commit

Permalink
cherry-pick(#27098): fix(har): handle invalid Expires/Max-Age (#27123)
Browse files Browse the repository at this point in the history
Fixes #27073.
  • Loading branch information
dgozman committed Sep 15, 2023
1 parent 476b74f commit ed919f3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/playwright-core/src/server/har/harTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export class HarTracer {
harEntry.response.cookies = this._options.omitCookies ? [] : event.cookies.map(c => {
return {
...c,
expires: c.expires === -1 ? undefined : new Date(c.expires).toISOString()
expires: c.expires === -1 ? undefined : safeDateToISOString(c.expires)
};
});

Expand Down Expand Up @@ -658,11 +658,11 @@ function parseCookie(c: string): har.Cookie {
if (name === 'Domain')
cookie.domain = value;
if (name === 'Expires')
cookie.expires = new Date(value).toISOString();
cookie.expires = safeDateToISOString(value);
if (name === 'HttpOnly')
cookie.httpOnly = true;
if (name === 'Max-Age')
cookie.expires = new Date(Date.now() + (+value) * 1000).toISOString();
cookie.expires = safeDateToISOString(Date.now() + (+value) * 1000);
if (name === 'Path')
cookie.path = value;
if (name === 'SameSite')
Expand All @@ -673,4 +673,11 @@ function parseCookie(c: string): har.Cookie {
return cookie;
}

function safeDateToISOString(value: string | number) {
try {
return new Date(value).toISOString();
} catch (e) {
}
}

const startedDateSymbol = Symbol('startedDate');
14 changes: 14 additions & 0 deletions tests/library/har.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,20 @@ it('should include set-cookies', async ({ contextFactory, server }, testInfo) =>
expect(new Date(cookies[2].expires).valueOf()).toBeGreaterThan(Date.now());
});

it('should skip invalid Expires', async ({ contextFactory, server }, testInfo) => {
const { page, getLog } = await pageWithHar(contextFactory, testInfo);
server.setRoute('/empty.html', (req, res) => {
res.setHeader('Set-Cookie', [
'name=value;Expires=Sat Sep 14 01:02:27 CET 2024',
]);
res.end();
});
await page.goto(server.EMPTY_PAGE);
const log = await getLog();
const cookies = log.entries[0].response.cookies;
expect(cookies[0]).toEqual({ name: 'name', value: 'value' });
});

it('should include set-cookies with comma', async ({ contextFactory, server, browserName }, testInfo) => {
it.fixme(browserName === 'webkit', 'We get "name1=val, ue1, name2=val, ue2" as a header value');
const { page, getLog } = await pageWithHar(contextFactory, testInfo);
Expand Down

0 comments on commit ed919f3

Please sign in to comment.