From 7ed13e676056b6c241af8c87f88258bf84a61b42 Mon Sep 17 00:00:00 2001 From: "user.email" Date: Wed, 23 Sep 2026 14:32:02 +0800 Subject: [PATCH] fix(preview): resolve relative file paths against session workspace for html and media preview --- src/client/api.ts | 7 +++++-- src/index.ts | 22 +++++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/client/api.ts b/src/client/api.ts index c067eb3c..7224da10 100644 --- a/src/client/api.ts +++ b/src/client/api.ts @@ -7,6 +7,7 @@ * request). Failures surface as {@link SidebarApiError} with the wire code. */ import { encodeHtmlUrl } from '../html-route.ts' +import { resolveSidebarPath } from './paths.ts' import type { LastActivity } from '../subagent-activity.ts' import type { SidechatLiveEvent, SidechatLogEvent, SidechatThreadInfo } from '../sidechat-core.ts' import type { SidebarJobView, SidebarSessionEvent } from '../context-types.ts' @@ -409,7 +410,8 @@ export function downloadUrl(scope: SessionScope, path: string): string { /** Shared URL builder for the /sidebar/file route (media vs download). */ function fileUrl(scope: SessionScope, path: string, download: boolean): string { - const params = new URLSearchParams({ sessionId: scope.sessionId, path }) + const resolved = resolveSidebarPath(scope.cwd, path) + const params = new URLSearchParams({ sessionId: scope.sessionId, path: resolved }) if (scope.cwd !== undefined && scope.cwd !== '') params.set('cwd', scope.cwd) if (download) params.set('download', '1') return `/sidebar/file?${params.toString()}` @@ -424,5 +426,6 @@ function fileUrl(scope: SessionScope, path: string, download: boolean): string { * client-side platform signal is needed. */ export function htmlUrl(scope: SessionScope, path: string): string { - return encodeHtmlUrl(scope.sessionId, path) + const resolved = resolveSidebarPath(scope.cwd, path) + return encodeHtmlUrl(scope.sessionId, resolved) } diff --git a/src/index.ts b/src/index.ts index 7d18a058..9bdc94da 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,7 +28,7 @@ import { type SidebarConfig, type SidebarPrefs, } from './config.ts' -import { parentOf, requireAbsolute, listDirectory, rootLabel } from './fs-tree.ts' +import { isWithin, parentOf, requireAbsolute, listDirectory, rootLabel } from './fs-tree.ts' import { resolveSessionPath } from './session-path.ts' import { renameWorkspaceEntry, removeWorkspaceEntry, writeWorkspaceUpload } from './fs-operations.ts' import { ensureWorkspacePath, ensureWorkspaceWritePath } from './path-security.ts' @@ -917,7 +917,16 @@ export function apply(ctx: Context, config?: SidebarConfig): void { const raw = url.searchParams.get('path') if (sessionId === null || raw === null) throw new SidebarError('bad-request', 'sessionId and path are required') const cwd = await sessionCwdOf(ctx, sessionId, url.searchParams.get('cwd') ?? undefined) - const path = await ensureWorkspacePath(cwd, raw, fenceEnabledOf(() => settingsFace)) + let target = raw + if (!isAbsolute(target)) { + target = join(cwd, target) + } else if (!isWithin(cwd, target)) { + const candidate = join(cwd, target.replace(/^[\/\\]+/, '')) + if (isWithin(cwd, candidate)) { + target = candidate + } + } + const path = await ensureWorkspacePath(cwd, target, fenceEnabledOf(() => settingsFace)) const info = await stat(path) if (!info.isFile() || info.size > resolved.mediaLimit) { throw new SidebarError('fs-error', 'not a file or too large', 400) @@ -976,7 +985,14 @@ export function apply(ctx: Context, config?: SidebarConfig): void { // real-path guard, with the same semantics as the media route's // fallback. const cwd = await sessionCwdOf(ctx, sessionId) - const absolute = await ensureWorkspacePath(cwd, path, fenceEnabledOf(() => settingsFace)) + let target = path + if (!isWithin(cwd, target)) { + const candidate = join(cwd, target.replace(/^[\/\\]+/, '')) + if (isWithin(cwd, candidate)) { + target = candidate + } + } + const absolute = await ensureWorkspacePath(cwd, target, fenceEnabledOf(() => settingsFace)) const info = await stat(absolute) if (!info.isFile() || info.size > resolved.mediaLimit) { throw new SidebarError('fs-error', 'not a file or too large', 400)