fix: 全仓审计加固与死代码清理,版本 1.1.87

- 回滚/备份:空 bak 改名 .bad 跳过;升级刷新 bak 补上单数 marker 补丁;统一原子写
- 补丁命中:optional 命中不符记软失败;#42 插入型替换加探针防重复插入
- Windows:进程枚举改用 | 分隔 + UTF-8,修复中文路径换包不杀旧进程;换包失败拉起原版
- 安全:asar 解包防越界;红队路径校验 realpath 防空 symlink;更新仅 https;报告回放去换行注入
- 提示词钩子:停用后 rewriteFn 返回原装配文本并清理 assemble guard;缓存签名纳入红队/策略/override
- 红队:listEngagements 默认不算统计;listVulns/报告支持 evidenceLimit
- 子代理名额占位即时生效并回退;--apply 有失败项退出码 1
- 清理:删除 lib/mnemon-settings.js 及 13 处死代码/未使用 import;移除 test 目录与 test 脚本
This commit is contained in:
叫我小杨同学 committed 2026-11-01 21:47:03 +08:00
1 parent 71d3a55884
commit f806b048ac
48 files changed
+890 -1652

No files matched your search

+71 -21
View File
@@ -15,6 +15,7 @@ import {
isMnemonPluginMessage,
isRedteamSession,
installAssembleGuard,
clearAssembleGuard,
modePromptText,
peelChatmlEnvelope,
escapeDshInterpolation,
@@ -83,6 +84,23 @@ const DEFAULTS = {
subagentControl: true, // false 立即回滚子代理审批接管
};
/* YAML 里写成 "false" 的布尔会被 JS 当真值:两个默认开启的开关一旦被写成字符串,
用户以为关了其实还开着。统一收敛一次。 */
export const BOOLEAN_CONFIG_KEYS = [
"enabled", "autoApplyOnStart", "autoUpdateOnStart", "autoRevertOnMissing",
"injectOnce", "stripMnemon", "verbose", "autoRetry", "autoContinue",
"capabilityProtocol", "hooksDenyBypass", "subagentControl",
];
export function normalizeConfig(raw) {
const cfg = { ...DEFAULTS, ...(raw || {}) };
for (const key of BOOLEAN_CONFIG_KEYS) {
const value = cfg[key];
if (typeof value === "string") cfg[key] = !/^(false|0|no|off)$/i.test(value.trim());
}
return cfg;
}
const log = (config, ...args) => {
if (config?.verbose) console.log("[dsh-purge]", ...args);
};
@@ -578,20 +596,39 @@ const liveRewriteCache = new WeakMap();
function rewriteLiveAssembly(config, assembled, context, decision = { mode: "full", taskKind: "general" }) {
// 同一份 assembly 可能同时经过 assemble guard 和 system-prompt/assemble 瀑布:
// 第二次进来内容没变,没必要再走一遍 22k 正文处理。WeakMap 随对象回收,不累积。
// 缓存键必须带上配置与会话签名:宿主复用同一个 assembly 对象时,光按对象做键
// 会把上一轮的改写结果套到这一轮(改了开关/换了会话却看不到变化)。
const sigHome = config.enabled !== false ? core.findDshHome() : "";
const sigPolicy = config.enabled !== false && sigHome ? rules.readWorkspacePolicy(sigHome) : "always";
const sigRedteam = config.enabled !== false && isRedteamSession(context);
const sigRevision = config.enabled !== false ? core.committedOverrideRevision() : "";
const cacheSig = [
config.enabled !== false,
config.capabilityProtocol !== false,
config.stripMnemon !== false,
config.anchorMode || "",
config.subagentControl !== false,
decision && decision.mode,
decision && decision.taskKind,
sigRedteam,
sigPolicy,
sigRevision,
(() => { try { const h = context?.agent?.session?.header || {}; return `${h.id || h.sessionId || ""}|${h.cwd || ""}`; } catch { return ""; } })(),
].join("");
if (assembled && typeof assembled === "object") {
const cached = liveRewriteCache.get(assembled);
if (cached) return cached;
if (cached && cached.sig === cacheSig) return cached.value;
}
const enabled = config.enabled !== false;
const redteam = enabled && isRedteamSession(context);
const redteam = sigRedteam;
const full = enabled ? readInjectFallback(context) : "";
const dshHome = enabled ? core.findDshHome() : "";
const dshHome = sigHome;
let cwd = "";
try { cwd = String(context?.agent?.session?.header?.cwd || "").trim(); } catch { /* ignore */ }
if (dshHome && cwd) {
try { rules.writeLastCwd(dshHome, cwd); } catch { /* ignore */ }
}
const policy = enabled && dshHome ? rules.readWorkspacePolicy(dshHome) : "always";
const policy = sigPolicy;
// 提示词框为空时才启用勾选的本地 cwd 规则兜底;避免跟 prompt-inject.md 叠加污染。
let localInject = "";
if (enabled && dshHome && !String(full || "").trim()) {
@@ -654,7 +691,7 @@ function rewriteLiveAssembly(config, assembled, context, decision = { mode: "ful
cwd,
});
if (assembled && typeof assembled === "object" && rewritten && typeof rewritten === "object") {
liveRewriteCache.set(assembled, rewritten);
liveRewriteCache.set(assembled, { sig: cacheSig, value: rewritten });
}
return rewritten;
}
@@ -670,21 +707,6 @@ function closerFollowBytes(sections) {
return joined.length - (at + closer.length);
}
/** 注册表若已经按旧文件算过 rev,立刻 rebuilt,让页面拿到的地址和缓存一致(#63)。 */
function publishClientBundleRevs(ctx, packageNames) {
if (!Array.isArray(packageNames) || packageNames.length === 0) return;
let mods = null;
try {
mods = ctx?.get?.("clientModules", false);
} catch {
mods = null;
}
if (!mods || typeof mods.rebuilt !== "function") return;
for (const id of packageNames) {
try { mods.rebuilt(id); } catch { /* 这一包还没进注册表,下次快照会读到新文件 */ }
}
}
/** Cordis 的 ctx 是 Proxy:未声明 inject 时点属性会抛 "cannot get property ... without inject",所以两段都要包 try。 */
function permissionPresetsOf(ctx) {
try {
@@ -852,6 +874,9 @@ function installIdentityOverride(ctx, config = {}) {
return d;
};
const rewriteFn = (assembled, context) => {
// 停用后旧 guard(包括隔离 loopCtx 上那份)必须立刻失效。不枚举 prompt:
// 每个 rewriteFn 都捕获同一个 ctx,读一个运行时标志即可。
if (ctx.__dshPurgeDisabled === true) return assembled;
// [方案2] installAssembleGuard 走的也是这条路径,同样决策 full/anchor。
const decision = shouldInjectAssembly(context);
logAssembleDecision("rewriteFn", decision);
@@ -1002,6 +1027,9 @@ function installIdentityOverride(ctx, config = {}) {
delete ctx.__dshPurgeSessionHook;
delete ctx.__dshPurgePresetHook;
delete ctx.__dshPurgePreStepHook;
/* 这两面 flag 以前没人清:热重载后 MnemonStrip / RegionGate 以为还装着,直接跳过。 */
delete ctx.__dshPurgeMnemonStrip;
delete ctx.__dshPurgeRegionGate;
// [v10] AssembleHook 也要清。原漏删,ctx 复用时新 apply 看到 flag=true 直接跳过
// ctx.on("system-prompt/assemble", ...),cordis event listener 永远不装 → 新对话首轮
// 完全依赖 400ms setInterval wrapAssemble 的 guard,时间窗没赶上就 inject 真空。
@@ -1596,14 +1624,36 @@ async function handleSkillsCommand(rawInput) {
}
}
/**
* 停用插件时把运行期钩子收干净。
* 宿主侧的补丁还在(要等用户 Apply/卸载才还原),它们靠 typeof 判断调用全局钩子;
* 这里把全局钩子换成空实现 —— 既不会让宿主崩,也不会在停用后继续注入提示词。
*/
function resetRuntimeHooks(ctx) {
for (const key of [
"__dshPurgeSessionHook", "__dshPurgePresetHook", "__dshPurgePreStepHook",
"__dshPurgeAssembleHook", "__dshPurgeMnemonStrip", "__dshPurgeRegionGate",
]) {
try { delete ctx[key]; } catch { /* ignore */ }
}
try { globalThis.__dshPurgeHungPrompt = () => ""; } catch { /* ignore */ }
try { globalThis.__dshPurgePrepareAssemble = () => null; } catch { /* ignore */ }
// guard 的 dispose 是 noop,停用后 slot.rewrite 仍指向旧闭包(enabled:true)→ 继续注入。
try { clearAssembleGuard(ctx.systemPrompt); } catch { /* 无 systemPrompt 时无需清理 */ }
// 隔离 loopCtx 上的 guard 与主 ctx 共用这个标志,一次置位全部失效。
try { ctx.__dshPurgeDisabled = true; } catch { /* ignore */ }
}
export function apply(ctx, config) {
const cfg = { ...DEFAULTS, ...(config || {}) };
const cfg = normalizeConfig(config);
if (!cfg.enabled) {
purgeLog("boot", { enabled: false });
try { clearOwnedSelectedDefault({ ctx, dshHome: core.findDshHome() }); } catch { /* 停用不被清默认挡住 */ }
try { removeStaleAgentPresetDir(core.findDshHome(), "redteam"); } catch { /* ignore */ }
resetRuntimeHooks(ctx);
return;
}
try { delete ctx.__dshPurgeDisabled; } catch { /* ignore */ }
cancelClearOwnedSelectedDefault();
try {
ctx.effect(() => () => {