Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

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

Yudhi Armyndharis20540f15a779
diff --git a/src/modules/template/template.service.spec.ts b/src/modules/template/template.service.spec.ts
index e2e8a782b..4a1156992 100644
--- a/src/modules/template/template.service.spec.ts
+++ b/src/modules/template/template.service.spec.ts
@@ -165,6 +165,13 @@ describe('TemplateService', () => {
expect(repository.findOne).not.toHaveBeenCalled();
});
+ // Ids are server-generated uuids, so none holds a NUL; the id lookup answers 404 the same way.
+ it('should throw NotFoundException for an id containing NUL without querying', async () => {
+ await expect(service.resolve('sess-1', { templateId: 'abc\u0000' })).rejects.toThrow(NotFoundException);
+ await expect(service.findOne('sess-1', 'abc\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 92800274f..abfcc96ac 100644
--- a/src/modules/template/template.service.ts
+++ b/src/modules/template/template.service.ts
@@ -52,7 +52,9 @@ export class TemplateService {
}
async findOne(sessionId: string, id: string): Promise<Template> {
- const template = await this.templateRepository.findOne({ where: { id, sessionId } });
+ // PostgreSQL rejects a NUL in a bound text parameter (a 500), and ids are server-generated uuids, so
+ // none can match: answer 404 without querying. Covers the :id routes and resolve() by id.
+ const template = id.includes('\u0000') ? null : await this.templateRepository.findOne({ where: { id, sessionId } });
if (!template) {
throw new NotFoundException(`Template with id '${id}' not found`);
}