tideway 0.7.17

A batteries-included Rust web framework built on Axum for building SaaS applications quickly
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
565
566
567
568
569
570
571
use crate::error::Result;
use async_trait::async_trait;
use hmac::{Hmac, Mac};
use sha2::Sha256;
use subtle::ConstantTimeEq;

type HmacSha256 = Hmac<Sha256>;

/// Trait for verifying webhook signatures
///
/// Different webhook providers use different signature algorithms.
/// Implement this trait to verify webhooks from your provider.
///
/// # Example
///
/// ```rust,ignore
/// use tideway::webhooks::{WebhookVerifier, HmacSha256Verifier};
///
/// // Create verifier with your webhook secret
/// let verifier = HmacSha256Verifier::new("whsec_your_secret_here");
///
/// // Verify incoming webhook
/// let payload = br#"{"event": "payment.completed"}"#;
/// let signature = "abc123..."; // From X-Signature header
/// let is_valid = verifier.verify_signature(payload, signature).await?;
/// ```
#[async_trait]
pub trait WebhookVerifier: Send + Sync {
    /// Verify the webhook signature
    ///
    /// # Arguments
    ///
    /// * `payload` - The raw webhook payload bytes
    /// * `signature` - The signature from the webhook headers
    ///
    /// # Returns
    ///
    /// `Ok(true)` if signature is valid, `Ok(false)` if invalid, `Err` on error
    async fn verify_signature(&self, payload: &[u8], signature: &str) -> Result<bool>;
}

/// No-op verifier that accepts all webhooks
///
/// **WARNING:** This verifier accepts ALL webhooks without verification.
/// Only use this for:
/// - Testing/development environments
/// - Webhook providers that don't support signatures
///
/// **NEVER use in production** for providers that support signatures.
#[derive(Debug, Clone, Copy, Default)]
pub struct NoVerification;

#[async_trait]
impl WebhookVerifier for NoVerification {
    async fn verify_signature(&self, _payload: &[u8], _signature: &str) -> Result<bool> {
        tracing::warn!(
            "NoVerification webhook verifier used - all webhooks accepted without verification"
        );
        Ok(true)
    }
}

/// HMAC-SHA256 webhook verifier with timing-safe comparison
///
/// This is the standard verification method used by most webhook providers
/// including Stripe, GitHub, Shopify, and others.
///
/// # Signature Formats
///
/// Different providers use different signature formats:
/// - **Hex encoded**: `a1b2c3d4...` (most common)
/// - **Hex with prefix**: `sha256=a1b2c3d4...` (GitHub style)
/// - **Base64 encoded**: `oWvD1A==...`
///
/// Use the appropriate constructor for your provider's format.
///
/// # Example
///
/// ```rust,ignore
/// use tideway::webhooks::HmacSha256Verifier;
///
/// // For hex-encoded signatures (default)
/// let verifier = HmacSha256Verifier::new("your_webhook_secret");
///
/// // For signatures with "sha256=" prefix (GitHub, etc.)
/// let verifier = HmacSha256Verifier::new_with_prefix("your_secret", "sha256=");
/// ```
pub struct HmacSha256Verifier {
    secret: Vec<u8>,
    /// Optional prefix to strip from signatures (e.g., "sha256=")
    signature_prefix: Option<String>,
    /// Whether signatures are base64 encoded (vs hex encoded)
    base64_encoded: bool,
}

impl HmacSha256Verifier {
    /// Create a new verifier with hex-encoded signatures (most common)
    pub fn new(secret: impl Into<Vec<u8>>) -> Self {
        Self {
            secret: secret.into(),
            signature_prefix: None,
            base64_encoded: false,
        }
    }

    /// Create a verifier that strips a prefix from signatures
    ///
    /// Use this for providers like GitHub that send signatures as `sha256=abc123...`
    pub fn new_with_prefix(secret: impl Into<Vec<u8>>, prefix: impl Into<String>) -> Self {
        Self {
            secret: secret.into(),
            signature_prefix: Some(prefix.into()),
            base64_encoded: false,
        }
    }

    /// Create a verifier for base64-encoded signatures
    pub fn new_base64(secret: impl Into<Vec<u8>>) -> Self {
        Self {
            secret: secret.into(),
            signature_prefix: None,
            base64_encoded: true,
        }
    }

    /// Compute the expected HMAC-SHA256 signature for a payload
    fn compute_signature(&self, payload: &[u8]) -> Vec<u8> {
        let mut mac =
            HmacSha256::new_from_slice(&self.secret).expect("HMAC can take key of any size");
        mac.update(payload);
        mac.finalize().into_bytes().to_vec()
    }

