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

web: Spoof prototypes to prevent false positive bot detections #18157

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
56 changes: 44 additions & 12 deletions web/packages/core/src/plugin-polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ FLASH_PLUGIN.install({
enabledPlugin: FLASH_PLUGIN,
});

declare global {
interface PluginArray {
install?: (plugin: Plugin) => void;
}

interface MimeTypeArray {
install?: (mimeType: MimeType) => void;
}
}
// declare global {
// interface PluginArray {
// install?: (plugin: Plugin) => void;
// }
//
// interface MimeTypeArray {
// install?: (mimeType: MimeType) => void;
// }
// }

/**
* Install a fake plugin such that detectors will see it in `navigator.plugins`.
Expand All @@ -205,13 +205,45 @@ export function installPlugin(plugin: RufflePlugin): void {
return;
}
if (!("install" in navigator.plugins) || !navigator.plugins["install"]) {
const plugins = new RufflePluginArray(navigator.plugins);

// bypass missing method
// @ts-expect-error: We're adding a method to the object
PluginArray.prototype.install = function (_) {};

const proto = Object.create(PluginArray.prototype);
Object.setPrototypeOf(plugins, proto);

Object.defineProperty(navigator, "plugins", {
value: new RufflePluginArray(navigator.plugins),
value: plugins,
writable: false,
});

// bypass TypeError on Firefox
Object.defineProperty(navigator.plugins, "namedItem", {
configurable: false,
enumerable: true,
value: function namedItem(str: string) {
return this[str] || null;
},
});

Object.defineProperty(navigator.plugins, "refresh", {
configurable: false,
enumerable: true,
value: function refresh() {},
});

Object.defineProperty(navigator.plugins, "item", {
configurable: false,
enumerable: true,
value: function item(index: number) {
return this[index] || null;
},
});
}

const plugins = navigator.plugins;
const plugins = navigator.plugins as RufflePluginArray;
plugins.install!(plugin);

if (
Expand All @@ -224,7 +256,7 @@ export function installPlugin(plugin: RufflePlugin): void {
});
}

const mimeTypes = navigator.mimeTypes;
const mimeTypes = navigator.mimeTypes as RuffleMimeTypeArray;
for (let i = 0; i < plugin.length; i += 1) {
mimeTypes.install!(plugin[i]!);
}
Expand Down