From 88c4dab0c9be8fe5c7cdd347297cfff8c24a490f Mon Sep 17 00:00:00 2001 From: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Date: Fri, 16 May 2025 08:54:16 -0300 Subject: [PATCH] WebpackPatcher: Avoid patching libDiscore without relying on name --- src/webpack/patchWebpack.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/webpack/patchWebpack.ts b/src/webpack/patchWebpack.ts index baba751f..300abbab 100644 --- a/src/webpack/patchWebpack.ts +++ b/src/webpack/patchWebpack.ts @@ -105,7 +105,11 @@ define(Function.prototype, "m", { } const fileName = stack.match(/\/assets\/(.+?\.js)/)?.[1]; - if (fileName?.includes("libdiscore")) { + + // Currently, sentry and libDiscore Webpack instances are not meant to be patched. + // As an extra measure, take advatange of the fact their files include the names and return early if it's one of them. + // Later down we also include other measures to avoid patching them. + if (["sentry", "libdiscore"].some(name => fileName?.toLowerCase()?.includes(name))) { return; } @@ -119,7 +123,10 @@ define(Function.prototype, "m", { define(this, "p", { value: bundlePath }); clearTimeout(bundlePathTimeout); - if (bundlePath !== "/assets/") { + // libDiscore init Webpack instance always returns a constanst string for the js filename of a chunk. + // In that case, avoid patching this instance, + // as it runs before the main Webpack instance and will make the WebpackRequire fallback not work properly, or init an wrongful main WebpackRequire. + if (bundlePath !== "/assets/" || /(?:=>|{return)"[^"]/.exec(String(this.u))) { return; } @@ -132,9 +139,9 @@ define(Function.prototype, "m", { } }); - // In the past, the sentry Webpack instance which we also wanted to patch used to rely on chunks being loaded before initting sentry. + // In the past, the sentry Webpack instance which we also wanted to patch used to rely on chunks being loaded before initing sentry. // This Webpack instance did not include actual chunk loading, and only awaited for them to be loaded, which means it did not include the bundlePath property. - // To keep backwards compability, in case this is ever the case again, and keep patching this type of instance, we explicity patch instances which include wreq.O and not wreq.p. + // To keep backwards compability, if this is ever the case again, and keep patching this type of instance, we explicity patch instances which include wreq.O and not wreq.p. // Since we cannot check what is the bundlePath of the instance to filter for the Discord bundlePath, we only patch it if wreq.p is not included, // which means the instance relies on another instance which does chunk loading, and that makes it very likely to only target Discord Webpack instances like the old sentry.