Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(template): answer 404 for a template name containing NUL

Yudhi Armyndharis8fd609eaa403
diff --git a/src/modules/template/template.service.spec.ts b/src/modules/template/template.service.spec.ts
index 4b8275bf5..e2e8a782b 100644
--- a/src/modules/template/template.service.spec.ts
+++ b/src/modules/template/template.service.spec.ts
@@ -158,6 +158,13 @@ describe('TemplateService', () => {
await expect(service.resolve('sess-1', { templateName: 'nope' })).rejects.toThrow(NotFoundException);
});
+ // PostgreSQL refuses a NUL in a bound text parameter, so the query would fail as a 500. No stored
+ // name can hold one (the write DTOs refuse it), so the lookup answers 404 without querying.
+ it('should throw NotFoundException for a name containing NUL without querying', async () => {
+ await expect(service.resolve('sess-1', { templateName: 'promo\u0000' })).rejects.toThrow(NotFoundException);
+ expect(repository.findOne).not.toHaveBeenCalled();
+ });
+
// A malformed request, not a missing resource: a client reading 404 as "template deleted" would
// take the wrong branch.
it('should throw BadRequestException when neither id nor name is provided', async () => {
diff --git a/src/modules/template/template.service.ts b/src/modules/template/template.service.ts
index 85d72c3c0..92800274f 100644
--- a/src/modules/template/template.service.ts
+++ b/src/modules/template/template.service.ts
@@ -71,6 +71,11 @@ export class TemplateService {
}
if (templateName) {
+ // PostgreSQL rejects a NUL in a bound text parameter (a 500), and the write DTOs refuse one, so no
+ // stored name can match: answer 404 without querying.
+ if (templateName.includes('\u0000')) {
+ throw new NotFoundException(`Template with name '${templateName}' not found`);
+ }
// Order by createdAt ASC so resolution is deterministic if more than one row shares a name
// (possible only on a DB predating the unique index); the migration keeps the earliest too.
const template = await this.templateRepository.findOne({