Skip to content

Commit 41cdca2

Browse files
committed
address bugbot comments
1 parent cd1ccf1 commit 41cdca2

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

apps/sim/app/api/credentials/[id]/members/route.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { and, eq } from 'drizzle-orm'
55
import { type NextRequest, NextResponse } from 'next/server'
66
import { z } from 'zod'
77
import { getSession } from '@/lib/auth'
8+
import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils'
89

910
const logger = createLogger('CredentialMembersAPI')
1011

@@ -37,7 +38,7 @@ export async function GET(_request: NextRequest, context: RouteContext) {
3738
const { id: credentialId } = await context.params
3839

3940
const [cred] = await db
40-
.select({ id: credential.id })
41+
.select({ id: credential.id, workspaceId: credential.workspaceId })
4142
.from(credential)
4243
.where(eq(credential.id, credentialId))
4344
.limit(1)
@@ -46,6 +47,15 @@ export async function GET(_request: NextRequest, context: RouteContext) {
4647
return NextResponse.json({ members: [] }, { status: 200 })
4748
}
4849

50+
const callerPerm = await getUserEntityPermissions(
51+
session.user.id,
52+
'workspace',
53+
cred.workspaceId
54+
)
55+
if (callerPerm === null) {
56+
return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
57+
}
58+
4959
const members = await db
5060
.select({
5161
id: credentialMember.id,

apps/sim/lib/auth/credential-access.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export async function authorizeCredentialUse(
3939
return { ok: false, error: auth.error || 'Authentication required' }
4040
}
4141

42+
const actingUserId = auth.authType === 'internal_jwt' ? callerUserId : auth.userId
43+
4244
const [workflowContext] = workflowId
4345
? await db
4446
.select({ workspaceId: workflowTable.workspaceId })
@@ -81,12 +83,9 @@ export async function authorizeCredentialUse(
8183
return { ok: false, error: 'Credential account not found' }
8284
}
8385

84-
const effectiveCallerId =
85-
callerUserId || (auth.authType !== 'internal_jwt' ? auth.userId : null)
86-
87-
if (effectiveCallerId) {
86+
if (actingUserId) {
8887
const requesterPerm = await getUserEntityPermissions(
89-
effectiveCallerId,
88+
actingUserId,
9089
'workspace',
9190
platformCredential.workspaceId
9291
)
@@ -97,7 +96,7 @@ export async function authorizeCredentialUse(
9796
.where(
9897
and(
9998
eq(credentialMember.credentialId, platformCredential.id),
100-
eq(credentialMember.userId, effectiveCallerId),
99+
eq(credentialMember.userId, actingUserId),
101100
eq(credentialMember.status, 'active')
102101
)
103102
)
@@ -167,16 +166,14 @@ export async function authorizeCredentialUse(
167166
return { ok: false, error: 'Credential account not found' }
168167
}
169168

170-
const legacyCallerId = callerUserId || (auth.authType !== 'internal_jwt' ? auth.userId : null)
171-
172-
if (legacyCallerId) {
169+
if (actingUserId) {
173170
const [membership] = await db
174171
.select({ id: credentialMember.id })
175172
.from(credentialMember)
176173
.where(
177174
and(
178175
eq(credentialMember.credentialId, workspaceCredential.id),
179-
eq(credentialMember.userId, legacyCallerId),
176+
eq(credentialMember.userId, actingUserId),
180177
eq(credentialMember.status, 'active')
181178
)
182179
)

0 commit comments

Comments
 (0)