Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

refactor(plugins): drop the unused ingress subscription set

Yudhi Armyndharis797af9557c29
diff --git a/src/core/plugins/plugin-sandbox-bridge.ts b/src/core/plugins/plugin-sandbox-bridge.ts
index a332193f3..4a4acd4af 100644
--- a/src/core/plugins/plugin-sandbox-bridge.ts
+++ b/src/core/plugins/plugin-sandbox-bridge.ts
@@ -321,19 +321,14 @@ export class PluginSandboxBridge {
const onHookSubscribe = this.buildHookSubscribeHandler(pluginId, plugin);
- // When the worker claims an ingress route, record it against the manifest-declared routes so the
- // host knows which routes this worker will handle. Same hardening as onHookSubscribe (the wire
- // `route` is an arbitrary untrusted string): drop when the manifest lacks 'webhook:ingress', drop
- // an undeclared route (warn once), dedup, and cap. subscribedRoutes is local to this enable call,
- // so it is dropped on disable exactly as subscribedEvents is.
- const subscribedRoutes = new Set<string>();
- const declaredRoutes = new Set((plugin.manifest.ingress ?? []).map(r => r.route));
+ // When the worker claims an ingress route, check the claim against the manifest-declared routes
+ // and log an undeclared one (warn once). Same hardening as onHookSubscribe (the wire `route` is an
+ // arbitrary untrusted string). Nothing is recorded: dispatch never consults the claim, and the
+ // worker answers 404 for a route it never registered.
const onWebhookSubscribe = makeOnWebhookSubscribe({
pluginId,
- declaredRoutes,
+ declaredRoutes: new Set((plugin.manifest.ingress ?? []).map(r => r.route)),
hasPermission: (plugin.manifest.permissions ?? []).includes(PluginCapabilityPermission.WEBHOOK_INGRESS),
- subscribed: subscribedRoutes,
- maxRoutes: declaredRoutes.size,
warn: (message, meta) => this.logger.warn(message, meta),
});
diff --git a/src/core/plugins/webhook-subscribe.spec.ts b/src/core/plugins/webhook-subscribe.spec.ts
index 365eb2155..11775b5e5 100644
--- a/src/core/plugins/webhook-subscribe.spec.ts
+++ b/src/core/plugins/webhook-subscribe.spec.ts
@@ -3,88 +3,35 @@ import { makeOnWebhookSubscribe } from './webhook-subscribe.util';
describe('onWebhookSubscribe hardening', () => {
const declaredRoutes = new Set(['chatwoot']);
- it('registers a declared route once and dedups repeats', () => {
- const subscribed = new Set<string>();
- const on = makeOnWebhookSubscribe({
- pluginId: 'p',
- declaredRoutes,
- hasPermission: true,
- subscribed,
- maxRoutes: 8,
- warn: jest.fn(),
- });
+ it('accepts a declared route without a warning', () => {
+ const warn = jest.fn();
+ const on = makeOnWebhookSubscribe({ pluginId: 'p', declaredRoutes, hasPermission: true, warn });
on('chatwoot');
on('chatwoot');
- expect([...subscribed]).toEqual(['chatwoot']);
- });
-
- it('silently drops a route the manifest never declared', () => {
- const subscribed = new Set<string>();
- makeOnWebhookSubscribe({
- pluginId: 'p',
- declaredRoutes,
- hasPermission: true,
- subscribed,
- maxRoutes: 8,
- warn: jest.fn(),
- })('unknown');
-
- expect(subscribed.size).toBe(0);
+ expect(warn).not.toHaveBeenCalled();
});
it('warns at most once about undeclared routes so a flood is not a log-flood vector', () => {
- const subscribed = new Set<string>();
const warn = jest.fn();
- const on = makeOnWebhookSubscribe({
- pluginId: 'p',
- declaredRoutes,
- hasPermission: true,
- subscribed,
- maxRoutes: 8,
- warn,
- });
+ const on = makeOnWebhookSubscribe({ pluginId: 'p', declaredRoutes, hasPermission: true, warn });
on('x');
on('y');
on('z');
expect(warn).toHaveBeenCalledTimes(1);
+ expect(warn).toHaveBeenCalledWith(
+ expect.stringContaining('undeclared ingress route'),
+ expect.objectContaining({ pluginId: 'p', route: 'x', action: 'sandbox_unknown_ingress_route' }),
+ );
});
it('silently drops all routes when the manifest lacks webhook:ingress', () => {
- const subscribed = new Set<string>();
const warn = jest.fn();
- makeOnWebhookSubscribe({
- pluginId: 'p',
- declaredRoutes,
- hasPermission: false,
- subscribed,
- maxRoutes: 8,
- warn,
- })('chatwoot');
+ makeOnWebhookSubscribe({ pluginId: 'p', declaredRoutes, hasPermission: false, warn })('unknown');
- expect(subscribed.size).toBe(0);
expect(warn).not.toHaveBeenCalled();
});
-
- it('size-caps the subscribed set', () => {
- const subscribed = new Set<string>();
- const routes = new Set(['a', 'b', 'c']);
- const on = makeOnWebhookSubscribe({
- pluginId: 'p',
- declaredRoutes: routes,
- hasPermission: true,
- subscribed,
- maxRoutes: 2,
- warn: jest.fn(),
- });
-
- on('a');
- on('b');
- on('c');
-
- expect(subscribed.size).toBe(2);
- });
});
diff --git a/src/core/plugins/webhook-subscribe.util.ts b/src/core/plugins/webhook-subscribe.util.ts
index 5cbc3f7f0..8eea153da 100644
--- a/src/core/plugins/webhook-subscribe.util.ts
+++ b/src/core/plugins/webhook-subscribe.util.ts
@@ -1,19 +1,18 @@
/**
- * Pure guard logic for a sandboxed worker's `webhook-subscribe` messages, mirroring the loader's
+ * Validation for a sandboxed worker's `webhook-subscribe` messages, mirroring the loader's
* onHookSubscribe hardening. The IPC boundary is untrusted: a hostile/buggy worker can post
- * `webhook-subscribe` with an undeclared route, the same route repeatedly, or a flood of fabricated
- * routes. Without guards each call records a live host-side ingress registration (unbounded growth).
+ * `webhook-subscribe` with an undeclared route, or a flood of fabricated ones.
*
- * Three guards, all keyed off the manifest (which the operator authored, not the worker): drop when
- * the manifest lacks `webhook:ingress` (silent — the plugin has no ingress at all), drop an
- * undeclared route (warn at most once so a flood isn't a log-flood vector), dedup, and a size cap.
+ * The claim is only checked and logged, never recorded: dispatch goes to whatever manifest-declared
+ * route the ingress layer resolved, and the worker answers 404 for a route it never registered. Keyed
+ * off the manifest (which the operator authored, not the worker): drop when the manifest lacks
+ * `webhook:ingress` (silent, since the plugin has no ingress at all), and drop an undeclared route with
+ * at most one warning, so a flood isn't a log-flood vector.
*/
export interface OnWebhookSubscribeDeps {
pluginId: string;
declaredRoutes: Set<string>; // routes from manifest.ingress[].route
hasPermission: boolean; // manifest declares webhook:ingress
- subscribed: Set<string>;
- maxRoutes: number;
warn: (message: string, meta: Record<string, unknown>) => void;
}
@@ -21,19 +20,12 @@ export function makeOnWebhookSubscribe(deps: OnWebhookSubscribeDeps): (route: st
let unknownRouteWarned = false;
return (route: string): void => {
if (!deps.hasPermission) return; // no ingress permission => the plugin claims no routes at all
- if (!deps.declaredRoutes.has(route)) {
- if (!unknownRouteWarned) {
- unknownRouteWarned = true; // warn at most once per plugin so a flood isn't a log-flood vector
- deps.warn(`Sandboxed plugin ${deps.pluginId} subscribed to an undeclared ingress route; ignoring`, {
- pluginId: deps.pluginId,
- route,
- action: 'sandbox_unknown_ingress_route',
- });
- }
- return;
- }
- if (deps.subscribed.has(route)) return;
- if (deps.subscribed.size >= deps.maxRoutes) return; // belt-and-suspenders cap
- deps.subscribed.add(route);
+ if (deps.declaredRoutes.has(route) || unknownRouteWarned) return;
+ unknownRouteWarned = true; // warn at most once per plugin so a flood isn't a log-flood vector
+ deps.warn(`Sandboxed plugin ${deps.pluginId} subscribed to an undeclared ingress route; ignoring`, {
+ pluginId: deps.pluginId,
+ route,
+ action: 'sandbox_unknown_ingress_route',
+ });
};
}