feat: 升级至 v0.3.16 — 工单修复 51 项 + 审查修复 30 项(安全加固/适配器/工具系统/数据层/前端)
This commit is contained in:
@@ -186,11 +186,44 @@ export class BrowserWindowManager {
|
||||
|
||||
// ===== browserEvaluate =====
|
||||
|
||||
/**
|
||||
* 在浏览器页面上下文中执行 JS
|
||||
*
|
||||
* #46 修复: 增强安全防护
|
||||
* - 审计日志:记录所有 executeJavaScript 调用,便于事后追溯恶意操作
|
||||
* - 超时控制:防止恶意 JS 无限阻塞主进程(Electron 原生不支持超时,用 Promise.race 模拟)
|
||||
*
|
||||
* 注意:完全沙箱隔离较难实现(需要 iframe/Web Worker + API 白名单),
|
||||
* 当前先实现审计 + 超时作为缓解措施。webPreferences 已配置 contextIsolation: true
|
||||
* 和 sandbox: true,Electron API 不会被暴露给页面。
|
||||
*/
|
||||
async evaluate(js: string): Promise<unknown> {
|
||||
this.ensureReady();
|
||||
const result = await this.win!.webContents.executeJavaScript(js, true);
|
||||
if (typeof result === 'string') return result;
|
||||
return result;
|
||||
|
||||
// #46 修复: 审计日志 — 记录所有 executeJavaScript 调用(截断前 500 字符),便于追溯
|
||||
log.info('[BrowserWindowManager] evaluate JS (first 500 chars):', js.substring(0, 500));
|
||||
|
||||
// #46 修复: 超时控制 — 防止恶意 JS 无限阻塞主进程
|
||||
const EVAL_TIMEOUT_MS = 10_000;
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
try {
|
||||
const result = await Promise.race([
|
||||
this.win!.webContents.executeJavaScript(js, true),
|
||||
new Promise<never>((_, reject) => {
|
||||
timer = setTimeout(() => {
|
||||
// 审查修复 M17: 超时后中止页面 JS 执行。
|
||||
// executeJavaScript 返回的 Promise 无法取消,页面脚本仍会继续运行,
|
||||
// 调用 webContents.stop() 中止页面正在执行的脚本(win 可能已销毁,try/catch 兜底)。
|
||||
try { this.win?.webContents.stop(); } catch {}
|
||||
reject(new Error(`evaluate timed out after ${EVAL_TIMEOUT_MS}ms`));
|
||||
}, EVAL_TIMEOUT_MS);
|
||||
}),
|
||||
]);
|
||||
if (typeof result === 'string') return result;
|
||||
return result;
|
||||
} finally {
|
||||
if (timer) clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
|
||||
// ===== browserExtract =====
|
||||
|
||||
Reference in New Issue
Block a user