From c56c6efeffd59df7c62e1f57bdc6085823e6360b Mon Sep 17 00:00:00 2001 From: Menghuan1918 Date: Fri, 13 Nov 2026 01:21:39 +0800 Subject: [PATCH] chore(compat): inline deprecated path aliases and drop ignored fence params --- src/fs-operations.ts | 22 ++++----------- src/index.ts | 22 +++++++-------- src/path-security.ts | 42 ++-------------------------- tests/fs-write-relative-path.spec.ts | 11 ++++---- 4 files changed, 25 insertions(+), 72 deletions(-) diff --git a/src/fs-operations.ts b/src/fs-operations.ts index 2409272f..7e19a680 100644 --- a/src/fs-operations.ts +++ b/src/fs-operations.ts @@ -26,7 +26,7 @@ import { createWriteStream } from 'node:fs' import { access, lstat, mkdir, rename, rm, stat, unlink, writeFile } from 'node:fs/promises' import { basename, dirname, join } from 'node:path' import { invalidateDirectoryCache, requireAbsolute } from './fs-tree.ts' -import { ensureWorkspaceWritePath, resolveTarget } from './path-security.ts' +import { resolveTarget } from './path-security.ts' import { encodeText, fileFormatOf, restoreEol } from './text-encoding.ts' import { SidebarError } from './wire.ts' @@ -51,8 +51,6 @@ export interface WorkspaceUploadInput { relativePath: string /** The request body stream (raw bytes). */ chunks: AsyncIterable - /** @deprecated IGNORED — containment was removed. Do not pass it. */ - fence?: boolean } /** @@ -144,7 +142,7 @@ export async function writeWorkspaceUpload(input: WorkspaceUploadInput): Promise throw new SidebarError('bad-request', 'relativePath must stay below the upload directory', 400) } const target = join(base, ...segments) - const safeTarget = await ensureWorkspaceWritePath(cwd, target) + const safeTarget = resolveTarget(cwd, target) const tmp = join(dirname(safeTarget), `.${basename(safeTarget)}.dsh-upload-${randomUUID()}.tmp`) await mkdir(dirname(safeTarget), { recursive: true }) const stream = createWriteStream(tmp, { flags: 'wx' }) @@ -222,7 +220,7 @@ export interface WorkspaceWriteInput { */ export async function writeWorkspaceText(input: WorkspaceWriteInput): Promise<{ mtimeMs?: number }> { const { cwd, path, content, expectedMtimeMs } = input - const absolute = await ensureWorkspaceWritePath(cwd, path) + const absolute = resolveTarget(cwd, path) // Detect the byte encoding AND the line ending BEFORE the temp write, so the // save round-trips what the file already used. const { encoding, eol } = await fileFormatOf(absolute) @@ -269,8 +267,6 @@ export interface WorkspaceRenameInput { path: string /** The new base name (single segment — rename never moves across directories). */ name: string - /** @deprecated IGNORED — containment was removed. Do not pass it. */ - fence?: boolean } /** @@ -368,7 +364,7 @@ export async function renameWorkspaceEntry(input: WorkspaceRenameInput): Promise } if (basename(absolute) === name) return { path: absolute } const destination = join(dirname(absolute), name) - const safeDestination = await ensureWorkspaceWritePath(cwd, destination) + const safeDestination = resolveTarget(cwd, destination) if (await pathExists(safeDestination)) { throw new SidebarError('fs-error', `"${name}" already exists`, 409) } @@ -389,8 +385,6 @@ export interface WorkspaceMkdirInput { path: string /** The new directory's base name (single segment — mkdir never nests). */ name: string - /** @deprecated IGNORED — containment was removed. Do not pass it. */ - fence?: boolean } /** @@ -406,7 +400,7 @@ export async function mkdirWorkspaceEntry(input: WorkspaceMkdirInput): Promise<{ const { cwd, path, name } = input assertSingleSegmentName(name) const { absolute } = resolveEntry(cwd, path) - const destination = await ensureWorkspaceWritePath(cwd, join(absolute, name)) + const destination = resolveTarget(cwd, join(absolute, name)) if (await pathExists(destination)) { throw new SidebarError('fs-error', `"${name}" already exists`, 409) } @@ -481,8 +475,6 @@ export interface WorkspaceCreateFileInput { path: string /** The new file's base name (single segment — creating never nests). */ name: string - /** @deprecated IGNORED — containment was removed. Do not pass it. */ - fence?: boolean } /** @@ -502,7 +494,7 @@ export async function createWorkspaceFile(input: WorkspaceCreateFileInput): Prom const { cwd, path, name } = input assertCreatableFileName(name) const { absolute } = await resolveEntry(cwd, path) - const destination = await ensureWorkspaceWritePath(cwd, join(absolute, name)) + const destination = resolveTarget(cwd, join(absolute, name)) if (await pathExists(destination)) { throw new SidebarError('fs-error', `"${name}" already exists`, 409) } @@ -525,8 +517,6 @@ export interface WorkspaceRemoveInput { cwd: string /** Absolute path of the row as the tree displays it (may be a symlink). */ path: string - /** @deprecated IGNORED — containment was removed. Do not pass it. */ - fence?: boolean } /** diff --git a/src/index.ts b/src/index.ts index a6e26f31..85844668 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,7 +29,7 @@ import { import { createProbeGate, listDirectory, messageOf, parentOf, requireAbsolute, rootLabel, type SidebarFsEntry } from './fs-tree.ts' import { resolveSessionPath } from './session-path.ts' import { createWorkspaceFile, mkdirWorkspaceEntry, renameWorkspaceEntry, removeWorkspaceEntry, writeWorkspaceText, writeWorkspaceUpload } from './fs-operations.ts' -import { ensureWorkspacePath } from './path-security.ts' +import { resolveTarget } from './path-security.ts' import { searchFiles } from './fs-search.ts' import { compileExcludePatterns } from './exclude-patterns.ts' import { decodeHtmlUrl } from './html-route.ts' @@ -806,7 +806,7 @@ function buildApi( 'fs.tree': async (payload) => { const { cwd } = await cwdOf(payload) const record = payload as { path?: unknown; exclude?: unknown } - const target = record.path === undefined ? cwd : await ensureWorkspacePath(cwd, requireString(payload, 'path')) + const target = record.path === undefined ? cwd : resolveTarget(cwd, requireString(payload, 'path')) // The client's explorerExclude pref rides the request (compiled here so // the listing and the singleton fold probe share one matcher). const exclude = compileExcludePatterns(record.exclude, cwd) @@ -850,7 +850,7 @@ function buildApi( const homeRelative = raw === '~' || raw.startsWith('~/') || raw.startsWith('~\\') const requested = isAbsolute(raw) || homeRelative ? raw : `${cwd}${sep}${raw}` try { - return await listDirectory(await ensureWorkspacePath(cwd, requested), resolved.listLimit, exclude, gate) + return await listDirectory(resolveTarget(cwd, requested), resolved.listLimit, exclude, gate) } catch (error) { // Per-level failure: the batch itself stays a success (one unreadable // directory must not blank the whole tree). @@ -878,7 +878,7 @@ function buildApi( // path or explicitly select a repository; missing files never change base. const selected = selectedRepoOf(payload) const base = selected === undefined ? cwd : await git.repoRoot(cwd, selected) - const path = await ensureWorkspacePath(base, requireString(payload, 'path')) + const path = resolveTarget(base, requireString(payload, 'path')) return await readText(path) }, // One byte window of a large text file, for the read-only paged preview. @@ -889,7 +889,7 @@ function buildApi( const { cwd } = await cwdOf(payload) const selected = selectedRepoOf(payload) const base = selected === undefined ? cwd : await git.repoRoot(cwd, selected) - const path = await ensureWorkspacePath(base, requireString(payload, 'path')) + const path = resolveTarget(base, requireString(payload, 'path')) const record = payload as { offset?: unknown } | null const offset = typeof record?.offset === 'number' && Number.isFinite(record.offset) && record.offset >= 0 ? Math.floor(record.offset) @@ -965,7 +965,7 @@ function buildApi( 'git.diff-head': async (payload) => { const { cwd } = await gitCwdOf(payload) const repoRoot = selectedRepoOf(payload) - const path = await ensureWorkspacePath(cwd, await resolveGitPath(cwd, requireString(payload, 'path'), repoRoot)) + const path = resolveTarget(cwd, await resolveGitPath(cwd, requireString(payload, 'path'), repoRoot)) return { diff: await git.diffHead(cwd, path, repoRoot) } }, 'git.stage': async (payload) => { @@ -1083,7 +1083,7 @@ function buildApi( 'git.blame': async (payload) => { const { cwd } = await gitCwdOf(payload) const repoRoot = selectedRepoOf(payload) - const path = await ensureWorkspacePath(cwd, await resolveGitPath(cwd, requireString(payload, 'path'), repoRoot)) + const path = resolveTarget(cwd, await resolveGitPath(cwd, requireString(payload, 'path'), repoRoot)) const startLine = requireLineNumber(payload, 'startLine') const endLine = requireLineNumber(payload, 'endLine') if (endLine < startLine) throw new SidebarError('bad-request', '"endLine" must not precede "startLine"') @@ -1272,7 +1272,7 @@ function buildApi( if (paths.length === 0) throw new SidebarError('bad-request', 'paths must be a non-empty array') const name = typeof record?.name === 'string' ? record.name : '' const selected: string[] = [] - for (const raw of paths) selected.push(await ensureWorkspacePath(cwd, raw)) + for (const raw of paths) selected.push(resolveTarget(cwd, raw)) return await archiveTasks.start({ sessionId, name, paths: selected }) }, 'archive.status': (payload) => @@ -1855,7 +1855,7 @@ export function apply(ctx: Context, config?: SidebarConfig): void { // session cwd, absolute stays, `~` expands against the home (#713), // remote-mirror namespaces project. Pre-joining here would paste a // `~` target after the cwd and hide it from the resolver. - const path = await ensureWorkspacePath(cwd, raw) + const path = resolveTarget(cwd, raw) const info = await statFileForRoute(path) if (!info.isFile()) { throw new SidebarError('fs-error', 'not a file', 400) @@ -1946,7 +1946,7 @@ export function apply(ctx: Context, config?: SidebarConfig): void { // real-path guard, with the same semantics as the media route's // fallback. const cwd = await activeWorktreeRootOf(ctx, sessionId, await sessionCwdOf(ctx, sessionId)) - const absolute = await ensureWorkspacePath(cwd, path) + const absolute = resolveTarget(cwd, path) const info = await statFileForRoute(absolute) if (!info.isFile()) { throw new SidebarError('fs-error', 'not a file', 400) @@ -2125,7 +2125,7 @@ async function handleFsWatchFrame( if (path === undefined || path === '') return try { const cwd = await activeWorktreeRootOf(ctx, sessionId, await sessionCwdOf(ctx, sessionId)) - const dir = await ensureWorkspacePath(cwd, path) + const dir = resolveTarget(cwd, path) if (frame.op === 'unwatch') { watchers.remove(dir) return diff --git a/src/path-security.ts b/src/path-security.ts index 2c341f70..51e24d7c 100644 --- a/src/path-security.ts +++ b/src/path-security.ts @@ -11,52 +11,16 @@ * (possibly session-relative) target into one absolute, lexically-normalized * path. `resolveSessionPath` joins it under the session cwd, `resolve()` * collapses `.` / `..` segments, and {@link requireAbsolute} rejects - * non-absolute input. No `realpath`, no `isWithin`, no 403. + * non-absolute input. No `realpath`, no containment guard, no 403. * - * Kept as one module (and the old names kept) so the call sites read the same - * way they did with the fence: the change is the SEMANTICS — "resolve" rather - * than "guard". + * Kept as one module so the call sites read the same 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' -/** - * Resolve an existing path to one absolute, lexically-normalized target. - * - * @param cwd - Session workspace directory (the base of relative targets). - * @param target - Client-supplied absolute or session-relative path. - * @param _fence - IGNORED. Kept as a positional parameter so every call site - * (and the route tests) did not have to change shape in the same commit; the - * containment decision it used to carry is gone for good. New code should - * omit it. - * @returns The absolute path used for the filesystem operation. - * @deprecated The name is historical — this is now plain resolution. Use - * {@link resolveTarget} in new code. - */ -export async function ensureWorkspacePath(cwd: string, target: string, _fence?: boolean): Promise { - return resolveTarget(cwd, target) -} - -/** - * Resolve a path for a WRITE destination (which may not exist yet). - * - * Lexical only, exactly like {@link ensureWorkspacePath}: a missing target is - * returned as the caller composed it (its parent is created on demand), and an - * existing symlink in the path is NOT resolved — the caller writes through it, - * which is what "no containment policy" means. - * - * @param cwd - Session workspace directory (the base of relative targets). - * @param target - Client-supplied absolute or session-relative path. - * @param _fence - IGNORED (see {@link ensureWorkspacePath}). - * @returns The absolute destination path. - * @deprecated The name is historical — this is now plain resolution. - */ -export async function ensureWorkspaceWritePath(cwd: string, target: string, _fence?: boolean): Promise { - return resolveTarget(cwd, target) -} - /** * The single resolution primitive: session-relative targets resolve under the * session cwd, absolute targets stay absolute, and the result is `resolve()`d diff --git a/tests/fs-write-relative-path.spec.ts b/tests/fs-write-relative-path.spec.ts index 64ac0c32..e5d624b1 100644 --- a/tests/fs-write-relative-path.spec.ts +++ b/tests/fs-write-relative-path.spec.ts @@ -2,7 +2,7 @@ import { mkdirSync } 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' +import { resolveTarget } from '../src/path-security.ts' import { removeTree } from './temp-tree.ts' const dirs: string[] = [] @@ -13,7 +13,7 @@ describe('fs.write resolves session-relative paths (#646)', () => { 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) + const result = resolveTarget(dir, 'pastes/test.txt') expect(result).toContain('pastes') expect(result).toContain('test.txt') }) @@ -23,7 +23,7 @@ describe('fs.write resolves session-relative paths (#646)', () => { dirs.push(dir) mkdirSync(dir, { recursive: true }) const abs = join(dir, 'abs.txt') - const result = await ensureWorkspaceWritePath(dir, abs, false) + const result = resolveTarget(dir, abs) expect(result).toContain('abs.txt') }) @@ -35,8 +35,7 @@ describe('fs.write resolves session-relative paths (#646)', () => { 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) + expect(resolveTarget(dir, target)).toBe(target) }) it('expands a ~ home path instead of joining it under the cwd (#713)', async () => { @@ -44,7 +43,7 @@ describe('fs.write resolves session-relative paths (#646)', () => { dirs.push(dir) mkdirSync(dir, { recursive: true }) const { homedir } = await import('node:os') - const result = await ensureWorkspaceWritePath(dir, '~/notes/x.txt', false) + const result = resolveTarget(dir, '~/notes/x.txt') expect(result.startsWith(homedir())).toBe(true) expect(result).toContain(join('notes', 'x.txt')) })