|
| 1 | +import crypto from 'crypto' |
| 2 | +import { createLogger } from '@sim/logger' |
| 3 | +import { NextResponse } from 'next/server' |
| 4 | +import { safeCompare } from '@/lib/core/security/encryption' |
| 5 | +import type { |
| 6 | + EventMatchContext, |
| 7 | + FormatInputContext, |
| 8 | + FormatInputResult, |
| 9 | + WebhookProviderHandler, |
| 10 | +} from '@/lib/webhooks/providers/types' |
| 11 | +import { createHmacVerifier } from '@/lib/webhooks/providers/utils' |
| 12 | + |
| 13 | +const logger = createLogger('WebhookProvider:Notion') |
| 14 | + |
| 15 | +/** |
| 16 | + * Validates a Notion webhook signature using HMAC SHA-256. |
| 17 | + * Notion sends X-Notion-Signature as "sha256=<hex>". |
| 18 | + */ |
| 19 | +function validateNotionSignature(secret: string, signature: string, body: string): boolean { |
| 20 | + try { |
| 21 | + if (!secret || !signature || !body) { |
| 22 | + logger.warn('Notion signature validation missing required fields', { |
| 23 | + hasSecret: !!secret, |
| 24 | + hasSignature: !!signature, |
| 25 | + hasBody: !!body, |
| 26 | + }) |
| 27 | + return false |
| 28 | + } |
| 29 | + |
| 30 | + const providedHash = signature.startsWith('sha256=') ? signature.slice(7) : signature |
| 31 | + const computedHash = crypto.createHmac('sha256', secret).update(body, 'utf8').digest('hex') |
| 32 | + |
| 33 | + logger.debug('Notion signature comparison', { |
| 34 | + computedSignature: `${computedHash.substring(0, 10)}...`, |
| 35 | + providedSignature: `${providedHash.substring(0, 10)}...`, |
| 36 | + computedLength: computedHash.length, |
| 37 | + providedLength: providedHash.length, |
| 38 | + match: computedHash === providedHash, |
| 39 | + }) |
| 40 | + |
| 41 | + return safeCompare(computedHash, providedHash) |
| 42 | + } catch (error) { |
| 43 | + logger.error('Error validating Notion signature:', error) |
| 44 | + return false |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +export const notionHandler: WebhookProviderHandler = { |
| 49 | + verifyAuth: createHmacVerifier({ |
| 50 | + configKey: 'webhookSecret', |
| 51 | + headerName: 'X-Notion-Signature', |
| 52 | + validateFn: validateNotionSignature, |
| 53 | + providerLabel: 'Notion', |
| 54 | + }), |
| 55 | + |
| 56 | + async formatInput({ body }: FormatInputContext): Promise<FormatInputResult> { |
| 57 | + const b = body as Record<string, unknown> |
| 58 | + return { |
| 59 | + input: { |
| 60 | + id: b.id, |
| 61 | + type: b.type, |
| 62 | + timestamp: b.timestamp, |
| 63 | + workspace_id: b.workspace_id, |
| 64 | + workspace_name: b.workspace_name, |
| 65 | + subscription_id: b.subscription_id, |
| 66 | + integration_id: b.integration_id, |
| 67 | + attempt_number: b.attempt_number, |
| 68 | + authors: b.authors || [], |
| 69 | + entity: b.entity || {}, |
| 70 | + data: b.data || {}, |
| 71 | + }, |
| 72 | + } |
| 73 | + }, |
| 74 | + |
| 75 | + async matchEvent({ webhook, workflow, body, requestId, providerConfig }: EventMatchContext) { |
| 76 | + const triggerId = providerConfig.triggerId as string | undefined |
| 77 | + const obj = body as Record<string, unknown> |
| 78 | + |
| 79 | + if (triggerId && triggerId !== 'notion_webhook') { |
| 80 | + const { isNotionPayloadMatch } = await import('@/triggers/notion/utils') |
| 81 | + if (!isNotionPayloadMatch(triggerId, obj)) { |
| 82 | + const eventType = obj.type as string | undefined |
| 83 | + logger.debug( |
| 84 | + `[${requestId}] Notion event mismatch for trigger ${triggerId}. Event: ${eventType}. Skipping execution.`, |
| 85 | + { |
| 86 | + webhookId: webhook.id, |
| 87 | + workflowId: workflow.id, |
| 88 | + triggerId, |
| 89 | + receivedEvent: eventType, |
| 90 | + } |
| 91 | + ) |
| 92 | + return NextResponse.json({ |
| 93 | + message: 'Event type does not match trigger configuration. Ignoring.', |
| 94 | + }) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return true |
| 99 | + }, |
| 100 | +} |
0 commit comments