Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(webhook): reject null for NOT NULL fields on a webhook update
Yudhi Armyndharis10340063b063
diff --git a/src/modules/webhook/dto/is-header-map.validator.ts b/src/modules/webhook/dto/is-header-map.validator.tsindex c5344ca9e..484152df6 100644--- a/src/modules/webhook/dto/is-header-map.validator.ts+++ b/src/modules/webhook/dto/is-header-map.validator.ts@@ -28,8 +28,8 @@ export function IsHeaderMap(options?: ValidationOptions) {options,validator: {validate(value: unknown): boolean {- if (value === undefined || value === null) return true; // @IsOptional handles absence- if (typeof value !== 'object' || Array.isArray(value)) return false;+ if (value === undefined) return true; // absence is the optional decorator's call+ if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;const entries = Object.entries(value as Record<string, unknown>);if (entries.length > MAX_HEADERS) return false;return entries.every(diff --git a/src/modules/webhook/dto/webhook.dto.spec.ts b/src/modules/webhook/dto/webhook.dto.spec.tsindex ddc0c0863..7c2679b60 100644--- a/src/modules/webhook/dto/webhook.dto.spec.ts+++ b/src/modules/webhook/dto/webhook.dto.spec.ts@@ -38,6 +38,20 @@ describe('webhook DTO event validation', () => {const errs = await errorsFor(UpdateWebhookDto, { events: ['nope'] });expect(errs.some(e => e.property === 'events')).toBe(true);});++ // These columns are NOT NULL: @IsOptional let null through and save() then answered 500.+ it.each(['url', 'events', 'headers', 'active', 'retryCount'])(+ 'UpdateWebhookDto: rejects null for the NOT NULL field %s',+ async field => {+ const errs = await errorsFor(UpdateWebhookDto, { [field]: null });+ expect(errs.map(e => e.property)).toEqual([field]);+ },+ );++ it('UpdateWebhookDto: still accepts an omitted field and a null filters (nullable column)', async () => {+ expect(await errorsFor(UpdateWebhookDto, {})).toHaveLength(0);+ expect(await errorsFor(UpdateWebhookDto, { filters: null })).toHaveLength(0);+ });});describe('webhook DTO custom-header validation', () => {diff --git a/src/modules/webhook/dto/webhook.dto.ts b/src/modules/webhook/dto/webhook.dto.tsindex d7af02fbd..40664b03f 100644--- a/src/modules/webhook/dto/webhook.dto.ts+++ b/src/modules/webhook/dto/webhook.dto.ts@@ -186,7 +186,9 @@ export class CreateWebhookDto {export class UpdateWebhookDto {@ApiPropertyOptional({ description: 'Webhook URL' })- @IsOptional()+ // Not @IsOptional: that also skips validation for null, which these NOT NULL columns cannot store+ // (save() then failed with a 500). Only an omitted field means "leave unchanged".+ @ValidateIf((_: UpdateWebhookDto, v: unknown) => v !== undefined)@IsUrl({ require_tld: false })url?: string;@@ -197,7 +199,7 @@ export class UpdateWebhookDto {isArray: true,minItems: 1,})- @IsOptional()+ @ValidateIf((_: UpdateWebhookDto, v: unknown) => v !== undefined)@IsArray()@ArrayMinSize(1)@IsIn([...WEBHOOK_EVENTS, '*'], { each: true })@@ -231,7 +233,7 @@ export class UpdateWebhookDto {description: 'Custom headers. Replaces the stored map wholesale. Never returned by any webhook route.',example: { 'X-Custom-Header': 'value' },})- @IsOptional()+ @ValidateIf((_: UpdateWebhookDto, v: unknown) => v !== undefined)@IsHeaderMap()headers?: Record<string, string>;@@ -251,7 +253,7 @@ export class UpdateWebhookDto {@ApiPropertyOptional({ description: 'Enable/disable webhook' })@ToStrictBoolean()- @IsOptional()+ @ValidateIf((_: UpdateWebhookDto, v: unknown) => v !== undefined)@IsBoolean()active?: boolean;@@ -264,7 +266,7 @@ export class UpdateWebhookDto {maximum: 5,})@ToStrictNumber()- @IsOptional()+ @ValidateIf((_: UpdateWebhookDto, v: unknown) => v !== undefined)@IsInt()@Min(0)@Max(5)