fix: Desktop 密封 host-commands/bin 只清理不注入,杜绝 dsh.cmd.dshpurge.bak 触发恢复模式

anywhere-labs/dsh-desktop 在插件加载前校验 host-commands/<profile>/bin,禁止 sibling 额外文件。现跳过密封目录的 shim 注入/bak 写入,启动与应用时强制外置或删除残留 *.dshpurge.bak,并在 README 写明恢复步骤(issue #9)。
This commit is contained in:
yang committed 2026-09-03 20:56:22 +08:00
1 parent ddf2b7ca0f
commit a91647c459
4 files changed
+78 -34

No files matched your search

+8 -7
View File
@@ -76,6 +76,7 @@ dsh plugin --profile web add .
- `/purge status` prints `DSH_HOME` and patches
- Settings shows the Rules card (Ctrl+F5 if cached)
4. **Expected skips:** `#20` / `#21` = `dsh-web-fetch-http`; `#28` / `#29` = `dsh-liangshen`. Missing those plugins → skip. **Do not treat as failure or thrash the install.**
5. **DSH Desktop recovery:** If you see `unexpected entries: dsh.cmd.dshpurge.bak`, delete `%APPDATA%\DSH Desktop\host-commands\desktop\bin\*.dshpurge.bak` (or uninstall this plugin), install the latest build, restart. Sealed bins are scrub-only.
**Uninstall (only if the user explicitly asks)**
@@ -263,19 +264,19 @@ node --check client.js
```
patch not applied? ──no──> skip
│yes
├─> backup <file>.dshpurge.bak (Desktop host-commands shim backups go under `$DSH_HOME/dsh-purge/shim-backups`)
├─> backup <file>.dshpurge.bak (Desktop host-commands backups stay under `$DSH_HOME/dsh-purge/shim-backups` only)
├─> apply matching replacements
├─> override shim (dsh.cmd / dsh.ps1 / unix dsh, **including Desktop host-commands launchers**)
├─> scrub illegal sibling `*.dshpurge.bak` inside sealed bins (keep inject)
├─> override shim (dsh.cmd / dsh.ps1 / unix dsh; **skip Desktop sealed host-commands bins**)
├─> scrub illegal sibling `*.dshpurge.bak` inside sealed bins (Desktop validates before plugins load)
├─> Windows: pin hide-console into real `lib/bin.js` (mnemon/git flash is best-effort)
└─> if `prompt-inject.md` is missing, write the default (do not refill an empty file)
```
**DSH Desktop (bugfix, feature kept):**
**DSH Desktop (anywhere-labs/dsh-desktop, issue #9):**
- Sealed bins allow rewriting whitelisted launchers; **extra sibling files are illegal**.
- Legacy in-bin `dsh.cmd.dshpurge.bak` is relocated/removed on startup to stop recovery mode / cmd flash loops.
- Shim cleaning still runs on Desktop; `applyRuntimeEnv()` only sets permission mode early, it does not replace the file shim.
- `host-commands/<profile>/bin` is sealed: whitelisted launchers only — **no** sibling extras (including `dsh.cmd.dshpurge.bak`).
- This plugin **scrubs only** sealed bins (no inject, no bak writes). In-process `applyRuntimeEnv()` covers env. Non-sealed launchers (npm global, etc.) still get shim inject.
- Startup/Apply relocates or deletes leftover `*.dshpurge.bak`. If already in recovery: delete `…\host-commands\desktop\bin\dsh.cmd.dshpurge.bak` (or uninstall this plugin), install the fixed build, restart.
**Prompt purge (refusal → allowed, keep harness optimizations):**
+7 -6
View File
@@ -73,6 +73,7 @@ dsh plugin --profile web add .
- `/purge status` 能打印 `DSH_HOME` 与补丁列表
- 设置页有「规则设定」卡片(有缓存就 Ctrl+F5)
4. **正常跳过**:`#20` / `#21` = 插件 `dsh-web-fetch-http`;`#28` / `#29` = 插件 `dsh-liangshen`(梁神)。没装对应插件时跳过,**不要当成失败重试乱改**。
5. **DSH Desktop 恢复模式**:若报 `unexpected entries: dsh.cmd.dshpurge.bak`,先删除 `%APPDATA%\DSH Desktop\host-commands\desktop\bin\*.dshpurge.bak`(或卸载本插件),再装本仓库最新版并重启。密封目录只清理不注入。
**卸载(仅当用户明确要求)**
@@ -265,17 +266,17 @@ node --check client.js
│是
├─> 备份原件 <文件>.dshpurge.bak(Desktop host-commands 的 shim 备份外置到 `$DSH_HOME/dsh-purge/shim-backups`)
├─> 按补丁列表替换对应文件
├─> 覆盖 shim(dsh.cmd / dsh.ps1 / unix dsh,**含 Desktop host-commands 启动器**)
├─> 清掉密封目录里误放的 sibling `*.dshpurge.bak`(不剥离已写入的 shim)
├─> 覆盖 shim(dsh.cmd / dsh.ps1 / unix dsh;**跳过 Desktop host-commands 密封目录**)
├─> 清掉密封目录里误放的 sibling `*.dshpurge.bak`(Desktop 启动前校验,残留会进恢复模式)
├─> Windows:把 hide-console 钉进真实 `lib/bin.js`(mnemon/git 闪窗为尽力修补,不挡完成)
└─> 若 `prompt-inject.md` 尚不存在则写入默认覆盖文本(已存在的空文件不覆盖)
```
**DSH Desktop 适配(修 bug,不砍功能):**
**DSH Desktop 适配(anywhere-labs/dsh-desktop,issue #9):**
- 密封目录允许改写白名单启动器;**禁止**在同目录留下额外文件。
- 旧版若把 `dsh.cmd.dshpurge.bak` 写进 `host-commands/.../bin`,启动时自动外置/删除该 bak,避免恢复模式与 CMD 连闪。
- shim 清洗逻辑对 Desktop 与 CLI 一视同仁;进程内 `applyRuntimeEnv()` 只是提前设权限,不是替代 shim。
- `host-commands/<profile>/bin` 是密封目录:只允许白名单启动器,**禁止**任何 sibling 额外文件(含 `dsh.cmd.dshpurge.bak`)。
- 本插件对密封目录 **只清理、不注入、不写 bak**;环境变量靠进程内 `applyRuntimeEnv()`。npm 全局等非密封启动器仍可 shim。
- 启动/应用时自动外置或删除误留的 `*.dshpurge.bak`。若已进恢复模式:先删掉 `…\host-commands\desktop\bin\dsh.cmd.dshpurge.bak`(或卸载本插件),再装新版本并重启。
**提示词清洗(拒绝 → 可执行,不删官方优化):**
+62 -20
View File
@@ -139,11 +139,15 @@ export function dirHasDshLauncher(dir) {
return SHIM_NAMES.some((name) => isFile(path.join(dir, name)));
}
/** Desktop `host-commands/<profile>/bin` is sealed: launchers OK, sibling `*.dshpurge.bak` is not. */
/** Desktop `host-commands/<profile>/bin` is sealed: launchers OK, sibling extras are not. */
export function isDesktopCommandRuntimeDir(dir) {
if (!dir) return false;
const n = path.normalize(dir).replace(/\\/g, "/").toLowerCase();
return n.includes("/host-commands/") && /(^|\/)bin$/.test(n);
// Canonical: .../host-commands/<profile>/bin
if (n.includes("/host-commands/") && /(^|\/)bin\/?$/.test(n)) return true;
// Fallback: anything under "%APPDATA%/DSH Desktop/.../bin"
if (n.includes("/dsh desktop/") && /(^|\/)bin\/?$/.test(n)) return true;
return false;
}
/** PATH entries + `%APPDATA%/DSH Desktop/host-commands/<profile>/bin`. */
@@ -167,13 +171,23 @@ function ensureDirSync(dir) {
} catch {}
}
/** Desktop sealed bins → `$DSH_HOME/dsh-purge/shim-backups`; otherwise sibling `*.dshpurge.bak`. */
/**
* Backup path for a launcher file.
* Desktop sealed bins MUST NOT get sibling `*.dshpurge.bak` — DSH Desktop refuses
* to start when host-commands/<profile>/bin contains unexpected entries (issue #9).
*/
export function shimBackupPath(fp) {
if (!isDesktopCommandRuntimeDir(path.dirname(fp))) return fp + ".dshpurge.bak";
const dir = path.dirname(fp);
if (!isDesktopCommandRuntimeDir(dir)) return fp + ".dshpurge.bak";
const bakRoot = path.join(findDshHome(), "dsh-purge", "shim-backups");
ensureDirSync(bakRoot);
const digest = createHash("sha1").update(path.normalize(fp)).digest("hex").slice(0, 20);
return path.join(bakRoot, `${path.basename(fp)}.${digest}.bak`);
const bak = path.join(bakRoot, `${path.basename(fp)}.${digest}.bak`);
// Defense: never return a path inside the sealed bin.
if (path.normalize(path.dirname(bak)).toLowerCase() === path.normalize(dir).toLowerCase()) {
return path.join(os.tmpdir(), `dsh-purge-${path.basename(fp)}.${digest}.bak`);
}
return bak;
}
/** Fill `DSH_HOME` if missing; never pin `DSH_PERMISSION_MODE` (UI must stay free). */
@@ -545,14 +559,18 @@ export function findShimDir() {
});
}
/** All launcher dirs that should receive shim inject (Desktop host-commands + npm-global, etc.). */
/** All launcher dirs that should receive shim inject.
* Desktop sealed host-commands/<profile>/bin is scrub-only (issue #9) — never inject / never write bak there.
*/
export function listShimDirsForPatch() {
return collectDirs([
findShimDir(),
...listDesktopCommandRuntimeDirs(),
...launcherDirsOf(findNpmPrefix()),
...pathEnvDirs(),
]).filter(dirHasDshLauncher);
])
.filter(dirHasDshLauncher)
.filter((dir) => !isDesktopCommandRuntimeDir(dir));
}
/**
@@ -2107,21 +2125,35 @@ function scrubInBinBak(dir, name, cleaned) {
const keepAsShimBackup = SHIM_NAMES.includes(original) && isFile(bak);
if (!keepAsShimBackup) {
fs.unlinkSync(bak);
cleaned.push(`deleted:${name}`);
try {
fs.unlinkSync(bak);
cleaned.push(`deleted:${name}`);
} catch (e) {
cleaned.push(`delete_failed:${name}:${e}`);
}
return;
}
// Prefer external archive, but ALWAYS remove the in-bin sibling — Desktop
// validates host-commands/<profile>/bin before plugins load (anywhere-labs/dsh-desktop #9).
const external = shimBackupPath(target);
if (!fs.existsSync(external)) {
ensureDirSync(path.dirname(external));
fs.copyFileSync(bak, external);
cleaned.push(`relocated_bak:${original}`);
} else {
cleaned.push(`external_bak_exists:${original}`);
try {
if (!fs.existsSync(external)) {
ensureDirSync(path.dirname(external));
fs.copyFileSync(bak, external);
cleaned.push(`relocated_bak:${original}`);
} else {
cleaned.push(`external_bak_exists:${original}`);
}
} catch (e) {
cleaned.push(`relocate_failed:${original}:${e}`);
}
try {
fs.unlinkSync(bak);
cleaned.push(`removed_in_bin_bak:${original}`);
} catch (e) {
cleaned.push(`remove_failed:${original}:${e}`);
}
fs.unlinkSync(bak);
cleaned.push(`removed_in_bin_bak:${original}`);
}
function stripCmdInjectSync(cmdPath, cleaned) {
@@ -2134,7 +2166,7 @@ function stripCmdInjectSync(cmdPath, cleaned) {
}
}
/** Relocate illegal in-bin `*.dshpurge.bak`; strip `dsh.cmd` inject (avoids cmd.exe flashes). */
/** Relocate/delete illegal in-bin `*.dshpurge.bak`; strip `dsh.cmd` inject. */
export function sanitizeDesktopCommandRuntimes() {
const report = [];
for (const dir of listDesktopCommandRuntimeDirs()) {
@@ -2146,9 +2178,14 @@ export function sanitizeDesktopCommandRuntimes() {
continue;
}
for (const name of names) {
if (!name.endsWith(".dshpurge.bak")) continue;
// Our artifacts only — never delete unrelated files in the sealed bin.
if (!/\.dshpurge\.bak$/i.test(name) && !/\.dshpurge\./i.test(name)) continue;
try {
scrubInBinBak(dir, name, cleaned);
if (name.toLowerCase().endsWith(".dshpurge.bak")) scrubInBinBak(dir, name, cleaned);
else {
fs.unlinkSync(path.join(dir, name));
cleaned.push(`deleted_artifact:${name}`);
}
} catch (e) {
cleaned.push(`error:${name}:${e}`);
}
@@ -2168,6 +2205,11 @@ async function stripWinCmdShim(fp, text) {
export async function patchShim(shimDir) {
const result = {};
if (!shimDir) return { error: "no_shim_dir" };
// Hard guard: never inject / never write bak into Desktop sealed bins (issue #9).
if (isDesktopCommandRuntimeDir(shimDir)) {
const scrubbed = sanitizeDesktopCommandRuntimes().filter((r) => path.normalize(r.dir) === path.normalize(shimDir));
return { skipped_sealed_desktop: true, scrubbed };
}
for (const fname of SHIM_NAMES) {
const fp = path.join(shimDir, fname);
if (!isFile(fp)) {
+1 -1
View File
@@ -247,7 +247,7 @@ async function handlePurgeCommand(rawInput, config) {
for (const row of scrubbed) lines.push(` ✓ ${row.dir} → ${row.cleaned.join(", ")}`);
}
lines.push("", "全部完成 / All done。重启 dsh 生效 / Restart dsh to take effect.");
lines.push("Desktop: 仍写入 host-commands 启动器;备份改放到 $DSH_HOME/dsh-purge/shim-backups。");
lines.push("Desktop: host-commands 密封目录只清理不注入;bak 禁止写进 bin(issue #9)。");
return { kind: "success", text: lines.join("\n") };
}
case "revert":