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 c08ab89866
commit 905383165b
4 files changed
+78 -34

No files matched your search

+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":