Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(status): bound recipients array (@ArrayMaxSize) + validate JID format (security)

yudhiebb802442330
diff --git a/src/modules/status/dto/send-media-status.dto.spec.ts b/src/modules/status/dto/send-media-status.dto.spec.ts
new file mode 100644
index 000000000..15b02572d
--- /dev/null
+++ b/src/modules/status/dto/send-media-status.dto.spec.ts
@@ -0,0 +1,94 @@
+import 'reflect-metadata';
+import { plainToInstance } from 'class-transformer';
+import { validate } from 'class-validator';
+import { SendImageStatusDto, SendVideoStatusDto } from './send-media-status.dto';
+
+describe('SendImageStatusDto recipients validation', () => {
+ const valid = { image: { url: 'https://example.com/i.png' }, recipients: ['[email protected]'] };
+
+ it('accepts a non-empty recipients array', async () => {
+ const errors = await validate(plainToInstance(SendImageStatusDto, valid));
+ expect(errors).toHaveLength(0);
+ });
+
+ it('rejects missing recipients', async () => {
+ const errors = await validate(plainToInstance(SendImageStatusDto, { image: valid.image }));
+ expect(errors.some((e) => e.property === 'recipients')).toBe(true);
+ });
+
+ it('rejects an empty recipients array (-> 400)', async () => {
+ const errors = await validate(plainToInstance(SendImageStatusDto, { image: valid.image, recipients: [] }));
+ expect(errors.some((e) => e.property === 'recipients')).toBe(true);
+ });
+
+ it('rejects non-string entries', async () => {
+ const errors = await validate(plainToInstance(SendImageStatusDto, { image: valid.image, recipients: [123] }));
+ expect(errors.some((e) => e.property === 'recipients')).toBe(true);
+ });
+
+ it('rejects more than 256 recipients', async () => {
+ const recipients = Array.from({ length: 257 }, (_, i) => `${i}@c.us`);
+ const errors = await validate(plainToInstance(SendImageStatusDto, { image: valid.image, recipients }));
+ expect(errors.some((e) => e.property === 'recipients')).toBe(true);
+ });
+
+ it('rejects malformed JIDs', async () => {
+ const errors = await validate(
+ plainToInstance(SendImageStatusDto, {
+ image: valid.image,
+ recipients: ['not-a-jid', '[email protected]', '@c.us', 'abc@lid'],
+ }),
+ );
+ expect(errors.some((e) => e.property === 'recipients')).toBe(true);
+ });
+
+ it('accepts @lid recipients', async () => {
+ const errors = await validate(plainToInstance(SendImageStatusDto, { image: valid.image, recipients: ['6281@lid'] }));
+ expect(errors).toHaveLength(0);
+ });
+});
+
+describe('SendVideoStatusDto recipients validation', () => {
+ const valid = { video: { url: 'https://example.com/v.mp4' }, recipients: ['[email protected]'] };
+
+ it('accepts a non-empty recipients array', async () => {
+ const errors = await validate(plainToInstance(SendVideoStatusDto, valid));
+ expect(errors).toHaveLength(0);
+ });
+
+ it('rejects missing recipients', async () => {
+ const errors = await validate(plainToInstance(SendVideoStatusDto, { video: valid.video }));
+ expect(errors.some((e) => e.property === 'recipients')).toBe(true);
+ });
+
+ it('rejects an empty recipients array (-> 400)', async () => {
+ const errors = await validate(plainToInstance(SendVideoStatusDto, { video: valid.video, recipients: [] }));
+ expect(errors.some((e) => e.property === 'recipients')).toBe(true);
+ });
+
+ it('rejects non-string entries', async () => {
+ const errors = await validate(plainToInstance(SendVideoStatusDto, { video: valid.video, recipients: [123] }));
+ expect(errors.some((e) => e.property === 'recipients')).toBe(true);
+ });
+
+ it('rejects more than 256 recipients', async () => {
+ const recipients = Array.from({ length: 257 }, (_, i) => `${i}@c.us`);
+ const errors = await validate(plainToInstance(SendVideoStatusDto, { video: valid.video, recipients }));
+ expect(errors.some((e) => e.property === 'recipients')).toBe(true);
+ });
+
+ it('rejects malformed JIDs', async () => {
+ const errors = await validate(
+ plainToInstance(SendVideoStatusDto, {
+ video: valid.video,
+ recipients: ['not-a-jid', '[email protected]', '@c.us', 'abc@lid'],
+ }),
+ );
+ expect(errors.some((e) => e.property === 'recipients')).toBe(true);
+ });
+
+ it('accepts @lid recipients', async () => {
+ const errors = await validate(plainToInstance(SendVideoStatusDto, { video: valid.video, recipients: ['6281@lid'] }));
+ expect(errors).toHaveLength(0);
+ });
+});
diff --git a/src/modules/status/dto/send-media-status.dto.ts b/src/modules/status/dto/send-media-status.dto.ts
index 2146b7faf..3ae6bdaab 100644
--- a/src/modules/status/dto/send-media-status.dto.ts
+++ b/src/modules/status/dto/send-media-status.dto.ts
@@ -1,4 +1,12 @@
-import { IsString, IsOptional, ValidateNested, IsArray, ArrayMinSize } from 'class-validator';
+import {
+ IsString,
+ IsOptional,
+ ValidateNested,
+ IsArray,
+ ArrayMinSize,
+ ArrayMaxSize,
+ Matches,
+} from 'class-validator';
import { Type } from 'class-transformer';
class StatusMediaInput {
@@ -26,7 +34,9 @@ export class SendImageStatusDto {
@IsArray()
@ArrayMinSize(1)
+ @ArrayMaxSize(256)
@IsString({ each: true })
+ @Matches(/^\d+@(c\.us|lid)$/, { each: true, message: 'Invalid recipient JID' })
recipients: string[];
}
@@ -41,6 +51,8 @@ export class SendVideoStatusDto {
@IsArray()
@ArrayMinSize(1)
+ @ArrayMaxSize(256)
@IsString({ each: true })
+ @Matches(/^\d+@(c\.us|lid)$/, { each: true, message: 'Invalid recipient JID' })
recipients: string[];
}
diff --git a/src/modules/status/dto/send-text-status.dto.spec.ts b/src/modules/status/dto/send-text-status.dto.spec.ts
index 5858d13df..e13a8afc9 100644
--- a/src/modules/status/dto/send-text-status.dto.spec.ts
+++ b/src/modules/status/dto/send-text-status.dto.spec.ts
@@ -25,4 +25,28 @@ describe('SendTextStatusDto recipients validation', () => {
const errors = await validate(plainToInstance(SendTextStatusDto, { text: 'hi', recipients: [123] }));
expect(errors.some((e) => e.property === 'recipients')).toBe(true);
});
+
+ it('rejects more than 256 recipients', async () => {
+ const recipients = Array.from({ length: 257 }, (_, i) => `${i}@c.us`);
+ const errors = await validate(plainToInstance(SendTextStatusDto, { text: 'hi', recipients }));
+ expect(errors.some((e) => e.property === 'recipients')).toBe(true);
+ });
+
+ it('accepts exactly 256 recipients', async () => {
+ const recipients = Array.from({ length: 256 }, (_, i) => `${i}@c.us`);
+ const errors = await validate(plainToInstance(SendTextStatusDto, { text: 'hi', recipients }));
+ expect(errors).toHaveLength(0);
+ });
+
+ it('rejects malformed JIDs', async () => {
+ const errors = await validate(
+ plainToInstance(SendTextStatusDto, { text: 'hi', recipients: ['not-a-jid', '[email protected]', '@c.us', 'abc@lid'] }),
+ );
+ expect(errors.some((e) => e.property === 'recipients')).toBe(true);
+ });
+
+ it('accepts @lid recipients', async () => {
+ const errors = await validate(plainToInstance(SendTextStatusDto, { text: 'hi', recipients: ['6281@lid'] }));
+ expect(errors).toHaveLength(0);
+ });
});
diff --git a/src/modules/status/dto/send-text-status.dto.ts b/src/modules/status/dto/send-text-status.dto.ts
index 79790ac35..7a5303b4a 100644
--- a/src/modules/status/dto/send-text-status.dto.ts
+++ b/src/modules/status/dto/send-text-status.dto.ts
@@ -1,4 +1,14 @@
-import { IsString, IsOptional, IsInt, Min, Max, Matches, IsArray, ArrayMinSize } from 'class-validator';
+import {
+ IsString,
+ IsOptional,
+ IsInt,
+ Min,
+ Max,
+ Matches,
+ IsArray,
+ ArrayMinSize,
+ ArrayMaxSize,
+} from 'class-validator';
export class SendTextStatusDto {
@IsString()
@@ -17,6 +27,8 @@ export class SendTextStatusDto {
@IsArray()
@ArrayMinSize(1)
+ @ArrayMaxSize(256)
@IsString({ each: true })
+ @Matches(/^\d+@(c\.us|lid)$/, { each: true, message: 'Invalid recipient JID' })
recipients: string[];
}