mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
refactor: improve file browser path resolution and navigation (#193)
- Add query parameter support for file paths to handle special characters - Enable navigation above base path with proper validation - Centralize URL encoding logic for file API calls - Update path validation to use workspace base instead of repos base - Add normalizePath utility for consistent path handling
This commit is contained in:
1 parent
40a2af7521
commit
31755c23f8
10 files changed
+153
-50
No files matched your search
@@ -13,11 +13,12 @@ import {
|
||||
getFileStats,
|
||||
listDirectory
|
||||
} from './file-operations'
|
||||
import { getReposPath, FILE_LIMITS } from '@opencode-manager/shared/config/env'
|
||||
import { getReposPath, getWorkspacePath, FILE_LIMITS } from '@opencode-manager/shared/config/env'
|
||||
import { ALLOWED_MIME_TYPES, type AllowedMimeType } from '@opencode-manager/shared'
|
||||
import type { ChunkedFileInfo, PatchOperation } from '@opencode-manager/shared'
|
||||
|
||||
const SHARED_WORKSPACE_BASE = getReposPath()
|
||||
const WORKSPACE_BASE = getWorkspacePath()
|
||||
|
||||
interface FileInfo {
|
||||
name: string
|
||||
@@ -234,12 +235,12 @@ export async function renameOrMoveFile(userPath: string, body: { newPath: string
|
||||
|
||||
function validatePath(userPath: string): string {
|
||||
const trimmed = userPath.trim()
|
||||
const normalized = path.normalize(trimmed).replace(/^(\.\.(\/|\\|$))+/, '')
|
||||
const normalized = path.normalize(trimmed || '.')
|
||||
const fullPath = path.join(SHARED_WORKSPACE_BASE, normalized)
|
||||
const resolved = path.resolve(fullPath)
|
||||
|
||||
const basePath = path.resolve(SHARED_WORKSPACE_BASE)
|
||||
if (!resolved.startsWith(basePath)) {
|
||||
const basePath = path.resolve(WORKSPACE_BASE)
|
||||
if (resolved !== basePath && !resolved.startsWith(`${basePath}${path.sep}`)) {
|
||||
throw { message: 'Path traversal detected', statusCode: 403 }
|
||||
}
|
||||
|
||||
@@ -369,4 +370,4 @@ export async function applyFilePatches(userPath: string, patches: PatchOperation
|
||||
await fs.writeFile(validatedPath, lines.join('\n'), 'utf8')
|
||||
|
||||
return { success: true, totalLines: lines.length }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user