import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.HexFormat; import java.util.List; import java.util.regex.Pattern; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; /** * v0.1 DRAFT. Borrou webhook signature verification, JDK only (Java 17+). * *

Every delivery carries {@code Borrou-Signature: t=,v1=}, the MAC * of {@code .} keyed with the subscription's whole signing secret ({@code whsec_...}, * from GET /v1/webhooks/subscriptions/{id}/signing-secret). Borrou signs with one secret today, * but a header may carry more than one v1 entry (as it would while a signing secret rotates): accept * the delivery if any one matches. * *

Verify the raw bytes exactly as received, before any JSON parsing; reject a timestamp more * than five minutes from your clock; then de-duplicate on the event's {@code id} (also * {@code Borrou-Event-Id}). */ public final class BorrouWebhooks { public static final String SIGNATURE_HEADER = "Borrou-Signature"; public static final long DEFAULT_TOLERANCE_SECONDS = 300; private static final Pattern TIMESTAMP = Pattern.compile("[0-9]{1,12}"); private static final Pattern SIGNATURE = Pattern.compile("[0-9a-f]{64}"); public enum Result { OK, MISSING, MALFORMED, STALE, MISMATCH } private BorrouWebhooks() {} /** With your clock and the default tolerance. */ public static Result verify(String header, byte[] rawBody, String secret) { return verify(header, rawBody, secret, DEFAULT_TOLERANCE_SECONDS, System.currentTimeMillis() / 1000); } public static Result verify(String header, byte[] rawBody, String secret, long toleranceSeconds, long nowSeconds) { if (header == null || header.isEmpty()) return Result.MISSING; Long timestamp = null; List candidates = new ArrayList<>(); for (String part : header.split(",", -1)) { String[] pieces = part.trim().split("=", -1); if (pieces.length != 2 || pieces[0].isEmpty()) return Result.MALFORMED; String key = pieces[0]; String value = pieces[1]; if (key.equals("t")) { timestamp = TIMESTAMP.matcher(value).matches() ? Long.valueOf(value) : null; } else if (key.equals("v1") && SIGNATURE.matcher(value).matches()) { candidates.add(HexFormat.of().parseHex(value)); } } if (timestamp == null || candidates.isEmpty()) return Result.MALFORMED; if (Math.abs(nowSeconds - timestamp) > toleranceSeconds) return Result.STALE; byte[] expected; try { Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256")); mac.update((timestamp + ".").getBytes(StandardCharsets.US_ASCII)); expected = mac.doFinal(rawBody); } catch (NoSuchAlgorithmException | InvalidKeyException e) { throw new IllegalStateException("HmacSHA256 is not available", e); } // Constant time, and every candidate compared, so the answer's timing says nothing about which. boolean matched = false; for (byte[] candidate : candidates) matched = MessageDigest.isEqual(candidate, expected) | matched; return matched ? Result.OK : Result.MISMATCH; } }