From 9d1313dc184eb4b42ef50063abc5f0580b9cbc0a Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Sat, 14 Nov 2026 14:06:16 -0800 Subject: [PATCH] fix(webhooks): reject auth that reads an unresolved env-var secret verifyProviderAuth resolved {{VAR}} references in the provider config with the keep-literal default and swallowed env-load failures, so a secret whose variable was missing or unreadable became its literal, guessable placeholder as the expected token or signing key. - env-load failure now returns 500 instead of continuing with an empty env - a delivery is rejected (401) when the handler's auth decision read a field whose reference did not resolve; free-text fields auth never reads (generic responseBody, TwiML) keep working with literal braces - env is read through resolveBackgroundWebhookEnv, matching execution's owner/billing-account identity split --- apps/sim/lib/webhooks/processor.test.ts | 92 ++++++++++++++++++++++-- apps/sim/lib/webhooks/processor.ts | 94 +++++++++++++++++-------- 2 files changed, 152 insertions(+), 34 deletions(-) diff --git a/apps/sim/lib/webhooks/processor.test.ts b/apps/sim/lib/webhooks/processor.test.ts index 36e76c349f3..5760da30f32 100644 --- a/apps/sim/lib/webhooks/processor.test.ts +++ b/apps/sim/lib/webhooks/processor.test.ts @@ -24,7 +24,7 @@ import { idMock, idMockFns } from '@sim/testing/mocks/id.mock' import { createMockRedis } from '@sim/testing/mocks/redis.mock' import { redisConfigMockFns, resetRedisConfigMock } from '@sim/testing/mocks/redis-config.mock' import { NextRequest, NextResponse } from 'next/server' -import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest' +import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' import { ADMISSION_REJECTION_CODE } from '@/lib/core/admission/rejection' import { ADMISSION_ERROR_CODE, @@ -38,11 +38,13 @@ type WebhookLookupRow = { workflow: Pick } -const { mockEnqueue, mockExecuteWebhookJob, mockProviderHandler } = vi.hoisted(() => ({ - mockEnqueue: vi.fn(), - mockExecuteWebhookJob: vi.fn().mockResolvedValue({ success: true }), - mockProviderHandler: { current: {} as Record }, -})) +const { mockEnqueue, mockExecuteWebhookJob, mockProviderHandler, mockResolveBackgroundWebhookEnv } = + vi.hoisted(() => ({ + mockEnqueue: vi.fn(), + mockResolveBackgroundWebhookEnv: vi.fn(), + mockExecuteWebhookJob: vi.fn().mockResolvedValue({ success: true }), + mockProviderHandler: { current: {} as Record }, + })) const mockPreprocessExecution = executionPreprocessingMockFns.mockPreprocessExecution @@ -85,6 +87,10 @@ vi.mock('@/lib/webhooks/providers', () => ({ getProviderHandler: vi.fn(() => mockProviderHandler.current), })) +vi.mock('@/lib/webhooks/env-resolver', () => ({ + resolveBackgroundWebhookEnv: mockResolveBackgroundWebhookEnv, +})) + vi.mock('@/background/webhook-execution', () => ({ executeWebhookJob: mockExecuteWebhookJob, })) @@ -118,7 +124,9 @@ import { handleWebhookEventFilter, parseWebhookBody, processPolledWebhookEvent, + verifyProviderAuth, } from '@/lib/webhooks/processor' +import { resolveEnvVarReferences } from '@/executor/utils/reference-validation' const mockGetInlineJobQueue = asyncJobsMockFns.mockGetInlineJobQueue const mockGetJobQueue = asyncJobsMockFns.mockGetJobQueue @@ -795,3 +803,75 @@ describe('handleProviderChallenges method gating', () => { expect(handleChallenge).not.toHaveBeenCalled() }) }) + +describe('verifyProviderAuth env-referenced secrets', () => { + const PLACEHOLDER = '{{WEBHOOK_SECRET}}' + + const verifyAuth = vi.fn( + ({ request, providerConfig }: { request: Request; providerConfig: Record }) => + request.headers.get('authorization') === `Bearer ${providerConfig.token}` + ? null + : new NextResponse('Unauthorized', { status: 401 }) + ) + + const deliver = ( + bearer: string, + providerConfig: Record = { token: PLACEHOLDER } + ) => + verifyProviderAuth( + { id: 'webhook-1', provider: 'generic', providerConfig } as never, + { id: 'workflow-1', userId: 'owner-1', workspaceId: 'workspace-1' } as never, + new NextRequest('http://localhost:3000/api/webhooks/trigger/abc', { + method: 'POST', + headers: { authorization: `Bearer ${bearer}` }, + }), + '{}', + 'req-1' + ) + + beforeAll(async () => { + const actual = await vi.importActual( + '@/executor/utils/reference-validation' + ) + vi.mocked(resolveEnvVarReferences).mockImplementation(actual.resolveEnvVarReferences) + }) + + afterAll(() => { + vi.mocked(resolveEnvVarReferences).mockImplementation((value) => value) + }) + + beforeEach(() => { + verifyAuth.mockClear() + mockResolveBackgroundWebhookEnv.mockReset() + mockProviderHandler.current = { verifyAuth } + }) + + it('authenticates with the resolved secret, read through the background webhook env', async () => { + mockResolveBackgroundWebhookEnv.mockImplementation(async (userId, workspaceId) => + userId === 'owner-1' && workspaceId === 'workspace-1' + ? { WEBHOOK_SECRET: 'correct-horse' } + : {} + ) + + await expect(deliver('correct-horse')).resolves.toBeNull() + }) + + it('rejects the literal placeholder when the referenced variable no longer exists', async () => { + mockResolveBackgroundWebhookEnv.mockResolvedValue({}) + + expect((await deliver(PLACEHOLDER))?.status).toBe(401) + }) + + it('ignores unresolved braces in a field auth never reads', async () => { + mockResolveBackgroundWebhookEnv.mockResolvedValue({ WEBHOOK_SECRET: 'correct-horse' }) + + const providerConfig = { token: PLACEHOLDER, responseBody: '{"greeting": "{{name}}"}' } + await expect(deliver('correct-horse', providerConfig)).resolves.toBeNull() + }) + + it('rejects the literal placeholder when the env cannot be loaded', async () => { + mockResolveBackgroundWebhookEnv.mockRejectedValue(new Error('Access denied to workspace')) + + expect((await deliver(PLACEHOLDER))?.status).toBe(500) + }) +}) diff --git a/apps/sim/lib/webhooks/processor.ts b/apps/sim/lib/webhooks/processor.ts index 6dfd7a55199..0c640cfd6f1 100644 --- a/apps/sim/lib/webhooks/processor.ts +++ b/apps/sim/lib/webhooks/processor.ts @@ -24,10 +24,10 @@ import { isPayloadSizeLimitError, readStreamToBufferWithLimit, } from '@/lib/core/utils/stream-limits' -import { getEffectiveDecryptedEnv } from '@/lib/environment/utils' import { preprocessExecution } from '@/lib/execution/preprocessing' import { WEBHOOK_MAX_BODY_BYTES } from '@/lib/webhooks/constants' import { deliverableWebhookPredicate } from '@/lib/webhooks/delivery-predicate' +import { resolveBackgroundWebhookEnv } from '@/lib/webhooks/env-resolver' import { createWebhookExecutionPrincipal } from '@/lib/webhooks/execution-principal' import { getPendingWebhookVerification, @@ -530,10 +530,6 @@ export async function findWebhooksByRoutingKey( return results } -function resolveEnvVars(value: string, envVars: Record): string { - return resolveEnvVarReferences(value, envVars) as string -} - /** True when any string value in the provider config contains an env-var reference (`{{VAR}}`). */ function providerConfigReferencesEnvVars(config: Record): boolean { for (const value of Object.values(config)) { @@ -544,24 +540,46 @@ function providerConfigReferencesEnvVars(config: Record): boole return false } -function resolveProviderConfigEnvVars( +/** + * Resolves the env-var references in the provider config's top-level string values for auth. + * + * A value whose reference has no variable keeps its literal `{{VAR}}` text and is exposed through + * a getter that records the read in `unresolvedReads`. Fields auth never reads, like a response + * body that legitimately contains braces, cannot affect the outcome; a handler that enumerates + * the config reads every field, so it errs toward rejecting. + */ +function resolveAuthProviderConfig( config: Record, envVars: Record -): Record { +): { config: Record; unresolvedReads: Set } { const resolved: Record = {} + const unresolvedReads = new Set() for (const [key, value] of Object.entries(config)) { - if (typeof value === 'string') { - resolved[key] = resolveEnvVars(value, envVars) - } else { - resolved[key] = value + const missingKeys: string[] = [] + const resolvedValue = + typeof value === 'string' ? resolveEnvVarReferences(value, envVars, { missingKeys }) : value + if (missingKeys.length === 0) { + resolved[key] = resolvedValue + continue } + Object.defineProperty(resolved, key, { + enumerable: true, + get: () => { + unresolvedReads.add(key) + return resolvedValue + }, + }) } - return resolved + return { config: resolved, unresolvedReads } } /** * Verify webhook provider authentication and signatures. * Delegates to the provider handler registry. + * + * Fails closed when the env vars the config references cannot be loaded, or when the handler's + * decision read a field whose reference did not resolve: that field still holds its literal + * `{{VAR}}` text, which would make a guessable placeholder the expected token or signing key. */ export async function verifyProviderAuth( foundWebhook: WebhookRecord, @@ -575,37 +593,57 @@ export async function verifyProviderAuth( } const handler = getProviderHandler(foundWebhook.provider) + if (!handler.verifyAuth) return null + const rawProviderConfig = parseProviderConfig(foundWebhook.providerConfig) /** - * Only fetch + decrypt the effective env when there is auth to verify AND the - * provider config actually references env vars (`{{VAR}}`). This avoids a DB - * read and decryption on the synchronous pre-ack path for the common case. + * Only fetch + decrypt the env when the provider config actually references env vars + * (`{{VAR}}`). This avoids a DB read and decryption on the synchronous pre-ack path for the + * common case. */ - let decryptedEnvVars: Record = {} - if (handler.verifyAuth && providerConfigReferencesEnvVars(rawProviderConfig)) { + let envVars: Record = {} + if (providerConfigReferencesEnvVars(rawProviderConfig)) { try { - decryptedEnvVars = await getEffectiveDecryptedEnv( + envVars = await resolveBackgroundWebhookEnv( foundWorkflow.userId, foundWorkflow.workspaceId ?? undefined ) } catch (error) { - logger.error('Failed to fetch environment variables', { requestId, error }) + logger.error('Failed to load environment variables for webhook auth', { + requestId, + webhookId: foundWebhook.id, + error, + }) + return NextResponse.json( + { error: 'Failed to verify webhook authentication' }, + { status: 500 } + ) } } - const providerConfig = resolveProviderConfigEnvVars(rawProviderConfig, decryptedEnvVars) + const { config: providerConfig, unresolvedReads } = resolveAuthProviderConfig( + rawProviderConfig, + envVars + ) + + const authResult = await handler.verifyAuth({ + webhook: { ...foundWebhook }, + workflow: { ...foundWorkflow }, + request, + rawBody, + requestId, + providerConfig, + }) + if (authResult) return authResult - if (handler.verifyAuth) { - const authResult = await handler.verifyAuth({ - webhook: { ...foundWebhook }, - workflow: { ...foundWorkflow }, - request, - rawBody, + if (unresolvedReads.size > 0) { + logger.warn('Webhook auth rejected: it read config fields with unresolved env vars', { requestId, - providerConfig, + webhookId: foundWebhook.id, + fields: [...unresolvedReads], }) - if (authResult) return authResult + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } return null