Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(webhook): require an http(s) scheme and cap the URL at 2048

Yudhi Armyndharis18876c499c09
diff --git a/src/modules/webhook/dto/webhook.dto.spec.ts b/src/modules/webhook/dto/webhook.dto.spec.ts
index 93c52b80f..982e74aa7 100644
--- a/src/modules/webhook/dto/webhook.dto.spec.ts
+++ b/src/modules/webhook/dto/webhook.dto.spec.ts
@@ -188,3 +188,32 @@ describe('webhook DTO filter validation', () => {
expect(errs.some(e => e.property === 'filters')).toBe(true);
});
});
+
+describe('webhook DTO url validation', () => {
+ // The column is varchar(2048); PostgreSQL refuses a longer value on insert with a 500.
+ const longUrl = (length: number) => 'https://x.example/' + 'a'.repeat(length - 'https://x.example/'.length);
+
+ const dtos = [
+ ['CreateWebhookDto', CreateWebhookDto],
+ ['UpdateWebhookDto', UpdateWebhookDto],
+ ] as const;
+
+ it.each(dtos)('%s accepts http(s) URLs, dotless hosts included', async (_name, cls) => {
+ for (const url of ['https://x.example/hook', 'http://localhost:3000/hook', longUrl(2048)]) {
+ expect(await errorsFor(cls, { url })).toHaveLength(0);
+ }
+ });
+
+ it.each(dtos)('%s rejects scheme-less, non-http(s) and over-length URLs', async (_name, cls) => {
+ for (const url of [
+ 'example.com/hook',
+ 'localhost:3000/hook',
+ 'user:pass@host/x',
+ 'ftp://x.example/y',
+ longUrl(2049),
+ ]) {
+ const errs = await errorsFor(cls, { url });
+ expect(errs.some(e => e.property === 'url')).toBe(true);
+ }
+ });
+});
diff --git a/src/modules/webhook/dto/webhook.dto.ts b/src/modules/webhook/dto/webhook.dto.ts
index c7661bfbe..188305064 100644
--- a/src/modules/webhook/dto/webhook.dto.ts
+++ b/src/modules/webhook/dto/webhook.dto.ts
@@ -101,14 +101,21 @@ export const WEBHOOK_EVENTS = [
...WEBHOOK_RESERVED_EVENTS,
] as const;
+const WEBHOOK_URL_OPTIONS = { require_tld: false, require_protocol: true, protocols: ['http', 'https'] };
+
export class CreateWebhookDto {
@ApiProperty({
description: 'Webhook URL to receive events',
example: 'https://your-server.com/webhook',
+ maxLength: 2048,
})
// require_tld:false allows hostnames without a dot (e.g. http://localhost:3000); the SSRF
- // guard still decides whether the host is actually allowed to be delivered to.
- @IsUrl({ require_tld: false })
+ // guard still decides whether the host is actually allowed to be delivered to. The scheme is
+ // required and must be http(s): the defaults also took 'example.com/hook' and 'ftp://...', which
+ // were stored and then failed every delivery. 2048 is the column width; PostgreSQL refuses a
+ // longer value on insert with a 500.
+ @IsUrl(WEBHOOK_URL_OPTIONS)
+ @MaxLength(2048)
url!: string;
@ApiPropertyOptional({
@@ -191,11 +198,12 @@ export class CreateWebhookDto {
}
export class UpdateWebhookDto {
- @ApiPropertyOptional({ description: 'Webhook URL' })
+ @ApiPropertyOptional({ description: 'Webhook URL', maxLength: 2048 })
// 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 })
+ @IsUrl(WEBHOOK_URL_OPTIONS)
+ @MaxLength(2048)
url?: string;
@ApiPropertyOptional({