Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix: 修复文件管理器创建和重命名的名称校验

geng yueaed1107f42b5
diff --git a/apps/desktop/resources/plugins/pi.file-manager/main.js b/apps/desktop/resources/plugins/pi.file-manager/main.js
index 67eb17425..c8713ca22 100644
--- a/apps/desktop/resources/plugins/pi.file-manager/main.js
+++ b/apps/desktop/resources/plugins/pi.file-manager/main.js
@@ -2521,6 +2521,12 @@ const WINDOWS_RESERVED = /^(?:con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\..*)?$/i;
function assertValidName(rawName) {
if (typeof rawName !== "string") throw fail("INVALID_NAME", "name is required");
const name = rawName.trim();
+ if (!name || name === "." || name === ".." || /[<>:"/\\|?*\x00-\x1f]/.test(name) || name.endsWith(".") || WINDOWS_RESERVED.test(name)) {
+ throw fail("INVALID_NAME", "name must be a valid file or directory name");
+ }
+ return name;
+}
+
function entryFromStat(name, rel, stat) {
return {
name,
@@ -2535,17 +2541,6 @@ function entryFromStat(name, rel, stat) {
git: null,
};
}
- return {
- name,
- path: rel,
- isDirectory: stat.isDirectory(),
- size: stat.isDirectory() ? undefined : stat.size,
- mtimeMs: stat.mtimeMs,
- ignored: false,
- isSymlink: false,
- outside: false,
- };
-}
async function handleCreate(payload) {
const parent = await resolveInsideRoot(payload?.parent ?? "", { allowRoot: true });
diff --git a/apps/desktop/test/file-manager-mutations.test.mjs b/apps/desktop/test/file-manager-mutations.test.mjs
new file mode 100644
index 000000000..526ed961a
--- /dev/null
+++ b/apps/desktop/test/file-manager-mutations.test.mjs
@@ -0,0 +1,46 @@
+import assert from "node:assert/strict";
+import { createRequire } from "node:module";
+import { mkdtemp, readFile, rm, stat } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import test from "node:test";
+
+test("bundled file manager creates, renames and moves real entries with valid names", async () => {
+ const root = await mkdtemp(join(tmpdir(), "pi-file-mutations-"));
+ const previous = globalThis.pi;
+ globalThis.pi = { workspace: { get: async () => ({ path: root }) }, plugin: { getDataPath: async () => null, getSettings: async () => ({}) } };
+ const require = createRequire(import.meta.url);
+ const entry = require.resolve("../resources/plugins/pi.file-manager/main.js");
+ delete require.cache[entry];
+ const plugin = require(entry);
+ try {
+ await plugin.onLoad();
+ const file = await plugin.onPanelInvoke("fm.create", { parent: "", name: " new-file.txt ", isDirectory: false });
+ assert.equal(file.ok, true, JSON.stringify(file));
+ assert.equal(file.entry.path, "new-file.txt");
+ assert.equal(await readFile(join(root, "new-file.txt"), "utf8"), "");
+ const folder = await plugin.onPanelInvoke("fm.create", { name: "新文件夹", isDirectory: true });
+ assert.equal(folder.ok, true, JSON.stringify(folder));
+ assert.equal(folder.entry.isDirectory, true);
+ const renamed = await plugin.onPanelInvoke("fm.rename", { path: "new-file.txt", newName: "renamed.txt" });
+ assert.equal(renamed.ok, true, JSON.stringify(renamed));
+ const moved = await plugin.onPanelInvoke("fm.move", { from: "renamed.txt", toDir: "新文件夹" });
+ assert.equal(moved.ok, true, JSON.stringify(moved));
+ assert.equal(moved.entry.path, "新文件夹/renamed.txt");
+ assert.equal((await stat(join(root, "新文件夹/renamed.txt"))).isFile(), true);
+ for (const name of ["", ".", "..", "../escape", "a/b", "a\\b", "bad\0name", "bad:name", "CON", "nul.txt", "trailing."]) {
+ const rejected = await plugin.onPanelInvoke("fm.create", { name });
+ assert.equal(rejected.ok, false, name);
+ assert.equal(rejected.code, "INVALID_NAME", JSON.stringify(rejected));
+ const renameRejected = await plugin.onPanelInvoke("fm.rename", { path: "新文件夹/renamed.txt", newName: name });
+ assert.equal(renameRejected.code, "INVALID_NAME", JSON.stringify(renameRejected));
+ }
+ const duplicate = await plugin.onPanelInvoke("fm.create", { name: "新文件夹", isDirectory: true });
+ assert.equal(duplicate.code, "EXISTS");
+ } finally {
+ await plugin.onUnload();
+ globalThis.pi = previous;
+ delete require.cache[entry];
+ await rm(root, { recursive: true, force: true });
+ }
+});
diff --git a/docs/spec/04-ux/08-component-spec.md b/docs/spec/04-ux/08-component-spec.md
index 398034650..184989401 100644
--- a/docs/spec/04-ux/08-component-spec.md
+++ b/docs/spec/04-ux/08-component-spec.md
@@ -4763,3 +4763,7 @@ Closing the connected Browser tab clears its connection. Subsequent session-only
### Issue #2164 correction
While a checkpoint estimate supplies context occupancy, use its recorded provider/model capacity, or the current exact session binding for legacy checkpoints. Keep historical request usage unchanged. Unknown capacity is marked approximate. Verify 20,619 / 1,000,000 = 2%, unknown routes, legacy records and usage after a newer request with `compaction-context-usage.test.mjs`.
+
+### File Manager mutation validation (#2184)
+
+The bundled File Manager validates each create/rename name before filesystem writes. Valid names return canonical entry metadata after create, rename or move. Empty names, dot segments, separators, control characters, Windows-reserved names and trailing dots fail with INVALID_NAME. Existing destinations fail with EXISTS; containment and protected-path gates remain enforced.
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index 12473a3dc..43661b032 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -20399,3 +20399,7 @@ While a checkpoint estimate supplies context occupancy, use its recorded provide
- Specs: `03-runtime/02-agent-runtime.md`, Session job controls.
- Acceptance: wait, report re-read and running-delegate cancellation complete
through the real tool loop. Milestone: maintenance. Status: automated.
+
+#### E2E-ISSUE-2184
+
+Load the actual bundled plugin in an isolated workspace. Create a file and Unicode directory, rename and move the file; verify disk entries and response metadata. Invalid create/rename names fail before writing, and duplicate destinations are refused. Coverage: apps/desktop/test/file-manager-mutations.test.mjs. Existing Electron directory-location path also passes.
diff --git a/docs/zh-CN/spec/04-ux/08-component-spec.md b/docs/zh-CN/spec/04-ux/08-component-spec.md
index f469785d4..07b6adced 100644
--- a/docs/zh-CN/spec/04-ux/08-component-spec.md
+++ b/docs/zh-CN/spec/04-ux/08-component-spec.md
@@ -3111,3 +3111,7 @@ default nor provider configuration. OAuth accounts remain in their separate sect
### Issue #2164 修正
压缩估算作为占用时使用检查点模型容量,旧检查点使用当前会话完整绑定;历史请求用量不变,未知容量标记估算。验证 20,619 / 1,000,000 为 2%、未知路由、旧记录及新请求恢复。
+
+### 文件管理器名称校验(#2184)
+
+新建和重命名前校验名称,创建、重命名和移动后返回正确条目信息。空名称、点路径、分隔符、控制字符、Windows 保留名及尾点返回 INVALID_NAME;已有目标返回 EXISTS。路径边界和受保护路径检查保持生效。
diff --git a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
index 918e091b6..8ad67bc7c 100644
--- a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
@@ -10420,3 +10420,7 @@ Electron 一侧——工具自动签发、指示器、"停止"中止轮次、绘
#### E2E-ISSUE-2164
压缩估算作为占用时使用检查点模型容量,旧检查点使用当前会话完整绑定;历史请求用量不变,未知容量标记估算。验证 20,619 / 1,000,000 为 2%、未知路由、旧记录及新请求恢复。
+
+#### E2E-ISSUE-2184
+
+隔离项目加载真实内置插件,创建文件和中文目录,重命名并移动文件,核查磁盘与返回元数据;非法名称及重复目标拒绝。覆盖 file-manager-mutations.test.mjs 和原有 Electron 目录定位路径。