mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
fix(files): resolve workspace paths and add sandboxed HTML preview (#367)
* fix(files): resolve workspace paths and add sandboxed HTML preview Accept workspace-absolute paths in the file service and return repo-relative paths for file reads and directory listings. Serve raw content under a CSP sandbox and preview HTML files in a sandboxed iframe with a raw toggle. Resolve file-click paths from the session directory and repo local path, detect absolute and html/htm file references while ignoring URLs, accept the legacy filePath input key and filediff metadata, fix the file browser navigating into an initially selected directory, and use focus-visible rings across shared UI primitives. * fix(files): address review feedback on navigation and path fallback Key initial directory navigation to the initial selection so viewport changes do not reset it, and open the mobile preview from a separate effect. Fall back to the legacy filePath input when path is empty, and cover both with regression tests.
This commit is contained in:
1 parent
5bd688de4b
commit
68b13c8e35
19 files changed
+307
-58
No files matched your search
@@ -120,6 +120,7 @@ export function createFileRoutes() {
|
||||
headers: {
|
||||
'Content-Type': result.mimeType || 'application/octet-stream',
|
||||
'Content-Length': result.size.toString(),
|
||||
'Content-Security-Policy': 'sandbox allow-scripts',
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -64,6 +64,7 @@ export async function getRawFileContent(userPath: string): Promise<Buffer> {
|
||||
|
||||
export async function getFile(userPath: string): Promise<FileInfo> {
|
||||
const validatedPath = validatePath(userPath)
|
||||
const responsePath = path.isAbsolute(userPath.trim()) ? path.relative(SHARED_WORKSPACE_BASE, validatedPath) : userPath
|
||||
logger.info(`Getting file for path: ${userPath} -> ${validatedPath}`)
|
||||
|
||||
try {
|
||||
@@ -84,7 +85,7 @@ export async function getFile(userPath: string): Promise<FileInfo> {
|
||||
for (const entry of entries) {
|
||||
children.push({
|
||||
name: entry.name,
|
||||
path: path.join(userPath, entry.name),
|
||||
path: path.join(responsePath, entry.name),
|
||||
isDirectory: entry.isDirectory,
|
||||
size: entry.size,
|
||||
lastModified: entry.lastModified,
|
||||
@@ -93,7 +94,7 @@ export async function getFile(userPath: string): Promise<FileInfo> {
|
||||
|
||||
return {
|
||||
name: path.basename(validatedPath),
|
||||
path: userPath,
|
||||
path: responsePath,
|
||||
isDirectory: true,
|
||||
size: 0,
|
||||
children: children.sort((a, b) => {
|
||||
@@ -127,7 +128,7 @@ export async function getFile(userPath: string): Promise<FileInfo> {
|
||||
|
||||
return {
|
||||
name: path.basename(validatedPath),
|
||||
path: userPath,
|
||||
path: responsePath,
|
||||
isDirectory: false,
|
||||
size: stats.size,
|
||||
mimeType,
|
||||
@@ -234,14 +235,21 @@ export async function renameOrMoveFile(userPath: string, body: { newPath: string
|
||||
}
|
||||
}
|
||||
|
||||
function isWithinBase(resolved: string, basePath: string): boolean {
|
||||
return resolved === basePath || resolved.startsWith(`${basePath}${path.sep}`)
|
||||
}
|
||||
|
||||
function validatePath(userPath: string): string {
|
||||
const trimmed = userPath.trim()
|
||||
const normalized = path.normalize(trimmed || '.')
|
||||
const fullPath = path.join(SHARED_WORKSPACE_BASE, normalized)
|
||||
const resolved = path.resolve(fullPath)
|
||||
|
||||
const basePath = path.resolve(WORKSPACE_BASE)
|
||||
if (resolved !== basePath && !resolved.startsWith(`${basePath}${path.sep}`)) {
|
||||
|
||||
if (path.isAbsolute(normalized) && isWithinBase(path.resolve(normalized), basePath)) {
|
||||
return path.resolve(normalized)
|
||||
}
|
||||
|
||||
const resolved = path.resolve(path.join(SHARED_WORKSPACE_BASE, normalized))
|
||||
if (!isWithinBase(resolved, basePath)) {
|
||||
throw { message: 'Path traversal detected', statusCode: 403 }
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user