Skip to content

Commit f7d8691

Browse files
christopherholland-workdaychristopherholland-workday
andauthored
Fix Chatflow Query to Protect Against Cross-Workspace Chatflow Disclosure (#6043)
Co-authored-by: christopherholland-workday <christopher.holland+evisort@workday.com>
1 parent 97b96f6 commit f7d8691

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

packages/server/src/controllers/chatflows/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ const getChatflowByApiKey = async (req: Request, res: Response, next: NextFuncti
9999
if (!apikey) {
100100
return res.status(401).send('Unauthorized')
101101
}
102-
const apiResponse = await chatflowsService.getChatflowByApiKey(apikey.id, req.query.keyonly)
102+
const apiResponse = await chatflowsService.getChatflowByApiKey(apikey.id, apikey.workspaceId, req.query.keyonly)
103103
return res.json(apiResponse)
104104
} catch (error) {
105105
next(error)

packages/server/src/services/chatflows/index.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ICommonObject, removeFolderFromStorage } from 'flowise-components'
22
import { StatusCodes } from 'http-status-codes'
3-
import { In } from 'typeorm'
3+
import { Brackets, In } from 'typeorm'
44
import { validate as isValidUUID } from 'uuid'
55
import { ChatflowType, IReactFlowObject } from '../../Interface'
66
import { FLOWISE_COUNTER_STATUS, FLOWISE_METRIC_COUNTERS } from '../../Interface.Metrics'
@@ -220,16 +220,21 @@ const getAllChatflowsCount = async (type?: ChatflowType, workspaceId?: string):
220220
}
221221
}
222222

223-
const getChatflowByApiKey = async (apiKeyId: string, keyonly?: unknown): Promise<any> => {
223+
const getChatflowByApiKey = async (apiKeyId: string, workspaceId: string, keyonly?: unknown): Promise<any> => {
224224
try {
225225
// Here we only get chatflows that are bounded by the apikeyid and chatflows that are not bounded by any apikey
226226
const appServer = getRunningExpressApp()
227227
let query = appServer.AppDataSource.getRepository(ChatFlow)
228228
.createQueryBuilder('cf')
229-
.where('cf.apikeyid = :apikeyid', { apikeyid: apiKeyId })
230-
if (keyonly === undefined) {
231-
query = query.orWhere('cf.apikeyid IS NULL').orWhere('cf.apikeyid = ""')
232-
}
229+
.where('cf.workspaceId = :workspaceId', { workspaceId })
230+
.andWhere(
231+
new Brackets((qb) => {
232+
qb.where('cf.apikeyid = :apikeyid', { apikeyid: apiKeyId })
233+
if (keyonly === undefined) {
234+
qb.orWhere('cf.apikeyid IS NULL').orWhere('cf.apikeyid = ""')
235+
}
236+
})
237+
)
233238

234239
const dbResponse = await query.orderBy('cf.name', 'ASC').getMany()
235240
if (dbResponse.length < 1) {

0 commit comments

Comments
 (0)