From 90471302835dad5251ea568091cfd6c21d757fd3 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Fri, 24 Dec 2021 19:00:07 +0100 Subject: feat: User agent data simulator --- packages/service-inject/src/index.ts | 27 ++++++ packages/service-inject/src/shims/userAgentData.ts | 102 ++++++++++++++++++++ packages/service-inject/src/utils.ts | 104 +++++++++++++++++++++ 3 files changed, 233 insertions(+) create mode 100644 packages/service-inject/src/index.ts create mode 100644 packages/service-inject/src/shims/userAgentData.ts create mode 100644 packages/service-inject/src/utils.ts (limited to 'packages/service-inject/src') diff --git a/packages/service-inject/src/index.ts b/packages/service-inject/src/index.ts new file mode 100644 index 0000000..7f48fdf --- /dev/null +++ b/packages/service-inject/src/index.ts @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2021-2022 Kristóf Marussy + * + * This file is part of Sophie. + * + * Sophie is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { shimUserAgentData } from './shims/userAgentData'; + +try { + shimUserAgentData(); +} catch (err) { + console.log('Failed to execute injected script:', err); +} diff --git a/packages/service-inject/src/shims/userAgentData.ts b/packages/service-inject/src/shims/userAgentData.ts new file mode 100644 index 0000000..be43823 --- /dev/null +++ b/packages/service-inject/src/shims/userAgentData.ts @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2021-2022 Kristóf Marussy + * + * This file is part of Sophie. + * + * Sophie is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { + defineProtoProperty, + deleteProtoProperty, + simulateNativeClass, + simulateNativeFunction, +} from '../utils'; + +export function shimUserAgentData(): void { + const brands = [ + { + brand: ' Not A; Brand', + version: '99', + }, + { + brand: 'Chromium', + version: '96', + } + ]; + const mobile = false; + const platform = 'Linux'; + + const simulatedNavigatorUa = simulateNativeClass('NavigatorUAData', function NavigatorUAData() { + // Nothing to initiailize. + }, { + brands: { + configurable: true, + enumerable: true, + get: simulateNativeFunction('brands', () => brands), + }, + mobile: { + configurable: true, + enumerable: true, + get: simulateNativeFunction('mobile', () => mobile), + }, + platform: { + configurable: true, + enumerable: true, + get: simulateNativeFunction('platform', () => platform), + }, + getHighEntropyValues: { + configurable: true, + enumerable: false, + value: simulateNativeFunction('getHighEntropyValues', (...args: unknown[]) => { + if (args.length == 0) { + throw new TypeError("Failed to execute 'getHighEntropyValues' on 'NavigatorUAData': 1 argument required, but only 0 present."); + } + const hints = Array.from(args[0] as Iterable); + if (hints.length === 0) { + return {}; + } + const data: Record = { + brands, + mobile, + } + if (hints.includes('platform')) { + data['platform'] = platform; + } + return Promise.resolve(data); + }) + }, + toJSON: { + configurable: true, + enumerable: false, + value: simulateNativeFunction('toJSON', () => ({ + brands, + mobile, + })), + writable: false, + }, + }); + + const simulatedUserAgentData = Reflect.construct(simulatedNavigatorUa, []); + defineProtoProperty(window.navigator, 'userAgentData', { + configurable: true, + enumerable: true, + get: simulateNativeFunction('userAgentData', () => simulatedUserAgentData), + }); +} + +export function deleteUserAgentData(): void { + deleteProtoProperty(window.navigator, 'userAgentData'); +} diff --git a/packages/service-inject/src/utils.ts b/packages/service-inject/src/utils.ts new file mode 100644 index 0000000..4bb3fba --- /dev/null +++ b/packages/service-inject/src/utils.ts @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2021-2022 Kristóf Marussy + * + * This file is part of Sophie. + * + * Sophie is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +/** + * Simulates a function defined in native code, i.e., one with + * `[native code]` in its `toString`. + * + * @param name The name of the function. + * @param f The function to transform. + * @return The transformed function. + */ +export function simulateNativeFunction( + name: string, + f: (this: null, ...args: P) => T, +): (...args: P) => T { + // Bound functions say `[native code]`, but unfortunately they omit the function name: + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/toString#description + // The type of `f` contains type variables, so we need some magic type casting. + const boundFunc = f.bind(null as ThisParameterType); + Object.defineProperty(boundFunc, 'name', { + configurable: true, + enumerable: false, + value: name, + writable: false, + }); + return boundFunc; +} + +/** + * Simulates a native class available on `globalThis`. + * + * @param name The name of the class. + * @param constructor The constructor function. Must already be a constructor (a named `function`). + * @param properties The properties to define on the prototype. + */ +export function simulateNativeClass( + name: string, + constructor: () => void, + properties: PropertyDescriptorMap, +) { + Object.defineProperties(constructor.prototype, { + [Symbol.toStringTag]: { + configurable: true, + enumerable: false, + value: name, + writable: false, + }, + ...properties, + }); + const simulatedConstructor = simulateNativeFunction(name, constructor); + Object.defineProperty(globalThis, name, { + configurable: true, + enumerable: true, + value: simulatedConstructor, + writable: true, + }); + return simulatedConstructor; +} + +/** + * Defines a property on the prototype of an object. + * + * Only use this with singleton objects, e.g., `window.navigator`. + * + * @param o The object to modify. Must be a singleton. + * @param property The key of the property being defined or modified. + * @param attributes The descriptor of the property being defined or modified. + */ +export function defineProtoProperty( + o: object, + property: PropertyKey, + attributes: PropertyDescriptor, +): void { + Object.defineProperty(Object.getPrototypeOf(o), property, attributes); +} + +/** + * Deletes a property from the prototype of an object. + * + * Only use this with singleton objects, e.g., `window.navigator`. + * + * @param o The object to modify. Must be a singleton. + * @param property The key of the property being deleted. + */ +export function deleteProtoProperty(o: object, property: PropertyKey): void { + Reflect.deleteProperty(Object.getPrototypeOf(o), property); +} -- cgit v1.2.3-54-g00ecf