Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(automation): refuse a NUL character in rule name and reply text
Yudhi Armyndharis5751ab3c3268
diff --git a/src/common/validation/no-nul-character.ts b/src/common/validation/no-nul-character.tsnew file mode 100644index 000000000..00a689346--- /dev/null+++ b/src/common/validation/no-nul-character.ts@@ -0,0 +1,5 @@+import { NotContains } from 'class-validator';++/** PostgreSQL text and varchar columns cannot store U+0000, so the write would fail as a 500. */+export const NoNulCharacter = (): PropertyDecorator =>+ NotContains('\u0000', { message: '$property must not contain a NUL character' });diff --git a/src/modules/automation/dto/automation-rule.dto.spec.ts b/src/modules/automation/dto/automation-rule.dto.spec.tsindex db4da341e..487112348 100644--- a/src/modules/automation/dto/automation-rule.dto.spec.ts+++ b/src/modules/automation/dto/automation-rule.dto.spec.ts@@ -1,5 +1,5 @@import { BadRequestException, ValidationPipe } from '@nestjs/common';-import { UpdateAutomationRuleDto } from './automation-rule.dto';+import { CreateAutomationRuleDto, UpdateAutomationRuleDto } from './automation-rule.dto';import { GLOBAL_VALIDATION_OPTIONS } from '../../../config/app-validation';describe('UpdateAutomationRuleDto', () => {@@ -17,3 +17,20 @@ describe('UpdateAutomationRuleDto', () => {await expect(through({ conditions: null })).resolves.toEqual({ conditions: null });});});++// PostgreSQL text and varchar columns cannot store U+0000; a NUL that got past validation failed the+// write with a 500.+describe.each([+ ['CreateAutomationRuleDto', CreateAutomationRuleDto],+ ['UpdateAutomationRuleDto', UpdateAutomationRuleDto],+])('%s NUL characters', (_name, metatype) => {+ const pipe = new ValidationPipe(GLOBAL_VALIDATION_OPTIONS);+ const valid = { name: 'greeting', replyText: 'hi there' };++ it.each(['name', 'replyText'])('rejects a NUL in %s with a 400', async key => {+ await expect(pipe.transform({ ...valid, [key]: 'a\u0000b' }, { type: 'body', metatype })).rejects.toBeInstanceOf(+ BadRequestException,+ );+ await expect(pipe.transform(valid, { type: 'body', metatype })).resolves.toEqual(valid);+ });+});diff --git a/src/modules/automation/dto/automation-rule.dto.ts b/src/modules/automation/dto/automation-rule.dto.tsindex 73c0cb57a..4d08a0a9d 100644--- a/src/modules/automation/dto/automation-rule.dto.ts+++ b/src/modules/automation/dto/automation-rule.dto.ts@@ -3,6 +3,7 @@ import { Expose, plainToInstance } from 'class-transformer';import { IsBoolean, IsInt, IsNotEmpty, IsOptional, IsString, Max, MaxLength, Min, ValidateIf } from 'class-validator';import { ToStrictBoolean, ToStrictNumber } from '../../../common/utils/strict-boolean';import { MaxCodePoints } from '../../../common/validation/max-code-points';+import { NoNulCharacter } from '../../../common/validation/no-nul-character';import { MESSAGE_TEXT_MAX_LENGTH } from '../../message/dto/send-message.dto';import { WebhookFilters } from '../../webhook/filters/filter-types';import { IsValidWebhookFilters } from '../../webhook/filters/filter-validation';@@ -29,6 +30,7 @@ export class CreateAutomationRuleDto {@IsString()@IsNotEmpty()@MaxCodePoints(100)+ @NoNulCharacter()name!: string;@ApiProperty({@@ -39,6 +41,7 @@ export class CreateAutomationRuleDto {@IsString()@IsNotEmpty()@MaxLength(MESSAGE_TEXT_MAX_LENGTH)+ @NoNulCharacter()replyText!: string;@ApiPropertyOptional({ description: CONDITIONS_DESCRIPTION })@@ -73,6 +76,7 @@ export class UpdateAutomationRuleDto {@IsString()@IsNotEmpty()@MaxCodePoints(100)+ @NoNulCharacter()name?: string;@ApiPropertyOptional({@@ -83,6 +87,7 @@ export class UpdateAutomationRuleDto {@IsString()@IsNotEmpty()@MaxLength(MESSAGE_TEXT_MAX_LENGTH)+ @NoNulCharacter()replyText?: string;@ApiPropertyOptional({ description: CONDITIONS_DESCRIPTION })