From 37fb9204b08620d38edb6f0864eec34355ec28e2 Mon Sep 17 00:00:00 2001 From: HuanLinOTO Date: Thu, 1 Oct 2026 17:19:40 +0800 Subject: [PATCH] =?UTF-8?q?fix(paths):=20~=20=E6=98=AF=E4=B8=BB=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E8=B7=AF=E5=BE=84=E8=80=8C=E4=B8=8D=E6=98=AF=E4=BC=9A?= =?UTF-8?q?=E8=AF=9D=E7=9B=B8=E5=AF=B9=E8=B7=AF=E5=BE=84=EF=BC=8C=E5=B1=95?= =?UTF-8?q?=E5=BC=80=E5=90=8E=E5=86=8D=E8=A7=A3=E6=9E=90=EF=BC=88Fixes=20#?= =?UTF-8?q?713=EF=BC=8C=E9=9A=8F=20Phase=202=20=E8=B7=AF=E5=BE=84=E7=B0=87?= =?UTF-8?q?=E9=A1=BA=E5=B8=A6=E4=BF=AE=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/paths.ts | 5 ++++- src/path-security.ts | 8 +++++++- tests/fs-write-relative-path.spec.ts | 10 ++++++++++ tests/paths.spec.ts | 8 ++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/client/paths.ts b/src/client/paths.ts index 7a26f522..467adef1 100644 --- a/src/client/paths.ts +++ b/src/client/paths.ts @@ -21,7 +21,10 @@ * comment — so a produced UNC path is never joined onto the cwd. */ export function isAbsolutePath(path: string): boolean { - return path.startsWith('/') || /^[A-Za-z]:[\\/]/.test(path) || /^[\\/]{2}[^\\/]/.test(path) + // A `~`-relative path (`~`, `~/...`, `~\...`) names the user's home, not a + // session-relative path (#713): never join it onto the cwd client-side. + return path === '~' || path.startsWith('~/') || path.startsWith('~\\') + || path.startsWith('/') || /^[A-Za-z]:[\\/]/.test(path) || /^[\\/]{2}[^\\/]/.test(path) } /** diff --git a/src/path-security.ts b/src/path-security.ts index 1969441a..2c341f70 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 { homedir } from 'node:os' import { isAbsolute, join } from 'node:path' import { requireAbsolute } from './fs-tree.ts' import { resolveSessionPath } from './session-path.ts' @@ -62,10 +63,15 @@ 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 { + // #713: `~` names the user's home directory, not a session-relative path — + // expand it before the join so it is not pasted after the cwd (ENOENT). + const homeExpanded = target === '~' || target.startsWith('~/') || target.startsWith('~\\') + ? join(homedir(), target.slice(1)) + : 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) + const joined = isAbsolute(homeExpanded) ? homeExpanded : join(cwd, homeExpanded) return requireAbsolute(resolveSessionPath(cwd, joined)) } diff --git a/tests/fs-write-relative-path.spec.ts b/tests/fs-write-relative-path.spec.ts index 82c5bbfe..28c6bd84 100644 --- a/tests/fs-write-relative-path.spec.ts +++ b/tests/fs-write-relative-path.spec.ts @@ -37,4 +37,14 @@ describe('fs.write resolves session-relative paths (#646)', () => { // The fence argument is ignored by design now; the path resolves lexically. await expect(ensureWorkspaceWritePath(dir, target, true)).resolves.toBe(target) }) + + it('expands a ~ home path instead of joining it under the cwd (#713)', async () => { + const dir = join(tmpdir(), 'dshm-713') + dirs.push(dir) + mkdirSync(dir, { recursive: true }) + const { homedir } = await import('node:os') + const result = await ensureWorkspaceWritePath(dir, '~/notes/x.txt', false) + expect(result.startsWith(homedir())).toBe(true) + expect(result).toContain(join('notes', 'x.txt')) + }) }) diff --git a/tests/paths.spec.ts b/tests/paths.spec.ts index 4c06c4e0..e6dc3718 100644 --- a/tests/paths.spec.ts +++ b/tests/paths.spec.ts @@ -57,6 +57,14 @@ describe('path helpers', () => { expect(isAbsolutePath('rel/x.ts')).toBe(false) }) + it('treats ~ as home-absolute, never a session-relative path (#713)', () => { + expect(isAbsolutePath('~')).toBe(true) + expect(isAbsolutePath('~/notes/x.md')).toBe(true) + expect(isAbsolutePath('~\\notes\\x.md')).toBe(true) + // `~other-user` is NOT the home shorthand; it stays relative-shaped. + expect(isAbsolutePath('~other/x.ts')).toBe(false) + }) + it('htmlUrl always marks UNC paths (platform-neutral marker)', () => { // The marker is platform-neutral now: the host resolves the decoded // '//server/share/...' form per-platform, so no cwd/OS signal is needed.