Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): judge an instance row per host key, and without a schema
Yudhi Armyndharisb6f06d44988f
diff --git a/src/core/plugins/plugin-activation.spec.ts b/src/core/plugins/plugin-activation.spec.tsindex 37a7ba22d..8505b66e0 100644--- a/src/core/plugins/plugin-activation.spec.ts+++ b/src/core/plugins/plugin-activation.spec.ts@@ -268,6 +268,43 @@ describe('an instance row that names no host, over an override that moved the ho);});+ // With no schema nothing can be told secret, so a row on a moved host keeps nothing, as the layers beneath it do.+ it('keeps nothing when the manifest declares no schema and the override moved the host', () => {+ const schemaless = { net: adapter.net };+ const resolved = resolvePluginConfig(baseConfig, { s: { baseUrl: 'https://evil.example' } }, 's', true, schemaless);+ expect(resolveInstanceConfig(resolved, row, schemaless, baseConfig)).toEqual({ baseUrl: 'https://evil.example' });+ });++ // A plugin may name several hosts. A row that names one of them still leaves the others to the layers beneath.+ describe('with two host keys', () => {+ const two: ConfigLayerManifest = {+ net: { allowConfigHosts: ['baseUrl', 'mediaUrl'] },+ configSchema: {+ type: 'object',+ properties: {+ baseUrl: { type: 'string' },+ mediaUrl: { type: 'string' },+ apiToken: { type: 'string', secret: true },+ },+ },+ };+ const base2 = { baseUrl: 'https://chat.example.com', mediaUrl: 'https://media.example.com', apiToken: 'BASE' };+ const row2 = { baseUrl: 'https://chat.example.com', apiToken: 'ROW-TOKEN' };++ it('withholds the secrets when an override moved a key the row leaves unset', () => {+ const resolved = resolvePluginConfig(base2, { s: { mediaUrl: 'https://evil.example' } }, 's', true, two);+ expect(resolveInstanceConfig(resolved, row2, two, base2)).not.toHaveProperty('apiToken');+ });++ it('keeps them when the override moved only a key the row names itself', () => {+ const resolved = resolvePluginConfig(base2, { s: { baseUrl: 'https://evil.example' } }, 's', true, two);+ expect(resolveInstanceConfig(resolved, row2, two, base2)).toMatchObject({+ baseUrl: 'https://chat.example.com',+ apiToken: 'ROW-TOKEN',+ });+ });+ });+it('is unchanged when the caller gives no base', () => {expect(resolveInstanceConfig(resolvedWith({ baseUrl: 'https://evil.example' }), row, adapter)).toMatchObject({apiToken: 'ROW-TOKEN',@@ -325,6 +362,17 @@ describe('restorableConfig: the stored layer a write may restore a masked secretinboxId: '3',});});++ // A stored host that is a list or blank text is not "no host": the secret was saved beside it, so a write+ // naming the base host does not get that secret back.+ it('does not treat a stored host that is a list or blank text as unset', () => {+ for (const odd of [['https://cw.example'], '', 5]) {+ const storedOdd = { baseUrl: odd, apiToken: 'OWN-TOKEN' };+ expect(+ restorableConfig(storedOdd, { baseUrl: 'https://cw.example', apiToken: '***' }, adapter, beneath),+ ).not.toHaveProperty('apiToken');+ }+ });});it('is the stored layer when the manifest names no host key', () => {diff --git a/src/core/plugins/plugin-activation.ts b/src/core/plugins/plugin-activation.tsindex 752b73bee..bb55acc28 100644--- a/src/core/plugins/plugin-activation.ts+++ b/src/core/plugins/plugin-activation.ts@@ -145,8 +145,10 @@ export function restorableConfig(): Record<string, unknown> | undefined {const hostKeys = manifest.net?.allowConfigHosts;if (!stored || !hostKeys?.length || !isPlainObject(incoming)) return stored;+ // Only a key the stored layer leaves unset is bound to the layer beneath; any value it holds, a list or a+ // blank text included, stays the host it is bound to, which no named host then equals.const bound = Object.fromEntries(- hostKeys.map(key => [key, hostOf(stored[key]) === null ? beneath[key] : stored[key]]),+ hostKeys.map(key => [key, stored[key] === undefined || stored[key] === null ? beneath[key] : stored[key]]),);if (!redirectsHost(bound, incoming, hostKeys)) return stored;const properties = manifest.configSchema?.properties;@@ -168,8 +170,8 @@ export function restorableConfig(* scope slice once siblings exist, for exactly that reason.** A row that names another host than the layer beneath does not inherit that layer's secrets, see- * {@link mergeLayer}. A row that names no host loses its own secrets when `base` is given and the layers- * beneath it moved a host key away from the base.+ * {@link mergeLayer}. A row that leaves a host key unset loses its own secrets when `base` is given and the+ * layers beneath it moved that key away from the base; with no schema to tell its secrets it keeps nothing.*/export function resolveInstanceConfig(resolved: Record<string, unknown>,@@ -180,17 +182,15 @@ export function resolveInstanceConfig(if (!instanceConfig) return resolved;const hostKeys = manifest.net?.allowConfigHosts;const properties = manifest.configSchema?.properties;- // A row that names no host key holds secrets bound to the host of the base config it was provisioned- // against. When the layers beneath it have moved a host key away from the base (a per-session override- // written by a session-restricted admin), those secrets would reach the new host, so the row keeps its- // other keys and loses its secrets.- const moved =- !!base && !!hostKeys?.length && !namesHost(instanceConfig, hostKeys) && redirectsHost(base, resolved, hostKeys);- return mergeLayer(- resolved,- moved && properties ? withoutSecrets(instanceConfig, properties) : instanceConfig,- manifest,- );+ // A host key the row leaves unset resolves from the layers beneath it, and the secrets the row holds are+ // bound to the base host it was provisioned against. When those layers have moved such a key away from the+ // base (a per-session override written by a session-restricted admin), the secrets would reach the new+ // host, so the row keeps its other keys and loses its secrets. Without a schema nothing can be told secret,+ // and so nothing is kept, as in mergeLayer. A key the row names itself is its own and is not checked.+ const unset = hostKeys?.filter(key => !namesHost(instanceConfig, [key])) ?? [];+ const moved = !!base && unset.length > 0 && redirectsHost(base, resolved, unset);+ const row = moved ? (properties ? withoutSecrets(instanceConfig, properties) : {}) : instanceConfig;+ return mergeLayer(resolved, row, manifest);}/**