Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(webhook): drop connection-level custom headers before delivery
Yudhi Armyndharis374859631111
diff --git a/src/modules/webhook/dto/webhook.dto.ts b/src/modules/webhook/dto/webhook.dto.tsindex 40664b03f..14d76a72c 100644--- a/src/modules/webhook/dto/webhook.dto.ts+++ b/src/modules/webhook/dto/webhook.dto.ts@@ -146,7 +146,9 @@ export class CreateWebhookDto {@ApiPropertyOptional({description:'Custom headers to include in webhook requests. Never returned by any webhook route. At delivery, ' +- '`content-type` and `x-openwa-*` names are stripped so a custom header cannot shadow a system one.',+ '`content-type` and `x-openwa-*` names are stripped so a custom header cannot shadow a system one, ' ++ 'and so are the connection-level names the HTTP client owns (`connection`, `content-length`, ' ++ '`expect`, `keep-alive`, `te`, `trailer`, `transfer-encoding`, `upgrade`).',example: { 'X-Custom-Header': 'value' },})@IsOptional()diff --git a/src/modules/webhook/utils/deliver-once.spec.ts b/src/modules/webhook/utils/deliver-once.spec.tsindex 6cc18fe2c..2052f2c3e 100644--- a/src/modules/webhook/utils/deliver-once.spec.ts+++ b/src/modules/webhook/utils/deliver-once.spec.ts@@ -1,4 +1,4 @@-import { postWebhookPayload } from './deliver-once';+import { postWebhookPayload, sanitizeCustomHeaders } from './deliver-once';/*** Direct coverage for the shared delivery core both paths (direct and queued processor) now route@@ -46,3 +46,34 @@ describe('postWebhookPayload', () => {);});});++describe('sanitizeCustomHeaders', () => {+ it('keeps ordinary custom headers and drops reserved system names', () => {+ expect(+ sanitizeCustomHeaders({+ 'X-Custom': 'v',+ Authorization: 'Bearer t',+ 'Content-Type': 'text/plain',+ 'X-OpenWA-Event': 'x',+ }),+ ).toEqual({ 'X-Custom': 'v', Authorization: 'Bearer t' });+ });++ // undici throws on several of these (every delivery then fails with "fetch failed") and a wrong+ // Content-Length breaks the request framing; the HTTP client owns all of them.+ it('drops connection-level and framing headers the HTTP client owns', () => {+ expect(+ sanitizeCustomHeaders({+ Connection: 'close',+ 'Content-Length': '1',+ Expect: '100-continue',+ 'Keep-Alive': 'timeout=5',+ TE: 'trailers',+ Trailer: 'X-Sum',+ 'Transfer-Encoding': 'chunked',+ Upgrade: 'h2c',+ 'X-Keep': 'v',+ }),+ ).toEqual({ 'X-Keep': 'v' });+ });+});diff --git a/src/modules/webhook/utils/deliver-once.ts b/src/modules/webhook/utils/deliver-once.tsindex 96417b1bd..c3dcb188a 100644--- a/src/modules/webhook/utils/deliver-once.ts+++ b/src/modules/webhook/utils/deliver-once.ts@@ -8,12 +8,17 @@ import { recordWebhookDeliveryFailure, statusCodeFromError } from './record-deli/*** Drop operator-supplied custom headers that target reserved names (Content-Type or any* X-OpenWA-* header) so a webhook config cannot forge the signature/event/idempotency- * headers. Spread the result BEFORE the system headers so system always wins.+ * headers. Spread the result BEFORE the system headers so system always wins. Connection-level+ * and framing headers are dropped too: the HTTP client owns them, undici throws on several+ * (failing every delivery) and a wrong Content-Length breaks the request.*/export function sanitizeCustomHeaders(custom: Record<string, string> | null | undefined): Record<string, string> {const safe: Record<string, string> = {};for (const [key, value] of Object.entries(custom ?? {})) {- if (!/^(content-type|x-openwa-)/i.test(key)) {+ if (+ !/^(content-type|x-openwa-)/i.test(key) &&+ !/^(connection|content-length|expect|keep-alive|te|trailer|transfer-encoding|upgrade)$/i.test(key)+ ) {safe[key] = value;}}