mirror of
https://github.com/hicccc77/WeFlow.git
synced 2026-03-24 23:06:51 +00:00
fix:尝试修复闪退的问题
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { app, BrowserWindow, ipcMain, nativeTheme } from 'electron'
|
||||
import { Worker } from 'worker_threads'
|
||||
import { join } from 'path'
|
||||
import { join, dirname } from 'path'
|
||||
import { autoUpdater } from 'electron-updater'
|
||||
import { readFile, writeFile, mkdir } from 'fs/promises'
|
||||
import { existsSync } from 'fs'
|
||||
@@ -28,6 +28,47 @@ const AUTO_UPDATE_ENABLED =
|
||||
process.env.AUTO_UPDATE_ENABLED === '1' ||
|
||||
(process.env.AUTO_UPDATE_ENABLED == null && !process.env.VITE_DEV_SERVER_URL)
|
||||
|
||||
// 使用白名单过滤 PATH,避免被第三方目录中的旧版 VC++ 运行库劫持。
|
||||
// 仅保留系统目录(Windows/System32/SysWOW64)和应用自身目录(可执行目录、resources)。
|
||||
function sanitizePathEnv() {
|
||||
// 开发模式不做裁剪,避免影响本地工具链
|
||||
if (process.env.VITE_DEV_SERVER_URL) return
|
||||
|
||||
const rawPath = process.env.PATH || process.env.Path
|
||||
if (!rawPath) return
|
||||
|
||||
const sep = process.platform === 'win32' ? ';' : ':'
|
||||
const parts = rawPath.split(sep).filter(Boolean)
|
||||
|
||||
const systemRoot = process.env.SystemRoot || process.env.WINDIR || ''
|
||||
const safePrefixes = [
|
||||
systemRoot,
|
||||
systemRoot ? join(systemRoot, 'System32') : '',
|
||||
systemRoot ? join(systemRoot, 'SysWOW64') : '',
|
||||
dirname(process.execPath),
|
||||
process.resourcesPath,
|
||||
join(process.resourcesPath || '', 'resources')
|
||||
].filter(Boolean)
|
||||
|
||||
const normalize = (p: string) => p.replace(/\\/g, '/').toLowerCase()
|
||||
const isSafe = (p: string) => {
|
||||
const np = normalize(p)
|
||||
return safePrefixes.some((prefix) => np.startsWith(normalize(prefix)))
|
||||
}
|
||||
|
||||
const filtered = parts.filter(isSafe)
|
||||
if (filtered.length !== parts.length) {
|
||||
const removed = parts.filter((p) => !isSafe(p))
|
||||
console.warn('[WeFlow] 使用白名单裁剪 PATH,移除目录:', removed)
|
||||
const nextPath = filtered.join(sep)
|
||||
process.env.PATH = nextPath
|
||||
process.env.Path = nextPath
|
||||
}
|
||||
}
|
||||
|
||||
// 启动时立即清理 PATH,后续创建的 worker 也能继承安全的环境
|
||||
sanitizePathEnv()
|
||||
|
||||
// 单例服务
|
||||
let configService: ConfigService | null = null
|
||||
|
||||
|
||||
Reference in New Issue
Block a user