Skip to content

Commit

Permalink
fix: don't inject request-specific headers (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
barjin authored Mar 20, 2023
1 parent 416ebdb commit 4397445
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
23 changes: 21 additions & 2 deletions packages/fingerprint-injector/src/fingerprint-injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ declare function runHeadlessFixes() : void;
export class FingerprintInjector {
private utilsJs = this._loadUtils();

/**
* Some HTTP headers depend on the request (for example Accept (with values application/json, image/png) etc.).
* This function filters out those headers and leaves only the browser-wide ones.
* @param headers Headers to be filtered.
* @returns Filtered headers.
*/
private onlyInjectableHeaders(headers: Record<string, string>): Record<string, string> {
const requestHeaders = ['accept-encoding', 'accept', 'cache-control', 'pragma',
'sec-fetch-dest', 'sec-fetch-mode', 'sec-fetch-site', 'sec-fetch-user', 'upgrade-insecure-requests'];

const filteredHeaders = { ...headers };

requestHeaders.forEach((header) => {
delete filteredHeaders[header];
});

return filteredHeaders;
}

/**
* Adds init script to the browser context, so the fingerprint is changed before every document creation.
* DISCLAIMER: Since Playwright does not support changing viewport and `user-agent` after the context is created,
Expand All @@ -41,7 +60,7 @@ export class FingerprintInjector {
enhancedFingerprint,
);

await browserContext.setExtraHTTPHeaders(headers);
await browserContext.setExtraHTTPHeaders(this.onlyInjectableHeaders(headers));

await browserContext.on('page', (page) => {
page.emulateMedia({ colorScheme: 'dark' })
Expand Down Expand Up @@ -76,7 +95,7 @@ export class FingerprintInjector {
deviceScaleFactor: screen.devicePixelRatio,

});
await page.setExtraHTTPHeaders(headers);
await page.setExtraHTTPHeaders(this.onlyInjectableHeaders(headers));

await page.emulateMediaFeatures([
{ name: 'prefers-color-scheme', value: 'dark' },
Expand Down
17 changes: 4 additions & 13 deletions test/fingerprint-injector/fingerprint-injector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,20 +304,11 @@ describe('FingerprintInjector', () => {
const requestHeaders = await requestObject.allHeaders?.() ?? requestObject.headers?.();
const { headers } = fingerprintWithHeaders;

function isHeaderInjectable(headerName: string) {
return ![
'accept',
'accept-encoding',
'sec-fetch-site',
'sec-fetch-mode',
'sec-fetch-dest',
].includes(headerName.toLowerCase());
}
// eslint-disable-next-line dot-notation
const onlyInjectable = (new FingerprintInjector())['onlyInjectableHeaders'];

for (const [header, value] of Object.entries(headers)) {
if (isHeaderInjectable(header)) {
expect(requestHeaders[header]).toBe(value);
}
for (const header of Object.keys(onlyInjectable(headers))) {
expect(requestHeaders[header]).toBe(headers[header]);
}
});
});
Expand Down

0 comments on commit 4397445

Please sign in to comment.