    /// Decode the provided signature from hex or base64
    ///
    /// # Returns
    ///
    /// * `Some(bytes)` - Successfully decoded signature
    /// * `None` - Decoding failed or required prefix was missing
    fn decode_signature(&self, signature: &str) -> Option<Vec<u8>> {
        // Strip prefix if configured - if prefix is required but missing, fail
        let sig = if let Some(ref prefix) = self.signature_prefix {
            match signature.strip_prefix(prefix.as_str()) {
                Some(stripped) => stripped,
                None => {
                    // Prefix was configured but not present in signature
                    // This is a validation failure - don't fall back to unprefixed
                    tracing::debug!(
                        expected_prefix = prefix,
                        "Webhook signature missing required prefix"
                    );
                    return None;
                }
            }
        } else {
            signature
        };

        if self.base64_encoded {
            base64::Engine::decode(&base64::engine::general_purpose::STANDARD, sig).ok()
        } else {
            hex_decode(sig)
        }
    }
}

/// Decode a hex string to bytes
fn hex_decode(s: &str) -> Option<Vec<u8>> {
    if s.len() % 2 != 0 {
        return None;
    }

    (0..s.len())
        .step_by(2)
        .map(|i| u8::from_str_radix(&s[i..i + 2], 16).ok())
        .collect()
}

/// Constant-time comparison to prevent timing attacks
///
/// Uses the `subtle` crate which provides compiler-optimization-resistant
/// constant-time operations. This prevents attackers from using timing
/// information to guess valid signatures byte-by-byte.
///
/// Unlike a naive XOR-and-fold implementation, the `subtle` crate uses
/// optimization barriers to prevent LLVM from converting bitwise operations
/// back into timing-leaking branches.
fn constant_time_compare(a: &[u8], b: &[u8]) -> bool {
    if a.len() != b.len() {
        return false;
    }

    // Use subtle's ConstantTimeEq which is resistant to compiler optimizations
    a.ct_eq(b).into()
}

