cherry-pick(#27098): fix(har): handle invalid Expires/Max-Age (#27123)

Fixes #27073.
This commit is contained in:
Dmitry Gozman 2023-09-15 09:26:39 -07:00 committed by GitHub
parent 476b74f7c4
commit ed919f3dda
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View file

@ -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)
};
});
@ -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')
@ -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');

View file

@ -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);