Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): count the webhook secret maximum in code points

Yudhi Armyndharisa2e3ff49054a
diff --git a/dashboard/src/utils/webhookAuth.test.ts b/dashboard/src/utils/webhookAuth.test.ts
index e261ea5f5..24f0574ce 100644
--- a/dashboard/src/utils/webhookAuth.test.ts
+++ b/dashboard/src/utils/webhookAuth.test.ts
@@ -75,7 +75,7 @@ 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
+// The gateway's @MinLength counts 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', () => {
@@ -86,6 +86,13 @@ test('the secret length is counted the way the gateway counts it', () => {
assert.equal(secretError('\u{1F600}'.repeat(255)), null);
});
+// The gateway's upper bound is @MaxCodePoints, which counts a presentation selector on its own, so a
+// selector-joined secret within 255 folded characters can still be rejected there.
+test('the secret maximum is counted in code points', () => {
+ assert.equal(secretError('\u2714\uFE0F'.repeat(127)), null);
+ assert.equal(secretError('\u2714\uFE0F'.repeat(128)), 'webhooks.auth.errors.secretLength');
+});
+
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 e960ff34a..7847c3e67 100644
--- a/dashboard/src/utils/webhookAuth.ts
+++ b/dashboard/src/utils/webhookAuth.ts
@@ -54,15 +54,16 @@ export function buildHeaderMap(rows: readonly HeaderRow[]): HeaderMapResult {
/**
* 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.
+ * Characters are counted as the gateway's validators count them: for the 16 minimum (@MinLength) a
+ * surrogate pair, or a character followed by a presentation selector (U+FE0E/U+FE0F), is one; the
+ * 255 maximum (@MaxCodePoints) counts code points, so a presentation selector counts on its own.
*/
export function secretError(secret: string): string | 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;
+ if (secret === '' || (length >= 16 && [...secret].length <= 255)) return null;
return 'webhooks.auth.errors.secretLength';
}