Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): require a permission before a plugin serves search queries
Yudhi Armyndharis28e7850ed395
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex ffb1bae0a..9e2b4d441 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -89,6 +89,29 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0`/api/infra`. It had no reference from `package.json`, any workflow, the Dockerfile, either composefile, or the documentation.+### Security++- **A sandboxed plugin can no longer serve the gateway's search queries without declaring a permission+ for it.** `ctx.registerSearchProvider` is installed unconditionally in every worker context, and a+ plugin declares itself a provider by sending `search-provider-register` over IPC — a path that never+ passes through the capability router gating `ctx.messages` / `ctx.net` / `ctx.engine`. Nothing between+ that declaration and the `SearchProviderRegistry` consulted the manifest, and under the shipped default+ `SEARCH_PROVIDER=auto` a registered provider is also made **active**, superseding `builtin-fts`. A+ plugin that declared no permissions at all could therefore see every query `GET /api/search` serves.++ Registration now requires the new `search:provide` permission. A plugin without it is refused before+ the provider reaches the registry, the active provider is left untouched, and the host logs one+ warning (`sandbox_search_provider_denied`) — bounded to a single line per enable, because+ `WorkerSearchRegistry` posts the declaration only on the plugin's first call. The check is warned+ rather than silently dropped (as the ingress-subscribe guard does) because there is no manifest+ `search` array, so no load-time validation can catch it and an operator would otherwise get no signal.+ `hasPermission` is a required field of `RegisterPluginSearchProviderDeps`, so the compiler — not+ reviewer discipline — is what keeps a future call site from re-opening the gap.++ **Action required for search-provider plugins:** add `"permissions": ["search:provide"]` to the+ manifest. Plugins that do not provide search are unaffected, as are all first-party plugins. No+ gateway payload changes.+## [0.12.1] - 2026-07-30### Addeddiff --git a/docs/19-plugin-architecture.md b/docs/19-plugin-architecture.mdindex 8cf3ef23b..eadd07280 100644--- a/docs/19-plugin-architecture.md+++ b/docs/19-plugin-architecture.md@@ -631,8 +631,9 @@ URL / catalog), not an npm/github source descriptor.### Permission model-There are exactly **five** capability permissions, declared in the manifest `permissions` array — four-enforced at the capability boundary, `webhook:ingress` at load and at route subscription:+There are exactly **six** capability permissions, declared in the manifest `permissions` array — four+enforced at the capability boundary, plus two on the worker-declaration bridges: `webhook:ingress` at+load and at route subscription, and `search:provide` when the provider declaration reaches the host:```typescript// src/core/plugins/plugin.interfaces.ts (doc comments condensed)@@ -648,6 +649,9 @@ export const PluginCapabilityPermission = {WEBHOOK_INGRESS: 'webhook:ingress',/** ctx.conversations.send plus ctx.handover.* and ctx.mappings.* — the normalized outbound surface. */CONVERSATION_SEND: 'conversation:send',+ /** ctx.registerSearchProvider — serve /api/search. Under SEARCH_PROVIDER=auto the provider is also+ * made ACTIVE, so an undeclared plugin would see every query the gateway serves. */+ SEARCH_PROVIDE: 'search:provide',} as const;```@@ -674,6 +678,15 @@ the permission, so the plugin never loads; and at the **ingress-subscribe guard*worker whose manifest lacks the permission is silently dropped (no `PluginCapabilityError`) and the pluginsimply owns no routes.+`search:provide` is enforced the same way, and for the same reason: a worker declares itself a search+provider by sending `search-provider-register` over IPC, which never passes through the capability router+that gates `ctx.messages` / `ctx.net` / `ctx.engine`. `registerPluginSearchProvider` checks the manifest+before the provider reaches the registry. Unlike the ingress guard this one **warns**+(`sandbox_search_provider_denied`) rather than dropping silently: there is no manifest `search` array, so+no load-time validation can catch it, and an operator would otherwise have no signal at all. The warning+is bounded to one line per enable — `WorkerSearchRegistry` posts the declaration only on the plugin's+first `ctx.registerSearchProvider` call.+Two further checks apply on top of the permission:- **Session scope.** Every capability but `net` is session-scoped through `assertSessionActive`, whichdiff --git a/docs/27-plugin-search-providers.md b/docs/27-plugin-search-providers.mdindex 3a66d1440..7ebb31c4d 100644--- a/docs/27-plugin-search-providers.md+++ b/docs/27-plugin-search-providers.md@@ -21,6 +21,20 @@ user-facing feature and the built-in DB-FTS default.## 27.2 The contract+### Declare the permission++The manifest must declare `search:provide`:++```json+{ "id": "meili", "name": "Meilisearch", "version": "1.0.0", "type": "extension",+ "main": "index.cjs", "permissions": ["search:provide"] }+```++Without it the host ignores the registration and logs one warning+(`sandbox_search_provider_denied`); the plugin keeps running and the active provider is unchanged.+The permission is required because under the default `SEARCH_PROVIDER=auto` a registered provider is+also made **active**, so it sees every query `GET /api/search` serves.+### Register the handlerIn `onEnable`, call `ctx.registerSearchProvider(handler)` with a function that takes a `SearchQuery` anddiff --git a/docs/30-plugin-sandboxing.md b/docs/30-plugin-sandboxing.mdindex 6e0102ac0..b38b7576f 100644--- a/docs/30-plugin-sandboxing.md+++ b/docs/30-plugin-sandboxing.md@@ -96,8 +96,9 @@ in-process plugin that calls it fails loud rather than silently never firing —4. **Declare your permissions.** A capability call is denied unless the manifest declares it:`messages:send` for `ctx.messages`, `engine:read` for `ctx.engine`, `net:fetch` for `ctx.net.fetch`(plus a `net.allow` host list), `conversation:send` for `ctx.conversations` / `ctx.handover` /- `ctx.mappings`, and `webhook:ingress` for `ctx.registerWebhook` (enforced at load and again at route- subscription). `ctx.storage` needs no permission — it is already per-plugin scoped. See+ `ctx.mappings`, `webhook:ingress` for `ctx.registerWebhook` (enforced at load and again at route+ subscription), and `search:provide` for `ctx.registerSearchProvider` (enforced when the declaration+ reaches the host). `ctx.storage` needs no permission — it is already per-plugin scoped. See[19 — Plugin Architecture](./19-plugin-architecture.md).Sandboxing was a **breaking change for third-party plugin authoring** when it shipped in v0.6.0.diff --git a/src/core/plugins/plugin-loader.service.spec.ts b/src/core/plugins/plugin-loader.service.spec.tsindex 8089c03f6..435be66ac 100644--- a/src/core/plugins/plugin-loader.service.spec.ts+++ b/src/core/plugins/plugin-loader.service.spec.ts@@ -979,7 +979,15 @@ describe('PluginLoaderService — search-provider worker-crash fallback', () =>fs.mkdirSync(path.join(tmpDir, 'ok'), { recursive: true });fs.writeFileSync(path.join(tmpDir, 'ok', 'manifest.json'),- JSON.stringify({ id: 'ok', name: 'OK', version: '1.0.0', type: 'extension', main: 'index.cjs' }),+ JSON.stringify({+ id: 'ok',+ name: 'OK',+ version: '1.0.0',+ type: 'extension',+ main: 'index.cjs',+ // This suite is about crash fallback, so the fixture must be a plugin the search bridge accepts.+ permissions: ['search:provide'],+ }),);fs.writeFileSync(path.join(tmpDir, 'ok', 'index.cjs'),@@ -1012,6 +1020,38 @@ describe('PluginLoaderService — search-provider worker-crash fallback', () =>expect(registry.list().map(p => p.id)).not.toContain('plugin:ok');expect(registry.active()?.id).toBe('builtin-fts'); // fell back, not pinned to the dead plugin});++ it('never activates a plugin that registers a search provider without declaring search:provide', async () => {+ // Same fixture, permission removed: ctx.registerSearchProvider is installed in EVERY worker context,+ // so this is the whole distance between an undeclared plugin and serving every /search query.+ fs.writeFileSync(+ path.join(tmpDir, 'ok', 'manifest.json'),+ JSON.stringify({ id: 'ok', name: 'OK', version: '1.0.0', type: 'extension', main: 'index.cjs' }),+ );+ const registry = new SearchProviderRegistry();+ registry.register({ id: 'builtin-fts', label: 'b', search: jest.fn(), health: jest.fn() });+ const config = {+ get: (k: string) =>+ k === 'search.provider' ? 'auto' : k === 'plugins.dir' || k === 'dataDir' ? tmpDir : undefined,+ } as unknown as ConfigService;+ const storage = new PluginStorageService(config);+ const loader = new CapturingLoader(config, new HookManager(), storage, {+ get: () => registry,+ } as unknown as ModuleRef);++ loader.loadPlugin(path.join(tmpDir, 'ok'));+ await loader.enablePlugin('ok');++ // Assert BEFORE reaping: terminate() runs onWorkerExit -> unregisterPluginSearchProvider, which+ // drops plugin:ok and restores builtin-fts on its own. Asserting after it would pass whether or+ // not the permission gate works at all. The finally still reaps, so no worker handle leaks.+ try {+ expect(registry.list().map(p => p.id)).not.toContain('plugin:ok');+ expect(registry.active()?.id).toBe('builtin-fts'); // auto mode must NOT hand the gateway to it+ } finally {+ await loader.lastHost!.terminate();+ }+ });});describe('PluginLoaderService — boot-time manifest validation parity with install', () => {diff --git a/src/core/plugins/plugin-loader.service.ts b/src/core/plugins/plugin-loader.service.tsindex 7c5140453..19230fcab 100644--- a/src/core/plugins/plugin-loader.service.ts+++ b/src/core/plugins/plugin-loader.service.ts@@ -1314,7 +1314,9 @@ export class PluginLoaderService implements OnModuleInit, OnApplicationBootstrap// When the worker declares itself a search provider (ctx.registerSearchProvider →// search-provider-register), register a PluginSearchProvider in the SearchProviderRegistry. The host// is in sandboxHosts by the time registration fires (during onLoad/onEnable), so look it up lazily- // like onHookSubscribe. Search disabled (no registry, or SEARCH_PROVIDER=none) → the util skips.+ // like onHookSubscribe. Search disabled (no registry, or SEARCH_PROVIDER=none) → the util skips, and+ // a manifest without 'search:provide' is denied there — the wire declaration is untrusted input, and+ // this bridge bypasses the capability router that gates the ctx.* capabilities.const onSearchProviderRegister = (): void => {const liveHost = this.sandboxHosts.get(pluginId);if (!liveHost) return;@@ -1325,6 +1327,8 @@ export class PluginLoaderService implements OnModuleInit, OnApplicationBootstraptimeoutMs: SANDBOX_SEARCH_TIMEOUT_MS,registry: this.getSearchRegistry(),mode: this.configService.get<string>('search.provider', 'auto'),+ hasPermission: (plugin.manifest.permissions ?? []).includes(PluginCapabilityPermission.SEARCH_PROVIDE),+ warn: (message, meta) => this.logger.warn(message, meta),});};diff --git a/src/core/plugins/plugin.interfaces.ts b/src/core/plugins/plugin.interfaces.tsindex 6850e0d5c..23160e8b6 100644--- a/src/core/plugins/plugin.interfaces.ts+++ b/src/core/plugins/plugin.interfaces.ts@@ -170,6 +170,12 @@ export const PluginCapabilityPermission = {WEBHOOK_INGRESS: 'webhook:ingress',/** `ctx.conversations.send` — normalized outbound send translated to MessageService. */CONVERSATION_SEND: 'conversation:send',+ /**+ * `ctx.registerSearchProvider` — serve the gateway's /search queries. Under the default+ * SEARCH_PROVIDER=auto a registered provider is also made ACTIVE, superseding builtin-fts, so an+ * undeclared plugin would otherwise see every search query the gateway serves.+ */+ SEARCH_PROVIDE: 'search:provide',} as const;export type PluginCapabilityPermission = (typeof PluginCapabilityPermission)[keyof typeof PluginCapabilityPermission];diff --git a/src/core/plugins/search-provider-registration.spec.ts b/src/core/plugins/search-provider-registration.spec.tsindex 1ed3f31ff..df14cb151 100644--- a/src/core/plugins/search-provider-registration.spec.ts+++ b/src/core/plugins/search-provider-registration.spec.ts@@ -11,6 +11,8 @@ const deps = (overrides: Partial<Parameters<typeof registerPluginSearchProvider>timeoutMs: 10000,registry: new SearchProviderRegistry(),mode: 'auto',+ hasPermission: true,+ warn: jest.fn(),...overrides,});@@ -28,6 +30,18 @@ describe('registerPluginSearchProvider', () => {expect(registry.active()?.id).toBe('plugin:meili');});+ it('denies a plugin that never declared the search:provide permission, even in auto mode', () => {+ const registry = new SearchProviderRegistry();+ registry.register(builtin());+ const warn = jest.fn();++ registerPluginSearchProvider(deps({ registry, hasPermission: false, warn }));++ expect(registry.list().map(p => p.id)).not.toContain('plugin:meili');+ expect(registry.active()?.id).toBe('builtin-fts');+ expect(warn).toHaveBeenCalledTimes(1);+ });+it('builtin-fts mode registers the plugin but leaves builtin active', () => {const registry = new SearchProviderRegistry();registry.register(builtin());diff --git a/src/core/plugins/search-provider-registration.util.ts b/src/core/plugins/search-provider-registration.util.tsindex a3e4ac2b1..c3718a674 100644--- a/src/core/plugins/search-provider-registration.util.ts+++ b/src/core/plugins/search-provider-registration.util.ts@@ -11,12 +11,21 @@ export interface RegisterPluginSearchProviderDeps {registry: SearchProviderRegistry | undefined;/** Resolved SEARCH_PROVIDER mode: 'auto' | 'builtin-fts' | 'none'. */mode: string;+ /** manifest declares search:provide */+ hasPermission: boolean;+ warn: (message: string, meta: Record<string, unknown>) => void;}/*** Register a sandboxed plugin as a SearchProvider when it declares itself one (search-provider-register).* Pure policy, extracted from the loader so it is unit-testable without constructing PluginLoaderService:*+ * - no search:provide → deny + warn. Checked FIRST, and warned even when search is off, because an+ * undeclared plugin reaching here is a privilege claim, not a config question:+ * `ctx.registerSearchProvider` is installed unconditionally in every worker+ * context, and this is the only gate between it and the registry (the+ * declaration arrives as an IPC message, so it never passes through the+ * capability router that gates `ctx.messages`/`ctx.net`/`ctx.engine`).* - registry undefined → search module not loaded (SEARCH_ENABLED=false); skip silently.* - mode 'none' → operator disabled search; skip.* - mode 'auto' → register AND setActive, superseding builtin-fts (the documented auto behavior).@@ -25,6 +34,15 @@ export interface RegisterPluginSearchProviderDeps {* Last-registered plugin wins in 'auto' if multiple register (Part 1 limitation).*/export function registerPluginSearchProvider(deps: RegisterPluginSearchProviderDeps): void {+ if (!deps.hasPermission) {+ // Bounded without a latch: WorkerSearchRegistry posts `search-provider-register` only on the+ // plugin's FIRST ctx.registerSearchProvider call, so this is at most one line per enable.+ deps.warn(`Sandboxed plugin ${deps.pluginId} declared a search provider without 'search:provide'; ignoring`, {+ pluginId: deps.pluginId,+ action: 'sandbox_search_provider_denied',+ });+ return;+ }if (!deps.registry) return;if (deps.mode === 'none') return;const provider = new PluginSearchProvider(deps.pluginId, deps.label, deps.transport, deps.timeoutMs);