Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add constraint relaxation by default & strict mode #233

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/fingerprint-generator/src/fingerprint-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ export class FingerprintGenerator extends HeaderGenerator {
})
: undefined;

return utils.getPossibleValues(this.fingerprintGeneratorNetwork, filteredValues);
try {
return utils.getPossibleValues(this.fingerprintGeneratorNetwork, filteredValues);
} catch (e) {
if (options?.strict) throw e;
delete filteredValues.screen;
return undefined;
}
})();

while (true) {
Expand Down
27 changes: 26 additions & 1 deletion packages/header-generator/src/header-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const headerGeneratorOptionsShape = {
locales: ow.optional.array.ofType(ow.string),
httpVersion: ow.optional.string.oneOf(SUPPORTED_HTTP_VERSIONS),
browserListQuery: ow.optional.string,
strict: ow.optional.boolean,
};

/**
Expand Down Expand Up @@ -113,6 +114,10 @@ export interface HeaderGeneratorOptions {
* Can be either 1 or 2. Default value is 2.
*/
httpVersion: HttpVersion;
/**
* If true, the generator will throw an error if it cannot generate headers based on the input.
*/
strict: boolean;
}

/**
Expand Down Expand Up @@ -155,6 +160,14 @@ export class HeaderGenerator {

private headersOrder: string[];

private relaxationOrder: (keyof typeof headerGeneratorOptionsShape)[] = [
'locales',
'devices',
'operatingSystems',
'browsers',
'browserListQuery',
];

/**
* @param options Default header generation options used - unless overridden.
*/
Expand All @@ -168,6 +181,7 @@ export class HeaderGenerator {
locales = ['en-US'],
httpVersion = '2',
browserListQuery = '',
strict = false,
} = options;
this.globalOptions = {
browsers: this._prepareBrowsersConfig(browsers as BrowsersType, browserListQuery, httpVersion),
Expand All @@ -176,6 +190,7 @@ export class HeaderGenerator {
locales,
httpVersion,
browserListQuery,
strict,
};
this.uniqueBrowsers = [];

Expand Down Expand Up @@ -251,7 +266,17 @@ export class HeaderGenerator {

return this.orderHeaders(converted2to1);
}
throw new Error('No headers based on this input can be generated. Please relax or change some of the requirements you specified.');

const relaxationIndex = this.relaxationOrder.findIndex((key) => options[key] !== undefined);
if (options.strict || relaxationIndex === -1) {
throw new Error('No headers based on this input can be generated. Please relax or change some of the requirements you specified.');
}

// Relax the requirements and try again
const relaxedOptions = { ...options };
const relaxationKey = this.relaxationOrder[relaxationIndex];
relaxedOptions[relaxationKey] = undefined as never;
return this.getHeaders(relaxedOptions, requestDependentHeaders, userAgentValues);
}

// Generate the actual headers
Expand Down
32 changes: 32 additions & 0 deletions test/fingerprint-generator/generation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,36 @@ describe('Generate fingerprints with basic constraints', () => {
},
})).toBeDefined();
});

test.only('[relaxation] header strict mode propagates', () => {
const fingerprintGenerator = new FingerprintGenerator();

expect(fingerprintGenerator.getFingerprint({
devices: ['mobile'],
operatingSystems: ['windows'],
})).toBeDefined();

expect(() => fingerprintGenerator.getFingerprint({
devices: ['mobile'],
operatingSystems: ['windows'],
strict: true,
})).toThrow();
});

test.only('[relaxation] strict mode works with fp-only features', () => {
const fingerprintGenerator = new FingerprintGenerator();

expect(fingerprintGenerator.getFingerprint({
screen: {
minHeight: 9999,
},
})).toBeDefined();

expect(() => fingerprintGenerator.getFingerprint({
screen: {
minHeight: 9999,
},
strict: true,
})).toThrow();
});
});
10 changes: 9 additions & 1 deletion test/header-generator/generation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,13 @@ describe('Generation tests', () => {
expect(/phone|android|mobile/i.test(headers['user-agent'])).toBeTruthy();
});

test('Throws an error when nothing can be generated', () => {
test('Strict mode throws an error when nothing can be generated', () => {
try {
headerGenerator.getHeaders({
browsers: [{
name: 'non-existing-browser',
}],
strict: true,
} as unknown as HeaderGeneratorOptions);
fail("HeaderGenerator didn't throw an error when trying to generate headers for a nonexisting browser.");
} catch (error) {
Expand All @@ -172,6 +173,13 @@ describe('Generation tests', () => {
}
});

test('Default mode generates an approximately good header', () => {
expect(() => headerGenerator.getHeaders({
devices: ['mobile'],
operatingSystems: ['windows'],
} as unknown as HeaderGeneratorOptions)).not.toThrow();
});

test('Supports browserListQuery generation', () => {
const headers = headerGenerator.getHeaders({
browserListQuery: 'last 15 firefox versions',
Expand Down