mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-16 06:19:21 +00:00
fix(studio): address code review findings for file management
- Fix InlineInput double-fire on Enter+blur with committed ref guard - Add null byte sanitization via extractFilePath helper in API routes - Fix stale editingFile closure — use editingPathRef instead - Add client-side filename validation (reject / \ ..) - Prevent drag-and-drop folder-into-own-subtree - Remove trivial handleMoveFile wrapper Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
2b6ca3fd6c
commit
7232313d77
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -298,6 +298,7 @@ function InlineInput({
|
||||
onCancel: () => void;
|
||||
}) {
|
||||
const inputRef = useRef<HTMLInputElement>(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);
|
||||
|
||||
Reference in New Issue
Block a user