fix: 插件沙箱不再覆盖 window - 用真实 globalThis 替代 fakeGlobal,修复 window.location.href undefined

This commit is contained in:
auto-bot
2026-06-14 00:42:00 +00:00
parent 9062a4fe5b
commit fa7f6fec15

View File

@@ -516,6 +516,11 @@ export class PluginManager {
exports: moduleObj.exports, exports: moduleObj.exports,
__dirname: '/', __dirname: '/',
__filename: '/plugin.js', __filename: '/plugin.js',
// Node 全局alias 到浏览器真实全局,以保持 window.location.href 正常工作)
global: globalThis,
globalThis: globalThis,
self: globalThis,
root: globalThis,
console, console,
setTimeout: setTimeout as any, setTimeout: setTimeout as any,
clearTimeout, clearTimeout,
@@ -552,11 +557,9 @@ export class PluginManager {
crypto: (globalThis as any).crypto, crypto: (globalThis as any).crypto,
performance: (globalThis as any).performance, performance: (globalThis as any).performance,
}; };
fakeGlobal.self = fakeGlobal; // 注意:不再覆盖 window / location / document / navigator
fakeGlobal.window = fakeGlobal; // 让 with(fakeGlobal) 在查找这些未覆盖变量时自动回落到浏览器真实全局作用域
fakeGlobal.global = fakeGlobal; // 确保 window.location.href / document.cookie / navigator.userAgent 等正常工作
fakeGlobal.globalThis = fakeGlobal;
fakeGlobal.root = fakeGlobal;
// 用 with(fakeGlobal) 包裹代码,让所有嵌套层都能访问 // 用 with(fakeGlobal) 包裹代码,让所有嵌套层都能访问
// require / module / exports / process / Buffer / zlib / http 等 // require / module / exports / process / Buffer / zlib / http 等