From b5f4afe12ec77f527080a4b7f812e12f9f73f8df Mon Sep 17 00:00:00 2001 From: oleg Date: Mon, 30 Sep 2024 15:42:37 +0200 Subject: [PATCH] fix(Chat Trigger Node): Fix Allowed Origins paramter (#11011) --- .../trigger/ChatTrigger/ChatTrigger.node.ts | 18 +++++++++++++----- packages/cli/src/webhooks/live-webhooks.ts | 16 +++++++++++----- packages/workflow/src/NodeHelpers.ts | 2 +- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/packages/@n8n/nodes-langchain/nodes/trigger/ChatTrigger/ChatTrigger.node.ts b/packages/@n8n/nodes-langchain/nodes/trigger/ChatTrigger/ChatTrigger.node.ts index 5c53a69006..489b4fe28b 100644 --- a/packages/@n8n/nodes-langchain/nodes/trigger/ChatTrigger/ChatTrigger.node.ts +++ b/packages/@n8n/nodes-langchain/nodes/trigger/ChatTrigger/ChatTrigger.node.ts @@ -1,4 +1,6 @@ -import { Node, NodeConnectionType } from 'n8n-workflow'; +import type { BaseChatMemory } from '@langchain/community/memory/chat_memory'; +import { pick } from 'lodash'; +import { Node, NodeConnectionType, commonCORSParameters } from 'n8n-workflow'; import type { IDataObject, IWebhookFunctions, @@ -10,10 +12,8 @@ import type { INodeProperties, } from 'n8n-workflow'; -import { pick } from 'lodash'; -import type { BaseChatMemory } from '@langchain/community/memory/chat_memory'; -import { createPage } from './templates'; import { validateAuth } from './GenericFunctions'; +import { createPage } from './templates'; import type { LoadPreviousSessionChatOption } from './types'; const CHAT_TRIGGER_PATH_IDENTIFIER = 'chat'; @@ -56,7 +56,6 @@ export class ChatTrigger extends Node { ], }, }, - supportsCORS: true, maxNodes: 1, inputs: `={{ (() => { if (!['hostedChat', 'webhook'].includes($parameter.mode)) { @@ -241,6 +240,15 @@ export class ChatTrigger extends Node { placeholder: 'Add Field', default: {}, options: [ + // CORS parameters are only valid for when chat is used in hosted or webhook mode + ...commonCORSParameters.map((p) => ({ + ...p, + displayOptions: { + show: { + '/mode': ['hostedChat', 'webhook'], + }, + }, + })), { ...allowFileUploadsOption, displayOptions: { diff --git a/packages/cli/src/webhooks/live-webhooks.ts b/packages/cli/src/webhooks/live-webhooks.ts index e9314060d7..4c6e0ef7e9 100644 --- a/packages/cli/src/webhooks/live-webhooks.ts +++ b/packages/cli/src/webhooks/live-webhooks.ts @@ -1,5 +1,5 @@ import type { Response } from 'express'; -import { Workflow, NodeHelpers } from 'n8n-workflow'; +import { Workflow, NodeHelpers, CHAT_TRIGGER_NODE_TYPE } from 'n8n-workflow'; import type { INode, IWebhookData, IHttpRequestMethods } from 'n8n-workflow'; import { Service } from 'typedi'; @@ -47,12 +47,18 @@ export class LiveWebhooks implements IWebhookManager { select: ['nodes'], }); + const isChatWebhookNode = (type: string, webhookId?: string) => + type === CHAT_TRIGGER_NODE_TYPE && `${webhookId}/chat` === path; + const nodes = workflowData?.nodes; const webhookNode = nodes?.find( - ({ type, parameters, typeVersion }) => - parameters?.path === path && - (parameters?.httpMethod ?? 'GET') === httpMethod && - 'webhook' in this.nodeTypes.getByNameAndVersion(type, typeVersion), + ({ type, parameters, typeVersion, webhookId }) => + (parameters?.path === path && + (parameters?.httpMethod ?? 'GET') === httpMethod && + 'webhook' in this.nodeTypes.getByNameAndVersion(type, typeVersion)) || + // Chat Trigger has doesn't have configurable path and is always using POST, so + // we need to use webhookId for matching + isChatWebhookNode(type, webhookId), ); return webhookNode?.parameters?.options as WebhookAccessControlOptions; } diff --git a/packages/workflow/src/NodeHelpers.ts b/packages/workflow/src/NodeHelpers.ts index 6869652f4f..701b50128c 100644 --- a/packages/workflow/src/NodeHelpers.ts +++ b/packages/workflow/src/NodeHelpers.ts @@ -259,7 +259,7 @@ const commonPollingParameters: INodeProperties[] = [ }, ]; -const commonCORSParameters: INodeProperties[] = [ +export const commonCORSParameters: INodeProperties[] = [ { displayName: 'Allowed Origins (CORS)', name: 'allowedOrigins',