diff --git a/lib/core.js b/lib/core.js index 9fcdeef..7a45ae9 100644 --- a/lib/core.js +++ b/lib/core.js @@ -313,12 +313,13 @@ export function silenceCmdFlash(aiBase = findAiBase()) { const customBash = silenceLiangshenCustomBashFlash(); const phase1 = silenceLiangshenPhase1Strip(); const mnemon = silenceMnemonGitFlash(); - // bin.js 写失败(missing_bin / ACL)不阻断 Apply。 + // bin.js 写失败(missing_bin / ACL)不阻断 Apply。源码 cli 跳过注入也算成功(#41)。 const ok = process.platform !== "win32" || hide.entry === "injected" || hide.entry === "already_injected" || hide.entry === "skipped_not_win32" || + hide.entry === "skipped_source_cli" || hide.entry === "missing_bin" || hide.entry === "sealed_asar" || (typeof hide.entry === "string" && hide.entry.startsWith("error:")); @@ -4020,6 +4021,7 @@ export async function backupAll(aiBase) { export async function revertAll(aiBase) { const files = targetFiles(aiBase); const reverted = []; + const skipped = []; const errors = []; for (const fp of flattenTargetPaths(files)) { const bak = fp + ".dshpurge.bak"; @@ -4031,9 +4033,12 @@ export async function revertAll(aiBase) { } catch (e) { errors.push([fp, String(e)]); } + continue; } + // 没有 bak 时绝不删除目标。源码部署的构建产物缺备份时,删了无法自愈(#41)。 + if (fs.existsSync(fp)) skipped.push(fp); } - return { reverted, errors }; + return { reverted, skipped, errors }; } export async function hasBackup(aiBase) { diff --git a/lib/hide-console.js b/lib/hide-console.js index 1920ed8..21d4998 100644 --- a/lib/hide-console.js +++ b/lib/hide-console.js @@ -176,6 +176,26 @@ function ioFor(explicit, targetPath) { return explicit ?? fsFor(targetPath); } +/** 源码 monorepo 的 apps/cli(tsdown 构建产物)。往这里注入/还原会动构建输出,缺 bak 时无法自愈(#41)。 */ +export function looksLikeSourceCliRoot(root, { fs, path: nodePath } = {}) { + const p = nodePath ?? requirePath(); + if (!root) return false; + const n = String(root).replace(/\\/g, "/").toLowerCase(); + if (/\/apps\/cli\/?$/.test(n)) return true; + if (/\/node_modules\//.test(n)) return false; + try { + const nodeFs = ioFor(fs, root); + const libDir = p.join(root, "lib"); + if (nodeFs.existsSync(p.join(libDir, "tsconfig.tsbuildinfo"))) return true; + if (nodeFs.existsSync(p.join(root, "tsdown.config.ts")) || nodeFs.existsSync(p.join(root, "tsdown.config.mjs"))) { + return true; + } + } catch { + /* ignore */ + } + return false; +} + export function resolveDshBinRoot(hint, { fs, path: nodePath } = {}) { const p = nodePath ?? requirePath(); const tries = []; @@ -196,6 +216,16 @@ export function resolveDshBinRoot(hint, { fs, path: nodePath } = {}) { if (typeof process.argv[1] === "string" && /[\\/]bin\.js$/i.test(process.argv[1])) { push(p.dirname(p.dirname(process.argv[1]))); } + for (const root of tries) { + try { + const binJs = p.join(root, "lib", "bin.js"); + if (isAsarSealedPath(root) || isAsarSealedPath(binJs)) continue; + // 源码树只作探测候选的最后手段;优先 node_modules 包,避免改写 apps/cli/lib(#41)。 + if (looksLikeSourceCliRoot(root, { fs, path: p })) continue; + if (ioFor(fs, binJs).existsSync(binJs)) return p.normalize(root); + } catch {} + } + // 没有包装配路径时,才回退到 argv 指向的源码 cli(仍禁止注入,见 installHideConsoleIntoBin)。 for (const root of tries) { try { const binJs = p.join(root, "lib", "bin.js"); @@ -213,6 +243,8 @@ export function installHideConsoleIntoBin(aiBase, { fs, path: nodePath } = {}) { const p = nodePath ?? requirePath(); const root = resolveDshBinRoot(aiBase, { fs, path: p }); if (!root) return "missing_bin"; + // 源码部署的 apps/cli/lib 是 tsdown 产物:只准读、不准写 sidecar / 改 bin.js(#41)。 + if (looksLikeSourceCliRoot(root, { fs, path: p })) return "skipped_source_cli"; const nodeFs = ioFor(fs, root); const binJs = p.join(root, "lib", "bin.js"); const side = p.join(root, "lib", `${IMPORT_MARK}.js`); @@ -248,12 +280,14 @@ export function revertHideConsoleFromBin(aiBase, { fs, path: nodePath } = {}) { const p = nodePath ?? requirePath(); const root = resolveDshBinRoot(aiBase, { fs, path: p }); if (!root) return "missing_bin"; + // 源码 cli:只清理本插件留下的 sidecar,绝不改/删 bin.js 或其它构建产物(#41)。 + const sourceCli = looksLikeSourceCliRoot(root, { fs, path: p }); const nodeFs = ioFor(fs, root); const binJs = p.join(root, "lib", "bin.js"); const side = p.join(root, "lib", `${IMPORT_MARK}.js`); const facadeSide = p.join(root, "lib", "dsh-purge-child-process-hide.mjs"); let changed = false; - if (nodeFs.existsSync(binJs)) { + if (!sourceCli && nodeFs.existsSync(binJs)) { try { const text = nodeFs.readFileSync(binJs, "utf8"); const next = text.replace(/^import "\.\/dsh-purge-hide-console\.js";.*\r?\n/gm, ""); diff --git a/lib/surface.js b/lib/surface.js index e9170b5..becd574 100644 --- a/lib/surface.js +++ b/lib/surface.js @@ -18,15 +18,18 @@ function exeName(execPath) { export function pathLooksDesktop(p) { const n = slash(p); if (!n) return false; + // 安装目录可能叫 "DeepSeek Harness" / "deepseek-harness",但源码仓库(monorepo)也叫这个名字 —— + // 所以只凭目录名不算,还必须带桌面安装形态(resources/、exe、.app/)。与 desktop.looksLikeDesktopName 对齐。 + const harnessDir = n.includes("/deepseek harness/") || n.includes("/deepseek-harness/"); return ( n.includes("/dsh desktop/") || n.includes("/dsh desktop.app/") || n.includes("dsh-desktop") || n.includes("dsh-plugin-desktop") || - n.includes("/deepseek harness/") || - n.includes("deepseek-harness") || + n.includes("/deepseek harness.app/") || + (harnessDir && (n.includes("/resources/") || n.includes("/contents/resources/") || n.endsWith("/resources"))) || /\/dsh desktop\.exe$/i.test(n) || - /\/deepseek harness\.exe$/i.test(n) || + /\/deepseek[- ]harness\.exe$/i.test(n) || n.endsWith("/host-process-entry.js") || n.endsWith("/desktop-cli.js") ); diff --git a/package.json b/package.json index 08755b6..72b01ef 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dsh-purge", - "version": "1.1.23", + "version": "1.1.24", "description": "DeepSeek Harness 网络安全红队工具:本机官方 Harness 受控评测与清洗,不同模型可换不同提示词;默认提示词面向国模「小码酱」。求 Star ⭐", "type": "module", "main": "lib/index.js", @@ -32,7 +32,8 @@ "test:yaml": "node scripts/test-yaml-comment.mjs", "test:update": "node scripts/test-update-guards.mjs", "test:rewind": "node scripts/test-rewind-plan.mjs", - "test:idempotence": "node scripts/test-patch-idempotence.mjs" + "test:idempotence": "node scripts/test-patch-idempotence.mjs", + "test:surface": "node scripts/test-surface-desktop.mjs" }, "license": "MIT", "keywords": [ diff --git a/release-notes.md b/release-notes.md index 1de1eb6..00b46bd 100644 --- a/release-notes.md +++ b/release-notes.md @@ -1,3 +1,16 @@ +# 1.1.24 + +## 中文 + +- **修复 [#40](https://github.com/YuJunZhiXue/dsh-purge/issues/40)**:`pathLooksDesktop` 不再仅凭路径里的 `deepseek-harness` 子串把源码版判成桌面端;需带 `resources/` / `.exe` / `.app` 等安装形态。 +- **修复 [#41](https://github.com/YuJunZhiXue/dsh-purge/issues/41)**:源码部署的 `apps/cli`(tsdown 构建产物)禁止 hide-console 注入;`revertAll` 在无 `.dshpurge.bak` 时只跳过、绝不删除目标文件。 +- 新增 `npm run test:surface` 回归。 + +## English + +- **Fix [#40](https://github.com/YuJunZhiXue/dsh-purge/issues/40)**: `pathLooksDesktop` no longer treats source trees as desktop just because the path contains `deepseek-harness`; require install shapes (`resources/`, `.exe`, `.app`). +- **Fix [#41](https://github.com/YuJunZhiXue/dsh-purge/issues/41)**: skip hide-console injection into source `apps/cli` build output; `revertAll` never deletes targets when `.dshpurge.bak` is missing. +- Add `npm run test:surface`. # 1.1.23 ## 中文 diff --git a/scripts/test-surface-desktop.mjs b/scripts/test-surface-desktop.mjs new file mode 100644 index 0000000..2586a31 --- /dev/null +++ b/scripts/test-surface-desktop.mjs @@ -0,0 +1,77 @@ +import assert from "node:assert/strict"; +import { pathLooksDesktop, detectSurface } from "../lib/surface.js"; +import { looksLikeSourceCliRoot, installHideConsoleIntoBin } from "../lib/hide-console.js"; + +// #40:源码仓库路径不能仅因目录名 deepseek-harness 被判成桌面端 +assert.equal( + pathLooksDesktop("/home/me/deepseek-harness/apps/cli/lib/bin.js"), + false, + "源码 cli 路径不应命中 pathLooksDesktop", +); +assert.equal( + pathLooksDesktop("/home/me/deepseek-harness/node_modules/@deepseek-ai/dsh/lib/bin.js"), + false, + "源码树里的 node_modules 也不应仅凭目录名判桌面", +); +assert.equal( + detectSurface({ + argv: ["/usr/bin/node", "/home/me/deepseek-harness/apps/cli/lib/bin.js"], + execPath: "/usr/bin/node", + env: {}, + }), + "web", + "源码版 argv 应判为 web", +); + +// 官方桌面安装形态仍应命中 +assert.equal( + pathLooksDesktop("C:/DeepSeek Harness/resources/app.asar/node_modules/@deepseek-ai/dsh"), + true, + "官方 Win resources 路径应判桌面", +); +assert.equal( + pathLooksDesktop("/Applications/DeepSeek Harness.app/Contents/Resources/app"), + true, + "官方 macOS .app Resources 应判桌面", +); +assert.equal( + pathLooksDesktop("C:/deepseek-harness/deepseek-harness.exe"), + true, + "官方 exe 路径应判桌面", +); +assert.equal( + pathLooksDesktop("C:/dsh-desktop/DSH Desktop.exe"), + true, + "第三方 DSH Desktop 应判桌面", +); + +// #41:源码 apps/cli 识别 + 禁止注入 +assert.equal(looksLikeSourceCliRoot("E:/DSH/apps/cli"), true); +assert.equal(looksLikeSourceCliRoot("/home/me/deepseek-harness/apps/cli"), true); +assert.equal( + looksLikeSourceCliRoot("/home/me/deepseek-harness/node_modules/@deepseek-ai/dsh"), + false, +); + +const prev = process.platform; +Object.defineProperty(process, "platform", { value: "win32" }); +try { + const status = installHideConsoleIntoBin("E:/DSH/apps/cli", { + fs: { + existsSync: () => true, + readFileSync: () => 'import "./x.js";\n', + writeFileSync: () => { + throw new Error("source cli must not be written"); + }, + unlinkSync: () => { + throw new Error("source cli must not be unlinked"); + }, + }, + path: await import("node:path").then((m) => m.default), + }); + assert.equal(status, "skipped_source_cli", "源码 cli 应跳过 hide-console 注入"); +} finally { + Object.defineProperty(process, "platform", { value: prev }); +} + +console.log("ok: surface desktop + source cli guards (#40/#41)");