#[async_trait]
impl WebhookVerifier for HmacSha256Verifier {
    async fn verify_signature(&self, payload: &[u8], signature: &str) -> Result<bool> {
        // Decode the provided signature
        let provided = match self.decode_signature(signature) {
            Some(bytes) => bytes,
            None => {
                tracing::debug!("Failed to decode webhook signature");
                return Ok(false);
            }
        };

        // Compute expected signature
        let expected = self.compute_signature(payload);

        // Use constant-time comparison to prevent timing attacks
        let is_valid = constant_time_compare(&expected, &provided);

        if !is_valid {
            tracing::debug!("Webhook signature verification failed");
        }

        Ok(is_valid)
    }
}

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

    // ============ Helper functions ============

    /// Compute a valid HMAC-SHA256 signature for testing
    fn compute_test_signature(secret: &[u8], payload: &[u8]) -> String {
        let mut mac = HmacSha256::new_from_slice(secret).expect("HMAC can take key of any size");
        mac.update(payload);
        let result = mac.finalize().into_bytes();
        // Convert to hex string
        result.iter().map(|b| format!("{:02x}", b)).collect()
    }

    // ============ hex_decode tests ============

    #[test]
    fn test_hex_decode_valid() {
        assert_eq!(hex_decode(""), Some(vec![]));
        assert_eq!(hex_decode("00"), Some(vec![0x00]));
        assert_eq!(hex_decode("ff"), Some(vec![0xff]));
        assert_eq!(hex_decode("0a1b2c"), Some(vec![0x0a, 0x1b, 0x2c]));
        assert_eq!(hex_decode("AABB"), Some(vec![0xaa, 0xbb])); // uppercase
    }

    #[test]
    fn test_hex_decode_invalid() {
        assert_eq!(hex_decode("0"), None); // odd length
        assert_eq!(hex_decode("0g"), None); // invalid char
        assert_eq!(hex_decode("xyz"), None); // invalid chars, odd length
    }

    // ============ constant_time_compare tests ============

    #[test]
    fn test_constant_time_compare_equal() {
        assert!(constant_time_compare(&[], &[]));
        assert!(constant_time_compare(&[1, 2, 3], &[1, 2, 3]));
        assert!(constant_time_compare(&[0xff; 32], &[0xff; 32]));
    }

    #[test]
    fn test_constant_time_compare_not_equal() {
        assert!(!constant_time_compare(&[1], &[2]));
        assert!(!constant_time_compare(&[1, 2, 3], &[1, 2, 4]));
        assert!(!constant_time_compare(&[0; 32], &[0xff; 32]));
    }

    #[test]
    fn test_constant_time_compare_different_lengths() {
        assert!(!constant_time_compare(&[1, 2], &[1, 2, 3]));
        assert!(!constant_time_compare(&[1, 2, 3], &[1, 2]));
        assert!(!constant_time_compare(&[], &[1]));
    }

    // ============ NoVerification tests ============

    #[tokio::test]
    async fn test_no_verification_always_returns_true() {
        let verifier = NoVerification;

        // Should return true regardless of payload and signature
        assert!(
            verifier
                .verify_signature(b"any payload", "any-signature")
                .await
                .unwrap()
        );
        assert!(verifier.verify_signature(b"", "").await.unwrap());
        assert!(verifier.verify_signature(b"test", "").await.unwrap());
        assert!(verifier.verify_signature(&[], "signature").await.unwrap());
    }

    #[tokio::test]
    async fn test_no_verification_with_various_payloads() {
        let verifier = NoVerification;

        let payloads = [
            b"simple text".as_slice(),
            b"{\"json\": \"data\"}".as_slice(),
            b"<xml>data</xml>".as_slice(),
            &[0u8, 1, 2, 3, 255], // Binary data
        ];

        for payload in payloads {
            let result = verifier.verify_signature(payload, "sig").await;
            assert!(result.is_ok());
            assert!(result.unwrap());
        }
    }

    // ============ HmacSha256Verifier creation tests ============

    #[test]
    fn test_hmac_sha256_verifier_creation() {
        let verifier = HmacSha256Verifier::new("secret-key");
        assert!(!verifier.base64_encoded);
        assert!(verifier.signature_prefix.is_none());
    }

    #[test]
    fn test_hmac_sha256_verifier_with_bytes() {
        let secret: Vec<u8> = vec![0x01, 0x02, 0x03, 0x04];
        let verifier = HmacSha256Verifier::new(secret);
        assert_eq!(verifier.secret, vec![0x01, 0x02, 0x03, 0x04]);
    }

    #[test]
    fn test_hmac_sha256_verifier_with_prefix() {
        let verifier = HmacSha256Verifier::new_with_prefix("secret", "sha256=");
        assert_eq!(verifier.signature_prefix, Some("sha256=".to_string()));
        assert!(!verifier.base64_encoded);
    }

    #[test]
    fn test_hmac_sha256_verifier_base64() {
        let verifier = HmacSha256Verifier::new_base64("secret");
        assert!(verifier.base64_encoded);
        assert!(verifier.signature_prefix.is_none());
    }

    // ============ HmacSha256Verifier verification tests ============

    #[tokio::test]
    async fn test_hmac_sha256_verifier_valid_signature() {
        let secret = b"my-webhook-secret";
        let payload = b"test payload";
        let verifier = HmacSha256Verifier::new(secret.to_vec());

        // Compute the correct signature
        let signature = compute_test_signature(secret, payload);

        let result = verifier.verify_signature(payload, &signature).await;
        assert!(result.is_ok());
        assert!(result.unwrap(), "Valid signature should pass verification");
    }

    #[tokio::test]
    async fn test_hmac_sha256_verifier_invalid_signature() {
        let secret = b"my-webhook-secret";
        let payload = b"test payload";
        let verifier = HmacSha256Verifier::new(secret.to_vec());

        // Use a completely wrong signature
        let wrong_signature = "0000000000000000000000000000000000000000000000000000000000000000";

        let result = verifier.verify_signature(payload, wrong_signature).await;
        assert!(result.is_ok());
        assert!(
            !result.unwrap(),
            "Invalid signature should fail verification"
        );
    }

    #[tokio::test]
    async fn test_hmac_sha256_verifier_wrong_secret() {
        let payload = b"test payload";

        // Create signature with one secret
        let signature = compute_test_signature(b"secret1", payload);

        // Verify with different secret
        let verifier = HmacSha256Verifier::new("secret2");
        let result = verifier.verify_signature(payload, &signature).await;
        assert!(result.is_ok());
        assert!(!result.unwrap(), "Signature with wrong secret should fail");
    }

    #[tokio::test]
    async fn test_hmac_sha256_verifier_modified_payload() {
        let secret = b"my-secret";
        let original_payload = b"original payload";
        let modified_payload = b"modified payload";

        // Create signature for original payload
        let signature = compute_test_signature(secret, original_payload);

        // Try to verify with modified payload
        let verifier = HmacSha256Verifier::new(secret.to_vec());
        let result = verifier
            .verify_signature(modified_payload, &signature)
            .await;
        assert!(result.is_ok());
        assert!(
            !result.unwrap(),
            "Modified payload should fail verification"
        );
    }

    #[tokio::test]
    async fn test_hmac_sha256_verifier_empty_signature() {
        let verifier = HmacSha256Verifier::new("secret");
        let payload = b"webhook payload";
        let signature = "";

        let result = verifier.verify_signature(payload, signature).await;
        assert!(result.is_ok());
        assert!(!result.unwrap(), "Empty signature should fail");
    }

    #[tokio::test]
    async fn test_hmac_sha256_verifier_malformed_signature() {
        let verifier = HmacSha256Verifier::new("secret");
        let payload = b"webhook payload";

        // Test various malformed signatures
        let malformed = ["not-hex", "abc", "xyz123", "0g0g0g"];

        for sig in malformed {
            let result = verifier.verify_signature(payload, sig).await;
            assert!(result.is_ok());
            assert!(
                !result.unwrap(),
                "Malformed signature '{}' should fail",
                sig
            );
        }
    }

    #[tokio::test]
    async fn test_hmac_sha256_verifier_with_prefix_valid() {
        let secret = b"github-secret";
        let payload = b"{\"action\": \"push\"}";
        let verifier = HmacSha256Verifier::new_with_prefix(secret.to_vec(), "sha256=");

        // Compute signature and add prefix
        let signature = format!("sha256={}", compute_test_signature(secret, payload));

        let result = verifier.verify_signature(payload, &signature).await;
        assert!(result.is_ok());
        assert!(result.unwrap(), "Prefixed signature should pass");
    }

    #[tokio::test]
    async fn test_hmac_sha256_verifier_with_prefix_missing_prefix() {
        let secret = b"github-secret";
        let payload = b"{\"action\": \"push\"}";
        let verifier = HmacSha256Verifier::new_with_prefix(secret.to_vec(), "sha256=");

        // Signature WITHOUT required prefix - should FAIL for security
        // When a prefix is configured, it's required - prevents accepting
        // signatures in wrong format
        let signature = compute_test_signature(secret, payload);

        let result = verifier.verify_signature(payload, &signature).await;
        assert!(result.is_ok());
        assert!(
            !result.unwrap(),
            "Signature missing required prefix should fail"
        );
    }

    #[tokio::test]
    async fn test_hmac_sha256_verifier_json_payload() {
        let secret = b"webhook-secret";
        let payload = br#"{"event":"payment.completed","data":{"id":"pay_123","amount":1000}}"#;
        let verifier = HmacSha256Verifier::new(secret.to_vec());

        let signature = compute_test_signature(secret, payload);

        let result = verifier.verify_signature(payload, &signature).await;
        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    #[tokio::test]
    async fn test_hmac_sha256_verifier_empty_payload() {
        let secret = b"secret";
        let payload = b"";
        let verifier = HmacSha256Verifier::new(secret.to_vec());

        let signature = compute_test_signature(secret, payload);

        let result = verifier.verify_signature(payload, &signature).await;
        assert!(result.is_ok());
        assert!(
            result.unwrap(),
            "Empty payload with valid signature should pass"
        );
    }

    #[tokio::test]
    async fn test_hmac_sha256_verifier_binary_payload() {
        let secret = b"secret";
        let payload: &[u8] = &[0x00, 0x01, 0xff, 0xfe, 0x80];
        let verifier = HmacSha256Verifier::new(secret.to_vec());

        let signature = compute_test_signature(secret, payload);

        let result = verifier.verify_signature(payload, &signature).await;
        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    // ============ WebhookVerifier trait tests ============

    struct CustomVerifier {
        should_pass: bool,
    }

    #[async_trait]
    impl WebhookVerifier for CustomVerifier {
        async fn verify_signature(&self, _payload: &[u8], _signature: &str) -> Result<bool> {
            Ok(self.should_pass)
        }
    }

    #[tokio::test]
    async fn test_custom_verifier_trait_impl() {
        let passing_verifier = CustomVerifier { should_pass: true };
        let failing_verifier = CustomVerifier { should_pass: false };

        assert!(
            passing_verifier
                .verify_signature(b"data", "sig")
                .await
                .unwrap()
        );
        assert!(
            !failing_verifier
                .verify_signature(b"data", "sig")
                .await
                .unwrap()
        );
    }

    #[tokio::test]
    async fn test_verifier_as_dyn_trait() {
        let verifier: Box<dyn WebhookVerifier> = Box::new(NoVerification);
        let result = verifier.verify_signature(b"test", "sig").await;
        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    #[tokio::test]
    async fn test_verifier_in_arc() {
        use std::sync::Arc;

        let secret = b"arc-secret";
        let payload = b"arc-test";
        let signature = compute_test_signature(secret, payload);

        let verifier: Arc<dyn WebhookVerifier> = Arc::new(HmacSha256Verifier::new(secret.to_vec()));
        let result = verifier.verify_signature(payload, &signature).await;
        assert!(result.is_ok());
        assert!(result.unwrap());
    }
}