From cbc7f7230a88f0b9b0508ae12cfa2df0c0a6a64e Mon Sep 17 00:00:00 2001 From: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Date: Thu, 27 Jun 2024 23:37:10 -0300 Subject: [PATCH 1/3] Fix acquiring WebpackRequire --- src/webpack/patchWebpack.ts | 105 +++++++++++++----------------------- 1 file changed, 36 insertions(+), 69 deletions(-) diff --git a/src/webpack/patchWebpack.ts b/src/webpack/patchWebpack.ts index 48f1b8147e..bd47cefad4 100644 --- a/src/webpack/patchWebpack.ts +++ b/src/webpack/patchWebpack.ts @@ -18,7 +18,7 @@ import { WEBPACK_CHUNK } from "@utils/constants"; import { Logger } from "@utils/Logger"; -import { canonicalizeMatch, canonicalizeReplacement } from "@utils/patches"; +import { canonicalizeReplacement } from "@utils/patches"; import { PatchReplacement } from "@utils/types"; import { WebpackInstance } from "discord-types/other"; @@ -27,7 +27,6 @@ import { patches } from "../plugins"; import { _initWebpack, beforeInitListeners, factoryListeners, moduleListeners, subscriptions, wreq } from "."; const logger = new Logger("WebpackInterceptor", "#8caaee"); -const initCallbackRegex = canonicalizeMatch(/{return \i\(".+?"\)}/); let webpackChunk: any[]; @@ -53,71 +52,6 @@ Object.defineProperty(window, WEBPACK_CHUNK, { } }); -// wreq.O is the webpack onChunksLoaded function -// Discord uses it to await for all the chunks to be loaded before initializing the app -// We monkey patch it to also monkey patch the initialize app callback to get immediate access to the webpack require and run our listeners before doing it -Object.defineProperty(Function.prototype, "O", { - configurable: true, - - set(onChunksLoaded: any) { - // When using react devtools or other extensions, or even when discord loads the sentry, we may also catch their webpack here. - // This ensures we actually got the right one - // this.e (wreq.e) is the method for loading a chunk, and only the main webpack has it - const { stack } = new Error(); - if ((stack?.includes("discord.com") || stack?.includes("discordapp.com")) && String(this.e).includes("Promise.all")) { - logger.info("Found main WebpackRequire.onChunksLoaded"); - - delete (Function.prototype as any).O; - - const originalOnChunksLoaded = onChunksLoaded; - onChunksLoaded = function (this: unknown, result: any, chunkIds: string[], callback: () => any, priority: number) { - if (callback != null && initCallbackRegex.test(callback.toString())) { - Object.defineProperty(this, "O", { - value: originalOnChunksLoaded, - configurable: true - }); - - const wreq = this as WebpackInstance; - - const originalCallback = callback; - callback = function (this: unknown) { - logger.info("Patched initialize app callback invoked, initializing our internal references to WebpackRequire and running beforeInitListeners"); - _initWebpack(wreq); - - for (const beforeInitListener of beforeInitListeners) { - beforeInitListener(wreq); - } - - originalCallback.apply(this, arguments as any); - }; - - callback.toString = originalCallback.toString.bind(originalCallback); - arguments[2] = callback; - } - - originalOnChunksLoaded.apply(this, arguments as any); - }; - - onChunksLoaded.toString = originalOnChunksLoaded.toString.bind(originalOnChunksLoaded); - - // Returns whether a chunk has been loaded - Object.defineProperty(onChunksLoaded, "j", { - set(v) { - delete onChunksLoaded.j; - onChunksLoaded.j = v; - originalOnChunksLoaded.j = v; - }, - configurable: true - }); - } - - Object.defineProperty(this, "O", { - value: onChunksLoaded, - configurable: true - }); - } -}); - // wreq.m is the webpack module factory. // normally, this is populated via webpackGlobal.push, which we patch below. // However, Discord has their .m prepopulated. @@ -133,13 +67,46 @@ Object.defineProperty(Function.prototype, "m", { // This ensures we actually got the right one const { stack } = new Error(); if ((stack?.includes("discord.com") || stack?.includes("discordapp.com")) && !Array.isArray(v)) { - logger.info("Found Webpack module factory", stack.match(/\/assets\/(.+?\.js)/)?.[1] ?? ""); + const fileName = stack.match(/\/assets\/(.+?\.js)/)?.[1] ?? ""; + + logger.info("Found Webpack module factory", fileName); patchFactories(v); + + // Define a setter for the bundlePath property of WebpackRequire. Only the main Webpack has this property. + // So if the setter is called, this means we can initialize the internal references to WebpackRequire. + Object.defineProperty(this, "p", { + configurable: true, + + set(this: WebpackInstance, bundlePath: string) { + Object.defineProperty(this, "p", { + value: bundlePath, + configurable: true, + enumerable: true, + writable: true + }); + + clearTimeout(setterTimeout); + + if (bundlePath !== "/assets/") return; + + logger.info(`Main Webpack found in ${fileName}, initializing internal references to WebpackRequire`); + _initWebpack(this); + + for (const beforeInitListener of beforeInitListeners) { + beforeInitListener(this); + } + } + }); + // setImmediate to clear this property setter if this is not the main Webpack. + // If this is the main Webpack, wreq.p will always be set before the timeout runs. + const setterTimeout = setTimeout(() => Reflect.deleteProperty(this, "p"), 0); } Object.defineProperty(this, "m", { value: v, - configurable: true + configurable: true, + enumerable: true, + writable: true }); } }); From 96873ccef70564823a76a89e310ed02de7ff1131 Mon Sep 17 00:00:00 2001 From: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Date: Thu, 27 Jun 2024 23:58:01 -0300 Subject: [PATCH 2/3] Temp fix for disabling Sentry --- src/plugins/_core/noTrack.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/plugins/_core/noTrack.ts b/src/plugins/_core/noTrack.ts index 67f6c6448e..9283bef527 100644 --- a/src/plugins/_core/noTrack.ts +++ b/src/plugins/_core/noTrack.ts @@ -20,6 +20,12 @@ import { definePluginSettings } from "@api/Settings"; import { Devs } from "@utils/constants"; import definePlugin, { OptionType } from "@utils/types"; +// FIXME Do this without monkey patching maybe +Object.defineProperty(window, "DiscordSentry", { + configurable: true, + set(v) { } +}); + const settings = definePluginSettings({ disableAnalytics: { type: OptionType.BOOLEAN, From df6ffd90e3160dc9583fe41039c5ec01166b982d Mon Sep 17 00:00:00 2001 From: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Date: Fri, 28 Jun 2024 00:53:09 -0300 Subject: [PATCH 3/3] Fix broken patches --- src/plugins/_core/noTrack.ts | 27 ++++++++++++--------------- src/plugins/showHiddenThings/index.ts | 4 ++-- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/plugins/_core/noTrack.ts b/src/plugins/_core/noTrack.ts index 9283bef527..26b36735a4 100644 --- a/src/plugins/_core/noTrack.ts +++ b/src/plugins/_core/noTrack.ts @@ -18,13 +18,7 @@ import { definePluginSettings } from "@api/Settings"; import { Devs } from "@utils/constants"; -import definePlugin, { OptionType } from "@utils/types"; - -// FIXME Do this without monkey patching maybe -Object.defineProperty(window, "DiscordSentry", { - configurable: true, - set(v) { } -}); +import definePlugin, { OptionType, StartAt } from "@utils/types"; const settings = definePluginSettings({ disableAnalytics: { @@ -52,13 +46,6 @@ export default definePlugin({ replace: "()=>{}", }, }, - { - find: "window.DiscordSentry=", - replacement: { - match: /^.+$/, - replace: "()=>{}", - } - }, { find: ".METRICS,", replacement: [ @@ -80,5 +67,15 @@ export default definePlugin({ replace: "getDebugLogging(){return false;" } }, - ] + ], + + startAt: StartAt.Init, + start() { + Object.defineProperty(window, "DiscordSentry", { + configurable: true, + set() { + Reflect.deleteProperty(window, "DiscordSentry"); + } + }); + } }); diff --git a/src/plugins/showHiddenThings/index.ts b/src/plugins/showHiddenThings/index.ts index b92c16c86c..599bcd36dd 100644 --- a/src/plugins/showHiddenThings/index.ts +++ b/src/plugins/showHiddenThings/index.ts @@ -110,8 +110,8 @@ export default definePlugin({ find: '"pepe","nude"', predicate: () => settings.store.disableDisallowedDiscoveryFilters, replacement: { - match: /\?\["pepe",.+?\]/, - replace: "?[]", + match: /(?<=[?=])\["pepe",.+?\]/, + replace: "[]", }, }, // patch request that queries if term is allowed