Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

fix(webhook): sanitize response header names to prevent process crash (#17003)

Chaker Atallahf34c43cd8cc7
diff --git a/packages/core/shared/package.json b/packages/core/shared/package.json
index 3a2490ffce5..eefb0293d35 100644
--- a/packages/core/shared/package.json
+++ b/packages/core/shared/package.json
@@ -1,6 +1,6 @@
{
"name": "@activepieces/shared",
- "version": "0.209.1",
+ "version": "0.210.0",
"type": "commonjs",
"sideEffects": false,
"main": "./dist/src/index.js",
diff --git a/packages/core/utils/src/index.ts b/packages/core/utils/src/index.ts
index 5a335eaca07..2d6134ac002 100644
--- a/packages/core/utils/src/index.ts
+++ b/packages/core/utils/src/index.ts
@@ -24,3 +24,4 @@ export * from './lib/byte-lru-cache'
export * from './lib/ai-provider-health'
export * from './lib/deno'
export * from './lib/sandbox-error'
+export * from './lib/http-header-utils'
diff --git a/packages/core/utils/src/lib/http-header-utils.ts b/packages/core/utils/src/lib/http-header-utils.ts
new file mode 100644
index 00000000000..10100f93da8
--- /dev/null
+++ b/packages/core/utils/src/lib/http-header-utils.ts
@@ -0,0 +1,15 @@
+export function sanitizeHttpHeaderName(name: string): string {
+ const invalidHeaderNameChars = /[^!#$%&'*+\-.^_`|~0-9A-Za-z]/g
+ return name.trim().replace(invalidHeaderNameChars, '')
+}
+
+export function sanitizeHttpHeaders(headers: Record<string, string> | undefined): Record<string, string> {
+ if (!headers) {
+ return {}
+ }
+ return Object.fromEntries(
+ Object.entries(headers)
+ .map(([key, value]) => [sanitizeHttpHeaderName(key), value])
+ .filter(([key]) => key !== ''),
+ )
+}
diff --git a/packages/pieces/core/webhook/package.json b/packages/pieces/core/webhook/package.json
index 9d2e6b7e134..8e98a996463 100644
--- a/packages/pieces/core/webhook/package.json
+++ b/packages/pieces/core/webhook/package.json
@@ -1,6 +1,6 @@
{
"name": "@activepieces/piece-webhook",
- "version": "0.1.42",
+ "version": "0.1.43",
"main": "./dist/src/index.js",
"types": "./dist/src/index.d.ts",
"scripts": {
diff --git a/packages/pieces/core/webhook/src/lib/actions/return-response-and-wait-for-next-webhook.ts b/packages/pieces/core/webhook/src/lib/actions/return-response-and-wait-for-next-webhook.ts
index c89d11a9c6f..b97ec6c08b0 100644
--- a/packages/pieces/core/webhook/src/lib/actions/return-response-and-wait-for-next-webhook.ts
+++ b/packages/pieces/core/webhook/src/lib/actions/return-response-and-wait-for-next-webhook.ts
@@ -3,6 +3,7 @@ import {
PieceAuth,
Property,
createAction,
+ sanitizeHttpHeaders,
} from '@activepieces/pieces-framework';
import { ExecutionType, StopResponse } from '@activepieces/pieces-framework';
@@ -104,7 +105,7 @@ import {
async run(context) {
const { fields, responseType } = context.propsValue;
const bodyInput = fields ['body'];
- const headers = fields['headers'] ?? {};
+ const headers = sanitizeHttpHeaders(fields['headers']);
const status = fields['status'];
const response: StopResponse = {
status: status ?? HTTP_STATUS_OK,
diff --git a/packages/pieces/core/webhook/src/lib/actions/return-response.ts b/packages/pieces/core/webhook/src/lib/actions/return-response.ts
index 44182b6be38..ad55d97eae9 100644
--- a/packages/pieces/core/webhook/src/lib/actions/return-response.ts
+++ b/packages/pieces/core/webhook/src/lib/actions/return-response.ts
@@ -3,6 +3,7 @@ import {
PieceAuth,
Property,
createAction,
+ sanitizeHttpHeaders,
} from '@activepieces/pieces-framework';
import { StopResponse } from '@activepieces/pieces-framework';
@@ -114,7 +115,7 @@ export const returnResponse = createAction({
async run(context) {
const { fields, responseType, respond } = context.propsValue;
const bodyInput = fields ['body'];
- const headers = fields['headers']?? {};
+ const headers = sanitizeHttpHeaders(fields['headers']);
const status = fields['status'];
diff --git a/packages/pieces/framework/package.json b/packages/pieces/framework/package.json
index c55e40b390f..b9855fc5ff5 100644
--- a/packages/pieces/framework/package.json
+++ b/packages/pieces/framework/package.json
@@ -1,6 +1,6 @@
{
"name": "@activepieces/pieces-framework",
- "version": "0.41.1",
+ "version": "0.42.0",
"type": "commonjs",
"main": "./dist/src/index.js",
"types": "./dist/src/index.d.ts",
diff --git a/packages/pieces/framework/src/index.ts b/packages/pieces/framework/src/index.ts
index 4fcfb2a394e..b943d21e910 100644
--- a/packages/pieces/framework/src/index.ts
+++ b/packages/pieces/framework/src/index.ts
@@ -17,6 +17,7 @@ export {
camelCase,
startCase,
tryCatch,
+ sanitizeHttpHeaders,
AIProviderName,
AiStepAction,
} from '@activepieces/core-utils';
diff --git a/packages/server/api/src/app/webhooks/webhook-controller.ts b/packages/server/api/src/app/webhooks/webhook-controller.ts
index 2d86f47cfa7..b5ce67513ab 100644
--- a/packages/server/api/src/app/webhooks/webhook-controller.ts
+++ b/packages/server/api/src/app/webhooks/webhook-controller.ts
@@ -1,11 +1,15 @@
import { wideEvent } from '@activepieces/server-utils'
import {
+ EngineHttpResponse,
+ isNil,
RAW_PAYLOAD_HEADER,
+ sanitizeHttpHeaders,
+ tryCatch,
WebhookUrlParams,
WebsocketClientEvent,
} from '@activepieces/shared'
-import { FastifyRequest } from 'fastify'
+import { FastifyReply, FastifyRequest } from 'fastify'
import { FastifyPluginAsyncZod } from 'fastify-type-provider-zod'
import { securityAccess } from '../core/security/authorization/fastify-security'
import { triggerSourceService } from '../trigger/trigger-source/trigger-source-service'
@@ -40,10 +44,7 @@ export const webhookController: FastifyPluginAsyncZod = async (app) => {
...extractHeaderFromRequest(request),
})
wideEvent.set({ webhook: { responseStatus: response.status } })
- await reply
- .status(response.status)
- .headers(response.headers)
- .send(response.body)
+ await sendWebhookResponse({ reply, response })
},
)
@@ -73,10 +74,7 @@ export const webhookController: FastifyPluginAsyncZod = async (app) => {
...extractHeaderFromRequest(request),
})
wideEvent.set({ webhook: { responseStatus: response.status } })
- await reply
- .status(response.status)
- .headers(response.headers)
- .send(response.body)
+ await sendWebhookResponse({ reply, response })
},
)
@@ -94,10 +92,7 @@ export const webhookController: FastifyPluginAsyncZod = async (app) => {
},
...extractHeaderFromRequest(request),
})
- await reply
- .status(response.status)
- .headers(response.headers)
- .send(response.body)
+ await sendWebhookResponse({ reply, response })
})
app.all('/:flowId/draft', WEBHOOK_PARAMS, async (request, reply) => {
@@ -111,10 +106,7 @@ export const webhookController: FastifyPluginAsyncZod = async (app) => {
execute: true,
...extractHeaderFromRequest(request),
})
- await reply
- .status(response.status)
- .headers(response.headers)
- .send(response.body)
+ await sendWebhookResponse({ reply, response })
})
app.all('/:flowId/test', WEBHOOK_PARAMS, async (request, reply) => {
@@ -128,15 +120,24 @@ export const webhookController: FastifyPluginAsyncZod = async (app) => {
execute: false,
...extractHeaderFromRequest(request),
})
+ await sendWebhookResponse({ reply, response })
+ })
+
+}
+
+async function sendWebhookResponse({ reply, response }: { reply: FastifyReply, response: EngineHttpResponse }): Promise<void> {
+ const { error } = await tryCatch(async () => {
await reply
.status(response.status)
- .headers(response.headers)
+ .headers(sanitizeHttpHeaders(response.headers))
.send(response.body)
})
-
+ if (!isNil(error) && !reply.raw.headersSent) {
+ reply.log.error({ error }, 'Failed to send webhook response')
+ await reply.status(500).send({ message: 'Failed to send webhook response' })
+ }
}
-
const WEBHOOK_PARAMS = {
config: {
security: securityAccess.public(),