Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 86 additions & 6 deletions apps/sim/lib/webhooks/processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -38,11 +38,13 @@ type WebhookLookupRow = {
workflow: Pick<WorkflowRecord, 'id'>
}

const { mockEnqueue, mockExecuteWebhookJob, mockProviderHandler } = vi.hoisted(() => ({
mockEnqueue: vi.fn(),
mockExecuteWebhookJob: vi.fn().mockResolvedValue({ success: true }),
mockProviderHandler: { current: {} as Record<string, unknown> },
}))
const { mockEnqueue, mockExecuteWebhookJob, mockProviderHandler, mockResolveBackgroundWebhookEnv } =
vi.hoisted(() => ({
mockEnqueue: vi.fn(),
mockResolveBackgroundWebhookEnv: vi.fn(),
mockExecuteWebhookJob: vi.fn().mockResolvedValue({ success: true }),
mockProviderHandler: { current: {} as Record<string, unknown> },
}))

const mockPreprocessExecution = executionPreprocessingMockFns.mockPreprocessExecution

Expand Down Expand Up @@ -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,
}))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<string, unknown> }) =>
request.headers.get('authorization') === `Bearer ${providerConfig.token}`
? null
: new NextResponse('Unauthorized', { status: 401 })
)

const deliver = (
bearer: string,
providerConfig: Record<string, unknown> = { 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<typeof import('@/executor/utils/reference-validation')>(
'@/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)
})
})
94 changes: 66 additions & 28 deletions apps/sim/lib/webhooks/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -530,10 +530,6 @@ export async function findWebhooksByRoutingKey(
return results
}

function resolveEnvVars(value: string, envVars: Record<string, string>): 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<string, unknown>): boolean {
for (const value of Object.values(config)) {
Expand All @@ -544,24 +540,46 @@ function providerConfigReferencesEnvVars(config: Record<string, unknown>): 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<string, unknown>,
envVars: Record<string, string>
): Record<string, unknown> {
): { config: Record<string, unknown>; unresolvedReads: Set<string> } {
const resolved: Record<string, unknown> = {}
const unresolvedReads = new Set<string>()
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,
Expand All @@ -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<string, string> = {}
if (handler.verifyAuth && providerConfigReferencesEnvVars(rawProviderConfig)) {
let envVars: Record<string, string> = {}
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
Expand Down
Loading