|
| 1 | +package com.cosium.standard_webhooks_consumer; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 4 | + |
| 5 | +import java.net.http.HttpHeaders; |
| 6 | +import java.time.Clock; |
| 7 | +import java.time.Duration; |
| 8 | +import java.time.Instant; |
| 9 | +import java.time.ZoneId; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.stream.Collectors; |
| 13 | +import org.junit.jupiter.api.DisplayName; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author Réda Housni Alaoui |
| 18 | + */ |
| 19 | +class SymmetricWebhookSignatureVerifierTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + @DisplayName("Verify valid signature") |
| 23 | + void test1() throws WebhookSignatureVerificationException { |
| 24 | + WebhookSignatureVerifier verifier = |
| 25 | + WebhookSignatureVerifier.builder("whsec_b6Ovv5eS7H5seJrGSStBYDivs8v2/KrFjfMaVZYsi7w=") |
| 26 | + .clock(Clock.fixed(Instant.ofEpochSecond(1737987215), ZoneId.systemDefault())) |
| 27 | + .build(); |
| 28 | + HttpHeaders httpHeaders = |
| 29 | + createHttpHeaders( |
| 30 | + Map.of( |
| 31 | + "webhook-id", |
| 32 | + "7a2486b3-31cf-4bd3-a460-df8845d16cd5", |
| 33 | + "webhook-timestamp", |
| 34 | + String.valueOf(1737987215), |
| 35 | + "webhook-signature", |
| 36 | + "v1,iayM3VaiYCEDP/CxWUFWcxUCJk2YmBDQHtHTsaHzrwo=")); |
| 37 | + verifier.verify(httpHeaders, "{\"greetings\": \"Hello World\"}"); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + @DisplayName("Verify invalid signature") |
| 42 | + void test2() { |
| 43 | + WebhookSignatureVerifier verifier = |
| 44 | + WebhookSignatureVerifier.builder("whsec_b6Ovv5eS7H5seJrGSStBYDivs8v2/KrFjfMaVZYsi7w=") |
| 45 | + .clock(Clock.fixed(Instant.ofEpochSecond(1737987215), ZoneId.systemDefault())) |
| 46 | + .build(); |
| 47 | + HttpHeaders httpHeaders = |
| 48 | + createHttpHeaders( |
| 49 | + Map.of( |
| 50 | + "webhook-id", |
| 51 | + "7a2486b3-31cf-4bd3-a460-df8845d16cd5", |
| 52 | + "webhook-timestamp", |
| 53 | + String.valueOf(1737987215), |
| 54 | + "webhook-signature", |
| 55 | + "v1,iayM3VaiYCEDP/CxWUFWcxUCJk2YmBDQHtHTsaHzrwo")); |
| 56 | + |
| 57 | + assertThatThrownBy(() -> verifier.verify(httpHeaders, "{\"greetings\": \"Hello World\"}")) |
| 58 | + .isInstanceOf(WebhookSignatureVerificationException.class) |
| 59 | + .hasMessageContaining( |
| 60 | + "The provided signature <iayM3VaiYCEDP/CxWUFWcxUCJk2YmBDQHtHTsaHzrwo> does not match the expected signature"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + @DisplayName("Verify invalid payload") |
| 65 | + void test3() { |
| 66 | + WebhookSignatureVerifier verifier = |
| 67 | + WebhookSignatureVerifier.builder("whsec_b6Ovv5eS7H5seJrGSStBYDivs8v2/KrFjfMaVZYsi7w=") |
| 68 | + .clock(Clock.fixed(Instant.ofEpochSecond(1737987215), ZoneId.systemDefault())) |
| 69 | + .build(); |
| 70 | + HttpHeaders httpHeaders = |
| 71 | + createHttpHeaders( |
| 72 | + Map.of( |
| 73 | + "webhook-id", |
| 74 | + "7a2486b3-31cf-4bd3-a460-df8845d16cd5", |
| 75 | + "webhook-timestamp", |
| 76 | + String.valueOf(1737987215), |
| 77 | + "webhook-signature", |
| 78 | + "v1,iayM3VaiYCEDP/CxWUFWcxUCJk2YmBDQHtHTsaHzrwo=")); |
| 79 | + |
| 80 | + assertThatThrownBy(() -> verifier.verify(httpHeaders, "{\"greetings\": \"Hello\"}")) |
| 81 | + .isInstanceOf(WebhookSignatureVerificationException.class) |
| 82 | + .hasMessageContaining( |
| 83 | + "The provided signature <iayM3VaiYCEDP/CxWUFWcxUCJk2YmBDQHtHTsaHzrwo=> does not match the expected signature"); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + @DisplayName("Verify invalid timestamp") |
| 88 | + void test4() { |
| 89 | + WebhookSignatureVerifier verifier = |
| 90 | + WebhookSignatureVerifier.builder("whsec_b6Ovv5eS7H5seJrGSStBYDivs8v2/KrFjfMaVZYsi7w=") |
| 91 | + .clock(Clock.fixed(Instant.ofEpochSecond(1737987215), ZoneId.systemDefault())) |
| 92 | + .build(); |
| 93 | + HttpHeaders httpHeaders = |
| 94 | + createHttpHeaders( |
| 95 | + Map.of( |
| 96 | + "webhook-id", |
| 97 | + "7a2486b3-31cf-4bd3-a460-df8845d16cd5", |
| 98 | + "webhook-timestamp", |
| 99 | + String.valueOf(1737987216), |
| 100 | + "webhook-signature", |
| 101 | + "v1,iayM3VaiYCEDP/CxWUFWcxUCJk2YmBDQHtHTsaHzrwo=")); |
| 102 | + |
| 103 | + assertThatThrownBy(() -> verifier.verify(httpHeaders, "{\"greetings\": \"Hello World\"}")) |
| 104 | + .isInstanceOf(WebhookSignatureVerificationException.class) |
| 105 | + .hasMessageContaining( |
| 106 | + "The provided signature <iayM3VaiYCEDP/CxWUFWcxUCJk2YmBDQHtHTsaHzrwo=> does not match the expected signature"); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + @DisplayName("Verify invalid message id") |
| 111 | + void test5() { |
| 112 | + WebhookSignatureVerifier verifier = |
| 113 | + WebhookSignatureVerifier.builder("whsec_b6Ovv5eS7H5seJrGSStBYDivs8v2/KrFjfMaVZYsi7w=") |
| 114 | + .clock(Clock.fixed(Instant.ofEpochSecond(1737987215), ZoneId.systemDefault())) |
| 115 | + .build(); |
| 116 | + HttpHeaders httpHeaders = |
| 117 | + createHttpHeaders( |
| 118 | + Map.of( |
| 119 | + "webhook-id", |
| 120 | + "7a2486b3-31cf-4bd3-a460-df8845d16cd6", |
| 121 | + "webhook-timestamp", |
| 122 | + String.valueOf(1737987215), |
| 123 | + "webhook-signature", |
| 124 | + "v1,iayM3VaiYCEDP/CxWUFWcxUCJk2YmBDQHtHTsaHzrwo=")); |
| 125 | + |
| 126 | + assertThatThrownBy(() -> verifier.verify(httpHeaders, "{\"greetings\": \"Hello World\"}")) |
| 127 | + .isInstanceOf(WebhookSignatureVerificationException.class) |
| 128 | + .hasMessageContaining( |
| 129 | + "The provided signature <iayM3VaiYCEDP/CxWUFWcxUCJk2YmBDQHtHTsaHzrwo=> does not match the expected signature"); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + @DisplayName("Build with invalid verification key") |
| 134 | + void test6() { |
| 135 | + WebhookSignatureVerifier.Builder builder = |
| 136 | + WebhookSignatureVerifier.builder("foo_b6Ovv5eS7H5seJrGSStBYDivs8v2/KrFjfMaVZYsi7w=") |
| 137 | + .clock(Clock.fixed(Instant.ofEpochSecond(1737987215), ZoneId.systemDefault())); |
| 138 | + |
| 139 | + assertThatThrownBy(builder::build) |
| 140 | + .isInstanceOf(RuntimeException.class) |
| 141 | + .hasMessageContaining( |
| 142 | + "Could not parse verification key <foo_b6Ovv5eS7H5seJrGSStB************************>"); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + @DisplayName("Verify missing signature header") |
| 147 | + void test7() { |
| 148 | + WebhookSignatureVerifier verifier = |
| 149 | + WebhookSignatureVerifier.builder("whsec_b6Ovv5eS7H5seJrGSStBYDivs8v2/KrFjfMaVZYsi7w=") |
| 150 | + .clock(Clock.fixed(Instant.ofEpochSecond(1737987215), ZoneId.systemDefault())) |
| 151 | + .build(); |
| 152 | + HttpHeaders httpHeaders = |
| 153 | + createHttpHeaders( |
| 154 | + Map.of( |
| 155 | + "webhook-id", |
| 156 | + "7a2486b3-31cf-4bd3-a460-df8845d16cd5", |
| 157 | + "webhook-timestamp", |
| 158 | + String.valueOf(1737987215))); |
| 159 | + |
| 160 | + assertThatThrownBy(() -> verifier.verify(httpHeaders, "{\"greetings\": \"Hello World\"}")) |
| 161 | + .isInstanceOf(WebhookSignatureVerificationException.class) |
| 162 | + .hasMessageContaining("No value found for header <webhook-signature>"); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + @DisplayName("Verify blank signature header") |
| 167 | + void test8() { |
| 168 | + WebhookSignatureVerifier verifier = |
| 169 | + WebhookSignatureVerifier.builder("whsec_b6Ovv5eS7H5seJrGSStBYDivs8v2/KrFjfMaVZYsi7w=") |
| 170 | + .clock(Clock.fixed(Instant.ofEpochSecond(1737987215), ZoneId.systemDefault())) |
| 171 | + .build(); |
| 172 | + HttpHeaders httpHeaders = |
| 173 | + createHttpHeaders( |
| 174 | + Map.of( |
| 175 | + "webhook-id", |
| 176 | + "7a2486b3-31cf-4bd3-a460-df8845d16cd5", |
| 177 | + "webhook-timestamp", |
| 178 | + String.valueOf(1737987215), |
| 179 | + "webhook-signature", |
| 180 | + " ")); |
| 181 | + |
| 182 | + assertThatThrownBy(() -> verifier.verify(httpHeaders, "{\"greetings\": \"Hello World\"}")) |
| 183 | + .isInstanceOf(WebhookSignatureVerificationException.class) |
| 184 | + .hasMessageContaining("No value found for header <webhook-signature>"); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + @DisplayName("Verify malformed signature header") |
| 189 | + void test9() { |
| 190 | + WebhookSignatureVerifier verifier = |
| 191 | + WebhookSignatureVerifier.builder("whsec_b6Ovv5eS7H5seJrGSStBYDivs8v2/KrFjfMaVZYsi7w=") |
| 192 | + .clock(Clock.fixed(Instant.ofEpochSecond(1737987215), ZoneId.systemDefault())) |
| 193 | + .build(); |
| 194 | + HttpHeaders httpHeaders = |
| 195 | + createHttpHeaders( |
| 196 | + Map.of( |
| 197 | + "webhook-id", |
| 198 | + "7a2486b3-31cf-4bd3-a460-df8845d16cd5", |
| 199 | + "webhook-timestamp", |
| 200 | + String.valueOf(1737987215), |
| 201 | + "webhook-signature", |
| 202 | + "iayM3VaiYCEDP/CxWUFWcxUCJk2YmBDQHtHTsaHzrwo=")); |
| 203 | + |
| 204 | + assertThatThrownBy(() -> verifier.verify(httpHeaders, "{\"greetings\": \"Hello World\"}")) |
| 205 | + .isInstanceOf(WebhookSignatureVerificationException.class) |
| 206 | + .hasMessageContaining( |
| 207 | + "No well-formed signature(s) found for signature header value <iayM3VaiYCEDP/CxWUFWcxUCJk2YmBDQHtHTsaHzrwo=>. A well-formed signature should have the form '$version,$base64encodedContent'."); |
| 208 | + } |
| 209 | + |
| 210 | + @Test |
| 211 | + @DisplayName("Verify malformed signature header") |
| 212 | + void test10() { |
| 213 | + WebhookSignatureVerifier verifier = |
| 214 | + WebhookSignatureVerifier.builder("whsec_b6Ovv5eS7H5seJrGSStBYDivs8v2/KrFjfMaVZYsi7w=") |
| 215 | + .clock(Clock.fixed(Instant.ofEpochSecond(1737987215), ZoneId.systemDefault())) |
| 216 | + .build(); |
| 217 | + HttpHeaders httpHeaders = |
| 218 | + createHttpHeaders( |
| 219 | + Map.of( |
| 220 | + "webhook-id", |
| 221 | + "7a2486b3-31cf-4bd3-a460-df8845d16cd5", |
| 222 | + "webhook-timestamp", |
| 223 | + String.valueOf(1737987215), |
| 224 | + "webhook-signature", |
| 225 | + ",iayM3VaiYCEDP/CxWUFWcxUCJk2YmBDQHtHTsaHzrwo=")); |
| 226 | + |
| 227 | + assertThatThrownBy(() -> verifier.verify(httpHeaders, "{\"greetings\": \"Hello World\"}")) |
| 228 | + .isInstanceOf(WebhookSignatureVerificationException.class) |
| 229 | + .hasMessageContaining( |
| 230 | + "No well-formed signature(s) found for signature header value <,iayM3VaiYCEDP/CxWUFWcxUCJk2YmBDQHtHTsaHzrwo=>. A well-formed signature should have the form '$version,$base64encodedContent'."); |
| 231 | + } |
| 232 | + |
| 233 | + @Test |
| 234 | + @DisplayName("Verify missing message id") |
| 235 | + void test11() { |
| 236 | + WebhookSignatureVerifier verifier = |
| 237 | + WebhookSignatureVerifier.builder("whsec_b6Ovv5eS7H5seJrGSStBYDivs8v2/KrFjfMaVZYsi7w=") |
| 238 | + .clock(Clock.fixed(Instant.ofEpochSecond(1737987215), ZoneId.systemDefault())) |
| 239 | + .build(); |
| 240 | + HttpHeaders httpHeaders = |
| 241 | + createHttpHeaders( |
| 242 | + Map.of( |
| 243 | + "webhook-timestamp", |
| 244 | + String.valueOf(1737987215), |
| 245 | + "webhook-signature", |
| 246 | + "v1,iayM3VaiYCEDP/CxWUFWcxUCJk2YmBDQHtHTsaHzrwo=")); |
| 247 | + |
| 248 | + assertThatThrownBy(() -> verifier.verify(httpHeaders, "{\"greetings\": \"Hello World\"}")) |
| 249 | + .isInstanceOf(WebhookSignatureVerificationException.class) |
| 250 | + .hasMessageContaining("No value found for header <webhook-id>"); |
| 251 | + } |
| 252 | + |
| 253 | + @Test |
| 254 | + @DisplayName("Verify blank message id") |
| 255 | + void test12() { |
| 256 | + WebhookSignatureVerifier verifier = |
| 257 | + WebhookSignatureVerifier.builder("whsec_b6Ovv5eS7H5seJrGSStBYDivs8v2/KrFjfMaVZYsi7w=") |
| 258 | + .clock(Clock.fixed(Instant.ofEpochSecond(1737987215), ZoneId.systemDefault())) |
| 259 | + .build(); |
| 260 | + HttpHeaders httpHeaders = |
| 261 | + createHttpHeaders( |
| 262 | + Map.of( |
| 263 | + "webhook-id", |
| 264 | + " ", |
| 265 | + "webhook-timestamp", |
| 266 | + String.valueOf(1737987215), |
| 267 | + "webhook-signature", |
| 268 | + "v1,iayM3VaiYCEDP/CxWUFWcxUCJk2YmBDQHtHTsaHzrwo=")); |
| 269 | + |
| 270 | + assertThatThrownBy(() -> verifier.verify(httpHeaders, "{\"greetings\": \"Hello World\"}")) |
| 271 | + .isInstanceOf(WebhookSignatureVerificationException.class) |
| 272 | + .hasMessageContaining("No value found for header <webhook-id>"); |
| 273 | + } |
| 274 | + |
| 275 | + @Test |
| 276 | + @DisplayName("Verify missing timestamp") |
| 277 | + void test13() { |
| 278 | + WebhookSignatureVerifier verifier = |
| 279 | + WebhookSignatureVerifier.builder("whsec_b6Ovv5eS7H5seJrGSStBYDivs8v2/KrFjfMaVZYsi7w=") |
| 280 | + .clock(Clock.fixed(Instant.ofEpochSecond(1737987215), ZoneId.systemDefault())) |
| 281 | + .build(); |
| 282 | + HttpHeaders httpHeaders = |
| 283 | + createHttpHeaders( |
| 284 | + Map.of( |
| 285 | + "webhook-id", |
| 286 | + "7a2486b3-31cf-4bd3-a460-df8845d16cd5", |
| 287 | + "webhook-signature", |
| 288 | + "v1,iayM3VaiYCEDP/CxWUFWcxUCJk2YmBDQHtHTsaHzrwo=")); |
| 289 | + |
| 290 | + assertThatThrownBy(() -> verifier.verify(httpHeaders, "{\"greetings\": \"Hello World\"}")) |
| 291 | + .isInstanceOf(WebhookSignatureVerificationException.class) |
| 292 | + .hasMessageContaining("No value found for header <webhook-timestamp>"); |
| 293 | + } |
| 294 | + |
| 295 | + @Test |
| 296 | + @DisplayName("Verify blank timestamp") |
| 297 | + void test14() { |
| 298 | + WebhookSignatureVerifier verifier = |
| 299 | + WebhookSignatureVerifier.builder("whsec_b6Ovv5eS7H5seJrGSStBYDivs8v2/KrFjfMaVZYsi7w=") |
| 300 | + .clock(Clock.fixed(Instant.ofEpochSecond(1737987215), ZoneId.systemDefault())) |
| 301 | + .build(); |
| 302 | + HttpHeaders httpHeaders = |
| 303 | + createHttpHeaders( |
| 304 | + Map.of( |
| 305 | + "webhook-id", |
| 306 | + "7a2486b3-31cf-4bd3-a460-df8845d16cd5", |
| 307 | + "webhook-timestamp", |
| 308 | + "", |
| 309 | + "webhook-signature", |
| 310 | + "v1,iayM3VaiYCEDP/CxWUFWcxUCJk2YmBDQHtHTsaHzrwo=")); |
| 311 | + |
| 312 | + assertThatThrownBy(() -> verifier.verify(httpHeaders, "{\"greetings\": \"Hello World\"}")) |
| 313 | + .isInstanceOf(WebhookSignatureVerificationException.class) |
| 314 | + .hasMessageContaining("No value found for header <webhook-timestamp>"); |
| 315 | + } |
| 316 | + |
| 317 | + @Test |
| 318 | + @DisplayName("Verify unparseable timestamp") |
| 319 | + void test15() { |
| 320 | + WebhookSignatureVerifier verifier = |
| 321 | + WebhookSignatureVerifier.builder("whsec_b6Ovv5eS7H5seJrGSStBYDivs8v2/KrFjfMaVZYsi7w=") |
| 322 | + .clock(Clock.fixed(Instant.ofEpochSecond(1737987215), ZoneId.systemDefault())) |
| 323 | + .build(); |
| 324 | + HttpHeaders httpHeaders = |
| 325 | + createHttpHeaders( |
| 326 | + Map.of( |
| 327 | + "webhook-id", |
| 328 | + "7a2486b3-31cf-4bd3-a460-df8845d16cd5", |
| 329 | + "webhook-timestamp", |
| 330 | + "yo", |
| 331 | + "webhook-signature", |
| 332 | + "v1,iayM3VaiYCEDP/CxWUFWcxUCJk2YmBDQHtHTsaHzrwo=")); |
| 333 | + |
| 334 | + assertThatThrownBy(() -> verifier.verify(httpHeaders, "{\"greetings\": \"Hello World\"}")) |
| 335 | + .isInstanceOf(WebhookSignatureVerificationException.class) |
| 336 | + .hasMessageContaining("Cannot parse timestamp <yo>"); |
| 337 | + } |
| 338 | + |
| 339 | + @Test |
| 340 | + @DisplayName("Verify too old timestamp") |
| 341 | + void test16() { |
| 342 | + WebhookSignatureVerifier verifier = |
| 343 | + WebhookSignatureVerifier.builder("whsec_b6Ovv5eS7H5seJrGSStBYDivs8v2/KrFjfMaVZYsi7w=") |
| 344 | + .clock( |
| 345 | + Clock.fixed( |
| 346 | + Instant.ofEpochSecond(1737987215 + Duration.ofMinutes(10).toSeconds()), |
| 347 | + ZoneId.systemDefault())) |
| 348 | + .build(); |
| 349 | + HttpHeaders httpHeaders = |
| 350 | + createHttpHeaders( |
| 351 | + Map.of( |
| 352 | + "webhook-id", |
| 353 | + "7a2486b3-31cf-4bd3-a460-df8845d16cd5", |
| 354 | + "webhook-timestamp", |
| 355 | + String.valueOf(1737987215), |
| 356 | + "webhook-signature", |
| 357 | + "v1,iayM3VaiYCEDP/CxWUFWcxUCJk2YmBDQHtHTsaHzrwo=")); |
| 358 | + |
| 359 | + assertThatThrownBy(() -> verifier.verify(httpHeaders, "{\"greetings\": \"Hello World\"}")) |
| 360 | + .isInstanceOf(WebhookSignatureVerificationException.class) |
| 361 | + .hasMessageContaining( |
| 362 | + "Message timestamp <1737987215 seconds> is too old compared to the current timestamp <1737987815 seconds>"); |
| 363 | + } |
| 364 | + |
| 365 | + private HttpHeaders createHttpHeaders(Map<String, String> headers) { |
| 366 | + return HttpHeaders.of( |
| 367 | + headers.entrySet().stream() |
| 368 | + .map(entry -> Map.entry(entry.getKey(), List.of(entry.getValue()))) |
| 369 | + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)), |
| 370 | + (s, s2) -> true); |
| 371 | + } |
| 372 | +} |
0 commit comments