Repository navigation
feat(webhook): add excludeFields and includeFields payload filters (#2695) - #2802
zanarellidev wants to merge 2 commits into
Conversation
Reviewer's GuideSchema and migrationsAdd nullable webhook filter columns and their migrations across the supported database schemas.
Data structuresExtend webhook configuration types with include and exclude field settings used by event and instance payloads.
APIExpose includeFields and excludeFields as supported webhook configuration properties in the webhook and instance validation schemas.
Add filter settings to the event and instance DTO webhook shapes.
Uses of the APIPropagate configured field filters when building and updating instance webhook settings.
Filter local webhook data before logging and dispatch while retaining the original payload when no filters change it.
ImplementationImplement non-mutating include and exclude filtering for nested object and array payload paths, with invalid-path hardening.
TestsAdd unit tests for filtering behavior, arrays, invalid paths, precedence, and input immutability.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 5 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src/api/integrations/event/webhook/webhook-payload-filter.ts" line_range="179" />
<code_context>
+ */
+export function filterWebhookPayload<T>(data: T, options?: WebhookPayloadFilterOptions): T {
+ if (data === null || data === undefined || typeof data !== 'object') {
+ return data;
+ }
+
</code_context>
<issue_to_address>
**Invalid include paths expose full payload**
When `includeFields` is configured with only forbidden or otherwise ignored paths, such as `['constructor.prototype']`, `normalizeFieldPaths` drops forbidden or otherwise unusable paths, so an `includeFields` list containing only those paths leaves both normalized lists empty and this branch returns the original data. The local webhook sends fields the caller asked to exclude by using an include-only filter.
Distinguish an absent/empty include configuration from a nonempty configuration that normalizes to no usable paths, and return an empty payload for the latter.
</issue_to_address>
### Comment 2
<location path="src/api/dto/instance.dto.ts" line_range="40-41" />
<code_context>
url?: string;
byEvents?: boolean;
base64?: boolean;
+ excludeFields?: string[];
+ includeFields?: string[];
};
chatwootAccountId?: string;
</code_context>
<issue_to_address>
**Malformed filters are silently ignored**
When a caller supplies a non-array `excludeFields` or `includeFields` value to instance creation, `instanceSchema` does not validate the nested `webhook` filters accepted by `InstanceDto`, so instance creation stores a non-array value; `normalizeFieldPaths` then ignores it and dispatches the webhook without the requested filtering.
Add nested webhook filter array definitions to `instanceSchema` so malformed values are rejected.
</issue_to_address>
### Comment 3
<location path="src/api/integrations/event/webhook/webhook-payload-filter.ts" line_range="52" />
<code_context>
+ }
+ return cloned as T;
+ }
+ return value;
+}
+
</code_context>
<issue_to_address>
**Filtering mutates shared event data**
When the payload contains a non-plain object with an own property targeted by `excludeFields`, `cloneValue` returns non-plain objects by reference, then `omitPath` can delete a nested field from that same object. The filter mutates the original event payload, affecting other consumers of the shared data.
Clone supported non-plain payload objects before applying exclusions, or reject/skip exclusions within values that cannot be safely copied.
</issue_to_address>
### Comment 4
<location path="src/api/integrations/event/webhook/webhook-payload-filter.ts" line_range="170" />
<code_context>
+ *
+ * When no filters are configured, the original `data` reference is returned unchanged.
+ */
+export function filterWebhookPayload<T>(data: T, options?: WebhookPayloadFilterOptions): T {
+ if (data === null || data === undefined || typeof data !== 'object') {
+ return data;
</code_context>
<issue_to_address>
**Filtered results retain full type**
When a caller uses `includeFields` and then relies on a property required by `T`, `includeFields` removes properties from the result, but the exported function still promises `T`; callers can therefore access omitted required properties without a TypeScript error and receive `undefined` at runtime.
Return a type that reflects filtering (or an explicitly unknown/partial result) instead of promising the original `T`.
</issue_to_address>
### Comment 5
<location path="src/api/integrations/event/webhook/webhook-payload-filter.ts" line_range="33-39" />
<code_context>
+ return paths;
+}
+
+function isPlainObject(value: unknown): value is Record<string, any> {
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
+ return false;
+ }
+ const proto = Object.getPrototypeOf(value);
+ return proto === Object.prototype || proto === null;
</code_context>
<issue_to_address>
**Payload filtering loses type safety**
The filter implementation uses `any` for payloads, intermediate results, and container types across its helpers, so TypeScript cannot check property access or mutations on this path. The new utility therefore does not meet the repository's requirement to avoid `any` and use proper typing.
Replace `any` with suitable `unknown`, JSON-value, and typed container/result types, narrowing values before accessing or mutating them.
Also at `src/api/integrations/event/webhook/webhook-payload-filter.ts:46-51`, `src/api/integrations/event/webhook/webhook-payload-filter.ts:55-162`, `src/api/integrations/event/webhook/webhook-payload-filter.ts:183-212`.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 4 findings to address first, and the filters change the data sent in webhook requests to external endpoints; an incorrect filter can send an incomplete or otherwise wrong payload that a recipient may retain or act on. Reverting restores future payloads but cannot retract requests already delivered.
Blocking findings: src/api/integrations/event/webhook/webhook-payload-filter.ts:179, src/api/dto/instance.dto.ts:41, src/api/integrations/event/webhook/webhook-payload-filter.ts:52, src/api/integrations/event/webhook/webhook-payload-filter.ts:170
|
Addressed the review findings in
@sourcery-ai review |
📋 Description
Adds optional
excludeFieldsandincludeFieldspayload filtering to local instance webhooks so users can omit heavy fields (e.g.base64) or restrict dispatched webhook payloads to specific fields.Summary of Changes
filterWebhookPayload(data, { excludeFields, includeFields })insrc/api/integrations/event/webhook/webhook-payload-filter.ts:message.imageMessage).includeFieldsand preserves indices across multi-indexexcludeFieldsremovals.__proto__,constructor,prototype).datareference unchanged when no filters are configured, and never mutates the shared internal event object used by global webhooks or other integrations.excludeFieldsandincludeFieldsacrossWebhookController(setandemit),webhookSchema,EventDto/InstanceDto,EventManager.setInstance,InstanceController, andwa.LocalWebHook.excludeFields Json?andincludeFields Json?columns tomodel Webhookinpostgresql,psql_bouncer, andmysqlPrisma schemas along with additive migrations.src/api/integrations/event/webhook/webhook-payload-filter.test.ts.🔗 Related Issue
Closes #2695
🧪 Type of Change
🧪 Testing
npm run lint:check(0 errors, 0 warnings)npm run db:generate && npm run build(tsc --noEmit && tsup, 0 errors)npx tsx --test src/api/integrations/event/webhook/webhook-payload-filter.test.ts(10/10 tests passed)✅ Checklist
Summary by Sourcery
Add configurable payload field filtering to local webhooks so dispatched data can be reduced to selected fields or have unwanted fields omitted.
New Features:
Enhancements:
Tests:
Chores: