Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): refuse a masked-secret removal that also adds entries
Yudhi Armyndharis9dad575c1cb9
diff --git a/src/modules/plugins/redact-config.spec.ts b/src/modules/plugins/redact-config.spec.tsindex 35e41f9aa..97ec84e6d 100644--- a/src/modules/plugins/redact-config.spec.ts+++ b/src/modules/plugins/redact-config.spec.ts@@ -320,6 +320,17 @@ describe('restoreSecretConfig (scalar-secret array)', () => {).toThrow(/re-enter/);});+ // Adding keys in the same save grows the array, so the removal must not slip past the check.+ it('rejects a removal paired with additions that leaves masked keys it cannot tell apart', () => {+ expect(() =>+ restoreSecretConfig(+ { keys: [SECRET_SENTINEL, 'new-1', 'new-2'] },+ { keys: ['old', 'keep'] },+ scalarSecretArraySchema,+ ),+ ).toThrow(/re-enter/);+ });+it('accepts a removal once the remaining keys are re-entered', () => {expect(restoreSecretConfig({ keys: ['k2', 'k3'] }, { keys: ['k1', 'k2', 'k3'] }, scalarSecretArraySchema)).toEqual({keys: ['k2', 'k3'],@@ -371,6 +382,29 @@ describe('restoreSecretConfig (row removal among rows that differ only by secret});});+ // The typed row carries no mask, so it must not take a stored row's slot or shift the survivors.+ it('restores every secret when a typed row is prepended before identical masked rows', () => {+ expect(+ restoreSecretConfig(+ {+ endpoints: [+ { url: 'a', token: 't-z' },+ { url: 'a', token: SECRET_SENTINEL },+ { url: 'a', token: SECRET_SENTINEL },+ ],+ },+ { endpoints: stored.endpoints.slice(0, 2) },+ nestedSchema,+ ),+ ).toEqual({+ endpoints: [+ { url: 'a', token: 't-z' },+ { url: 'a', token: 't-a' },+ { url: 'a', token: 't-b' },+ ],+ });+ });+// The unique row sits before or between the identical ones, so every survivor shifts position.it.each([['leading', [{ url: 'c', token: 't-c' }, ...stored.endpoints.slice(0, 2)]],diff --git a/src/modules/plugins/redact-config.ts b/src/modules/plugins/redact-config.tsindex b5efc3463..1526cabe7 100644--- a/src/modules/plugins/redact-config.ts+++ b/src/modules/plugins/redact-config.ts@@ -126,8 +126,16 @@ function restoreValue(}const sameLength = incoming.length === existingArr.length;const sigs = incoming.map(item => elementSignature(item, itemField));+ // Only an element that still carries a mask needs a stored counterpart; one whose secrets were all+ // typed in is stored as provided, so it must not count toward, or take the slot of, a stored row.+ const masked = incoming.map(+ (item: unknown) =>+ stableStringify(restoreValue(item, undefined, itemField)) !== stableStringify({ keep: true, value: item }),+ );const inCount = new Map<string, number>();- for (const s of sigs) inCount.set(s, (inCount.get(s) ?? 0) + 1);+ sigs.forEach((s, i) => {+ if (masked[i]) inCount.set(s, (inCount.get(s) ?? 0) + 1);+ });const seen = new Map<string, number>();return {keep: true,@@ -137,12 +145,13 @@ function restoreValue(const group = bySig.get(s) ?? [];const storedCount = group.length;const k = seen.get(s) ?? 0;- seen.set(s, k + 1);- // A removal among stored rows that share this signature (scalar secrets, or rows that differ- // only by a secret) sends the same payload whichever row went, so no binding is safe: position- // would keep the removed secret and drop a kept one. An element that carries a masked secret- // there is rejected; one whose secrets were all re-entered is stored as provided.- if (incoming.length < existingArr.length && storedCount > 1 && (inCount.get(s) ?? 0) < storedCount) {+ if (masked[i]) seen.set(s, k + 1);+ // Fewer masked elements than stored rows sharing this signature (scalar secrets, or rows that+ // differ only by a secret) means one was removed, and the payload is the same whichever row+ // went, even when the same save adds new ones. No binding is safe: position would keep the+ // removed secret and drop a kept one. An element that carries a masked secret there is+ // rejected; one whose secrets were all re-entered is stored as provided.+ if (!sameLength && storedCount > 1 && (inCount.get(s) ?? 0) < storedCount) {const unbound = restoreValue(item, undefined, itemField);if (stableStringify(unbound) !== stableStringify(restoreValue(item, group[0], itemField))) {throw new BadRequestException(@@ -154,8 +163,8 @@ function restoreValue(// Resolution order: (1) an unambiguous content match keeps a row's secret across// reorder/insert/removal; (2) on an unchanged length, fall back to the positional twin (an// in-place edit — position i is the same logical row, incl. a non-secret-field rename); (3) on a- // length change that kept every stored element with this signature, bind the k-th incoming one- // to the k-th stored one, since a removal before or between them shifts their positions; (4)+ // length change that kept every stored element with this signature, bind the k-th masked incoming+ // one to the k-th stored one, since a removal before or between them shifts their positions; (4)// otherwise bind the positional twin ONLY when its masked signature still equals this element's,// so an append keeps each surviving sentinel's stored secret while a genuinely-new or// signature-changed row at that position is never grafted with a stored secret.