Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(call): refuse a call-link startTime past the largest valid Date

Yudhi Armyndharisb3f3de36824a
diff --git a/src/modules/call/dto/create-call-link.dto.spec.ts b/src/modules/call/dto/create-call-link.dto.spec.ts
new file mode 100644
index 000000000..61b1934e7
--- /dev/null
+++ b/src/modules/call/dto/create-call-link.dto.spec.ts
@@ -0,0 +1,22 @@
+import { BadRequestException, ValidationPipe } from '@nestjs/common';
+import { CreateCallLinkDto } from './create-call-link.dto';
+import { GLOBAL_VALIDATION_OPTIONS } from '../../../config/app-validation';
+
+describe('CreateCallLinkDto', () => {
+ const pipe = new ValidationPipe(GLOBAL_VALIDATION_OPTIONS);
+ const through = (value: object): Promise<unknown> =>
+ pipe.transform(value, { type: 'body', metatype: CreateCallLinkDto });
+
+ // Past the largest valid JS Date, whatsapp-web.js builds an Invalid Date and Baileys an
+ // exponent-notation start time, so the request must stop here with a 400.
+ it.each([8_640_000_000_000_001, 1e16, 1e300])('rejects startTime %d with a 400', async startTime => {
+ await expect(through({ type: 'video', startTime })).rejects.toBeInstanceOf(BadRequestException);
+ });
+
+ it('accepts the largest valid Date', async () => {
+ await expect(through({ type: 'video', startTime: 8_640_000_000_000_000 })).resolves.toEqual({
+ type: 'video',
+ startTime: 8_640_000_000_000_000,
+ });
+ });
+});
diff --git a/src/modules/call/dto/create-call-link.dto.ts b/src/modules/call/dto/create-call-link.dto.ts
index eb733317f..d847e1f73 100644
--- a/src/modules/call/dto/create-call-link.dto.ts
+++ b/src/modules/call/dto/create-call-link.dto.ts
@@ -1,5 +1,8 @@
import { ApiProperty } from '@nestjs/swagger';
-import { IsIn, IsInt, IsString, Min } from 'class-validator';
+import { IsIn, IsInt, IsString, Max, Min } from 'class-validator';
+
+/** The largest epoch-ms value a JS Date can hold; anything above it is an Invalid Date. */
+const MAX_DATE_MS = 8_640_000_000_000_000;
export class CreateCallLinkDto {
@ApiProperty({
@@ -17,8 +20,11 @@ export class CreateCallLinkDto {
'whatsapp-web.js generates an event-linked call and has no notion of "no start time", so a ' +
'link for right now is `Date.now()` rather than an omitted field.',
example: 1800000000000,
+ minimum: 1,
+ maximum: MAX_DATE_MS,
})
@IsInt()
@Min(1)
+ @Max(MAX_DATE_MS)
startTime!: number;
}