diff --git a/paseo-shared-browser/README.md b/paseo-shared-browser/README.md index d79120e5..252a0f5f 100644 --- a/paseo-shared-browser/README.md +++ b/paseo-shared-browser/README.md @@ -171,11 +171,12 @@ workspace or project is matched by directory: the workspace whose directory is t the deepest one containing it. Both directories are normalized first (separators, `.` and `..` segments), so a path that climbs out of a workspace does not match it. The lookup includes background workspaces. If the list is longer than 20 pages of 200, or the lookup fails, those lists match nothing. Workspaces that -share a directory cannot be told apart, so listing any of them matches them all. Matching is lexical: nothing on disk is consulted, so symlinks are not resolved and letter case is not folded. The settings read has a 2 second deadline, and a read that is too slow is treated like an unreadable setting, so every new agent gets the tool. The workspace lookup has its own 2 second deadline, and a timeout there counts as no match. Either timeout logs one line, and a slow lookup never holds up agent creation. The check runs before the adapter, its credential or the supervisor are set up, so +share a directory cannot be told apart, so listing any of them matches them all. Matching is lexical: nothing on disk is consulted, so symlinks are not resolved and letter case is not folded. A path is only compared when it starts at a root (`/` or a drive such as `C:`); a relative workspace or agent directory never matches. Backslashes are read as separators and only drive paths are modelled, so a UNC path is not recognized and a literal backslash in a POSIX file name counts as a separator. The settings read has a 2 second deadline, and a read that is too slow is treated like an unreadable setting, so every new agent gets the tool. The workspace lookup has its own 2 second deadline, and a timeout there counts as no match. Either timeout logs one line, and a slow lookup never holds up agent creation. The check runs before the adapter, its credential or the supervisor are set up, so an agent that is not allowed costs nothing. It applies to agents created after the setting is saved. A setting that cannot be read or is invalid falls back to every new agent, and the plugin log gets one line saying so. The settings screen reports an entry over 256 characters or a list over 200 entries instead of -cutting it off. +cutting it off. Changing the mode in the settings screen also saves the list text as typed, and shows +the error instead of saving when a list does not parse. ## Runtime environment overrides diff --git a/paseo-shared-browser/shared/agent-access.ts b/paseo-shared-browser/shared/agent-access.ts index 09ff32aa..15c387a3 100644 --- a/paseo-shared-browser/shared/agent-access.ts +++ b/paseo-shared-browser/shared/agent-access.ts @@ -87,6 +87,11 @@ export function normalizeDirectory(path: string): string { return absolute ? `${drive}/${joined}` : joined; } +/** True for a normalized path that starts at a root (`/` or a drive such as `C:/`). A relative path never matches a workspace. */ +export function isAbsoluteDirectory(normalized: string): boolean { + return normalized.startsWith("/") || /^[A-Za-z]:\//.test(normalized); +} + /** * The workspaces whose directory is the agent's directory, or the deepest directory that contains it. * Several workspaces can share one directory, so this returns all of them. @@ -96,11 +101,13 @@ export function workspacesForDirectory( workspaces: readonly WorkspaceRef[], ): WorkspaceRef[] { const target = normalizeDirectory(cwd); + if (!isAbsoluteDirectory(target)) return []; let depth = -1; let found: WorkspaceRef[] = []; for (const workspace of workspaces) { if (!workspace.workspaceDirectory) continue; const directory = normalizeDirectory(workspace.workspaceDirectory); + if (!isAbsoluteDirectory(directory)) continue; const prefix = directory.endsWith("/") ? directory : `${directory}/`; if (target !== directory && !target.startsWith(prefix)) continue; if (directory.length > depth) { diff --git a/paseo-shared-browser/tests/agent-filter.test.ts b/paseo-shared-browser/tests/agent-filter.test.ts index 0c362e9f..8ad128d2 100644 --- a/paseo-shared-browser/tests/agent-filter.test.ts +++ b/paseo-shared-browser/tests/agent-filter.test.ts @@ -5,6 +5,7 @@ import { agentAccessSchema, agentGetsBrowser, DEFAULT_AGENT_ACCESS, + isAbsoluteDirectory, normalizeDirectory, parseEntries, type WorkspaceRef, @@ -268,3 +269,34 @@ describe("dot segments cannot reach a workspace the agent is not in", () => { ]); }); }); + +describe("only absolute directories match a workspace", () => { + it.each([ + ["/", true], + ["/etc", true], + ["C:/", true], + ["c:/x", true], + ["", false], + [".", false], + ["..", false], + ["etc", false], + ["C:x", false], + ])("isAbsoluteDirectory(%j) is %s", (path, expected) => { + expect(isAbsoluteDirectory(normalizeDirectory(path))).toBe(expected); + }); + + it("does not let a relative workspace directory contain an absolute agent directory", () => { + const relative = [{ id: "r", projectId: "p", workspaceDirectory: "." }]; + expect(workspacesForDirectory("/etc", relative)).toEqual([]); + expect(workspacesForDirectory("/", relative)).toEqual([]); + const empty = [{ id: "e", projectId: "p", workspaceDirectory: "" }]; + expect(workspacesForDirectory("/etc", empty)).toEqual([]); + }); + + it("does not match a relative or empty agent directory, even against a root workspace", () => { + const root = [{ id: "root", projectId: "p", workspaceDirectory: "/" }]; + for (const cwd of ["", ".", "etc", "../etc"]) + expect(workspacesForDirectory(cwd, root)).toEqual([]); + expect(workspacesForDirectory("/etc", root).map((w) => w.id)).toEqual(["root"]); + }); +}); diff --git a/paseo-shared-browser/tests/contracts.test.ts b/paseo-shared-browser/tests/contracts.test.ts index eaf5ccf0..60b0b26d 100644 --- a/paseo-shared-browser/tests/contracts.test.ts +++ b/paseo-shared-browser/tests/contracts.test.ts @@ -415,6 +415,32 @@ describe("agent access", () => { } }); + it("a read that rejects after the timeout fired is not an unhandled rejection", async () => { + vi.useFakeTimers(); + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + const unhandled: unknown[] = []; + const listener = (reason: unknown) => unhandled.push(reason); + process.on("unhandledRejection", listener); + try { + const hook = captureAgentCreateHook( + () => + new Promise((_, reject) => setTimeout(() => reject(new Error("late")), 3_000)), + ); + const pending = hook({ request: request("claude") }); + await vi.advanceTimersByTimeAsync(2_000); + await expect(pending).resolves.toMatchObject({ + config: { mcpServers: { "shared-browser": expect.anything() } }, + }); + await vi.advanceTimersByTimeAsync(5_000); + vi.useRealTimers(); + await new Promise((resolve) => setImmediate(resolve)); + expect(unhandled).toEqual([]); + } finally { + process.off("unhandledRejection", listener); + warn.mockRestore(); + } + }); + it("leaves no timer behind when the read and the lookup finish in time", async () => { vi.useFakeTimers(); const hook = captureAgentCreateHook(ready({ workspaces: ["ws_a"] }));