Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: extract sanitizeFileName util to prevent path traversal in generated file names

YuTengjing0abde1623d20
diff --git a/eslint-suppressions.json b/eslint-suppressions.json
index b02d2b9693e..49607007cf7 100644
--- a/eslint-suppressions.json
+++ b/eslint-suppressions.json
@@ -665,11 +665,6 @@
"count": 1
}
},
- "src/server/routers/async/image.ts": {
- "object-shorthand": {
- "count": 1
- }
- },
"src/server/routers/lambda/__tests__/integration/aiAgent.createClientGroupAgentTaskThread.integration.test.ts": {
"@typescript-eslint/no-non-null-asserted-optional-chain": {
"count": 2
diff --git a/src/app/(backend)/api/webhooks/video/[provider]/route.ts b/src/app/(backend)/api/webhooks/video/[provider]/route.ts
index 2d7da6aa8d2..c2df7fd4b30 100644
--- a/src/app/(backend)/api/webhooks/video/[provider]/route.ts
+++ b/src/app/(backend)/api/webhooks/video/[provider]/route.ts
@@ -20,6 +20,7 @@ import { GenerationModel } from '@/database/models/generation';
import { generationBatches } from '@/database/schemas';
import { getServerDB } from '@/database/server';
import { VideoGenerationService } from '@/server/services/generation/video';
+import { sanitizeFileName } from '@/utils/sanitizeFileName';
const log = debug('lobe-video:webhook');
@@ -185,10 +186,7 @@ export const POST = async (req: Request, { params }: { params: Promise<{ provide
{
fileHash: processResult.fileHash,
fileType: processResult.mimeType,
- name: `${(batch?.prompt ?? generation.id)
- .replaceAll(/[^\w\u4E00-\u9FFF -]/g, '_')
- .trim()
- .slice(0, 50)}.mp4`,
+ name: `${sanitizeFileName(batch?.prompt ?? '', generation.id)}.mp4`,
size: processResult.fileSize,
url: processResult.videoKey,
},
diff --git a/src/server/routers/async/image.ts b/src/server/routers/async/image.ts
index 332b573e7ec..b0835a396e2 100644
--- a/src/server/routers/async/image.ts
+++ b/src/server/routers/async/image.ts
@@ -16,11 +16,10 @@ import { GenerationBatchModel } from '@/database/models/generationBatch';
import { asyncAuthedProcedure, asyncRouter as router } from '@/libs/trpc/async';
import { initModelRuntimeFromDB } from '@/server/modules/ModelRuntime';
import { GenerationService } from '@/server/services/generation';
+import { sanitizeFileName } from '@/utils/sanitizeFileName';
const log = debug('lobe-image:async');
-// Constants for better maintainability
-const FILENAME_MAX_LENGTH = 50;
const IMAGE_URL_PREVIEW_LENGTH = 100;
const imageProcedure = asyncAuthedProcedure.use(async (opts) => {
@@ -270,7 +269,7 @@ export const imageRouter = router({
await chargeAfterGenerate({
metadata: {
asyncTaskId: taskId,
- generationBatchId: generationBatchId,
+ generationBatchId,
modelId: model,
topicId: generationTopicId,
},
@@ -343,8 +342,7 @@ export const imageRouter = router({
path: uploadedImageUrl,
width: image.width,
},
- name: `${params.prompt.slice(0, FILENAME_MAX_LENGTH)}.${image.extension}`,
- // Use first 50 characters of prompt as filename
+ name: `${sanitizeFileName(params.prompt, generationId)}.${image.extension}`,
size: image.size,
url: uploadedImageUrl,
},
diff --git a/src/utils/sanitizeFileName.ts b/src/utils/sanitizeFileName.ts
new file mode 100644
index 00000000000..ee585e42ada
--- /dev/null
+++ b/src/utils/sanitizeFileName.ts
@@ -0,0 +1,19 @@
+const DEFAULT_MAX_LENGTH = 50;
+
+/**
+ * Sanitize user input for use as a file name.
+ * Strips path separators and control characters, keeping alphanumeric,
+ * CJK, spaces and hyphens. Falls back to `fallback` when the result is empty.
+ */
+export function sanitizeFileName(
+ input: string,
+ fallback: string,
+ maxLength: number = DEFAULT_MAX_LENGTH,
+): string {
+ const sanitized = input
+ .replaceAll(/[^\w\u4E00-\u9FFF -]/g, '_')
+ .trim()
+ .slice(0, maxLength);
+
+ return sanitized || fallback;
+}