Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(automation): skip a rule whose stored conditions is not an array

Yudhi Armyndharis42d38e2e171b
diff --git a/src/modules/automation/automation-rules.service.spec.ts b/src/modules/automation/automation-rules.service.spec.ts
index c12d47f7c..dafc60736 100644
--- a/src/modules/automation/automation-rules.service.spec.ts
+++ b/src/modules/automation/automation-rules.service.spec.ts
@@ -239,6 +239,10 @@ describe('AutomationRulesService', () => {
it.each([
['a null condition', { conditions: [null] }, inbound()],
['a non-list conditions value on a broadcast message', { conditions: 'x' }, inbound({ kind: 'broadcast' })],
+ ['a non-list conditions value', { conditions: 'x' }, inbound()],
+ ['an object as the conditions value', { conditions: {} }, inbound()],
+ ['a string as the conditions object', 'x', inbound()],
+ ['an array as the conditions object', [], inbound()],
])('a rule with %s is skipped on its own; later rules still answer', async (_label, conditions, message) => {
const broken = await service.create('sessA', { name: 'broken', replyText: 'broken-reply', cooldownSeconds: 0 });
// Stored the way a restore writes it: the DTO would refuse this value.
diff --git a/src/modules/automation/automation-rules.service.ts b/src/modules/automation/automation-rules.service.ts
index 5fa771f5a..64415248c 100644
--- a/src/modules/automation/automation-rules.service.ts
+++ b/src/modules/automation/automation-rules.service.ts
@@ -177,9 +177,20 @@ export class AutomationRulesService {
// order (creation order) is the tiebreak the operator can reason about. A rule without a `kind`
// condition skips the opt-in chat kinds; naming the kind is how a rule reaches them. A rule whose
// stored conditions are malformed (a restore bypasses the DTO) is skipped on its own, so it cannot
- // silence every other rule of the session.
+ // silence every other rule of the session. A `conditions` that is not a plain object, or a
+ // non-array `conditions.conditions`, is refused explicitly: evaluateFilters reads either as "no
+ // filter", which would answer every inbound message.
const rule = rules.find(candidate => {
try {
+ const conditions: unknown = candidate.conditions;
+ if (
+ conditions != null &&
+ (typeof conditions !== 'object' ||
+ Array.isArray(conditions) ||
+ (candidate.conditions?.conditions != null && !Array.isArray(candidate.conditions.conditions)))
+ ) {
+ throw new TypeError('conditions must be an object with a conditions array');
+ }
return (
(!optInOnly || candidate.conditions?.conditions?.some(c => c.field === 'kind')) &&
evaluateFilters(candidate.conditions, 'message.received', message, resolveLid)