Skip to content

Commit 1bac5fb

Browse files
authored
Merge branch 'main' into feat/fix-agentflow-windows-system-build-issue
2 parents 9af85c8 + b44affd commit 1bac5fb

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

packages/components/src/storageUtils.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Readable } from 'node:stream'
1313
import path from 'path'
1414
import sanitize from 'sanitize-filename'
1515
import { getUserHome } from './utils'
16-
import { isPathTraversal, isValidUUID } from './validator'
16+
import { isPathTraversal, isUnsafeFilePath, isValidUUID } from './validator'
1717

1818
const dirSize = async (directoryPath: string) => {
1919
let totalSize = 0
@@ -752,13 +752,26 @@ export const streamStorageFile = async (
752752
throw new Error('Invalid chatflowId format - must be a valid UUID')
753753
}
754754

755+
if (!chatId) {
756+
throw new Error('chatId is missing')
757+
}
758+
755759
// Check for path traversal attempts
756760
if (isPathTraversal(chatflowId) || isPathTraversal(chatId)) {
757761
throw new Error('Invalid path characters detected in chatflowId or chatId')
758762
}
759763

764+
if (!fileName || isUnsafeFilePath(fileName)) {
765+
throw new Error('Invalid or unsafe fileName detected')
766+
}
767+
760768
const storageType = getStorageType()
761769
const sanitizedFilename = sanitize(fileName)
770+
771+
if (!sanitizedFilename || sanitizedFilename.includes('/') || sanitizedFilename.includes('\\')) {
772+
throw new Error('Invalid filename after sanitization')
773+
}
774+
762775
if (storageType === 's3') {
763776
const { s3Client, Bucket } = getS3Config()
764777

packages/server/src/controllers/get-upload-file/index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Request, Response, NextFunction } from 'express'
22
import fs from 'fs'
33
import contentDisposition from 'content-disposition'
4-
import { streamStorageFile } from 'flowise-components'
4+
import { isUnsafeFilePath, isValidUUID, streamStorageFile } from 'flowise-components'
55
import { StatusCodes } from 'http-status-codes'
66
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
77
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
@@ -18,6 +18,20 @@ const streamUploadedFile = async (req: Request, res: Response, next: NextFunctio
1818
const fileName = req.query.fileName as string
1919
const download = req.query.download === 'true' // Check if download parameter is set
2020

21+
// Validate input formats to prevent path traversal attacks
22+
if (!chatflowId || !isValidUUID(chatflowId)) {
23+
return res.status(400).send(`Invalid chatflowId format`)
24+
}
25+
26+
if (!chatId) {
27+
return res.status(400).send(`chatId is missing`)
28+
}
29+
30+
// Check for path traversal and unsafe characters in fileName
31+
if (isUnsafeFilePath(fileName)) {
32+
return res.status(400).send(`Invalid path characters detected in filename`)
33+
}
34+
2135
const appServer = getRunningExpressApp()
2236

2337
// This can be public API, so we can only get orgId from the chatflow

0 commit comments

Comments
 (0)