@@ -24,10 +24,10 @@ import {
2424 isPayloadSizeLimitError ,
2525 readStreamToBufferWithLimit ,
2626} from '@/lib/core/utils/stream-limits'
27- import { getEffectiveDecryptedEnv } from '@/lib/environment/utils'
2827import { preprocessExecution } from '@/lib/execution/preprocessing'
2928import { WEBHOOK_MAX_BODY_BYTES } from '@/lib/webhooks/constants'
3029import { deliverableWebhookPredicate } from '@/lib/webhooks/delivery-predicate'
30+ import { resolveBackgroundWebhookEnv } from '@/lib/webhooks/env-resolver'
3131import { createWebhookExecutionPrincipal } from '@/lib/webhooks/execution-principal'
3232import {
3333 getPendingWebhookVerification ,
@@ -530,10 +530,6 @@ export async function findWebhooksByRoutingKey(
530530 return results
531531}
532532
533- function resolveEnvVars ( value : string , envVars : Record < string , string > ) : string {
534- return resolveEnvVarReferences ( value , envVars ) as string
535- }
536-
537533/** True when any string value in the provider config contains an env-var reference (`{{VAR}}`). */
538534function providerConfigReferencesEnvVars ( config : Record < string , unknown > ) : boolean {
539535 for ( const value of Object . values ( config ) ) {
@@ -544,24 +540,46 @@ function providerConfigReferencesEnvVars(config: Record<string, unknown>): boole
544540 return false
545541}
546542
547- function resolveProviderConfigEnvVars (
543+ /**
544+ * Resolves the env-var references in the provider config's top-level string values for auth.
545+ *
546+ * A value whose reference has no variable keeps its literal `{{VAR}}` text and is exposed through
547+ * a getter that records the read in `unresolvedReads`. Fields auth never reads, like a response
548+ * body that legitimately contains braces, cannot affect the outcome; a handler that enumerates
549+ * the config reads every field, so it errs toward rejecting.
550+ */
551+ function resolveAuthProviderConfig (
548552 config : Record < string , unknown > ,
549553 envVars : Record < string , string >
550- ) : Record < string , unknown > {
554+ ) : { config : Record < string , unknown > ; unresolvedReads : Set < string > } {
551555 const resolved : Record < string , unknown > = { }
556+ const unresolvedReads = new Set < string > ( )
552557 for ( const [ key , value ] of Object . entries ( config ) ) {
553- if ( typeof value === 'string' ) {
554- resolved [ key ] = resolveEnvVars ( value , envVars )
555- } else {
556- resolved [ key ] = value
558+ const missingKeys : string [ ] = [ ]
559+ const resolvedValue =
560+ typeof value === 'string' ? resolveEnvVarReferences ( value , envVars , { missingKeys } ) : value
561+ if ( missingKeys . length === 0 ) {
562+ resolved [ key ] = resolvedValue
563+ continue
557564 }
565+ Object . defineProperty ( resolved , key , {
566+ enumerable : true ,
567+ get : ( ) => {
568+ unresolvedReads . add ( key )
569+ return resolvedValue
570+ } ,
571+ } )
558572 }
559- return resolved
573+ return { config : resolved , unresolvedReads }
560574}
561575
562576/**
563577 * Verify webhook provider authentication and signatures.
564578 * Delegates to the provider handler registry.
579+ *
580+ * Fails closed when the env vars the config references cannot be loaded, or when the handler's
581+ * decision read a field whose reference did not resolve: that field still holds its literal
582+ * `{{VAR}}` text, which would make a guessable placeholder the expected token or signing key.
565583 */
566584export async function verifyProviderAuth (
567585 foundWebhook : WebhookRecord ,
@@ -575,37 +593,57 @@ export async function verifyProviderAuth(
575593 }
576594
577595 const handler = getProviderHandler ( foundWebhook . provider )
596+ if ( ! handler . verifyAuth ) return null
597+
578598 const rawProviderConfig = parseProviderConfig ( foundWebhook . providerConfig )
579599
580600 /**
581- * Only fetch + decrypt the effective env when there is auth to verify AND the
582- * provider config actually references env vars (`{{VAR}}`). This avoids a DB
583- * read and decryption on the synchronous pre-ack path for the common case.
601+ * Only fetch + decrypt the env when the provider config actually references env vars
602+ * (`{{VAR}}`). This avoids a DB read and decryption on the synchronous pre-ack path for the
603+ * common case.
584604 */
585- let decryptedEnvVars : Record < string , string > = { }
586- if ( handler . verifyAuth && providerConfigReferencesEnvVars ( rawProviderConfig ) ) {
605+ let envVars : Record < string , string > = { }
606+ if ( providerConfigReferencesEnvVars ( rawProviderConfig ) ) {
587607 try {
588- decryptedEnvVars = await getEffectiveDecryptedEnv (
608+ envVars = await resolveBackgroundWebhookEnv (
589609 foundWorkflow . userId ,
590610 foundWorkflow . workspaceId ?? undefined
591611 )
592612 } catch ( error ) {
593- logger . error ( 'Failed to fetch environment variables' , { requestId, error } )
613+ logger . error ( 'Failed to load environment variables for webhook auth' , {
614+ requestId,
615+ webhookId : foundWebhook . id ,
616+ error,
617+ } )
618+ return NextResponse . json (
619+ { error : 'Failed to verify webhook authentication' } ,
620+ { status : 500 }
621+ )
594622 }
595623 }
596624
597- const providerConfig = resolveProviderConfigEnvVars ( rawProviderConfig , decryptedEnvVars )
625+ const { config : providerConfig , unresolvedReads } = resolveAuthProviderConfig (
626+ rawProviderConfig ,
627+ envVars
628+ )
629+
630+ const authResult = await handler . verifyAuth ( {
631+ webhook : { ...foundWebhook } ,
632+ workflow : { ...foundWorkflow } ,
633+ request,
634+ rawBody,
635+ requestId,
636+ providerConfig,
637+ } )
638+ if ( authResult ) return authResult
598639
599- if ( handler . verifyAuth ) {
600- const authResult = await handler . verifyAuth ( {
601- webhook : { ...foundWebhook } ,
602- workflow : { ...foundWorkflow } ,
603- request,
604- rawBody,
640+ if ( unresolvedReads . size > 0 ) {
641+ logger . warn ( 'Webhook auth rejected: it read config fields with unresolved env vars' , {
605642 requestId,
606- providerConfig,
643+ webhookId : foundWebhook . id ,
644+ fields : [ ...unresolvedReads ] ,
607645 } )
608- if ( authResult ) return authResult
646+ return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
609647 }
610648
611649 return null
0 commit comments