feat: public WhatsApp webhook with signature checks on both sides

mobile-api answers Meta's handshake only for the right verify token and
accepts events only with a valid X-Hub-Signature-256 under a company's
app secret, capped at 1 MB and rate limited per IP, on an exact
exemption. It forwards the exact bytes and signature; the WhatsApp
service re-verifies them itself, so the ingest trusts no caller. The
gateway exempts exactly POST /whatsapp/webhook/ingest from the user JWT,
and the log redactor masks the handshake's token and challenge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-12-02 11:03:56 +08:00
1 parent 846c22697e
commit a397de8c30
18 files changed
+759 -40

No files matched your search

@@ -57,7 +57,9 @@ public class JWTAuthFilter implements GlobalFilter, Ordered {
publicPath(HttpMethod.POST, "/counter/checkin/lookup"),
publicPath(HttpMethod.POST, "/counter/checkin/byGuest"),
// Password hint on public password screens: switched-on rule names and numbers only, no user data
publicPath(HttpMethod.GET, "/passwordRule/adminActive"));
publicPath(HttpMethod.GET, "/passwordRule/adminActive"),
// WhatsApp webhook forward from mobile-api: no user token; the service re-verifies Meta's signature itself
publicPath(HttpMethod.POST, "/whatsapp/webhook/ingest"));
private static Map.Entry<HttpMethod, Pattern> publicPath(HttpMethod method, String regex) {
return Map.entry(method, Pattern.compile(regex));
@@ -37,6 +37,30 @@ final class LogRedactor {
return sb.toString();
}
/** Query params whose values never reach a log: Meta's webhook handshake sends the verify token and challenge in the URL. */
private static final List<String> SENSITIVE_PARAMS = List.of("hub.verify_token=", "hub.challenge=");
/** The raw query string with the values of the sensitive params masked. */
static String query(String rawQuery) {
if (rawQuery == null || rawQuery.isEmpty()) {
return rawQuery;
}
StringBuilder sb = new StringBuilder(rawQuery.length());
int start = 0;
while (start <= rawQuery.length()) {
int amp = rawQuery.indexOf('&', start);
String pair = rawQuery.substring(start, amp < 0 ? rawQuery.length() : amp);
String name = SENSITIVE_PARAMS.stream().filter(pair::startsWith).findFirst().orElse(null);
sb.append(name == null ? pair : name + MASK);
if (amp < 0) {
break;
}
sb.append('&');
start = amp + 1;
}
return sb.toString();
}
/** Path prefixes followed by a bearer-style invite, check-in or registration-link token. */
private static final List<String> TOKEN_PREFIXES = List.of("/counter/eventGuest/findByToken/", "/counter/checkin/findEvent/",
"/counter/registrationLink/findByToken/");
@@ -92,4 +92,19 @@ class JWTAuthFilterPublicRegistrationPathTest {
assertThat(ex.getResponse().getStatusCode()).as(c[0] + " " + c[1]).isEqualTo(HttpStatus.UNAUTHORIZED);
}
}
@Test
void whatsAppIngestIsPublicButOnlyThatExactPostPath() {
assertThat(passes(call(HttpMethod.POST, "/whatsapp/webhook/ingest"))).isTrue();
for (String[] c : new String[][] {
{"GET", "/whatsapp/webhook/ingest"},
{"POST", "/whatsapp/webhook/ingest/x"},
{"POST", "/x/whatsapp/webhook/ingest"},
{"POST", "/whatsapp/ownership/userChanged"},
{"GET", "/whatsapp/conversations"} }) {
MockServerWebExchange ex = call(HttpMethod.valueOf(c[0]), c[1]);
assertThat(passes(ex)).as(c[0] + " " + c[1]).isFalse();
assertThat(ex.getResponse().getStatusCode()).as(c[0] + " " + c[1]).isEqualTo(HttpStatus.UNAUTHORIZED);
}
}
}
@@ -61,4 +61,15 @@ class LogRedactorTest {
.isEqualTo("/locationHierarchy/findParentByIdAndType/28/C");
assertTimeoutPreemptively(Duration.ofSeconds(1), () -> LogRedactor.path("/" + "eyJ.".repeat(20_000)));
}
@Test
@DisplayName("query: WhatsApp webhook verify token and challenge values are masked, other params kept")
void queryWebhookParamsMasked() {
assertThat(LogRedactor.query("hub.mode=subscribe&hub.verify_token=S3cretTok&hub.challenge=998877"))
.isEqualTo("hub.mode=subscribe&hub.verify_token=***&hub.challenge=***");
assertThat(LogRedactor.query("hub.challenge=1&x=y&hub.verify_token=")).isEqualTo("hub.challenge=***&x=y&hub.verify_token=***");
assertThat(LogRedactor.query("page=2&size=10")).isEqualTo("page=2&size=10");
assertThat(LogRedactor.query(null)).isNull();
assertThat(LogRedactor.query("")).isEmpty();
}
}