uselesskey-webhook 0.9.1

Deterministic webhook fixtures for GitHub, Stripe, and Slack signature tests.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#![forbid(unsafe_code)]

//! Webhook fixtures built on `uselesskey-core`.
//!
//! This crate provides deterministic provider-style webhook fixtures with canonical
//! payloads, signature input strings, and signed headers.

mod fixture;
mod model;
mod payload;
mod secret;
mod signature;

pub use fixture::WebhookFactoryExt;
pub use model::{
    NearMissScenario, NearMissWebhookFixture, WebhookFixture, WebhookPayloadSpec, WebhookProfile,
};

/// Cache domain for webhook fixtures.
pub const DOMAIN_WEBHOOK_FIXTURE: &str = "uselesskey:webhook:fixture";

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::BTreeMap;

    use crate::fixture::build_fixture_from_seed;
    use crate::payload::{canonical_payload, stable_spec_bytes};
    use crate::secret::build_secret;
    use crate::signature::hmac_sha256_hex;
    use rand_chacha10::ChaCha20Rng;
    use rand_core10::{Rng, SeedableRng};
    use uselesskey_core::{Factory, Seed};

    #[test]
    fn hmac_sha256_matches_rfc4231_test_vector() {
        let key = [0x0b_u8; 20];
        let digest = hmac_sha256_hex(&key, b"Hi There");

        assert_eq!(
            digest,
            "b0344c61d8db38535ca8afceaf0bf12b\
             881dc200c9833da726e9376c2e32cff7"
                .replace(char::is_whitespace, "")
        );
    }

    #[test]
    fn hmac_sha256_preserves_block_sized_key_without_hashing() {
        let key = [0xaa_u8; 64];
        let digest = hmac_sha256_hex(&key, b"block-size boundary");

        assert_eq!(
            digest,
            "4bf714ba9df6b88605adb3e0a8a8b6d0320041fc2577408eaeb6e7120a03cf43"
        );
    }

    fn verify_github(secret: &str, payload: &str, headers: &BTreeMap<String, String>) -> bool {
        let expected = format!(
            "sha256={}",
            hmac_sha256_hex(secret.as_bytes(), payload.as_bytes())
        );
        headers.get("X-Hub-Signature-256") == Some(&expected)
    }

    fn verify_stripe(
        secret: &str,
        payload: &str,
        headers: &BTreeMap<String, String>,
        now: i64,
        tolerance_secs: i64,
    ) -> bool {
        let Some(sig_header) = headers.get("Stripe-Signature") else {
            return false;
        };
        let mut ts = None;
        let mut v1 = None;
        for part in sig_header.split(',') {
            if let Some(v) = part.strip_prefix("t=") {
                ts = v.parse::<i64>().ok();
            }
            if let Some(v) = part.strip_prefix("v1=") {
                v1 = Some(v.to_string());
            }
        }
        let Some(ts) = ts else {
            return false;
        };
        if (now - ts).abs() > tolerance_secs {
            return false;
        }
        let base = format!("{ts}.{payload}");
        let expected = hmac_sha256_hex(secret.as_bytes(), base.as_bytes());
        v1.as_deref() == Some(expected.as_str())
    }

    fn verify_slack(
        secret: &str,
        payload: &str,
        headers: &BTreeMap<String, String>,
        now: i64,
        tolerance_secs: i64,
    ) -> bool {
        let Some(ts_str) = headers.get("X-Slack-Request-Timestamp") else {
            return false;
        };
        let Ok(ts) = ts_str.parse::<i64>() else {
            return false;
        };
        if (now - ts).abs() > tolerance_secs {
            return false;
        }
        let Some(sig) = headers.get("X-Slack-Signature") else {
            return false;
        };
        let base = format!("v0:{ts}:{payload}");
        let expected = format!("v0={}", hmac_sha256_hex(secret.as_bytes(), base.as_bytes()));
        sig == &expected
    }

    #[test]
    fn deterministic_github_fixture_is_stable() {
        let fx = Factory::deterministic(Seed::from_env_value("webhook-gh").unwrap());
        let a = fx.webhook_github("repo", WebhookPayloadSpec::Canonical);
        let b = fx.webhook_github("repo", WebhookPayloadSpec::Canonical);
        assert_eq!(a.secret, b.secret);
        assert_eq!(a.payload, b.payload);
        assert_eq!(a.headers, b.headers);
        assert!(verify_github(&a.secret, &a.payload, &a.headers));
    }

    #[test]
    fn provider_signature_paths_verify() {
        let fx = Factory::deterministic(Seed::from_env_value("webhook-providers").unwrap());
        let gh = fx.webhook(WebhookProfile::GitHub, "a", WebhookPayloadSpec::Canonical);
        let st = fx.webhook_stripe("b", WebhookPayloadSpec::Canonical);
        let sl = fx.webhook_slack("c", WebhookPayloadSpec::Canonical);

        assert!(verify_github(&gh.secret, &gh.payload, &gh.headers));
        assert!(verify_stripe(
            &st.secret,
            &st.payload,
            &st.headers,
            st.timestamp,
            300
        ));
        assert!(verify_slack(
            &sl.secret,
            &sl.payload,
            &sl.headers,
            sl.timestamp,
            300
        ));
    }

    #[test]
    fn payload_spec_stable_bytes_are_shape_sensitive() {
        assert_eq!(WebhookPayloadSpec::Canonical.stable_bytes(), b"canonical");
        assert_eq!(
            WebhookPayloadSpec::Raw("one".to_string()).stable_bytes(),
            b"raw:one"
        );
        assert_ne!(
            WebhookPayloadSpec::Raw("one".to_string()).stable_bytes(),
            WebhookPayloadSpec::Raw("two".to_string()).stable_bytes()
        );
        assert_ne!(
            stable_spec_bytes(WebhookProfile::GitHub, &WebhookPayloadSpec::Canonical),
            stable_spec_bytes(WebhookProfile::Stripe, &WebhookPayloadSpec::Canonical)
        );
    }

    #[test]
    fn generated_timestamp_uses_expected_seeded_window() {
        let seed = [7_u8; 32];
        let mut rng = ChaCha20Rng::from_seed(seed);
        let mut secret_bytes = [0_u8; 32];
        rng.fill_bytes(&mut secret_bytes);
        let expected = 1_700_000_000_i64 + (rng.next_u32() as i64 % 200_000_000_i64);

        let fixture = build_fixture_from_seed(
            WebhookProfile::Stripe,
            "billing",
            WebhookPayloadSpec::Canonical,
            &seed,
        );

        assert_eq!(fixture.timestamp, expected);
        assert!((1_700_000_000..1_900_000_000).contains(&fixture.timestamp));
    }

    #[test]
    fn generated_secrets_match_provider_shapes() {
        let mut rng = ChaCha20Rng::from_seed([9_u8; 32]);
        let github = build_secret(WebhookProfile::GitHub, &mut rng);
        let stripe = build_secret(WebhookProfile::Stripe, &mut rng);
        let slack = build_secret(WebhookProfile::Slack, &mut rng);

        assert_eq!(github.len(), "ghs_".len() + 43);
        assert!(github.starts_with("ghs_"));
        assert!(
            github["ghs_".len()..]
                .bytes()
                .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-' || byte == b'_')
        );

        assert_eq!(stripe.len(), "whsec_".len() + 64);
        assert!(stripe.starts_with("whsec_"));
        assert_lower_hex(&stripe["whsec_".len()..]);

        assert_eq!(slack.len(), 64);
        assert_lower_hex(&slack);
    }

    #[test]
    fn header_shape_matches_provider_conventions() {
        let fx = Factory::deterministic(Seed::from_env_value("webhook-headers").unwrap());
        let gh = fx.webhook_github("r", WebhookPayloadSpec::Canonical);
        assert!(
            gh.headers
                .get("X-Hub-Signature-256")
                .is_some_and(|v| v.starts_with("sha256="))
        );

        let st = fx.webhook_stripe("r", WebhookPayloadSpec::Canonical);
        let stripe_header = st.headers.get("Stripe-Signature").expect("stripe header");
        assert!(stripe_header.contains("t="));
        assert!(stripe_header.contains(",v1="));

        let sl = fx.webhook_slack("r", WebhookPayloadSpec::Canonical);
        assert!(sl.headers.contains_key("X-Slack-Request-Timestamp"));
        assert!(
            sl.headers
                .get("X-Slack-Signature")
                .is_some_and(|v| v.starts_with("v0="))
        );
    }

    #[test]
    fn near_miss_negatives_fail_provider_verification() {
        let fx = Factory::deterministic(Seed::from_env_value("webhook-nearmiss").unwrap());
        let st = fx.webhook_stripe("billing", WebhookPayloadSpec::Canonical);
        let now = st.timestamp;

        let stale = st.near_miss_stale_timestamp(300);
        assert_eq!(stale.timestamp, st.timestamp - 301);
        assert_eq!(
            stale.signature_input,
            format!("{}.{}", stale.timestamp, stale.payload)
        );
        assert!(!verify_stripe(
            &st.secret,
            &st.payload,
            &stale.headers,
            now,
            300
        ));

        let wrong_secret = st.near_miss_wrong_secret();
        assert!(!verify_stripe(
            &st.secret,
            &wrong_secret.payload,
            &wrong_secret.headers,
            wrong_secret.timestamp,
            300
        ));

        let tampered = st.near_miss_tampered_payload();
        assert!(!verify_stripe(
            &tampered.secret,
            &st.payload,
            &tampered.headers,
            tampered.timestamp,
            300
        ));
    }

    #[test]
    fn debug_redacts_secret() {
        let fx = Factory::random();
        let fixture = fx.webhook_slack("debug", WebhookPayloadSpec::Canonical);
        let out = format!("{fixture:?}");
        assert!(!out.contains(&fixture.secret));
        assert!(out.contains("WebhookFixture"));

        let near_miss = fixture.near_miss_wrong_secret();
        let out = format!("{near_miss:?}");
        assert!(!out.contains(&near_miss.secret));
        assert!(out.contains("NearMissWebhookFixture"));
    }

    #[test]
    fn canonical_payload_escapes_special_characters_in_label() {
        let fx = Factory::deterministic(Seed::from_env_value("webhook-label-escape").unwrap());
        let label = "repo\"line\nbreak\\slash";
        let fixtures = [
            fx.webhook_github(label, WebhookPayloadSpec::Canonical),
            fx.webhook_stripe(label, WebhookPayloadSpec::Canonical),
            fx.webhook_slack(label, WebhookPayloadSpec::Canonical),
        ];

        for fixture in fixtures {
            let parsed: serde_json::Value =
                serde_json::from_str(&fixture.payload).expect("canonical payload should be valid");
            let serialized = parsed.to_string();
            assert!(
                serialized.contains("repo\\\"line\\nbreak\\\\slash"),
                "serialized payload should preserve escaped label, got: {serialized}"
            );
        }
    }

    #[test]
    fn canonical_payload_preserves_plain_label_field_order() {
        assert_eq!(
            canonical_payload(
                WebhookProfile::GitHub,
                "repo",
                WebhookPayloadSpec::Canonical,
                12
            ),
            "{\"action\":\"opened\",\"repository\":{\"full_name\":\"acme/repo\"},\"number\":1012}"
        );
        assert_eq!(
            canonical_payload(
                WebhookProfile::Stripe,
                "billing",
                WebhookPayloadSpec::Canonical,
                0x0f
            ),
            "{\"id\":\"evt_0000000f\",\"type\":\"checkout.session.completed\",\"data\":{\"object\":{\"metadata\":{\"label\":\"billing\"}}}}"
        );
        assert_eq!(
            canonical_payload(
                WebhookProfile::Slack,
                "alerts",
                WebhookPayloadSpec::Canonical,
                0x10
            ),
            "{\"type\":\"event_callback\",\"team_id\":\"T00000010\",\"event\":{\"type\":\"app_mention\",\"text\":\"ping alerts\"}}"
        );
    }

    fn assert_lower_hex(value: &str) {
        assert!(
            value
                .bytes()
                .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)),
            "expected lowercase hex: {value}"
        );
    }

    #[test]
    fn webhook_profile_stable_tag_is_unique_per_variant() {
        use crate::model::WebhookProfile;
        let tags = [
            WebhookProfile::GitHub.stable_tag(),
            WebhookProfile::Stripe.stable_tag(),
            WebhookProfile::Slack.stable_tag(),
        ];
        assert_eq!(tags, ["github", "stripe", "slack"]);

        let unique: std::collections::HashSet<&&str> = tags.iter().collect();
        assert_eq!(unique.len(), tags.len(), "stable_tag values must be unique");
    }

    #[test]
    fn raw_payload_spec_is_returned_verbatim() {
        let fx = Factory::deterministic_from_str("webhook-raw-spec");
        let payload = "{\"custom\":\"shape\"}".to_string();
        let fixture = fx.webhook_github("raw-repo", WebhookPayloadSpec::Raw(payload.clone()));

        assert_eq!(fixture.payload, payload);
        assert!(verify_github(
            &fixture.secret,
            &fixture.payload,
            &fixture.headers
        ));
    }

    #[test]
    fn raw_payload_distinct_strings_yield_distinct_cache_identities() {
        let fx = Factory::deterministic_from_str("webhook-raw-cache");
        let one = fx.webhook_stripe("svc", WebhookPayloadSpec::Raw("one".to_string()));
        let two = fx.webhook_stripe("svc", WebhookPayloadSpec::Raw("two".to_string()));

        assert_ne!(one.payload, two.payload);
        assert_ne!(one.signature_input, two.signature_input);
        assert_ne!(
            one.headers.get("Stripe-Signature"),
            two.headers.get("Stripe-Signature")
        );
    }

    #[test]
    fn stale_timestamp_near_miss_works_for_all_profiles() {
        let fx = Factory::deterministic_from_str("webhook-stale-all");
        let max_age = 300_i64;

        for profile in [
            WebhookProfile::GitHub,
            WebhookProfile::Stripe,
            WebhookProfile::Slack,
        ] {
            let base = fx.webhook(profile, "svc", WebhookPayloadSpec::Canonical);
            let stale = base.near_miss_stale_timestamp(max_age);

            assert_eq!(stale.scenario, NearMissScenario::StaleTimestamp);
            assert_eq!(stale.timestamp, base.timestamp - max_age - 1);
            // GitHub does not include a timestamp in its signed input, so the
            // signature_input is just the payload; for Stripe/Slack the input
            // includes the (stale) timestamp.
            match profile {
                WebhookProfile::GitHub => {
                    assert_eq!(stale.signature_input, stale.payload);
                }
                WebhookProfile::Stripe => {
                    assert_eq!(
                        stale.signature_input,
                        format!("{}.{}", stale.timestamp, stale.payload)
                    );
                }
                WebhookProfile::Slack => {
                    assert_eq!(
                        stale.signature_input,
                        format!("v0:{}:{}", stale.timestamp, stale.payload)
                    );
                }
            }
        }
    }

    #[test]
    fn wrong_secret_near_miss_works_for_github_and_slack() {
        let fx = Factory::deterministic_from_str("webhook-wrong-secret");

        let gh = fx.webhook_github("svc", WebhookPayloadSpec::Canonical);
        let gh_wrong = gh.near_miss_wrong_secret();
        assert_eq!(gh_wrong.scenario, NearMissScenario::WrongSecret);
        assert_ne!(gh_wrong.secret, gh.secret);
        assert!(gh_wrong.secret.ends_with("_wrong"));
        assert!(!verify_github(
            &gh.secret,
            &gh_wrong.payload,
            &gh_wrong.headers
        ));

        let sl = fx.webhook_slack("svc", WebhookPayloadSpec::Canonical);
        let sl_wrong = sl.near_miss_wrong_secret();
        assert_eq!(sl_wrong.scenario, NearMissScenario::WrongSecret);
        assert_ne!(sl_wrong.secret, sl.secret);
        assert!(!verify_slack(
            &sl.secret,
            &sl_wrong.payload,
            &sl_wrong.headers,
            sl_wrong.timestamp,
            300
        ));
    }

    #[test]
    fn tampered_payload_near_miss_works_for_github_and_slack() {
        let fx = Factory::deterministic_from_str("webhook-tampered");

        let gh = fx.webhook_github("svc", WebhookPayloadSpec::Canonical);
        let gh_tampered = gh.near_miss_tampered_payload();
        assert_eq!(gh_tampered.scenario, NearMissScenario::TamperedPayload);
        assert_ne!(gh_tampered.payload, gh.payload);
        // The tampered fixture re-signs its own modified payload, so it
        // verifies against itself; verifying the *original* payload with the
        // tampered signature must fail.
        assert!(!verify_github(
            &gh.secret,
            &gh.payload,
            &gh_tampered.headers
        ));

        let sl = fx.webhook_slack("svc", WebhookPayloadSpec::Canonical);
        let sl_tampered = sl.near_miss_tampered_payload();
        assert_eq!(sl_tampered.scenario, NearMissScenario::TamperedPayload);
        assert_ne!(sl_tampered.payload, sl.payload);
        assert!(!verify_slack(
            &sl.secret,
            &sl.payload,
            &sl_tampered.headers,
            sl_tampered.timestamp,
            300
        ));
    }

    #[test]
    fn hmac_sha256_long_key_is_hashed_first() {
        // RFC 4231 test vector 4: 131-byte key (longer than the 64-byte block),
        // exercising the SHA-256 pre-hash branch of hmac_sha256_hex.
        let key = vec![0xaa_u8; 131];
        let digest = hmac_sha256_hex(
            &key,
            b"Test Using Larger Than Block-Size Key - Hash Key First",
        );
        assert_eq!(
            digest,
            "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54"
        );
    }

    #[test]
    fn hmac_sha256_short_key_is_zero_padded() {
        // A short key should be zero-padded into the 64-byte block, not hashed.
        // This exercises the else-branch of hmac_sha256_hex with a sub-block-size key.
        let short = b"key";
        let padded = {
            let mut padded = [0_u8; 64];
            padded[..short.len()].copy_from_slice(short);
            padded.to_vec()
        };
        assert_eq!(
            hmac_sha256_hex(short, b"hello"),
            hmac_sha256_hex(&padded, b"hello"),
            "short key must zero-pad into the same block as the explicitly padded key"
        );
    }

    #[test]
    fn debug_redacts_secret_for_all_profiles() {
        let fx = Factory::deterministic_from_str("webhook-debug-all");

        for profile in [
            WebhookProfile::GitHub,
            WebhookProfile::Stripe,
            WebhookProfile::Slack,
        ] {
            let fixture = fx.webhook(profile, "svc", WebhookPayloadSpec::Canonical);
            let dbg = format!("{fixture:?}");
            assert!(dbg.contains("WebhookFixture"));
            assert!(
                !dbg.contains(&fixture.secret),
                "Debug for {profile:?} must not leak secret: {dbg}"
            );
        }
    }

    #[test]
    fn debug_redacts_secret_for_all_near_miss_scenarios() {
        let fx = Factory::deterministic_from_str("webhook-debug-nm");
        let base = fx.webhook_stripe("svc", WebhookPayloadSpec::Canonical);

        let scenarios = [
            base.near_miss_stale_timestamp(300),
            base.near_miss_wrong_secret(),
            base.near_miss_tampered_payload(),
        ];

        for fixture in scenarios {
            let dbg = format!("{fixture:?}");
            assert!(dbg.contains("NearMissWebhookFixture"));
            assert!(
                !dbg.contains(&fixture.secret),
                "Debug for {:?} must not leak secret: {dbg}",
                fixture.scenario
            );
        }
    }
}