Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(api): reject null on NOT NULL update fields with a 400

Yudhi Armyndharis8b855704f610
diff --git a/src/modules/automation/dto/automation-rule.dto.spec.ts b/src/modules/automation/dto/automation-rule.dto.spec.ts
new file mode 100644
index 000000000..db4da341e
--- /dev/null
+++ b/src/modules/automation/dto/automation-rule.dto.spec.ts
@@ -0,0 +1,19 @@
+import { BadRequestException, ValidationPipe } from '@nestjs/common';
+import { UpdateAutomationRuleDto } from './automation-rule.dto';
+import { GLOBAL_VALIDATION_OPTIONS } from '../../../config/app-validation';
+
+describe('UpdateAutomationRuleDto', () => {
+ const pipe = new ValidationPipe(GLOBAL_VALIDATION_OPTIONS);
+ const through = (value: object): Promise<unknown> =>
+ pipe.transform(value, { type: 'body', metatype: UpdateAutomationRuleDto });
+
+ // These four columns are NOT NULL: a null that got past validation reached save() and answered 500.
+ it.each(['name', 'replyText', 'cooldownSeconds', 'enabled'])('rejects %s: null with a 400', async key => {
+ await expect(through({ [key]: null })).rejects.toBeInstanceOf(BadRequestException);
+ });
+
+ it('still accepts an empty body and conditions: null, which clears the conditions', async () => {
+ await expect(through({})).resolves.toEqual({});
+ await expect(through({ conditions: null })).resolves.toEqual({ conditions: null });
+ });
+});
diff --git a/src/modules/automation/dto/automation-rule.dto.ts b/src/modules/automation/dto/automation-rule.dto.ts
index a53dc3627..3ae5f3356 100644
--- a/src/modules/automation/dto/automation-rule.dto.ts
+++ b/src/modules/automation/dto/automation-rule.dto.ts
@@ -1,6 +1,6 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Expose, plainToInstance } from 'class-transformer';
-import { IsBoolean, IsInt, IsNotEmpty, IsOptional, IsString, Max, MaxLength, Min } from 'class-validator';
+import { IsBoolean, IsInt, IsNotEmpty, IsOptional, IsString, Max, MaxLength, Min, ValidateIf } from 'class-validator';
import { ToStrictBoolean, ToStrictNumber } from '../../../common/utils/strict-boolean';
import { MESSAGE_TEXT_MAX_LENGTH } from '../../message/dto/send-message.dto';
import { WebhookFilters } from '../../webhook/filters/filter-types';
@@ -65,7 +65,8 @@ export class CreateAutomationRuleDto {
export class UpdateAutomationRuleDto {
@ApiPropertyOptional({ description: 'Display name for the rule', maxLength: 100 })
- @IsOptional()
+ // Not @IsOptional on the NOT NULL columns: that also skips null, which then reaches save() as a 500.
+ @ValidateIf((o: UpdateAutomationRuleDto) => o.name !== undefined)
@IsString()
@IsNotEmpty()
@MaxLength(100)
@@ -75,7 +76,7 @@ export class UpdateAutomationRuleDto {
description: 'Text sent back into the chat when the rule matches',
maxLength: MESSAGE_TEXT_MAX_LENGTH,
})
- @IsOptional()
+ @ValidateIf((o: UpdateAutomationRuleDto) => o.replyText !== undefined)
@IsString()
@IsNotEmpty()
@MaxLength(MESSAGE_TEXT_MAX_LENGTH)
@@ -87,7 +88,7 @@ export class UpdateAutomationRuleDto {
conditions?: WebhookFilters | null;
@ApiPropertyOptional({ description: COOLDOWN_DESCRIPTION, minimum: 0, maximum: AUTOMATION_COOLDOWN_MAX_SECONDS })
- @IsOptional()
+ @ValidateIf((o: UpdateAutomationRuleDto) => o.cooldownSeconds !== undefined)
@ToStrictNumber()
@IsInt()
@Min(0)
@@ -95,7 +96,7 @@ export class UpdateAutomationRuleDto {
cooldownSeconds?: number;
@ApiPropertyOptional({ description: 'Whether the rule is active' })
- @IsOptional()
+ @ValidateIf((o: UpdateAutomationRuleDto) => o.enabled !== undefined)
@ToStrictBoolean()
@IsBoolean()
enabled?: boolean;
diff --git a/src/modules/integration/dto/instance.dto.spec.ts b/src/modules/integration/dto/instance.dto.spec.ts
index 7936497a8..e3aa72adf 100644
--- a/src/modules/integration/dto/instance.dto.spec.ts
+++ b/src/modules/integration/dto/instance.dto.spec.ts
@@ -14,6 +14,11 @@ describe('UpdateInstanceDto', () => {
await expect(through({ sessionScope: null })).resolves.toEqual({ sessionScope: null });
});
+ // plugin_instances.enabled is NOT NULL: a null that got past validation reached save() and answered 500.
+ it('rejects enabled: null with a 400', async () => {
+ await expect(through({ enabled: null })).rejects.toBeInstanceOf(BadRequestException);
+ });
+
it('still rejects an empty sessionScope', async () => {
await expect(through({ sessionScope: '' })).rejects.toBeInstanceOf(BadRequestException);
});
diff --git a/src/modules/integration/dto/instance.dto.ts b/src/modules/integration/dto/instance.dto.ts
index a6c64aff7..0e3eab06d 100644
--- a/src/modules/integration/dto/instance.dto.ts
+++ b/src/modules/integration/dto/instance.dto.ts
@@ -1,4 +1,14 @@
-import { IsBoolean, IsNotEmpty, IsObject, IsOptional, IsString, Matches, MaxLength, MinLength } from 'class-validator';
+import {
+ IsBoolean,
+ IsNotEmpty,
+ IsObject,
+ IsOptional,
+ IsString,
+ Matches,
+ MaxLength,
+ MinLength,
+ ValidateIf,
+} from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IngressUrl } from '../ingress-url';
import { ToStrictBoolean } from '../../../common/utils/strict-boolean';
@@ -65,7 +75,8 @@ export class UpdateInstanceDto {
example: true,
})
@ToStrictBoolean()
- @IsOptional()
+ // Not @IsOptional: that also skips null, which then reaches the NOT NULL column as a 500.
+ @ValidateIf((o: UpdateInstanceDto) => o.enabled !== undefined)
@IsBoolean()
enabled?: boolean;