Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): restore masked secret rows by signature on a shrink

Yudhi Armyndharis489966211d07
diff --git a/src/modules/plugins/redact-config.spec.ts b/src/modules/plugins/redact-config.spec.ts
index a50b7c07c..35e41f9aa 100644
--- a/src/modules/plugins/redact-config.spec.ts
+++ b/src/modules/plugins/redact-config.spec.ts
@@ -308,14 +308,91 @@ describe('restoreSecretConfig (scalar-secret array)', () => {
).toEqual({ keys: ['k1', 'k2', 'k3'] });
});
- it('preserves the remaining stored secrets when the last key is removed (length shrinks)', () => {
- expect(
+ // Removing ANY one key sends the same ['***','***'], so the host cannot tell which stored key went.
+ // Binding by position would keep a removed (possibly revoked) key and drop a kept one: reject instead.
+ it('rejects a removal that leaves masked keys it cannot tell apart', () => {
+ expect(() =>
restoreSecretConfig(
{ keys: [SECRET_SENTINEL, SECRET_SENTINEL] },
{ keys: ['k1', 'k2', 'k3'] },
scalarSecretArraySchema,
),
- ).toEqual({ keys: ['k1', 'k2'] });
+ ).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'],
+ });
+ });
+});
+
+describe('restoreSecretConfig (row removal among rows that differ only by secret)', () => {
+ const stored = {
+ endpoints: [
+ { url: 'a', token: 't-a' },
+ { url: 'a', token: 't-b' },
+ { url: 'c', token: 't-c' },
+ ],
+ };
+
+ it('rejects removing one of two rows whose non-secret content is identical', () => {
+ expect(() =>
+ restoreSecretConfig(
+ {
+ endpoints: [
+ { url: 'a', token: SECRET_SENTINEL },
+ { url: 'c', token: SECRET_SENTINEL },
+ ],
+ },
+ stored,
+ nestedSchema,
+ ),
+ ).toThrow(/re-enter/);
+ });
+
+ it('still restores every secret when the removed row was the only one with its content', () => {
+ expect(
+ restoreSecretConfig(
+ {
+ endpoints: [
+ { url: 'a', token: SECRET_SENTINEL },
+ { url: 'a', token: SECRET_SENTINEL },
+ ],
+ },
+ stored,
+ nestedSchema,
+ ),
+ ).toEqual({
+ endpoints: [
+ { 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)]],
+ ['middle', [stored.endpoints[0], { url: 'c', token: 't-c' }, stored.endpoints[1]]],
+ ])('still restores every secret when the removed unique row was %s', (_, endpoints) => {
+ expect(
+ restoreSecretConfig(
+ {
+ endpoints: [
+ { url: 'a', token: SECRET_SENTINEL },
+ { url: 'a', token: SECRET_SENTINEL },
+ ],
+ },
+ { endpoints },
+ nestedSchema,
+ ),
+ ).toEqual({
+ endpoints: [
+ { url: 'a', token: 't-a' },
+ { url: 'a', token: 't-b' },
+ ],
+ });
});
});
diff --git a/src/modules/plugins/redact-config.ts b/src/modules/plugins/redact-config.ts
index 50b61fd64..b5efc3463 100644
--- a/src/modules/plugins/redact-config.ts
+++ b/src/modules/plugins/redact-config.ts
@@ -1,3 +1,4 @@
+import { BadRequestException } from '@nestjs/common';
import type { PluginConfigField, PluginConfigSchema } from '../../core/plugins/plugin.interfaces';
/** Mask shown for a stored secret on read. Treated as "unchanged" on write. */
@@ -116,34 +117,59 @@ function restoreValue(
// then can't bind a sentinel to a different row's secret. Only an unambiguous match (exactly one
// stored element with that signature) restores; otherwise the sentinel is treated as "nothing
// stored" (dropped), so a secret may be lost on an ambiguous edit but is never mis-targeted.
- const sigCount = new Map<string, number>();
- const sigFirst = new Map<string, unknown>();
+ const bySig = new Map<string, unknown[]>();
for (const ex of existingArr) {
const s = elementSignature(ex, itemField);
- sigCount.set(s, (sigCount.get(s) ?? 0) + 1);
- if (!sigFirst.has(s)) sigFirst.set(s, ex);
+ const group = bySig.get(s);
+ if (group) group.push(ex);
+ else bySig.set(s, [ex]);
}
const sameLength = incoming.length === existingArr.length;
+ const sigs = incoming.map(item => elementSignature(item, itemField));
+ const inCount = new Map<string, number>();
+ for (const s of sigs) inCount.set(s, (inCount.get(s) ?? 0) + 1);
+ const seen = new Map<string, number>();
return {
keep: true,
value: incoming
.map((item, i) => {
- const s = elementSignature(item, itemField);
+ const s = sigs[i];
+ 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) {
+ const unbound = restoreValue(item, undefined, itemField);
+ if (stableStringify(unbound) !== stableStringify(restoreValue(item, group[0], itemField))) {
+ throw new BadRequestException(
+ 'Removing an entry that shares its masked content with another is ambiguous; re-enter the remaining secret values',
+ );
+ }
+ return unbound;
+ }
// 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, bind the positional twin ONLY when its masked signature still equals this
- // element's, so an append/removal 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.
+ // 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)
+ // 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.
const twin = existingArr[i];
const match =
- sigCount.get(s) === 1
- ? sigFirst.get(s)
+ storedCount === 1
+ ? group[0]
: sameLength
? twin
- : twin !== undefined && elementSignature(twin, itemField) === s
- ? twin
- : undefined;
+ : inCount.get(s) === storedCount
+ ? group[k]
+ : twin !== undefined && elementSignature(twin, itemField) === s
+ ? twin
+ : undefined;
return restoreValue(item, match, itemField);
})
// Honor keep:false like restoreObject does (drop the element) rather than emitting `.value`