Skip to content

Commit a030329

Browse files
fix(kb): auth check for create doc tool (#687)
1 parent 1420f48 commit a030329

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

apps/sim/app/api/knowledge/[id]/documents/route.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { type NextRequest, NextResponse } from 'next/server'
44
import { z } from 'zod'
55
import { getSession } from '@/lib/auth'
66
import { createLogger } from '@/lib/logs/console-logger'
7+
import { getUserId } from '@/app/api/auth/oauth/utils'
78
import { db } from '@/db'
89
import { document } from '@/db/schema'
910
import { checkKnowledgeBaseAccess, processDocumentAsync } from '../../utils'
@@ -269,27 +270,41 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
269270
const { id: knowledgeBaseId } = await params
270271

271272
try {
272-
const session = await getSession()
273-
if (!session?.user?.id) {
274-
logger.warn(`[${requestId}] Unauthorized document creation attempt`)
275-
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
273+
const body = await req.json()
274+
const { workflowId } = body
275+
276+
logger.info(`[${requestId}] Knowledge base document creation request`, {
277+
knowledgeBaseId,
278+
workflowId,
279+
hasWorkflowId: !!workflowId,
280+
bodyKeys: Object.keys(body),
281+
})
282+
283+
const userId = await getUserId(requestId, workflowId)
284+
285+
if (!userId) {
286+
const errorMessage = workflowId ? 'Workflow not found' : 'Unauthorized'
287+
const statusCode = workflowId ? 404 : 401
288+
logger.warn(`[${requestId}] Authentication failed: ${errorMessage}`, {
289+
workflowId,
290+
hasWorkflowId: !!workflowId,
291+
})
292+
return NextResponse.json({ error: errorMessage }, { status: statusCode })
276293
}
277294

278-
const accessCheck = await checkKnowledgeBaseAccess(knowledgeBaseId, session.user.id)
295+
const accessCheck = await checkKnowledgeBaseAccess(knowledgeBaseId, userId)
279296

280297
if (!accessCheck.hasAccess) {
281298
if ('notFound' in accessCheck && accessCheck.notFound) {
282299
logger.warn(`[${requestId}] Knowledge base not found: ${knowledgeBaseId}`)
283300
return NextResponse.json({ error: 'Knowledge base not found' }, { status: 404 })
284301
}
285302
logger.warn(
286-
`[${requestId}] User ${session.user.id} attempted to create document in unauthorized knowledge base ${knowledgeBaseId}`
303+
`[${requestId}] User ${userId} attempted to create document in unauthorized knowledge base ${knowledgeBaseId}`
287304
)
288305
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
289306
}
290307

291-
const body = await req.json()
292-
293308
// Check if this is a bulk operation
294309
if (body.bulk === true) {
295310
// Handle bulk processing (replaces process-documents endpoint)

apps/sim/tools/knowledge/create_document.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export const knowledgeCreateDocumentTool: ToolConfig<any, KnowledgeCreateDocumen
6565
'Content-Type': 'application/json',
6666
}),
6767
body: (params) => {
68+
const workflowId = params._context?.workflowId
6869
const textContent = params.content?.trim()
6970
const documentName = params.name?.trim()
7071

@@ -111,7 +112,7 @@ export const knowledgeCreateDocumentTool: ToolConfig<any, KnowledgeCreateDocumen
111112
},
112113
]
113114

114-
return {
115+
const requestBody = {
115116
documents: documents,
116117
processingOptions: {
117118
chunkSize: 1024,
@@ -121,7 +122,10 @@ export const knowledgeCreateDocumentTool: ToolConfig<any, KnowledgeCreateDocumen
121122
lang: 'en',
122123
},
123124
bulk: true,
125+
...(workflowId && { workflowId }),
124126
}
127+
128+
return requestBody
125129
},
126130
isInternalRoute: true,
127131
},

0 commit comments

Comments
 (0)