Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(template): refuse a NUL character in template text with 400

Yudhi Armyndharisc53ed693e66a
diff --git a/src/modules/template/dto/template.dto.spec.ts b/src/modules/template/dto/template.dto.spec.ts
index 8558779bb..558447b60 100644
--- a/src/modules/template/dto/template.dto.spec.ts
+++ b/src/modules/template/dto/template.dto.spec.ts
@@ -43,3 +43,22 @@ describe('template name length', () => {
await expect(through('a'.repeat(101))).rejects.toBeInstanceOf(BadRequestException);
});
});
+
+// PostgreSQL text and varchar columns cannot hold U+0000, so a value carrying one passed validation and
+// then failed the INSERT or UPDATE as a 500.
+describe('template text fields', () => {
+ const pipe = new ValidationPipe(GLOBAL_VALIDATION_OPTIONS);
+ const valid = { name: 'welcome', body: 'hi', header: 'top', footer: 'bottom' };
+
+ it.each([
+ ['create', CreateTemplateDto],
+ ['update', UpdateTemplateDto],
+ ])('%s rejects a NUL character in any text field', async (_label, metatype) => {
+ for (const field of ['name', 'body', 'header', 'footer']) {
+ await expect(
+ pipe.transform({ ...valid, [field]: 'a\u0000b' }, { type: 'body', metatype }),
+ ).rejects.toBeInstanceOf(BadRequestException);
+ }
+ await expect(pipe.transform(valid, { type: 'body', metatype })).resolves.toMatchObject(valid);
+ });
+});
diff --git a/src/modules/template/dto/template.dto.ts b/src/modules/template/dto/template.dto.ts
index c8e9d139f..1fa1f748b 100644
--- a/src/modules/template/dto/template.dto.ts
+++ b/src/modules/template/dto/template.dto.ts
@@ -1,11 +1,14 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
-import { IsString, IsNotEmpty, IsOptional, MaxLength, ValidateIf } from 'class-validator';
+import { IsString, IsNotEmpty, IsOptional, MaxLength, NotContains, ValidateIf } from 'class-validator';
import { MaxCodePoints } from '../../../common/validation/max-code-points';
const NAME_MAX_LENGTH = 100;
const BODY_MAX_LENGTH = 4096;
const HEADER_FOOTER_MAX_LENGTH = 1024;
+// PostgreSQL text and varchar columns cannot store U+0000, so the write would fail as a 500.
+const NoNulCharacter = () => NotContains('\u0000', { message: '$property must not contain a NUL character' });
+
export class CreateTemplateDto {
@ApiProperty({
description: 'Unique template name within the session',
@@ -15,6 +18,7 @@ export class CreateTemplateDto {
@IsString()
@IsNotEmpty()
@MaxCodePoints(NAME_MAX_LENGTH)
+ @NoNulCharacter()
name!: string;
@ApiProperty({
@@ -25,6 +29,7 @@ export class CreateTemplateDto {
@IsString()
@IsNotEmpty()
@MaxLength(BODY_MAX_LENGTH)
+ @NoNulCharacter()
body!: string;
@ApiPropertyOptional({
@@ -35,6 +40,7 @@ export class CreateTemplateDto {
@IsOptional()
@IsString()
@MaxLength(HEADER_FOOTER_MAX_LENGTH)
+ @NoNulCharacter()
header?: string;
@ApiPropertyOptional({
@@ -45,6 +51,7 @@ export class CreateTemplateDto {
@IsOptional()
@IsString()
@MaxLength(HEADER_FOOTER_MAX_LENGTH)
+ @NoNulCharacter()
footer?: string;
}
@@ -55,6 +62,7 @@ export class UpdateTemplateDto {
@IsString()
@IsNotEmpty()
@MaxCodePoints(NAME_MAX_LENGTH)
+ @NoNulCharacter()
name?: string;
@ApiPropertyOptional({ description: 'Template body with {{variable}} placeholders', maxLength: BODY_MAX_LENGTH })
@@ -62,18 +70,21 @@ export class UpdateTemplateDto {
@IsString()
@IsNotEmpty()
@MaxLength(BODY_MAX_LENGTH)
+ @NoNulCharacter()
body?: string;
@ApiPropertyOptional({ description: 'Optional header text', maxLength: HEADER_FOOTER_MAX_LENGTH })
@IsOptional()
@IsString()
@MaxLength(HEADER_FOOTER_MAX_LENGTH)
+ @NoNulCharacter()
header?: string;
@ApiPropertyOptional({ description: 'Optional footer text', maxLength: HEADER_FOOTER_MAX_LENGTH })
@IsOptional()
@IsString()
@MaxLength(HEADER_FOOTER_MAX_LENGTH)
+ @NoNulCharacter()
footer?: string;
}