mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +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
@@ -1,10 +1,18 @@
|
||||
import { Hono } from 'hono'
|
||||
import { Hono, type Context } from 'hono'
|
||||
import type { ContentfulStatusCode } from 'hono/utils/http-status'
|
||||
import * as fileService from '../services/files'
|
||||
import * as archiveService from '../services/archive'
|
||||
import { logger } from '../utils/logger'
|
||||
import { getErrorMessage, getStatusCode } from '../utils/error-utils'
|
||||
|
||||
function decodeFilePath(path: string): string {
|
||||
return decodeURIComponent(path)
|
||||
}
|
||||
|
||||
function getFilePathFromRequest(c: Context, fallbackPath: string): string {
|
||||
return c.req.query('path') ?? decodeFilePath(fallbackPath)
|
||||
}
|
||||
|
||||
export function createFileRoutes() {
|
||||
const app = new Hono()
|
||||
|
||||
@@ -13,7 +21,7 @@ export function createFileRoutes() {
|
||||
|
||||
if (path.endsWith('/download-zip')) {
|
||||
const match = path.match(/\/api\/files\/(.+?)\/download-zip$/)
|
||||
const userPath = match?.[1]
|
||||
const userPath = match?.[1] ? decodeFilePath(match[1]) : undefined
|
||||
|
||||
if (!userPath) {
|
||||
return c.json({ error: 'No path provided' }, 400)
|
||||
@@ -61,7 +69,7 @@ export function createFileRoutes() {
|
||||
}
|
||||
|
||||
if (path.endsWith('/ignored-paths')) {
|
||||
const userPath = path.replace(/\/api\/files\/(.+?)\/ignored-paths$/, '$1')
|
||||
const userPath = decodeFilePath(path.replace(/\/api\/files\/(.+?)\/ignored-paths$/, '$1'))
|
||||
|
||||
if (!userPath || userPath === '/ignored-paths') {
|
||||
return c.json({ error: 'No path provided' }, 400)
|
||||
@@ -77,7 +85,7 @@ export function createFileRoutes() {
|
||||
}
|
||||
|
||||
try {
|
||||
const userPath = path.replace(/^\/api\/files\//, '') || ''
|
||||
const userPath = getFilePathFromRequest(c, path.replace(/^\/api\/files\//, '') || '')
|
||||
const download = c.req.query('download') === 'true'
|
||||
const raw = c.req.query('raw') === 'true'
|
||||
const startLineParam = c.req.query('startLine')
|
||||
@@ -127,7 +135,7 @@ export function createFileRoutes() {
|
||||
|
||||
app.post('/*', async (c) => {
|
||||
try {
|
||||
const path = c.req.path.replace(/^\/api\/files\//, '') || ''
|
||||
const path = getFilePathFromRequest(c, c.req.path.replace(/^\/api\/files\//, '') || '')
|
||||
const body = await c.req.parseBody()
|
||||
|
||||
const file = body.file as File
|
||||
@@ -146,7 +154,7 @@ export function createFileRoutes() {
|
||||
|
||||
app.put('/*', async (c) => {
|
||||
try {
|
||||
const path = c.req.path.replace(/^\/api\/files\//, '') || ''
|
||||
const path = getFilePathFromRequest(c, c.req.path.replace(/^\/api\/files\//, '') || '')
|
||||
const body = await c.req.json()
|
||||
|
||||
const result = await fileService.createFileOrFolder(path, body)
|
||||
@@ -159,7 +167,7 @@ export function createFileRoutes() {
|
||||
|
||||
app.delete('/*', async (c) => {
|
||||
try {
|
||||
const path = c.req.path.replace(/^\/api\/files\//, '') || ''
|
||||
const path = getFilePathFromRequest(c, c.req.path.replace(/^\/api\/files\//, '') || '')
|
||||
|
||||
await fileService.deleteFileOrFolder(path)
|
||||
return c.json({ success: true })
|
||||
@@ -171,7 +179,7 @@ export function createFileRoutes() {
|
||||
|
||||
app.patch('/*', async (c) => {
|
||||
try {
|
||||
const path = c.req.path.replace(/^\/api\/files\//, '') || ''
|
||||
const path = getFilePathFromRequest(c, c.req.path.replace(/^\/api\/files\//, '') || '')
|
||||
const body = await c.req.json()
|
||||
|
||||
if (body.patches && Array.isArray(body.patches)) {
|
||||
|
||||
@@ -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