Skip to content

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

fix(plugin-sdk): bound plugin directory listings

vastsa6f513074f55e
diff --git a/apps/desktop/electron/main/plugin-runtime.ts b/apps/desktop/electron/main/plugin-runtime.ts
index e575685e6..655b88263 100644
--- a/apps/desktop/electron/main/plugin-runtime.ts
+++ b/apps/desktop/electron/main/plugin-runtime.ts
@@ -29,6 +29,7 @@ import {
normalizeFsPath,
parseNetDomains,
parseSkillFrontmatter,
+ PLUGIN_FS_LIST_MAX_ENTRIES,
pluginMcpToolKey,
pluginSkillId,
pluginThemeId,
@@ -801,8 +802,6 @@ export const MAX_COMPLETE_SYSTEM_CHARS = 32 * 1024;
export const MAX_COMPLETE_MESSAGE_CHARS = 200_000;
/** Kept from the pre-scope implementation: a listing is not a search index. */
const MAX_GLOB_MATCHES = 500;
-/** Entries returned for one directory. A tree is walked lazily, not dumped. */
-const MAX_LIST_ENTRIES = 1000;
/** Maximum bytes one plugin range call may cross the broker with. */
const MAX_FS_READ_RANGE_BYTES = 8 * 1024 * 1024;
/**
@@ -6064,7 +6063,7 @@ export class PluginRuntime {
mtimeMs?: number;
}> = [];
for (const name of names.sort()) {
- if (entries.length >= MAX_LIST_ENTRIES) break;
+ if (entries.length >= PLUGIN_FS_LIST_MAX_ENTRIES) break;
const childRel = rel ? `${rel}/${name}` : name;
const full = join(dir, name);
if (isDeniedFsPath(childRel)) continue;
diff --git a/apps/pi-host/src/plugin-workspace-files.test.ts b/apps/pi-host/src/plugin-workspace-files.test.ts
index 2dac8a88c..929302c34 100644
--- a/apps/pi-host/src/plugin-workspace-files.test.ts
+++ b/apps/pi-host/src/plugin-workspace-files.test.ts
@@ -55,6 +55,17 @@ it("enforces scope on both the requested and canonical file", { timeout: 60_000
expect(await read("list", "docs", [])).toMatchObject({ entries: [] });
});
+it("caps a remote directory listing at the plugin SDK limit", { timeout: 60_000 }, async () => {
+ const { root, read } = await fixture();
+ await Promise.all(Array.from({ length: 1005 }, (_, index) =>
+ writeFile(join(root, `item-${String(index).padStart(4, "0")}.txt`), ""),
+ ));
+ const result = await read("list", "", ["**"]);
+ expect(result).toMatchObject({ kind: "list" });
+ if (result.kind !== "list") throw new Error("expected a directory listing");
+ expect(result.entries).toHaveLength(1000);
+});
+
it("refuses credentials, escaped links, protected Host data and browse-root escapes", { timeout: 60_000 }, async () => {
const { home, root, data, read } = await fixture();
await symlink(join(root, ".env"), join(root, "docs", "credential.txt"));
diff --git a/apps/pi-host/src/plugin-workspace-files.ts b/apps/pi-host/src/plugin-workspace-files.ts
index 925b6ce11..e5a7cded1 100644
--- a/apps/pi-host/src/plugin-workspace-files.ts
+++ b/apps/pi-host/src/plugin-workspace-files.ts
@@ -2,7 +2,7 @@ import { readdir, realpath, stat } from "node:fs/promises";
import { isAbsolute, join, relative, resolve, sep } from "node:path";
import { RacpError } from "@pi-desktop/agent-host";
import { previewFile } from "@pi-desktop/host-runtime";
-import { isDeniedFsPath, isFsPathInScope } from "@pi-desktop/plugin-sdk";
+import { isDeniedFsPath, isFsPathInScope, PLUGIN_FS_LIST_MAX_ENTRIES } from "@pi-desktop/plugin-sdk";
import type { WorkspacePluginFilesRequest, WorkspacePluginFilesResult } from "@pi-desktop/shared";
import type { RacpHostOperations } from "@pi-desktop/racp";
@@ -85,6 +85,7 @@ export function createWorkspacePluginFiles(options: {
}
const entries: Extract<WorkspacePluginFilesResult, { kind: "list" }>["entries"] = [];
for (const entry of (await readdir(target, { withFileTypes: true })).sort((a, b) => a.name.localeCompare(b.name))) {
+ if (entries.length >= PLUGIN_FS_LIST_MAX_ENTRIES) break;
const childRef = ref ? `${ref}/${entry.name}` : entry.name;
try {
const child = await authorize(childRef, false);
diff --git a/packages/plugin-sdk/src/fs-policy.ts b/packages/plugin-sdk/src/fs-policy.ts
index 7fc573d99..392b8ad06 100644
--- a/packages/plugin-sdk/src/fs-policy.ts
+++ b/packages/plugin-sdk/src/fs-policy.ts
@@ -18,6 +18,8 @@ export type PluginFsRoot = "workspace" | "userSelected";
export type PluginFsMode = "read" | "write" | "delete";
export const PLUGIN_FS_MODES = ["read", "write", "delete"] as const;
+/** Maximum entries returned by one `pi.fs.list` call. */
+export const PLUGIN_FS_LIST_MAX_ENTRIES = 1000;
export type PluginFsRule = {
root: PluginFsRoot;
diff --git a/packages/plugin-sdk/src/index.ts b/packages/plugin-sdk/src/index.ts
index 3eac52a87..b250d0be0 100644
--- a/packages/plugin-sdk/src/index.ts
+++ b/packages/plugin-sdk/src/index.ts
@@ -2561,6 +2561,7 @@ export {
FS_DENY_DIR_SEGMENTS,
FS_DENY_FILE_PATTERNS,
LEGACY_FS_PERMISSIONS,
+ PLUGIN_FS_LIST_MAX_ENTRIES,
PLUGIN_FS_MODES,
type MatchFsGlobOptions,
type PluginFsMode,