Skip to content

Commit

Permalink
Fix sorting of http only and partitionKey.
Browse files Browse the repository at this point in the history
Fix types.
  • Loading branch information
amovar18 committed Jul 29, 2024
1 parent d2c6320 commit 998303c
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 40 deletions.
16 changes: 13 additions & 3 deletions packages/analysis-utils/src/browserManagement/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* External dependencies.
*/
import puppeteer, { Browser, HTTPResponse, Page, Protocol } from 'puppeteer';
import { parse } from 'simple-cookie';
import { parse, type Cookie } from 'simple-cookie';
import {
type CookieData,
type ScriptTagUnderCheck,
Expand Down Expand Up @@ -292,7 +292,7 @@ export class BrowserManagement {
const cookies: CookieData[] = headersToBeParsed
.split('\n')
.map((headerLine) => {
const parsedCookie: CookieData['parsedCookie'] = parse(headerLine);
const parsedCookie: Cookie = parse(headerLine);

const url = this.pageResponses[pageId][requestId]?.url;

Expand Down Expand Up @@ -338,6 +338,7 @@ export class BrowserManagement {
expires: parsedCookie.expires || 'Session',
httponly: parsedCookie.httponly || false,
secure: parsedCookie.secure || false,
partitionKey: '',
},
networkEvents: {
responseEvents: [
Expand Down Expand Up @@ -410,6 +411,7 @@ export class BrowserManagement {
expires: associatedCookie.cookie.expires || 'Session',
httpOnly: associatedCookie.cookie.httpOnly || false,
secure: associatedCookie.cookie.secure || false,
partitionKey: '',
},
networkEvents: {
requestEvents: [
Expand All @@ -429,6 +431,12 @@ export class BrowserManagement {
url: this.pageRequests[pageId][requestId]?.url || '',
headerType: 'request' as CookieData['headerType'],
};

if (associatedCookie.cookie?.partitionKey) {
singleCookie.parsedCookie.partitionKey =
associatedCookie.cookie?.partitionKey?.topLevelSite;
}

return singleCookie;
});

Expand Down Expand Up @@ -553,7 +561,9 @@ export class BrowserManagement {
cookie.domain = '.' + cookie.domain;
}
const key = cookie.name + ':' + cookie.domain + ':' + cookie.path;
frameCookies[key] = { parsedCookie: cookie };
frameCookies[key] = {
parsedCookie: { ...cookie, partitionKey: '' },
};
});

const frameUrl = new URL(frame.url()).origin;
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/cookies.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export type responseEvent = {

export type CookieData = {
parsedCookie: ParsedCookie & {
partitionKey?: string;
partitionKey: string;
priority?: 'Low' | 'Medium' | 'High';
size?: number;
};
Expand Down Expand Up @@ -200,7 +200,7 @@ export type CookieJsonDataType = {
name: string;
value: string;
domain: string;
partitionKey?: string;
partitionKey: string;
path: string;
expires: string;
httpOnly: boolean;
Expand Down
19 changes: 13 additions & 6 deletions packages/common/src/utils/parseRequestWillBeSentExtraInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export default function parseRequestWillBeSentExtraInfo(
cookie.expires
);

const { partitionKey, ...cookieWithoutPartitionKey } = cookie;

let domain;

if (cookie?.domain) {
Expand All @@ -66,15 +68,11 @@ export default function parseRequestWillBeSentExtraInfo(
const singleCookie: CookieData = {
isBlocked: blockedReasons.length !== 0,
parsedCookie: {
...cookie,
...cookieWithoutPartitionKey,
expires: effectiveExpirationDate,
samesite: cookie.sameSite?.toLowerCase() ?? '',
domain,
partitionKey:
cookie?.partitionKey && typeof cookie?.partitionKey === 'string'
? cookie?.partitionKey
: //@ts-ignore This is to handle both stable and canary version of Chrome.
cookie?.partitionKey?.topLevelSite,
partitionKey: '',
},
networkEvents: {
requestEvents: [
Expand All @@ -100,6 +98,15 @@ export default function parseRequestWillBeSentExtraInfo(
: undefined,
};

if (partitionKey) {
if (typeof partitionKey === 'string') {
singleCookie.parsedCookie.partitionKey = partitionKey as string;
} else {
singleCookie.parsedCookie.partitionKey =
partitionKey?.topLevelSite as string;
}
}

//Sometimes frameId comes empty so it shows data in other frames where cookie should not be shown.
if (frameIds.length > 0) {
singleCookie.frameIdList = [...frameIds];
Expand Down
25 changes: 12 additions & 13 deletions packages/common/src/utils/parseResponseReceivedExtraInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* External dependencies.
*/
import { parse } from 'simple-cookie';
import { parse, type Cookie } from 'simple-cookie';
import type { Protocol } from 'devtools-protocol';

/**
Expand Down Expand Up @@ -59,7 +59,7 @@ export default function parseResponseReceivedExtraInfo(
const responseToParse = headers['set-cookie'] ?? headers['Set-Cookie'];

responseToParse?.split('\n').forEach((headerLine: string) => {
let parsedCookie: CookieData['parsedCookie'] = parse(headerLine);
const parsedCookie: Cookie = parse(headerLine);

const blockedCookie = blockedCookies.find((c) => {
if (c.cookie) {
Expand All @@ -81,17 +81,6 @@ export default function parseResponseReceivedExtraInfo(
parsedCookie.expires
);

if (headerLine.toLowerCase().includes('partitioned')) {
parsedCookie = {
...parsedCookie,
partitionKey:
cookiePartitionKey && typeof cookiePartitionKey === 'string'
? cookiePartitionKey
: //@ts-ignore This is to handle both stable and canary version of Chrome.
cookiePartitionKey?.topLevelSite,
};
}

let domain;

if (parsedCookie?.domain) {
Expand All @@ -108,6 +97,7 @@ export default function parseResponseReceivedExtraInfo(
expires: effectiveExpirationDate,
samesite: parsedCookie.samesite ?? '',
domain,
partitionKey: '',
},
networkEvents: {
requestEvents: [],
Expand Down Expand Up @@ -135,6 +125,15 @@ export default function parseResponseReceivedExtraInfo(
: undefined,
};

if (headerLine.toLowerCase().includes('partitioned')) {
if (typeof cookiePartitionKey === 'string') {
singleCookie.parsedCookie.partitionKey = cookiePartitionKey as string;
} else {
singleCookie.parsedCookie.partitionKey =
cookiePartitionKey?.topLevelSite as string;
}
}

//Sometimes frameId comes empty so it shows data in other frames where cookie should not be shown.
if (frameIds.length > 0) {
singleCookie.frameIdList = [...frameIds];
Expand Down
14 changes: 7 additions & 7 deletions packages/design-system/src/test-data/cookieMockData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
/**
* External dependencies.
*/
import { type Cookie as ParsedCookie } from 'simple-cookie';
import { TabCookies, TabFrames } from '@google-psat/common';
import { CookieData, TabCookies, TabFrames } from '@google-psat/common';

export const emptyAnalytics = {
platform: '',
Expand All @@ -40,37 +39,38 @@ const emptyCookie = {
httponly: false,
path: '',
expires: '',
partitionKey: '',
};

export const uncategorized1pCookie: ParsedCookie = {
export const uncategorized1pCookie: CookieData['parsedCookie'] = {
...emptyCookie,
name: '_cb',
value: 'v1%3A168740954476563235',
domain: '.cnn.com',
};

export const uncategorized3pCookie: ParsedCookie = {
export const uncategorized3pCookie: CookieData['parsedCookie'] = {
...emptyCookie,
name: 'pubsyncexp',
value: 'uncategorized3pCookie',
domain: '.ads.pubmatic.com',
};

export const known1pCookie: ParsedCookie = {
export const known1pCookie: CookieData['parsedCookie'] = {
...emptyCookie,
name: '__qca',
value: 'known1pCookie',
domain: '.cnn.com',
};

export const known3pCookie: ParsedCookie = {
export const known3pCookie: CookieData['parsedCookie'] = {
...emptyCookie,
name: 'KRTBCOOKIE_290',
value: 'known3pCookie',
domain: '.pubmatic.com',
};

export const known3pCookieWithValue: ParsedCookie = {
export const known3pCookieWithValue: CookieData['parsedCookie'] = {
...emptyCookie,
name: 'KRTBCOOKIE_290',
value: 'known3p_Cookie-with%20value',
Expand Down
15 changes: 12 additions & 3 deletions packages/extension/src/store/synchnorousCookieStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,14 +632,23 @@ class SynchnorousCookieStore {
const parsedCookie = {
...this.tabsData[tabId][cookieKey].parsedCookie,
...cookie.parsedCookie,
httponly:
cookie.parsedCookie.httponly ??
this.tabsData[tabId][cookieKey].parsedCookie.httponly,
priority:
cookie.parsedCookie?.priority ??
this.tabsData[tabId][cookieKey].parsedCookie?.priority ??
'Medium',
partitionKey:
cookie.parsedCookie?.partitionKey ??
this.tabsData[tabId][cookieKey].parsedCookie?.partitionKey,
partitionKey: '',
};
if (
cookie.parsedCookie?.partitionKey ||
this.tabsData[tabId][cookieKey].parsedCookie?.partitionKey
) {
parsedCookie.partitionKey =
cookie.parsedCookie?.partitionKey ||
this.tabsData[tabId][cookieKey].parsedCookie?.partitionKey;
}

const networkEvents: CookieData['networkEvents'] = {
requestEvents: [
Expand Down
21 changes: 15 additions & 6 deletions packages/extension/src/utils/parseNetworkCookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ export default function parseNetworkCookies(
const effectiveExpirationDate = calculateEffectiveExpiryDate(
cookie.expires
);

const { partitionKey, ...cookieWithoutPartitionKey } = cookie;

const singleCookie: CookieData = {
isBlocked: false,
blockedReasons: [],
parsedCookie: {
...cookie,
...cookieWithoutPartitionKey,
httponly: cookie?.httpOnly ?? false,
expires: effectiveExpirationDate,
samesite: cookie.sameSite ?? '',
partitionKey:
cookie?.partitionKey && typeof cookie?.partitionKey === 'string'
? cookie?.partitionKey
: //@ts-ignore This is to handle both stable and canary version of Chrome.
cookie?.partitionKey?.topLevelSite,
partitionKey: '',
},
networkEvents: {
requestEvents: [],
Expand All @@ -69,6 +69,15 @@ export default function parseNetworkCookies(
frameIdList: [frameId],
};

if (partitionKey) {
if (typeof partitionKey === 'string') {
singleCookie.parsedCookie.partitionKey = partitionKey as string;
} else {
singleCookie.parsedCookie.partitionKey =
partitionKey?.topLevelSite as string;
}
}

parsedCookies.push(singleCookie);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const tempSinglePageData: CompleteJson = {
expires: 'Session',
httpOnly: false,
secure: true,
partitionKey: '',
},
analytics: {
platform: 'Unknown',
Expand Down Expand Up @@ -106,6 +107,7 @@ export const tempMultiPageData: CompleteJson[] = [
expires: 'Session',
httpOnly: false,
secure: true,
partitionKey: '',
},
analytics: {
platform: 'Unknown',
Expand Down Expand Up @@ -157,6 +159,7 @@ export const tempMultiPageData: CompleteJson[] = [
expires: 'Session',
httpOnly: false,
secure: true,
partitionKey: '',
},
analytics: {
platform: 'Unknown',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('reshapeCookies', () => {
expires: 'Session',
httpOnly: false,
secure: true,
partitionKey: '',
},
analytics: {
platform: 'Unknown',
Expand Down

0 comments on commit 998303c

Please sign in to comment.