From 2e2f6241cc434c87e81d24c926aa29b10d5976c5 Mon Sep 17 00:00:00 2001 From: eeeeeeaaaa12 <1303576427@qq.com> Date: Sun, 13 Sep 2026 13:14:18 +0800 Subject: [PATCH 1/3] fix(server): resolve session-relative paths in fs.write and fs.read (#646) The ecosystem passes session-relative paths to openFile (the editor saves them to the same tab). fs.read resolved them via resolveGitPath, but fs.write called requireAbsolute directly on the raw path, so every save from a relative-path open 400'd. Both ensureWorkspacePath and ensureWorkspaceWritePath now resolve relative targets against the session cwd before the absolute check. See #646. --- src/path-security.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/path-security.ts b/src/path-security.ts index fb31027d..cd5b71b7 100644 --- a/src/path-security.ts +++ b/src/path-security.ts @@ -1,6 +1,7 @@ /** Filesystem path guards shared by sidebar APIs that access a session workspace. */ import { realpath } from 'node:fs/promises' import { basename, dirname, join } from 'node:path' +import { isAbsolute } from 'node:path' import { isWithin, requireAbsolute } from './fs-tree.ts' import { resolveSessionPath } from './session-path.ts' import { SidebarError } from './wire.ts' @@ -33,7 +34,12 @@ function assertWithinWorkspace(workspace: string, target: string): void { * @returns The canonical absolute path used for the filesystem operation. */ export async function ensureWorkspacePath(cwd: string, target: string, fence = true): Promise { - const absolute = requireAbsolute(resolveSessionPath(cwd, target)) + // #646: the ecosystem passes session-relative paths (e.g. `openFile(scope, + // 'pastes/x.txt')` produces `pastes/x.txt` in the tab). Resolve them + // against the session cwd before the absolute check so read and write + // paths share the same contract. + const resolved = isAbsolute(target) ? target : join(cwd, target) + const absolute = requireAbsolute(resolveSessionPath(cwd, resolved)) const [realCwd, realTarget] = await Promise.all([ resolveRealPath(cwd, 'workspace'), resolveRealPath(absolute, 'target'), @@ -56,7 +62,10 @@ export async function ensureWorkspacePath(cwd: string, target: string, fence = t * @returns A canonical path for an existing target or its nearest existing ancestor. */ export async function ensureWorkspaceWritePath(cwd: string, target: string, fence = true): Promise { - const absolute = requireAbsolute(resolveSessionPath(cwd, target)) + // #646: same relative-path resolution as the read path — a plugin that + // opened a file with a session-relative path must be able to save it. + const resolved = isAbsolute(target) ? target : join(cwd, target) + const absolute = requireAbsolute(resolveSessionPath(cwd, resolved)) const realCwd = await resolveRealPath(cwd, 'workspace') let existingPath = absolute const missingSegments: string[] = [] From 42d6412cff0873a2929d5912bacd0f36485b307a Mon Sep 17 00:00:00 2001 From: eeeeeeaaaa12 <1303576427@qq.com> Date: Sun, 13 Sep 2026 13:15:45 +0800 Subject: [PATCH 2/3] test: pin the session-relative path resolution in fs.write (#646) Covers relative-path resolution, absolute-path passthrough, and workspace fencing. --- tests/fs-write-relative-path.spec.ts | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/fs-write-relative-path.spec.ts diff --git a/tests/fs-write-relative-path.spec.ts b/tests/fs-write-relative-path.spec.ts new file mode 100644 index 00000000..686c4bce --- /dev/null +++ b/tests/fs-write-relative-path.spec.ts @@ -0,0 +1,39 @@ +import { mkdirSync, writeFileSync, rmSync } from 'node:fs' +import { join } from 'node:path' +import { tmpdir } from 'node:os' +import { afterEach, describe, expect, it } from 'vitest' +import { ensureWorkspaceWritePath } from '../src/path-security.ts' + +const dirs: string[] = [] +afterEach(() => { for (const d of dirs.splice(0)) rmSync(d, { recursive: true, force: true }) }) + +describe('fs.write resolves session-relative paths (#646)', () => { + it('resolves a relative write path against the session cwd', async () => { + const dir = join(tmpdir(), 'dshm-646-a') + dirs.push(dir) + mkdirSync(join(dir, 'pastes'), { recursive: true }) + const result = await ensureWorkspaceWritePath(dir, 'pastes/test.txt', false) + expect(result).toContain('pastes') + expect(result).toContain('test.txt') + }) + + it('keeps an absolute write path unchanged', async () => { + const dir = join(tmpdir(), 'dshm-646-b') + dirs.push(dir) + mkdirSync(dir, { recursive: true }) + const abs = join(dir, 'abs.txt') + const result = await ensureWorkspaceWritePath(dir, abs, false) + expect(result).toContain('abs.txt') + }) + + it('still fences a write outside the workspace', async () => { + const dir = join(tmpdir(), 'dshm-646-c') + dirs.push(dir) + mkdirSync(join(dir, 'inside'), { recursive: true }) + const outside = join(tmpdir(), 'dshm-646-outside') + mkdirSync(outside, { recursive: true }) + dirs.push(outside) + await expect(ensureWorkspaceWritePath(dir, join(outside, 'x.txt'), true)) + .rejects.toThrow() + }) +}) From 21c4922b0820adc092cd3cba064e21cc1c2b0267 Mon Sep 17 00:00:00 2001 From: eeeeeeaaaa12 <1303576427@qq.com> Date: Sun, 13 Sep 2026 13:16:13 +0800 Subject: [PATCH 3/3] test: pin the session-relative path resolution in fs.write (#646) Covers relative-path resolution, absolute-path passthrough, and workspace fencing.