-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat(triggers): add Notion webhook triggers #3989
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
51236f0
feat(triggers): add Notion webhook triggers for all event types
waleedlatif1 a4eef8b
fix(triggers): resolve type field collision in Notion trigger outputs
waleedlatif1 0a77dad
fix(triggers): clarify Notion webhook signing secret vs verification_…
waleedlatif1 d2719f5
refactor(webhooks): use createHmacVerifier for Notion provider handler
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import crypto from 'crypto' | ||
| import { createLogger } from '@sim/logger' | ||
| import { NextResponse } from 'next/server' | ||
| import { safeCompare } from '@/lib/core/security/encryption' | ||
| import type { | ||
| AuthContext, | ||
| EventMatchContext, | ||
| FormatInputContext, | ||
| FormatInputResult, | ||
| WebhookProviderHandler, | ||
| } from '@/lib/webhooks/providers/types' | ||
|
|
||
| const logger = createLogger('WebhookProvider:Notion') | ||
|
|
||
| /** | ||
| * Validates a Notion webhook signature using HMAC SHA-256. | ||
| * Notion sends X-Notion-Signature as "sha256=<hex>". | ||
| */ | ||
| function validateNotionSignature(secret: string, signature: string, body: string): boolean { | ||
| try { | ||
| if (!secret || !signature || !body) { | ||
| logger.warn('Notion signature validation missing required fields', { | ||
| hasSecret: !!secret, | ||
| hasSignature: !!signature, | ||
| hasBody: !!body, | ||
| }) | ||
| return false | ||
| } | ||
|
|
||
| const providedHash = signature.startsWith('sha256=') ? signature.slice(7) : signature | ||
| const computedHash = crypto.createHmac('sha256', secret).update(body, 'utf8').digest('hex') | ||
|
|
||
| logger.debug('Notion signature comparison', { | ||
| computedSignature: `${computedHash.substring(0, 10)}...`, | ||
| providedSignature: `${providedHash.substring(0, 10)}...`, | ||
| computedLength: computedHash.length, | ||
| providedLength: providedHash.length, | ||
| match: computedHash === providedHash, | ||
| }) | ||
|
|
||
| return safeCompare(computedHash, providedHash) | ||
| } catch (error) { | ||
| logger.error('Error validating Notion signature:', error) | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| export const notionHandler: WebhookProviderHandler = { | ||
| verifyAuth({ request, rawBody, requestId, providerConfig }: AuthContext) { | ||
| const secret = providerConfig.webhookSecret as string | undefined | ||
| if (!secret) { | ||
| return null | ||
| } | ||
|
|
||
| const signature = request.headers.get('X-Notion-Signature') | ||
| if (!signature) { | ||
| logger.warn(`[${requestId}] Notion webhook missing signature header`) | ||
| return new NextResponse('Unauthorized - Missing Notion signature', { status: 401 }) | ||
| } | ||
|
|
||
| if (!validateNotionSignature(secret, signature, rawBody)) { | ||
| logger.warn(`[${requestId}] Notion signature verification failed`, { | ||
| signatureLength: signature.length, | ||
| secretLength: secret.length, | ||
| }) | ||
| return new NextResponse('Unauthorized - Invalid Notion signature', { status: 401 }) | ||
| } | ||
|
|
||
| return null | ||
| }, | ||
|
|
||
| async formatInput({ body }: FormatInputContext): Promise<FormatInputResult> { | ||
| const b = body as Record<string, unknown> | ||
| return { | ||
| input: { | ||
| id: b.id, | ||
| type: b.type, | ||
| timestamp: b.timestamp, | ||
| workspace_id: b.workspace_id, | ||
| workspace_name: b.workspace_name, | ||
| subscription_id: b.subscription_id, | ||
| integration_id: b.integration_id, | ||
| attempt_number: b.attempt_number, | ||
| authors: b.authors || [], | ||
| entity: b.entity || {}, | ||
| data: b.data || {}, | ||
| }, | ||
| } | ||
| }, | ||
|
|
||
| async matchEvent({ webhook, workflow, body, requestId, providerConfig }: EventMatchContext) { | ||
| const triggerId = providerConfig.triggerId as string | undefined | ||
| const obj = body as Record<string, unknown> | ||
|
|
||
| if (triggerId && triggerId !== 'notion_webhook') { | ||
| const { isNotionPayloadMatch } = await import('@/triggers/notion/utils') | ||
| if (!isNotionPayloadMatch(triggerId, obj)) { | ||
| const eventType = obj.type as string | undefined | ||
| logger.debug( | ||
| `[${requestId}] Notion event mismatch for trigger ${triggerId}. Event: ${eventType}. Skipping execution.`, | ||
| { | ||
| webhookId: webhook.id, | ||
| workflowId: workflow.id, | ||
| triggerId, | ||
| receivedEvent: eventType, | ||
| } | ||
| ) | ||
| return NextResponse.json({ | ||
| message: 'Event type does not match trigger configuration. Ignoring.', | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| return true | ||
| }, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { NotionIcon } from '@/components/icons' | ||
| import { buildTriggerSubBlocks } from '@/triggers' | ||
| import { | ||
| buildCommentEventOutputs, | ||
| buildNotionExtraFields, | ||
| notionSetupInstructions, | ||
| notionTriggerOptions, | ||
| } from '@/triggers/notion/utils' | ||
| import type { TriggerConfig } from '@/triggers/types' | ||
|
|
||
| /** | ||
| * Notion Comment Created Trigger | ||
| */ | ||
| export const notionCommentCreatedTrigger: TriggerConfig = { | ||
| id: 'notion_comment_created', | ||
| name: 'Notion Comment Created', | ||
| provider: 'notion', | ||
| description: 'Trigger workflow when a comment or suggested edit is added in Notion', | ||
| version: '1.0.0', | ||
| icon: NotionIcon, | ||
|
|
||
| subBlocks: buildTriggerSubBlocks({ | ||
| triggerId: 'notion_comment_created', | ||
| triggerOptions: notionTriggerOptions, | ||
| setupInstructions: notionSetupInstructions('comment.created'), | ||
| extraFields: buildNotionExtraFields('notion_comment_created'), | ||
| }), | ||
|
|
||
| outputs: buildCommentEventOutputs(), | ||
|
|
||
| webhook: { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'X-Notion-Signature': 'sha256=...', | ||
| }, | ||
| }, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { NotionIcon } from '@/components/icons' | ||
| import { buildTriggerSubBlocks } from '@/triggers' | ||
| import { | ||
| buildDatabaseEventOutputs, | ||
| buildNotionExtraFields, | ||
| notionSetupInstructions, | ||
| notionTriggerOptions, | ||
| } from '@/triggers/notion/utils' | ||
| import type { TriggerConfig } from '@/triggers/types' | ||
|
|
||
| /** | ||
| * Notion Database Created Trigger | ||
| */ | ||
| export const notionDatabaseCreatedTrigger: TriggerConfig = { | ||
| id: 'notion_database_created', | ||
| name: 'Notion Database Created', | ||
| provider: 'notion', | ||
| description: 'Trigger workflow when a new database is created in Notion', | ||
| version: '1.0.0', | ||
| icon: NotionIcon, | ||
|
|
||
| subBlocks: buildTriggerSubBlocks({ | ||
| triggerId: 'notion_database_created', | ||
| triggerOptions: notionTriggerOptions, | ||
| setupInstructions: notionSetupInstructions('database.created'), | ||
| extraFields: buildNotionExtraFields('notion_database_created'), | ||
| }), | ||
|
|
||
| outputs: buildDatabaseEventOutputs(), | ||
|
|
||
| webhook: { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'X-Notion-Signature': 'sha256=...', | ||
| }, | ||
| }, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { NotionIcon } from '@/components/icons' | ||
| import { buildTriggerSubBlocks } from '@/triggers' | ||
| import { | ||
| buildDatabaseEventOutputs, | ||
| buildNotionExtraFields, | ||
| notionSetupInstructions, | ||
| notionTriggerOptions, | ||
| } from '@/triggers/notion/utils' | ||
| import type { TriggerConfig } from '@/triggers/types' | ||
|
|
||
| /** | ||
| * Notion Database Deleted Trigger | ||
| */ | ||
| export const notionDatabaseDeletedTrigger: TriggerConfig = { | ||
| id: 'notion_database_deleted', | ||
| name: 'Notion Database Deleted', | ||
| provider: 'notion', | ||
| description: 'Trigger workflow when a database is deleted in Notion', | ||
| version: '1.0.0', | ||
| icon: NotionIcon, | ||
|
|
||
| subBlocks: buildTriggerSubBlocks({ | ||
| triggerId: 'notion_database_deleted', | ||
| triggerOptions: notionTriggerOptions, | ||
| setupInstructions: notionSetupInstructions('database.deleted'), | ||
| extraFields: buildNotionExtraFields('notion_database_deleted'), | ||
| }), | ||
|
|
||
| outputs: buildDatabaseEventOutputs(), | ||
|
|
||
| webhook: { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'X-Notion-Signature': 'sha256=...', | ||
| }, | ||
| }, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { NotionIcon } from '@/components/icons' | ||
| import { buildTriggerSubBlocks } from '@/triggers' | ||
| import { | ||
| buildDatabaseEventOutputs, | ||
| buildNotionExtraFields, | ||
| notionSetupInstructions, | ||
| notionTriggerOptions, | ||
| } from '@/triggers/notion/utils' | ||
| import type { TriggerConfig } from '@/triggers/types' | ||
|
|
||
| /** | ||
| * Notion Database Schema Updated Trigger | ||
| * | ||
| * Fires when a database schema (properties/columns) is modified. | ||
| */ | ||
| export const notionDatabaseSchemaUpdatedTrigger: TriggerConfig = { | ||
| id: 'notion_database_schema_updated', | ||
| name: 'Notion Database Schema Updated', | ||
| provider: 'notion', | ||
| description: 'Trigger workflow when a database schema is modified in Notion', | ||
| version: '1.0.0', | ||
| icon: NotionIcon, | ||
|
|
||
| subBlocks: buildTriggerSubBlocks({ | ||
| triggerId: 'notion_database_schema_updated', | ||
| triggerOptions: notionTriggerOptions, | ||
| setupInstructions: notionSetupInstructions('database.schema_updated'), | ||
| extraFields: buildNotionExtraFields('notion_database_schema_updated'), | ||
| }), | ||
|
|
||
| outputs: buildDatabaseEventOutputs(), | ||
|
|
||
| webhook: { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'X-Notion-Signature': 'sha256=...', | ||
| }, | ||
| }, | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.