From 776cdb8e1122d89ede4f2d2332d8351354721a91 Mon Sep 17 00:00:00 2001 From: Tom Schuster Date: Mon, 7 Oct 2024 22:47:15 +0200 Subject: [PATCH] web: PluginArray, MimeType and MimeTypeArray prototype spoofing --- web/packages/core/src/plugin-polyfill.ts | 60 ++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/web/packages/core/src/plugin-polyfill.ts b/web/packages/core/src/plugin-polyfill.ts index b9fb4413bf77..99cec3075999 100644 --- a/web/packages/core/src/plugin-polyfill.ts +++ b/web/packages/core/src/plugin-polyfill.ts @@ -38,11 +38,13 @@ class RuffleMimeTypeArray implements MimeTypeArray { * @param mimeType The mime type to install */ install(mimeType: MimeType): void { + const wrapper = new RuffleMimeType(mimeType); + const index = this.__mimeTypes.length; - this.__mimeTypes.push(mimeType); - this.__namedMimeTypes[mimeType.type] = mimeType; - this[mimeType.type] = mimeType; - this[index] = mimeType; + this.__mimeTypes.push(wrapper); + this.__namedMimeTypes[mimeType.type] = wrapper; + this[wrapper.type] = wrapper; + this[index] = wrapper; } item(index: number): MimeType { @@ -66,6 +68,43 @@ class RuffleMimeTypeArray implements MimeTypeArray { [Symbol.iterator](): IterableIterator { return this.__mimeTypes[Symbol.iterator](); } + + get [Symbol.toStringTag](): string { + return "MimeTypeArray"; + } +} + +/** + * Replacement object for the built-in MimeType object. + * This only exists, because the built-in type is not constructable and we + * need to spoof `window.MimeType`. + */ +class RuffleMimeType implements MimeType { + private readonly __mimeType: MimeType; + + constructor(mimeType: MimeType) { + this.__mimeType = mimeType; + } + + get type(): string { + return this.__mimeType.type; + } + + get description(): string { + return this.__mimeType.description; + } + + get suffixes(): string { + return this.__mimeType.suffixes; + } + + get enabledPlugin(): Plugin { + return this.__mimeType.enabledPlugin; + } + + get [Symbol.toStringTag](): string { + return "MimeType"; + } } /** @@ -141,6 +180,10 @@ class RufflePluginArray implements PluginArray { return this.__plugins[Symbol.iterator](); } + get [Symbol.toStringTag](): string { + return "PluginArray"; + } + get length(): number { return this.__plugins.length; } @@ -205,6 +248,9 @@ export function installPlugin(plugin: RufflePlugin): void { return; } if (!("install" in navigator.plugins) || !navigator.plugins["install"]) { + Object.defineProperty(window, "PluginArray", { + value: RufflePluginArray, + }); Object.defineProperty(navigator, "plugins", { value: new RufflePluginArray(navigator.plugins), writable: false, @@ -218,6 +264,12 @@ export function installPlugin(plugin: RufflePlugin): void { plugin.length > 0 && (!("install" in navigator.mimeTypes) || !navigator.mimeTypes["install"]) ) { + Object.defineProperty(window, "MimeTypeArray", { + value: RuffleMimeTypeArray, + }); + Object.defineProperty(window, "MimeType", { + value: RuffleMimeType, + }); Object.defineProperty(navigator, "mimeTypes", { value: new RuffleMimeTypeArray(navigator.mimeTypes), writable: false,