Skip to content

OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.

TypeScript

fix(security): harden proxy and fix critical review findings

lvalics3ad462c1bfd7
diff --git a/apps/web/src/components/editor/AssetsPanel.tsx b/apps/web/src/components/editor/AssetsPanel.tsx
index db93e399..35161338 100644
--- a/apps/web/src/components/editor/AssetsPanel.tsx
+++ b/apps/web/src/components/editor/AssetsPanel.tsx
@@ -542,9 +542,13 @@ export const AssetsPanel: React.FC = () => {
deleteMedia,
replaceMediaAsset,
updateSettings,
+ setKieAIItemState,
} = useProjectStore();
const mediaItems = project.mediaLibrary.items;
+ // KieAI store
+ const { retryTask } = useKieAIStore();
+
// UI store
const { select, isSelected, startDrag } = useUIStore();
@@ -769,7 +773,7 @@ export const AssetsPanel: React.FC = () => {
toast.error("Asset not found", "Cannot load the image data for this asset.");
return;
}
- const mimeType = blob.type || item.name.match(/\.png$/i) ? "image/png" : "image/jpeg";
+ const mimeType = blob.type || (item.name.match(/\.png$/i) ? "image/png" : "image/jpeg");
const file = new File([blob], item.name, { type: mimeType as string });
setKieaiDialog({ file, previewUrl: item.thumbnailUrl });
} catch (err) {
@@ -778,9 +782,6 @@ export const AssetsPanel: React.FC = () => {
}
}, []);
- const { retryTask } = useKieAIStore();
- const { setKieAIItemState } = useProjectStore();
-
const handleRetryKieAI = useCallback((item: MediaItem) => {
if (!item.kieaiTaskId) return;
// Reset error state and re-activate polling
diff --git a/apps/web/src/hooks/useKieAIPoller.ts b/apps/web/src/hooks/useKieAIPoller.ts
index 74a6f13d..8e8a9076 100644
--- a/apps/web/src/hooks/useKieAIPoller.ts
+++ b/apps/web/src/hooks/useKieAIPoller.ts
@@ -9,9 +9,10 @@
* - On exhaustion: marks task as failed; UI shows a manual retry button
* - On API success: downloads result, replaces placeholder, removes task
* - Task is ALWAYS removed on API success/fail — never left stuck
+ * - Tasks older than 3 days are auto-expired
*/
-import { useEffect, useRef } from "react";
+import { useEffect, useRef, useCallback } from "react";
import { useProjectStore } from "../stores/project-store";
import { useKieAIStore, MAX_POLL_RETRIES } from "../stores/kieai-store";
import { pollTaskOnce, getResultUrl } from "../services/kieai/image-generation";
@@ -21,6 +22,24 @@ const FIRST_POLL_DELAY_MS = 5_000;
const POLL_INTERVAL_IMAGE_MS = 30_000;
const POLL_INTERVAL_VIDEO_MS = 120_000;
+/** Tasks older than 3 days are expired (KieAI cleans up server-side too) */
+const TASK_MAX_AGE_MS = 3 * 24 * 60 * 60 * 1000;
+
+/** Allowed result URL host for SSRF protection */
+const ALLOWED_RESULT_HOSTS = new Set([
+ "tempfile.aiquickdraw.com",
+ "cdn.kie.ai",
+]);
+
+function isAllowedResultUrl(url: string): boolean {
+ try {
+ const parsed = new URL(url);
+ return ALLOWED_RESULT_HOSTS.has(parsed.hostname);
+ } catch {
+ return false;
+ }
+}
+
export function useKieAIPoller() {
const tasks = useKieAIStore((s) => s.tasks);
const removeTask = useKieAIStore((s) => s.removeTask);
@@ -33,6 +52,17 @@ export function useKieAIPoller() {
const timersRef = useRef<Map<string, ReturnType<typeof setTimeout>>>(new Map());
const inFlightRef = useRef<Set<string>>(new Set());
+ // Use refs for callbacks to avoid stale closures in recursive setTimeout
+ const replacePlaceholderRef = useRef(replacePlaceholder);
+ replacePlaceholderRef.current = replacePlaceholder;
+ const setKieAIItemStateRef = useRef(setKieAIItemState);
+ setKieAIItemStateRef.current = setKieAIItemState;
+
+ const handleExpiredTask = useCallback((taskId: string, mediaId: string) => {
+ markFailed(taskId);
+ setKieAIItemStateRef.current(mediaId, false, true);
+ }, [markFailed]);
+
useEffect(() => {
const activeTasks = tasks.filter(
(t) => t.projectId === projectId && !t.failed,
@@ -41,6 +71,13 @@ export function useKieAIPoller() {
// Start a polling loop for each new active task
for (const task of activeTasks) {
if (timersRef.current.has(task.taskId)) continue;
+ if (inFlightRef.current.has(task.taskId)) continue;
+
+ // Expire tasks older than 3 days
+ if (Date.now() - task.createdAt > TASK_MAX_AGE_MS) {
+ handleExpiredTask(task.taskId, task.mediaId);
+ continue;
+ }
const interval =
task.type === "video" ? POLL_INTERVAL_VIDEO_MS : POLL_INTERVAL_IMAGE_MS;
@@ -50,10 +87,6 @@ export function useKieAIPoller() {
? FIRST_POLL_DELAY_MS - elapsed
: Math.max(0, interval - (elapsed % interval));
- console.log(
- `[KieAIPoller] scheduling ${task.taskId} — first poll in ${Math.round(firstDelay / 1000)}s`,
- );
-
const doPoll = async () => {
timersRef.current.delete(task.taskId);
if (inFlightRef.current.has(task.taskId)) return;
@@ -61,33 +94,41 @@ export function useKieAIPoller() {
try {
const record = await pollTaskOnce(task.taskId);
- console.log(`[KieAIPoller] ${task.taskId} state: ${record.state}`);
if (record.state === "success") {
// API says done — always remove the task, even if download fails
try {
const url = getResultUrl(record);
- console.log(`[KieAIPoller] downloading result: ${url}`);
+
+ if (!isAllowedResultUrl(url)) {
+ throw new KieAIError(403, `Result URL host not allowed: ${url}`);
+ }
+
const res = await fetch(url);
+ if (!res.ok) {
+ throw new KieAIError(res.status, `Download failed: HTTP ${res.status}`);
+ }
+
const blob = await res.blob();
- await replacePlaceholder(task.mediaId, blob, task.suggestedName);
- console.log(`[KieAIPoller] ✓ replaced placeholder for ${task.taskId}`);
+ if (blob.size === 0) {
+ throw new KieAIError(500, "Downloaded result is empty");
+ }
+
+ await replacePlaceholderRef.current(task.mediaId, blob, task.suggestedName);
} catch (downloadErr) {
console.error(`[KieAIPoller] download failed for ${task.taskId}:`, downloadErr);
- // At minimum clear the pending spinner so the user isn't stuck
- setKieAIItemState(task.mediaId, false, true);
+ setKieAIItemStateRef.current(task.mediaId, false, true);
} finally {
removeTask(task.taskId);
}
} else if (record.state === "fail") {
console.warn(`[KieAIPoller] task ${task.taskId} failed: ${record.failMsg}`);
- setKieAIItemState(task.mediaId, false, true);
+ setKieAIItemStateRef.current(task.mediaId, false, true);
removeTask(task.taskId);
} else {
- // Still generating — schedule next poll (no error, don't touch retries)
- console.log(`[KieAIPoller] ${task.taskId} still ${record.state}, retry in ${interval / 1000}s`);
+ // Still generating — schedule next poll
const t = setTimeout(doPoll, interval);
timersRef.current.set(task.taskId, t);
}
@@ -95,7 +136,7 @@ export function useKieAIPoller() {
// Auth error — give up immediately, don't count as a retry
if (err instanceof KieAIError && err.code === 401) {
console.warn("[KieAIPoller] auth error — stopping poll for", task.taskId);
- setKieAIItemState(task.mediaId, false, true);
+ setKieAIItemStateRef.current(task.mediaId, false, true);
removeTask(task.taskId);
return;
}
@@ -115,12 +156,8 @@ export function useKieAIPoller() {
`[KieAIPoller] ${task.taskId} exhausted ${MAX_POLL_RETRIES} retries — marking failed`,
);
markFailed(task.taskId);
- setKieAIItemState(task.mediaId, false, true);
+ setKieAIItemStateRef.current(task.mediaId, false, true);
} else {
- console.warn(
- `[KieAIPoller] poll error for ${task.taskId} (retry ${currentRetries}/${MAX_POLL_RETRIES}):`,
- err,
- );
const t = setTimeout(doPoll, interval);
timersRef.current.set(task.taskId, t);
}
@@ -141,13 +178,14 @@ export function useKieAIPoller() {
timersRef.current.delete(taskId);
}
}
- }, [tasks, projectId, removeTask, incrementRetry, markFailed, replacePlaceholder, setKieAIItemState]);
+ }, [tasks, projectId, removeTask, incrementRetry, markFailed, handleExpiredTask]);
// Cleanup on unmount
useEffect(() => {
+ const timers = timersRef.current;
return () => {
- for (const timer of timersRef.current.values()) clearTimeout(timer);
- timersRef.current.clear();
+ for (const timer of timers.values()) clearTimeout(timer);
+ timers.clear();
};
}, []);
}
diff --git a/apps/web/src/services/kieai/client.ts b/apps/web/src/services/kieai/client.ts
index f44e0f4d..230d8684 100644
--- a/apps/web/src/services/kieai/client.ts
+++ b/apps/web/src/services/kieai/client.ts
@@ -46,9 +46,13 @@ export async function kieaiPostJson<TBody extends object, TData>(
body: JSON.stringify(body),
});
+ if (!res.ok) {
+ throw new KieAIError(res.status, `HTTP ${res.status}: ${res.statusText}`);
+ }
+
const json = (await res.json()) as KieAIResponse<TData>;
- if (!json.success && json.code !== 200) {
+ if (json.code !== 200) {
throw new KieAIError(json.code, json.msg);
}
@@ -69,8 +73,12 @@ export async function kieaiGet<TData>(
headers: { Authorization: `Bearer ${apiKey}` },
});
+ if (!res.ok) {
+ throw new KieAIError(res.status, `HTTP ${res.status}: ${res.statusText}`);
+ }
+
const json = (await res.json()) as KieAIResponse<TData>;
- if (!json.success && json.code !== 200) {
+ if (json.code !== 200) {
throw new KieAIError(json.code, json.msg);
}
return json.data;
@@ -92,9 +100,13 @@ export async function kieaiPostForm<TData>(
body: form,
});
+ if (!res.ok) {
+ throw new KieAIError(res.status, `HTTP ${res.status}: ${res.statusText}`);
+ }
+
const json = (await res.json()) as KieAIResponse<TData>;
- if (!json.success && json.code !== 200) {
+ if (json.code !== 200) {
throw new KieAIError(json.code, json.msg);
}
diff --git a/apps/web/src/services/kieai/image-generation.ts b/apps/web/src/services/kieai/image-generation.ts
index 7124dbc4..eeb0d00e 100644
--- a/apps/web/src/services/kieai/image-generation.ts
+++ b/apps/web/src/services/kieai/image-generation.ts
@@ -105,7 +105,8 @@ export interface TaskRecord {
taskId: string;
model: string;
state: TaskState;
- resultJson: { resultUrls?: string[]; resultObject?: unknown } | null;
+ /** API returns this as a JSON string — getResultUrl handles parsing */
+ resultJson: string | { resultUrls?: string[]; resultObject?: unknown } | null;
failCode?: number;
failMsg?: string;
costTime?: number;
@@ -165,7 +166,7 @@ export async function pollTask(
attempt++;
await new Promise<void>((res, rej) => {
const t = setTimeout(res, delay);
- signal?.addEventListener("abort", () => { clearTimeout(t); rej(new DOMException("Aborted", "AbortError")); });
+ signal?.addEventListener("abort", () => { clearTimeout(t); rej(new DOMException("Aborted", "AbortError")); }, { once: true });
});
}
@@ -191,10 +192,16 @@ export async function pollTaskOnce(taskId: string): Promise<TaskRecord> {
*/
export function getResultUrl(record: TaskRecord): string {
// resultJson is returned as a JSON string by the API — parse it if needed
- const rj: Record<string, unknown> | null =
- typeof record.resultJson === "string"
- ? (JSON.parse(record.resultJson) as Record<string, unknown>)
- : (record.resultJson as Record<string, unknown> | null);
+ let rj: Record<string, unknown> | null = null;
+ if (typeof record.resultJson === "string") {
+ try {
+ rj = JSON.parse(record.resultJson) as Record<string, unknown>;
+ } catch {
+ throw new KieAIError(500, "Failed to parse resultJson from API");
+ }
+ } else {
+ rj = record.resultJson as Record<string, unknown> | null;
+ }
// Try every known field path
const candidates: unknown[] = [
diff --git a/apps/web/src/stores/project-store.ts b/apps/web/src/stores/project-store.ts
index 8f346c95..4df11522 100644
--- a/apps/web/src/stores/project-store.ts
+++ b/apps/web/src/stores/project-store.ts
@@ -872,7 +872,7 @@ export const useProjectStore = create<ProjectState>()(
set({
project: {
...project,
- mediaLibrary: { items: updatedItems },
+ mediaLibrary: { ...project.mediaLibrary, items: updatedItems },
modifiedAt: Date.now(),
},
});
@@ -904,9 +904,10 @@ export const useProjectStore = create<ProjectState>()(
bitmap.close();
const thumbBlob = await canvas.convertToBlob({ type: "image/jpeg", quality: 0.75 });
- thumbnailUrl = await new Promise<string>((resolve) => {
+ thumbnailUrl = await new Promise<string>((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result as string);
+ reader.onerror = () => reject(reader.error);
reader.readAsDataURL(thumbBlob);
});
} catch (thumbErr) {
@@ -945,7 +946,7 @@ export const useProjectStore = create<ProjectState>()(
set({
project: {
...project,
- mediaLibrary: { items: updatedItems },
+ mediaLibrary: { ...project.mediaLibrary, items: updatedItems },
modifiedAt: Date.now(),
},
});