Skip to content

Commit 0968598

Browse files
waleedlatif1claude
andcommitted
fix(mcp): tighten 401 detection, hash OAuth state at rest
- Use word-boundary regex for 401 match in form auth heuristic - SHA-256 hash OAuth state in DB; lookup by hash to prevent replay if DB read leaks Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 9671131 commit 0968598

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

apps/sim/app/workspace/[workspaceId]/settings/components/mcp/components/mcp-server-form-modal/mcp-server-form-modal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ export function McpServerFormModal({
520520
if (!connectionResult.success) {
521521
const errorText = (connectionResult.error || '').toLowerCase()
522522
const looksLikeAuthRequired =
523-
errorText.includes('401') ||
523+
/\b401\b/.test(errorText) ||
524524
errorText.includes('unauthorized') ||
525525
errorText.includes('oauth') ||
526526
errorText.includes('authentication')

apps/sim/lib/mcp/oauth/storage.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createHash } from 'node:crypto'
12
import type {
23
OAuthClientInformationMixed,
34
OAuthTokens,
@@ -8,6 +9,10 @@ import { generateId } from '@sim/utils/id'
89
import { and, eq } from 'drizzle-orm'
910
import { decryptSecret, encryptSecret } from '@/lib/core/security/encryption'
1011

12+
function hashState(state: string): string {
13+
return createHash('sha256').update(state).digest('hex')
14+
}
15+
1116
export interface McpOauthRow {
1217
id: string
1318
mcpServerId: string
@@ -107,7 +112,7 @@ export async function loadOauthRowByState(state: string): Promise<McpOauthRow |
107112
const [row] = await db
108113
.select()
109114
.from(mcpServerOauth)
110-
.where(eq(mcpServerOauth.state, state))
115+
.where(eq(mcpServerOauth.state, hashState(state)))
111116
.limit(1)
112117
if (!row) return null
113118
return {
@@ -154,7 +159,7 @@ export async function saveCodeVerifier(rowId: string, verifier: string): Promise
154159
export async function saveState(rowId: string, state: string): Promise<void> {
155160
await db
156161
.update(mcpServerOauth)
157-
.set({ state, updatedAt: new Date() })
162+
.set({ state: hashState(state), updatedAt: new Date() })
158163
.where(eq(mcpServerOauth.id, rowId))
159164
}
160165

0 commit comments

Comments
 (0)