Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
feat(plugins): derive net.fetch allowlist from config-declared hosts (#588)
Yudhi Armyndharis0245ef6bd510
diff --git a/src/core/plugins/plugin-loader.service.ts b/src/core/plugins/plugin-loader.service.tsindex 6c16a48e7..a3c470cc4 100644--- a/src/core/plugins/plugin-loader.service.ts+++ b/src/core/plugins/plugin-loader.service.ts@@ -24,7 +24,7 @@ import {PluginLogger,validateIngressManifest,} from './plugin.interfaces';-import { isNetHostAllowed, performPluginFetch } from './plugin-net';+import { effectiveNetAllow, isNetHostAllowed, performPluginFetch } from './plugin-net';import { PluginStorageService } from './plugin-storage.service';import { isPluginActiveForSession, resolvePluginConfig } from './plugin-activation';import { PluginWorkerHost } from './sandbox/plugin-worker-host';@@ -973,12 +973,24 @@ export class PluginLoaderService implements OnModuleInit, OnModuleDestroy {} satisfies PluginEngineReadCapability,net: {fetch: async (url, init) => {- // Two gates: the declared permission, then the manifest host allowlist. The SSRF guard- // inside performPluginFetch still blocks internal IPs even when the host is allowlisted.+ // Two gates: the declared permission, then the effective host allowlist = manifest net.allow+ // UNION the hosts of net.allowConfigHosts keys across the base config AND every per-session+ // override. The host gate has no firing-session context for a sandboxed plugin's cap round-trip,+ // so admit every operator-configured tenant host (all public + still SSRF-guarded at connect)+ // rather than resolving a single, possibly wrong (base-only), one. The SSRF guard inside+ // performPluginFetch still blocks internal IPs even when the host is allowlisted.this.assertPermission(plugin.manifest, PluginCapabilityPermission.NET_FETCH);- if (!isNetHostAllowed(plugin.manifest.net?.allow, url)) {+ const netConfigs = [plugin.config ?? {}, ...Object.values(plugin.sessionConfig ?? {})];+ const allow = [+ ...new Set(+ netConfigs.flatMap(cfg =>+ effectiveNetAllow(plugin.manifest.net?.allow, plugin.manifest.net?.allowConfigHosts, cfg),+ ),+ ),+ ];+ if (!isNetHostAllowed(allow, url)) {throw new PluginCapabilityError(- `Plugin ${plugin.manifest.id} may not fetch ${url} — add its host to the manifest net.allow list`,+ `Plugin ${plugin.manifest.id} may not fetch ${url} — add its host to net.allow or net.allowConfigHosts`,);}return performPluginFetch(url, init);diff --git a/src/core/plugins/plugin-net.spec.ts b/src/core/plugins/plugin-net.spec.tsindex 89d075c49..cc1f61fd8 100644--- a/src/core/plugins/plugin-net.spec.ts+++ b/src/core/plugins/plugin-net.spec.ts@@ -1,4 +1,4 @@-import { isNetHostAllowed, performPluginFetch } from './plugin-net';+import { effectiveNetAllow, isNetHostAllowed, performPluginFetch } from './plugin-net';import type { withSafeFetch } from '../../common/security/ssrf-guard';/** A stand-in for withSafeFetch that hands `use` a canned Response and records the init it was given. */@@ -96,3 +96,25 @@ describe('performPluginFetch', () => {await expect(performPluginFetch('https://api.example.com/t', {}, { fetch: fetcher })).rejects.toThrow(/cap/i);});});++describe('effectiveNetAllow', () => {+ it('adds the host of each named config URL to the static allowlist', () => {+ expect(effectiveNetAllow(['api.static.com'], ['baseUrl'], { baseUrl: 'https://chat.acme.com' })).toEqual([+ 'api.static.com',+ 'chat.acme.com',+ ]);+ });+ it('ignores missing / non-string / non-https / credentialed config values', () => {+ expect(effectiveNetAllow([], ['baseUrl'], {})).toEqual([]);+ expect(effectiveNetAllow([], ['baseUrl'], { baseUrl: 42 })).toEqual([]);+ expect(effectiveNetAllow([], ['baseUrl'], { baseUrl: 'not a url' })).toEqual([]);+ expect(effectiveNetAllow([], ['baseUrl'], { baseUrl: 'http://x' })).toEqual([]); // https-only+ expect(effectiveNetAllow([], ['baseUrl'], { baseUrl: 'https://u:p@x' })).toEqual([]); // no credentials+ });+ it("never admits the '*' wildcard sentinel from a config value, and preserves an explicit port", () => {+ expect(effectiveNetAllow([], ['baseUrl'], { baseUrl: 'https://*' })).toEqual([]); // bare '*' would open all hosts+ expect(effectiveNetAllow([], ['baseUrl'], { baseUrl: 'https://%2A' })).toEqual([]); // encoded '*' too+ expect(effectiveNetAllow([], ['baseUrl'], { baseUrl: 'https://*:443/x' })).toEqual([]);+ expect(effectiveNetAllow([], ['baseUrl'], { baseUrl: 'https://host.com:8443' })).toEqual(['host.com:8443']);+ });+});diff --git a/src/core/plugins/plugin-net.ts b/src/core/plugins/plugin-net.tsindex 6f714c875..104ce981b 100644--- a/src/core/plugins/plugin-net.ts+++ b/src/core/plugins/plugin-net.ts@@ -27,6 +27,33 @@ export interface PluginNetResponse {body: string;}+/**+ * The effective outbound-host allowlist for a plugin: its static manifest `net.allow` plus the host of+ * every `net.allowConfigHosts` config key that resolves to an https URL. Lets a marketplace adapter reach+ * an operator-configured host (e.g. a Chatwoot base URL) without `net.allow:['*']`. Credentialed or+ * non-https values are ignored; the SSRF guard still blocks private IPs at connect regardless.+ */+export function effectiveNetAllow(+ allow: string[] | undefined,+ allowConfigHosts: string[] | undefined,+ config: Record<string, unknown>,+): string[] {+ const out = [...(allow ?? [])];+ for (const key of allowConfigHosts ?? []) {+ const raw = config[key];+ if (typeof raw !== 'string') continue;+ try {+ const u = new URL(raw);+ if (u.protocol !== 'https:' || u.username || u.password) continue;+ if (u.hostname.includes('*')) continue; // never let a config value inject the '*' wildcard sentinel+ out.push(u.host); // host:port when a port is set, else bare host+ } catch {+ // Not a URL — skip.+ }+ }+ return out;+}+/*** Is `url` allowed by a plugin's manifest `net.allow` list? Deny-by-default. `'*'` allows any host* (the SSRF guard still blocks internal IPs at connect time); an entry may be `host:port` (exact) ordiff --git a/src/core/plugins/plugin.interfaces.ts b/src/core/plugins/plugin.interfaces.tsindex ff3931331..ddf588ce2 100644--- a/src/core/plugins/plugin.interfaces.ts+++ b/src/core/plugins/plugin.interfaces.ts@@ -90,8 +90,9 @@ export interface PluginManifest {// Outbound-HTTP host allowlist for `ctx.net.fetch` (requires the `net:fetch` permission). Each// entry is `host:port` (exact) or a bare `host` (any port); `'*'` allows any public host. Absent /- // empty = deny all. The SSRF guard still blocks internal IPs regardless of this list.- net?: { allow?: string[] };+ // empty = deny all. `allowConfigHosts` additionally admits the host of each named config key (e.g. an+ // operator-set base URL), resolved at fetch time. The SSRF guard still blocks internal IPs regardless.+ net?: { allow?: string[]; allowConfigHosts?: string[] };// Localized dashboard text (name/description/config field titles) per locale code. English is the// base manifest + fallback. Dashboard-only; does not affect runtime behavior.