The vulnerability is a stored Cross-Site Scripting (XSS) issue in the n8n LangChain Chat Trigger Node. An authenticated n8n user can configure a chat trigger and set the initialMessages field to contain a malicious JavaScript payload. When a victim visits the public URL for this chat, the payload executes in their browser.
The root cause is the insufficient sanitization of the initialMessages parameter.
The analysis of the patch d4ef191be0b39b65efa68559a3b8d5dad2e102b2 reveals the vulnerable code path:
-
The ChatTrigger.webhook method in ChatTrigger.node.ts is the webhook handler. It reads the initialMessages parameter from the node's configuration using ctx.getNodeParameter('initialMessages', ''). In the vulnerable version, this raw input is only minimally processed (split by newlines) before being passed to the createPage function.
-
The createPage function in templates.ts receives these messages and uses JSON.stringify to embed them into a JavaScript configuration object within the generated HTML page. JSON.stringify is not sufficient for XSS prevention in this context, as a crafted payload can break out of the JavaScript string and execute arbitrary code.
The patch addresses this by introducing a sanitizeUserInput function that uses the sanitize-html library to strip all HTML tags and also removes dangerous URL schemes like javascript:. This sanitization is applied to the initialMessages before they are embedded in the HTML, effectively neutralizing the XSS threat.
Therefore, the key functions that would appear in a runtime profile during the exploitation of this vulnerability are ChatTrigger.webhook, which handles the request and retrieves the malicious input, and createPage, which constructs the vulnerable HTML page.