|
| 1 | +package io.mailtrap.webhooks; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import javax.crypto.Mac; |
| 6 | +import javax.crypto.spec.SecretKeySpec; |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | +import java.util.HexFormat; |
| 9 | + |
| 10 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 12 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 13 | + |
| 14 | +class WebhookSignaturesTest { |
| 15 | + |
| 16 | + // --------------------------------------------------------------------- |
| 17 | + // Cross-SDK shared fixture — DO NOT CHANGE. |
| 18 | + // |
| 19 | + // The same (payload, signing_secret, expected_signature) triple is |
| 20 | + // embedded verbatim in the test suites of every official Mailtrap SDK |
| 21 | + // (Ruby, Python, PHP, Node.js, Java, .NET) to guarantee byte-for-byte |
| 22 | + // compatibility of the verification algorithm across languages. Keep |
| 23 | + // these three strings in sync with the other SDKs. |
| 24 | + // --------------------------------------------------------------------- |
| 25 | + private static final String FIXTURE_PAYLOAD = |
| 26 | + "{\"event\":\"delivery\",\"sending_stream\":\"transactional\",\"category\":\"welcome\"," |
| 27 | + + "\"message_id\":\"a8b1d8f6-1f8d-4a3c-9b2e-1a2b3c4d5e6f\"," |
| 28 | + + "\"email\":\"recipient@example.com\"," |
| 29 | + + "\"event_id\":\"f1e2d3c4-b5a6-7890-1234-567890abcdef\"," |
| 30 | + + "\"timestamp\":1716070000}"; |
| 31 | + private static final String FIXTURE_SIGNING_SECRET = "8d9a3c0e7f5b2d4a6c1e9f8b3a7d5c2e"; |
| 32 | + private static final String FIXTURE_EXPECTED_SIGNATURE = |
| 33 | + "6d262e2611cd09be1f948382b5c611d63b0e585c4c9c5e40139d6ac3876d5433"; |
| 34 | + |
| 35 | + // --------------------------------------------------------------------- |
| 36 | + // 1. Valid signature → true |
| 37 | + // --------------------------------------------------------------------- |
| 38 | + @Test |
| 39 | + void verify_withValidSignature_returnsTrue() { |
| 40 | + assertTrue(WebhookSignatures.verify(FIXTURE_PAYLOAD, FIXTURE_EXPECTED_SIGNATURE, FIXTURE_SIGNING_SECRET)); |
| 41 | + } |
| 42 | + |
| 43 | + // --------------------------------------------------------------------- |
| 44 | + // 2. Wrong secret → false |
| 45 | + // --------------------------------------------------------------------- |
| 46 | + @Test |
| 47 | + void verify_withWrongSecret_returnsFalse() { |
| 48 | + assertFalse(WebhookSignatures.verify(FIXTURE_PAYLOAD, FIXTURE_EXPECTED_SIGNATURE, "wrong_secret_value")); |
| 49 | + } |
| 50 | + |
| 51 | + // --------------------------------------------------------------------- |
| 52 | + // 3. Payload tampered (one byte changed) → false |
| 53 | + // --------------------------------------------------------------------- |
| 54 | + @Test |
| 55 | + void verify_withTamperedPayload_returnsFalse() { |
| 56 | + // Flip "delivery" to "delivere" — same length, different bytes. |
| 57 | + final String tampered = FIXTURE_PAYLOAD.replace("\"delivery\"", "\"delivere\""); |
| 58 | + assertFalse(WebhookSignatures.verify(tampered, FIXTURE_EXPECTED_SIGNATURE, FIXTURE_SIGNING_SECRET)); |
| 59 | + } |
| 60 | + |
| 61 | + // --------------------------------------------------------------------- |
| 62 | + // 4. Signature with wrong length → false (no throw) |
| 63 | + // --------------------------------------------------------------------- |
| 64 | + @Test |
| 65 | + void verify_withSignatureOfWrongLength_returnsFalse() { |
| 66 | + final String tooShort = FIXTURE_EXPECTED_SIGNATURE.substring(0, 63); |
| 67 | + final String tooLong = FIXTURE_EXPECTED_SIGNATURE + "a"; |
| 68 | + |
| 69 | + assertFalse(WebhookSignatures.verify(FIXTURE_PAYLOAD, tooShort, FIXTURE_SIGNING_SECRET)); |
| 70 | + assertFalse(WebhookSignatures.verify(FIXTURE_PAYLOAD, tooLong, FIXTURE_SIGNING_SECRET)); |
| 71 | + } |
| 72 | + |
| 73 | + // --------------------------------------------------------------------- |
| 74 | + // 5. Signature with non-hex characters → false (no throw) |
| 75 | + // --------------------------------------------------------------------- |
| 76 | + @Test |
| 77 | + void verify_withNonHexCharactersInSignature_returnsFalse() { |
| 78 | + // Same length (64), but contains 'z' which is not a hex digit. |
| 79 | + final String nonHex = "z" + FIXTURE_EXPECTED_SIGNATURE.substring(1); |
| 80 | + assertEquals(SIGNATURE_HEX_LENGTH(), nonHex.length()); |
| 81 | + assertFalse(WebhookSignatures.verify(FIXTURE_PAYLOAD, nonHex, FIXTURE_SIGNING_SECRET)); |
| 82 | + } |
| 83 | + |
| 84 | + // --------------------------------------------------------------------- |
| 85 | + // 6. Empty signature string → false |
| 86 | + // --------------------------------------------------------------------- |
| 87 | + @Test |
| 88 | + void verify_withEmptySignature_returnsFalse() { |
| 89 | + assertFalse(WebhookSignatures.verify(FIXTURE_PAYLOAD, "", FIXTURE_SIGNING_SECRET)); |
| 90 | + } |
| 91 | + |
| 92 | + // --------------------------------------------------------------------- |
| 93 | + // 7. Empty signingSecret → false |
| 94 | + // --------------------------------------------------------------------- |
| 95 | + @Test |
| 96 | + void verify_withEmptySigningSecret_returnsFalse() { |
| 97 | + assertFalse(WebhookSignatures.verify(FIXTURE_PAYLOAD, FIXTURE_EXPECTED_SIGNATURE, "")); |
| 98 | + } |
| 99 | + |
| 100 | + // --------------------------------------------------------------------- |
| 101 | + // 8. Empty payload with non-empty signature → false |
| 102 | + // --------------------------------------------------------------------- |
| 103 | + @Test |
| 104 | + void verify_withEmptyPayload_returnsFalse() { |
| 105 | + assertFalse(WebhookSignatures.verify("", FIXTURE_EXPECTED_SIGNATURE, FIXTURE_SIGNING_SECRET)); |
| 106 | + } |
| 107 | + |
| 108 | + // --------------------------------------------------------------------- |
| 109 | + // 9. Known-good fixture round-trip — independently recompute the HMAC |
| 110 | + // in the test (not via the helper) and assert it matches both the |
| 111 | + // embedded expected signature AND the helper's verdict. |
| 112 | + // --------------------------------------------------------------------- |
| 113 | + @Test |
| 114 | + void verify_fixtureRoundTrip_matchesIndependentlyComputedHmac() throws Exception { |
| 115 | + // Recompute the HMAC-SHA256 independently of the helper, using the JDK |
| 116 | + // primitives directly. If this drifts from FIXTURE_EXPECTED_SIGNATURE, |
| 117 | + // either the fixture is wrong or the algorithm/encoding has changed. |
| 118 | + final Mac mac = Mac.getInstance("HmacSHA256"); |
| 119 | + mac.init(new SecretKeySpec( |
| 120 | + FIXTURE_SIGNING_SECRET.getBytes(StandardCharsets.UTF_8), "HmacSHA256")); |
| 121 | + final byte[] digest = mac.doFinal(FIXTURE_PAYLOAD.getBytes(StandardCharsets.UTF_8)); |
| 122 | + final String computedHex = HexFormat.of().formatHex(digest); |
| 123 | + |
| 124 | + assertEquals(FIXTURE_EXPECTED_SIGNATURE, computedHex, |
| 125 | + "Independently computed HMAC must equal embedded fixture signature"); |
| 126 | + |
| 127 | + assertTrue(WebhookSignatures.verify(FIXTURE_PAYLOAD, FIXTURE_EXPECTED_SIGNATURE, FIXTURE_SIGNING_SECRET), |
| 128 | + "Helper must agree the fixture is valid"); |
| 129 | + } |
| 130 | + |
| 131 | + // --------------------------------------------------------------------- |
| 132 | + // Bonus: null inputs → false (no NullPointerException) |
| 133 | + // --------------------------------------------------------------------- |
| 134 | + @Test |
| 135 | + void verify_withNullPayload_returnsFalse() { |
| 136 | + assertFalse(WebhookSignatures.verify(null, FIXTURE_EXPECTED_SIGNATURE, FIXTURE_SIGNING_SECRET)); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void verify_withNullSignature_returnsFalse() { |
| 141 | + assertFalse(WebhookSignatures.verify(FIXTURE_PAYLOAD, null, FIXTURE_SIGNING_SECRET)); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + void verify_withNullSigningSecret_returnsFalse() { |
| 146 | + assertFalse(WebhookSignatures.verify(FIXTURE_PAYLOAD, FIXTURE_EXPECTED_SIGNATURE, null)); |
| 147 | + } |
| 148 | + |
| 149 | + private static int SIGNATURE_HEX_LENGTH() { |
| 150 | + return WebhookSignatures.SIGNATURE_HEX_LENGTH; |
| 151 | + } |
| 152 | +} |
0 commit comments