Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(template): count the template name limit in code points

Yudhi Armyndharis2615229cc6b0
diff --git a/src/modules/template/dto/template.dto.spec.ts b/src/modules/template/dto/template.dto.spec.ts
index a139d6a21..8558779bb 100644
--- a/src/modules/template/dto/template.dto.spec.ts
+++ b/src/modules/template/dto/template.dto.spec.ts
@@ -1,5 +1,5 @@
import { BadRequestException, ValidationPipe } from '@nestjs/common';
-import { UpdateTemplateDto } from './template.dto';
+import { CreateTemplateDto, UpdateTemplateDto } from './template.dto';
import { GLOBAL_VALIDATION_OPTIONS } from '../../../config/app-validation';
describe('UpdateTemplateDto', () => {
@@ -24,3 +24,22 @@ describe('UpdateTemplateDto', () => {
await expect(through({ [field]: null })).resolves.toEqual({ [field]: null });
});
});
+
+// The name column is varchar(100), which PostgreSQL counts in code points. A length check that folds
+// a presentation selector (U+FE0F) into the character before it let 200 code points through, and the
+// INSERT then failed as a 500.
+describe('template name length', () => {
+ const pipe = new ValidationPipe(GLOBAL_VALIDATION_OPTIONS);
+ const emoji = '\u2714\uFE0F';
+
+ it.each([
+ ['create', CreateTemplateDto, { body: 'hi' }],
+ ['update', UpdateTemplateDto, {}],
+ ])('%s counts the name in code points', async (_label, metatype, rest) => {
+ const through = (name: string): Promise<unknown> => pipe.transform({ ...rest, name }, { type: 'body', metatype });
+ await expect(through(emoji.repeat(100))).rejects.toBeInstanceOf(BadRequestException);
+ await expect(through(emoji.repeat(50))).resolves.toMatchObject({ name: emoji.repeat(50) });
+ await expect(through('a'.repeat(100))).resolves.toMatchObject({ name: 'a'.repeat(100) });
+ await expect(through('a'.repeat(101))).rejects.toBeInstanceOf(BadRequestException);
+ });
+});
diff --git a/src/modules/template/dto/template.dto.ts b/src/modules/template/dto/template.dto.ts
index b4d3f3b7f..d4a93d89e 100644
--- a/src/modules/template/dto/template.dto.ts
+++ b/src/modules/template/dto/template.dto.ts
@@ -1,10 +1,16 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
-import { IsString, IsNotEmpty, IsOptional, MaxLength, ValidateIf } from 'class-validator';
+import { IsString, IsNotEmpty, IsOptional, Matches, MaxLength, ValidateIf } from 'class-validator';
const NAME_MAX_LENGTH = 100;
const BODY_MAX_LENGTH = 4096;
const HEADER_FOOTER_MAX_LENGTH = 1024;
+// name is a varchar(100) column, and PostgreSQL counts code points. @MaxLength does not: it folds a
+// presentation selector (U+FE0F) into the character before it, so 100 emoji of two code points each
+// passed and the INSERT failed as a 500. The u flag makes each matched unit one code point.
+const NAME_PATTERN = new RegExp(`^[\\s\\S]{0,${NAME_MAX_LENGTH}}$`, 'u');
+const NAME_LENGTH_MESSAGE = `name must be shorter than or equal to ${NAME_MAX_LENGTH} characters`;
+
export class CreateTemplateDto {
@ApiProperty({
description: 'Unique template name within the session',
@@ -13,7 +19,7 @@ export class CreateTemplateDto {
})
@IsString()
@IsNotEmpty()
- @MaxLength(NAME_MAX_LENGTH)
+ @Matches(NAME_PATTERN, { message: NAME_LENGTH_MESSAGE })
name!: string;
@ApiProperty({
@@ -53,7 +59,7 @@ export class UpdateTemplateDto {
@ValidateIf((o: UpdateTemplateDto) => o.name !== undefined)
@IsString()
@IsNotEmpty()
- @MaxLength(NAME_MAX_LENGTH)
+ @Matches(NAME_PATTERN, { message: NAME_LENGTH_MESSAGE })
name?: string;
@ApiPropertyOptional({ description: 'Template body with {{variable}} placeholders', maxLength: BODY_MAX_LENGTH })