Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): count webhook secret length the way the gateway does

Yudhi Armyndharisfa44aeb53b52
diff --git a/dashboard/src/utils/webhookAuth.test.ts b/dashboard/src/utils/webhookAuth.test.ts
index d752fd810..e261ea5f5 100644
--- a/dashboard/src/utils/webhookAuth.test.ts
+++ b/dashboard/src/utils/webhookAuth.test.ts
@@ -75,6 +75,17 @@ test('a secret is empty or 16 to 255 characters, and is not trimmed', () => {
assert.equal(secretError(` ${'x'.repeat(10)} `), 'webhooks.auth.errors.secretLength');
});
+// The gateway's @MinLength/@MaxLength count an emoji (a surrogate pair) or a character plus its
+// presentation selector as one; UTF-16 length counted them as two, so 8 emoji passed inline and
+// then came back as a raw 400.
+test('the secret length is counted the way the gateway counts it', () => {
+ assert.equal(secretError('\u{1F600}'.repeat(8)), 'webhooks.auth.errors.secretLength');
+ assert.equal(secretError('\u{1F600}'.repeat(16)), null);
+ assert.equal(secretError('\u2764\uFE0F'.repeat(8)), 'webhooks.auth.errors.secretLength');
+ assert.equal(secretError('\u2764\uFE0F'.repeat(16)), null);
+ assert.equal(secretError('\u{1F600}'.repeat(255)), null);
+});
+
test('a generated secret is 64 hex characters and passes the length check', () => {
const secret = generateSecret();
assert.match(secret, /^[0-9a-f]{64}$/);
diff --git a/dashboard/src/utils/webhookAuth.ts b/dashboard/src/utils/webhookAuth.ts
index b7737bb52..e960ff34a 100644
--- a/dashboard/src/utils/webhookAuth.ts
+++ b/dashboard/src/utils/webhookAuth.ts
@@ -52,9 +52,17 @@ export function buildHeaderMap(rows: readonly HeaderRow[]): HeaderMapResult {
return { ok: true, headers };
}
-/** Null when the secret is acceptable: empty (not sent) or 16 to 255 characters. Never trimmed. */
+/**
+ * Null when the secret is acceptable: empty (not sent) or 16 to 255 characters. Never trimmed.
+ * Characters are counted as the gateway's validator counts them: a surrogate pair, or a character
+ * followed by a presentation selector (U+FE0E/U+FE0F), is one.
+ */
export function secretError(secret: string): string | null {
- if (secret === '' || (secret.length >= 16 && secret.length <= 255)) return null;
+ const length =
+ secret.length -
+ (secret.match(/[^\uFE0F\uFE0E][\uFE0F\uFE0E]/g)?.length ?? 0) -
+ (secret.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g)?.length ?? 0);
+ if (secret === '' || (length >= 16 && length <= 255)) return null;
return 'webhooks.auth.errors.secretLength';
}