diff --git a/packages/core/src/studio-api/routes/files.ts b/packages/core/src/studio-api/routes/files.ts index 89e68b6af2..9b110b0625 100644 --- a/packages/core/src/studio-api/routes/files.ts +++ b/packages/core/src/studio-api/routes/files.ts @@ -13,12 +13,19 @@ import { resolve, dirname } from "node:path"; import type { StudioApiAdapter } from "../types.js"; import { isSafePath } from "../helpers/safePath.js"; +function extractFilePath(reqPath: string, projectId: string): string | null { + const filePath = decodeURIComponent(reqPath.replace(`/projects/${projectId}/files/`, "")); + if (filePath.includes("\0")) return null; + return filePath; +} + export function registerFileRoutes(api: Hono, adapter: StudioApiAdapter): void { // Read file content api.get("/projects/:id/files/*", async (c) => { const project = await adapter.resolveProject(c.req.param("id")); if (!project) return c.json({ error: "not found" }, 404); - const filePath = decodeURIComponent(c.req.path.replace(`/projects/${project.id}/files/`, "")); + const filePath = extractFilePath(c.req.path, project.id); + if (!filePath) return c.json({ error: "forbidden" }, 403); const file = resolve(project.dir, filePath); if (!isSafePath(project.dir, file) || !existsSync(file)) { return c.text("not found", 404); @@ -31,7 +38,8 @@ export function registerFileRoutes(api: Hono, adapter: StudioApiAdapter): void { api.put("/projects/:id/files/*", async (c) => { const project = await adapter.resolveProject(c.req.param("id")); if (!project) return c.json({ error: "not found" }, 404); - const filePath = decodeURIComponent(c.req.path.replace(`/projects/${project.id}/files/`, "")); + const filePath = extractFilePath(c.req.path, project.id); + if (!filePath) return c.json({ error: "forbidden" }, 403); const file = resolve(project.dir, filePath); if (!isSafePath(project.dir, file)) { return c.json({ error: "forbidden" }, 403); @@ -47,7 +55,8 @@ export function registerFileRoutes(api: Hono, adapter: StudioApiAdapter): void { api.post("/projects/:id/files/*", async (c) => { const project = await adapter.resolveProject(c.req.param("id")); if (!project) return c.json({ error: "not found" }, 404); - const filePath = decodeURIComponent(c.req.path.replace(`/projects/${project.id}/files/`, "")); + const filePath = extractFilePath(c.req.path, project.id); + if (!filePath) return c.json({ error: "forbidden" }, 403); const file = resolve(project.dir, filePath); if (!isSafePath(project.dir, file)) return c.json({ error: "forbidden" }, 403); if (existsSync(file)) return c.json({ error: "already exists" }, 409); @@ -62,7 +71,8 @@ export function registerFileRoutes(api: Hono, adapter: StudioApiAdapter): void { api.delete("/projects/:id/files/*", async (c) => { const project = await adapter.resolveProject(c.req.param("id")); if (!project) return c.json({ error: "not found" }, 404); - const filePath = decodeURIComponent(c.req.path.replace(`/projects/${project.id}/files/`, "")); + const filePath = extractFilePath(c.req.path, project.id); + if (!filePath) return c.json({ error: "forbidden" }, 403); const file = resolve(project.dir, filePath); if (!isSafePath(project.dir, file) || !existsSync(file)) { return c.json({ error: "not found" }, 404); @@ -80,7 +90,8 @@ export function registerFileRoutes(api: Hono, adapter: StudioApiAdapter): void { api.patch("/projects/:id/files/*", async (c) => { const project = await adapter.resolveProject(c.req.param("id")); if (!project) return c.json({ error: "not found" }, 404); - const filePath = decodeURIComponent(c.req.path.replace(`/projects/${project.id}/files/`, "")); + const filePath = extractFilePath(c.req.path, project.id); + if (!filePath) return c.json({ error: "forbidden" }, 403); const file = resolve(project.dir, filePath); if (!isSafePath(project.dir, file) || !existsSync(file)) { return c.json({ error: "not found" }, 404); diff --git a/packages/studio/src/App.tsx b/packages/studio/src/App.tsx index 5a7c092c9a..c9b854f89a 100644 --- a/packages/studio/src/App.tsx +++ b/packages/studio/src/App.tsx @@ -304,11 +304,11 @@ export function StudioApp() { method: "DELETE", }); if (res.ok) { - if (editingFile?.path === path) setEditingFile(null); + if (editingPathRef.current === path) setEditingFile(null); await refreshFileTree(); } }, - [editingFile, refreshFileTree], + [refreshFileTree], ); const handleRenameFile = useCallback( @@ -321,13 +321,13 @@ export function StudioApp() { body: JSON.stringify({ newPath }), }); if (res.ok) { - if (editingFile?.path === oldPath) { + if (editingPathRef.current === oldPath) { handleFileSelect(newPath); } await refreshFileTree(); } }, - [editingFile, refreshFileTree, handleFileSelect], + [refreshFileTree, handleFileSelect], ); const handleDuplicateFile = useCallback( @@ -348,12 +348,7 @@ export function StudioApp() { [refreshFileTree, handleFileSelect], ); - const handleMoveFile = useCallback( - async (oldPath: string, newPath: string) => { - await handleRenameFile(oldPath, newPath); - }, - [handleRenameFile], - ); + const handleMoveFile = handleRenameFile; const handleLint = useCallback(async () => { const pid = projectIdRef.current; diff --git a/packages/studio/src/components/editor/FileTree.tsx b/packages/studio/src/components/editor/FileTree.tsx index 740ad7e25b..bf9583730c 100644 --- a/packages/studio/src/components/editor/FileTree.tsx +++ b/packages/studio/src/components/editor/FileTree.tsx @@ -298,6 +298,7 @@ function InlineInput({ onCancel: () => void; }) { const inputRef = useRef(null); + const committedRef = useRef(false); const [value, setValue] = useState(defaultValue); // eslint-disable-next-line no-restricted-syntax @@ -314,11 +315,17 @@ function InlineInput({ } }, [defaultValue]); + const commit = (name: string) => { + if (committedRef.current) return; + committedRef.current = true; + onCommit(name); + }; + const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter") { e.preventDefault(); const trimmed = value.trim(); - if (trimmed) onCommit(trimmed); + if (trimmed && !(/[/\\]/.test(trimmed) || trimmed.includes(".."))) commit(trimmed); else onCancel(); } else if (e.key === "Escape") { e.preventDefault(); @@ -328,7 +335,8 @@ function InlineInput({ const handleBlur = () => { const trimmed = value.trim(); - if (trimmed && trimmed !== defaultValue) onCommit(trimmed); + if (trimmed && trimmed !== defaultValue && !(/[/\\]/.test(trimmed) || trimmed.includes(".."))) + commit(trimmed); else onCancel(); }; @@ -779,8 +787,8 @@ export const FileTree = memo(function FileTree({ ? sourcePath.slice(sourcePath.lastIndexOf("/") + 1) : sourcePath; const newPath = folderPath ? `${folderPath}/${fileName}` : fileName; - // Don't move to same location - if (newPath !== sourcePath) { + // Don't move to same location or into own subtree + if (newPath !== sourcePath && !folderPath.startsWith(sourcePath + "/")) { onMoveFile(sourcePath, newPath); } setDragOverFolder(null);