Future proof ContextMenuAPI against mangled export

This commit is contained in:
Nuckyz 2024-06-19 23:49:56 -03:00
parent a01ee40591
commit 426c949ee4
No known key found for this signature in database
GPG key ID: 440BF8296E1C4AD9
4 changed files with 136 additions and 73 deletions

View file

@ -121,7 +121,7 @@ export function findGroupChildrenByChildId(id: string | string[], children: Arra
} }
interface ContextMenuProps { interface ContextMenuProps {
contextMenuApiArguments?: Array<any>; contextMenuAPIArguments?: Array<any>;
navId: string; navId: string;
children: Array<ReactElement | null>; children: Array<ReactElement | null>;
"aria-label": string; "aria-label": string;
@ -135,7 +135,7 @@ export function _usePatchContextMenu(props: ContextMenuProps) {
children: cloneMenuChildren(props.children), children: cloneMenuChildren(props.children),
}; };
props.contextMenuApiArguments ??= []; props.contextMenuAPIArguments ??= [];
const contextMenuPatches = navPatches.get(props.navId); const contextMenuPatches = navPatches.get(props.navId);
if (!Array.isArray(props.children)) props.children = [props.children]; if (!Array.isArray(props.children)) props.children = [props.children];
@ -143,7 +143,7 @@ export function _usePatchContextMenu(props: ContextMenuProps) {
if (contextMenuPatches) { if (contextMenuPatches) {
for (const patch of contextMenuPatches) { for (const patch of contextMenuPatches) {
try { try {
patch(props.children, ...props.contextMenuApiArguments); patch(props.children, ...props.contextMenuAPIArguments);
} catch (err) { } catch (err) {
ContextMenuLogger.error(`Patch for ${props.navId} errored,`, err); ContextMenuLogger.error(`Patch for ${props.navId} errored,`, err);
} }
@ -152,7 +152,7 @@ export function _usePatchContextMenu(props: ContextMenuProps) {
for (const patch of globalPatches) { for (const patch of globalPatches) {
try { try {
patch(props.navId, props.children, ...props.contextMenuApiArguments); patch(props.navId, props.children, ...props.contextMenuAPIArguments);
} catch (err) { } catch (err) {
ContextMenuLogger.error("Global patch errored,", err); ContextMenuLogger.error("Global patch errored,", err);
} }

View file

@ -18,6 +18,38 @@
import { Devs } from "@utils/constants"; import { Devs } from "@utils/constants";
import definePlugin from "@utils/types"; import definePlugin from "@utils/types";
import { filters, waitFor, waitForSubscriptions } from "@webpack";
/**
* The last var name which the ContextMenu module was WebpackRequire'd and assigned to
*/
let lastVarName = "";
/**
* The key exporting the ContextMenu module "Menu"
*/
let exportKey: PropertyKey = "";
/**
* The id of the module exporting the ContextMenu module "Menu"
*/
let modId: PropertyKey = "";
let mangledCallback: (...args: any[]) => any;
waitFor(filters.byCode("Menu API only allows Items and groups of Items as children."), mangledCallback = (_, modInfo) => {
exportKey = modInfo.exportKey;
modId = modInfo.id;
waitForSubscriptions.delete(nonMangledCallback);
});
let nonMangledCallback: (...args: any[]) => any;
waitFor(filters.byProps("Menu", "MenuItem"), nonMangledCallback = (_, modInfo) => {
exportKey = "Menu";
modId = modInfo.id;
waitForSubscriptions.delete(mangledCallback);
});
export default definePlugin({ export default definePlugin({
name: "ContextMenuAPI", name: "ContextMenuAPI",
@ -34,12 +66,26 @@ export default definePlugin({
} }
}, },
{ {
find: ".Menu,{", find: "navId:",
all: true, all: true,
replacement: { noWarn: true,
match: /Menu,{(?<=\.jsxs?\)\(\i\.Menu,{)/g, replacement: [
replace: "$&contextMenuApiArguments:typeof arguments!=='undefined'?arguments:[]," {
} get match() {
return RegExp(`${String(modId)}(?<=(\\i)=.+?)`);
},
replace: (id, varName) => {
lastVarName = varName;
return id;
}
},
{
get match() {
return RegExp(`${String(exportKey)},{(?<=${lastVarName}\\.${String(exportKey)},{)`, "g");
},
replace: "$&contextMenuAPIArguments:typeof arguments!=='undefined'?arguments:[],"
}
]
} }
] ]
}); });

View file

@ -24,7 +24,7 @@ import { WebpackInstance } from "discord-types/other";
import { traceFunction } from "../debug/Tracer"; import { traceFunction } from "../debug/Tracer";
import { patches } from "../plugins"; import { patches } from "../plugins";
import { _initWebpack, beforeInitListeners, factoryListeners, moduleListeners, subscriptions, wreq } from "."; import { _initWebpack, beforeInitListeners, factoryListeners, moduleListeners, waitForSubscriptions, wreq } from ".";
const logger = new Logger("WebpackInterceptor", "#8caaee"); const logger = new Logger("WebpackInterceptor", "#8caaee");
const initCallbackRegex = canonicalizeMatch(/{return \i\(".+?"\)}/); const initCallbackRegex = canonicalizeMatch(/{return \i\(".+?"\)}/);
@ -204,8 +204,7 @@ function patchFactories(factories: Record<string, (module: any, exports: any, re
} }
exports = module.exports; exports = module.exports;
if (exports == null) return;
if (!exports) return;
// There are (at the time of writing) 11 modules exporting the window // There are (at the time of writing) 11 modules exporting the window
// Make these non enumerable to improve webpack search performance // Make these non enumerable to improve webpack search performance
@ -240,32 +239,37 @@ function patchFactories(factories: Record<string, (module: any, exports: any, re
for (const callback of moduleListeners) { for (const callback of moduleListeners) {
try { try {
callback(exports, id); callback(exports, { id, factory: originalMod });
} catch (err) { } catch (err) {
logger.error("Error in Webpack module listener:\n", err, callback); logger.error("Error in Webpack module listener:\n", err, callback);
} }
} }
for (const [filter, callback] of subscriptions) { for (const [filter, callback] of waitForSubscriptions) {
try { try {
if (exports && filter(exports)) { if (filter(exports)) {
subscriptions.delete(filter); waitForSubscriptions.delete(filter);
callback(exports, id); callback(exports, { id, factory: originalMod, exportKey: "" });
} else if (typeof exports === "object") { continue;
if (exports.default && filter(exports.default)) { }
subscriptions.delete(filter);
callback(exports.default, id); if (typeof exports !== "object") continue;
} else {
for (const nested in exports) if (nested.length <= 3) { if (exports.default != null && filter(exports.default)) {
if (exports[nested] && filter(exports[nested])) { waitForSubscriptions.delete(filter);
subscriptions.delete(filter); callback(exports.default, { id, factory: originalMod, exportKey: "default" });
callback(exports[nested], id); continue;
} }
}
for (const key in exports) if (key.length <= 3) {
if (exports[key] != null && filter(exports[key])) {
waitForSubscriptions.delete(filter);
callback(exports[key], { id, factory: originalMod, exportKey: key });
break;
} }
} }
} catch (err) { } catch (err) {
logger.error("Error while firing callback for Webpack subscription:\n", err, filter, callback); logger.error("Error while firing callback for Webpack waitFor subscription:\n", err, filter, callback);
} }
} }
} as any as { toString: () => string, original: any, (...args: any[]): void; }; } as any as { toString: () => string, original: any, (...args: any[]): void; };

View file

@ -68,11 +68,21 @@ export const filters = {
} }
}; };
export type CallbackFn = (mod: any, id: string) => void; export type ModListenerInfo = {
id: PropertyKey;
factory: (module: any, exports: any, require: WebpackInstance) => void;
};
export type ModCallbackInfo = ModListenerInfo & {
exportKey: PropertyKey;
};
export type ModListenerFn = (module: any, info: ModListenerInfo) => void;
export type ModCallbackFn = (module: any, info: ModCallbackInfo) => void;
export const subscriptions = new Map<FilterFn, CallbackFn>();
export const moduleListeners = new Set<CallbackFn>();
export const factoryListeners = new Set<(factory: (module: any, exports: any, require: WebpackInstance) => void) => void>(); export const factoryListeners = new Set<(factory: (module: any, exports: any, require: WebpackInstance) => void) => void>();
export const moduleListeners = new Set<ModListenerFn>();
export const waitForSubscriptions = new Map<FilterFn, ModCallbackFn>();
export const beforeInitListeners = new Set<(wreq: WebpackInstance) => void>(); export const beforeInitListeners = new Set<(wreq: WebpackInstance) => void>();
export function _initWebpack(webpackRequire: WebpackInstance) { export function _initWebpack(webpackRequire: WebpackInstance) {
@ -106,7 +116,7 @@ export const find = traceFunction("find", function find(filter: FilterFn, { isIn
for (const key in cache) { for (const key in cache) {
const mod = cache[key]; const mod = cache[key];
if (!mod.loaded || !mod?.exports) continue; if (!mod?.loaded || mod?.exports == null) continue;
if (filter(mod.exports)) { if (filter(mod.exports)) {
return isWaitFor ? [mod.exports, key] : mod.exports; return isWaitFor ? [mod.exports, key] : mod.exports;
@ -114,16 +124,13 @@ export const find = traceFunction("find", function find(filter: FilterFn, { isIn
if (typeof mod.exports !== "object") continue; if (typeof mod.exports !== "object") continue;
if (mod.exports.default && filter(mod.exports.default)) { if (mod.exports.default != null && filter(mod.exports.default)) {
const found = mod.exports.default; return isWaitFor ? [mod.exports.default, key] : mod.exports.default;
return isWaitFor ? [found, key] : found;
} }
// the length check makes search about 20% faster for (const key in mod.exports) if (key.length <= 3) {
for (const nestedMod in mod.exports) if (nestedMod.length <= 3) { if (mod.exports[key] != null && filter(mod.exports[key])) {
const nested = mod.exports[nestedMod]; return isWaitFor ? [mod.exports[key], key] : mod.exports[key];
if (nested && filter(nested)) {
return isWaitFor ? [nested, key] : nested;
} }
} }
} }
@ -142,18 +149,25 @@ export function findAll(filter: FilterFn) {
const ret = [] as any[]; const ret = [] as any[];
for (const key in cache) { for (const key in cache) {
const mod = cache[key]; const mod = cache[key];
if (!mod.loaded || !mod?.exports) continue; if (!mod?.loaded || mod?.exports == null) continue;
if (filter(mod.exports)) if (filter(mod.exports)) {
ret.push(mod.exports); ret.push(mod.exports);
else if (typeof mod.exports !== "object")
continue; continue;
}
if (mod.exports.default && filter(mod.exports.default)) if (typeof mod.exports !== "object") continue;
if (mod.exports.default != null && filter(mod.exports.default)) {
ret.push(mod.exports.default); ret.push(mod.exports.default);
else for (const nestedMod in mod.exports) if (nestedMod.length <= 3) { continue;
const nested = mod.exports[nestedMod]; }
if (nested && filter(nested)) ret.push(nested);
for (const key in mod.exports) if (key.length <= 3) {
if (mod.exports[key] && filter(mod.exports[key])) {
ret.push(mod.exports[key]);
break;
}
} }
} }
@ -190,7 +204,7 @@ export const findBulk = traceFunction("findBulk", function findBulk(...filterFns
outer: outer:
for (const key in cache) { for (const key in cache) {
const mod = cache[key]; const mod = cache[key];
if (!mod.loaded || !mod?.exports) continue; if (!mod.loaded || mod?.exports == null) continue;
for (let j = 0; j < length; j++) { for (let j = 0; j < length; j++) {
const filter = filters[j]; const filter = filters[j];
@ -204,26 +218,23 @@ export const findBulk = traceFunction("findBulk", function findBulk(...filterFns
break; break;
} }
if (typeof mod.exports !== "object") if (typeof mod.exports !== "object") continue;
continue;
if (mod.exports.default && filter(mod.exports.default)) { if (mod.exports.default && filter(mod.exports.default)) {
results[j] = mod.exports.default; results[j] = mod.exports.default;
filters[j] = undefined; filters[j] = undefined;
if (++found === length) break outer; if (++found === length) break outer;
break; continue;
} }
for (const nestedMod in mod.exports) for (const key in mod.exports) if (key.length <= 3) {
if (nestedMod.length <= 3) { if (mod.exports[key] && filter(mod.exports[key])) {
const nested = mod.exports[nestedMod]; results[j] = mod.exports[key];
if (nested && filter(nested)) { filters[j] = undefined;
results[j] = nested; if (++found === length) break outer;
filters[j] = undefined; break;
if (++found === length) break outer;
continue outer;
}
} }
}
} }
} }
@ -293,7 +304,7 @@ export const lazyWebpackSearchHistory = [] as Array<["find" | "findByProps" | "f
* Note that the example below exists already as an api, see {@link findByPropsLazy} * Note that the example below exists already as an api, see {@link findByPropsLazy}
* @example const mod = proxyLazy(() => findByProps("blah")); console.log(mod.blah); * @example const mod = proxyLazy(() => findByProps("blah")); console.log(mod.blah);
*/ */
export function proxyLazyWebpack<T = any>(factory: () => any, attempts?: number) { export function proxyLazyWebpack<T = any>(factory: () => T, attempts?: number) {
if (IS_REPORTER) lazyWebpackSearchHistory.push(["proxyLazyWebpack", [factory]]); if (IS_REPORTER) lazyWebpackSearchHistory.push(["proxyLazyWebpack", [factory]]);
return proxyLazy<T>(factory, attempts); return proxyLazy<T>(factory, attempts);
@ -446,25 +457,27 @@ export function findExportedComponentLazy<T extends object = any>(...props: stri
* }) * })
*/ */
export const mapMangledModule = traceFunction("mapMangledModule", function mapMangledModule<S extends string>(code: string, mappers: Record<S, FilterFn>): Record<S, any> { export const mapMangledModule = traceFunction("mapMangledModule", function mapMangledModule<S extends string>(code: string, mappers: Record<S, FilterFn>): Record<S, any> {
const exports = {} as Record<S, any>; const mapping = {} as Record<S, any>;
const id = findModuleId(code); const id = findModuleId(code);
if (id === null) if (id === null) return mapping;
return exports;
const exports = wreq(id as any);
const mod = wreq(id as any);
outer: outer:
for (const key in mod) { for (const key in exports) {
const member = mod[key]; const value = exports[key];
for (const newName in mappers) { for (const newName in mappers) {
// if the current mapper matches this module // if the current mapper matches this module
if (mappers[newName](member)) { if (mappers[newName](value)) {
exports[newName] = member; mapping[newName] = value;
continue outer; continue outer;
} }
} }
} }
return exports;
return mapping;
}); });
/** /**
@ -570,7 +583,7 @@ export function extractAndLoadChunksLazy(code: string[], matcher = DefaultExtrac
* Wait for a module that matches the provided filter to be registered, * Wait for a module that matches the provided filter to be registered,
* then call the callback with the module as the first argument * then call the callback with the module as the first argument
*/ */
export function waitFor(filter: string | string[] | FilterFn, callback: CallbackFn, { isIndirect = false }: { isIndirect?: boolean; } = {}) { export function waitFor(filter: string | string[] | FilterFn, callback: ModCallbackFn, { isIndirect = false }: { isIndirect?: boolean; } = {}) {
if (IS_REPORTER && !isIndirect) lazyWebpackSearchHistory.push(["waitFor", Array.isArray(filter) ? filter : [filter]]); if (IS_REPORTER && !isIndirect) lazyWebpackSearchHistory.push(["waitFor", Array.isArray(filter) ? filter : [filter]]);
if (typeof filter === "string") if (typeof filter === "string")
@ -585,7 +598,7 @@ export function waitFor(filter: string | string[] | FilterFn, callback: Callback
if (existing) return void callback(existing, id); if (existing) return void callback(existing, id);
} }
subscriptions.set(filter, callback); waitForSubscriptions.set(filter, callback);
} }
/** /**