Skip to content

Commit 19ef526

Browse files
authored
fix(webhooks): eliminate redundant DB queries from webhook execution path (#3523)
* fix(webhooks): eliminate redundant DB queries from webhook execution path * chore(webhooks): remove implementation-detail comments * fix(webhooks): restore auth-first ordering and add credential resolution warning - Revert parallel auth+preprocessing to sequential auth→preprocessing to prevent rate-limit exhaustion via unauthenticated requests - Add warning log when credential account resolution fails in background job * fix(webhooks): restore auth-before-reachability ordering and remove dead credentialAccountUserId field - Move reachability test back after auth to prevent path enumeration - Remove dead credentialAccountUserId from WebhookExecutionPayload - Simplify credential resolution condition in background job
1 parent ff2a152 commit 19ef526

File tree

4 files changed

+126
-158
lines changed

4 files changed

+126
-158
lines changed

apps/sim/app/api/webhooks/trigger/[path]/route.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,9 @@ vi.mock('@/lib/webhooks/processor', () => ({
324324
return null
325325
}
326326
),
327-
checkWebhookPreprocessing: vi.fn().mockResolvedValue(null),
327+
checkWebhookPreprocessing: vi
328+
.fn()
329+
.mockResolvedValue({ error: null, actorUserId: 'test-user-id' }),
328330
formatProviderErrorResponse: vi.fn().mockImplementation((_webhook, error, status) => {
329331
const { NextResponse } = require('next/server')
330332
return NextResponse.json({ error }, { status })

apps/sim/app/api/webhooks/trigger/[path]/route.ts

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { generateRequestId } from '@/lib/core/utils/request'
44
import {
55
checkWebhookPreprocessing,
66
findAllWebhooksForPath,
7-
formatProviderErrorResponse,
87
handlePreDeploymentVerification,
98
handleProviderChallenges,
109
handleProviderReachabilityTest,
@@ -82,7 +81,6 @@ export async function POST(
8281
requestId
8382
)
8483
if (authError) {
85-
// For multi-webhook, log and continue to next webhook
8684
if (webhooksForPath.length > 1) {
8785
logger.warn(`[${requestId}] Auth failed for webhook ${foundWebhook.id}, continuing to next`)
8886
continue
@@ -92,39 +90,18 @@ export async function POST(
9290

9391
const reachabilityResponse = handleProviderReachabilityTest(foundWebhook, body, requestId)
9492
if (reachabilityResponse) {
95-
// Reachability test should return immediately for the first webhook
9693
return reachabilityResponse
9794
}
9895

99-
let preprocessError: NextResponse | null = null
100-
try {
101-
preprocessError = await checkWebhookPreprocessing(foundWorkflow, foundWebhook, requestId)
102-
if (preprocessError) {
103-
if (webhooksForPath.length > 1) {
104-
logger.warn(
105-
`[${requestId}] Preprocessing failed for webhook ${foundWebhook.id}, continuing to next`
106-
)
107-
continue
108-
}
109-
return preprocessError
110-
}
111-
} catch (error) {
112-
logger.error(`[${requestId}] Unexpected error during webhook preprocessing`, {
113-
error: error instanceof Error ? error.message : String(error),
114-
stack: error instanceof Error ? error.stack : undefined,
115-
webhookId: foundWebhook.id,
116-
workflowId: foundWorkflow.id,
117-
})
118-
96+
const preprocessResult = await checkWebhookPreprocessing(foundWorkflow, foundWebhook, requestId)
97+
if (preprocessResult.error) {
11998
if (webhooksForPath.length > 1) {
99+
logger.warn(
100+
`[${requestId}] Preprocessing failed for webhook ${foundWebhook.id}, continuing to next`
101+
)
120102
continue
121103
}
122-
123-
return formatProviderErrorResponse(
124-
foundWebhook,
125-
'An unexpected error occurred during preprocessing',
126-
500
127-
)
104+
return preprocessResult.error
128105
}
129106

130107
if (foundWebhook.blockId) {
@@ -152,6 +129,7 @@ export async function POST(
152129
const response = await queueWebhookExecution(foundWebhook, foundWorkflow, body, request, {
153130
requestId,
154131
path,
132+
actorUserId: preprocessResult.actorUserId,
155133
})
156134
responses.push(response)
157135
}

0 commit comments

Comments
 (0)