diff --git a/src/path-security.ts b/src/path-security.ts index bf57c8a9..1969441a 100644 --- a/src/path-security.ts +++ b/src/path-security.ts @@ -17,6 +17,7 @@ * way they did with the fence: the change is the SEMANTICS — "resolve" rather * than "guard". */ +import { isAbsolute, join } from 'node:path' import { requireAbsolute } from './fs-tree.ts' import { resolveSessionPath } from './session-path.ts' @@ -61,5 +62,10 @@ export async function ensureWorkspaceWritePath(cwd: string, target: string, _fen * (lexical `..` collapse). Throws fs-error for a non-absolute result. */ export function resolveTarget(cwd: string, target: string): string { - return requireAbsolute(resolveSessionPath(cwd, target)) + // #646: the ecosystem passes session-relative targets (e.g. `openFile(scope, + // 'pastes/x.txt')` keeps `pastes/x.txt` in the tab). Join them onto the + // session cwd before the absolute check so every entry point (fs.read / + // write / rename / remove / upload, media, html) shares ONE contract. + const joined = isAbsolute(target) ? target : join(cwd, target) + return requireAbsolute(resolveSessionPath(cwd, joined)) } diff --git a/tests/fs-write-relative-path.spec.ts b/tests/fs-write-relative-path.spec.ts new file mode 100644 index 00000000..82c5bbfe --- /dev/null +++ b/tests/fs-write-relative-path.spec.ts @@ -0,0 +1,40 @@ +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('keeps a write outside the workspace (containment was removed with the path-security rewrite)', 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) + const target = join(outside, 'x.txt') + // The fence argument is ignored by design now; the path resolves lexically. + await expect(ensureWorkspaceWritePath(dir, target, true)).resolves.toBe(target) + }) +})