github-bot-sdk 0.2.1

A comprehensive Rust SDK for GitHub App integration with authentication, webhooks, and API client
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
//! Tests for webhook signature validation.

use super::*;
use crate::auth::{GitHubAppId, PrivateKey, SecretProvider};
use crate::error::SecretError;
use async_trait::async_trait;
use chrono::Duration;
use std::sync::Arc;

// ============================================================================
// Mock Secret Provider
// ============================================================================

struct MockSecretProvider {
    webhook_secret: String,
}

impl MockSecretProvider {
    fn new(secret: String) -> Self {
        Self {
            webhook_secret: secret,
        }
    }
}

#[async_trait]
impl SecretProvider for MockSecretProvider {
    async fn get_private_key(&self) -> Result<PrivateKey, SecretError> {
        // Not used in signature validation
        Err(SecretError::NotFound {
            key: "private_key".to_string(),
        })
    }

    async fn get_app_id(&self) -> Result<GitHubAppId, SecretError> {
        // Not used in signature validation
        Ok(GitHubAppId::new(12345))
    }

    async fn get_webhook_secret(&self) -> Result<String, SecretError> {
        Ok(self.webhook_secret.clone())
    }

    fn cache_duration(&self) -> Duration {
        Duration::minutes(5)
    }
}

// ============================================================================
// Test: Valid Signature Validation
// ============================================================================

#[tokio::test]
async fn test_validate_with_valid_signature() {
    // Arrange: Create validator with known secret
    let secret = "test_webhook_secret";
    let secret_provider = Arc::new(MockSecretProvider::new(secret.to_string()));
    let validator = SignatureValidator::new(secret_provider);

    // GitHub webhook example payload
    let payload = br#"{"action":"opened","number":1,"pull_request":{"id":1}}"#;

    // Compute expected signature manually for verification
    // This is the HMAC-SHA256 of the payload with the secret
    // Expected: sha256=<hex_encoded_hmac>
    use hmac::{Hmac, KeyInit, Mac};
    use sha2::Sha256;
    type HmacSha256 = Hmac<Sha256>;

    let mut mac = HmacSha256::new_from_slice(secret.as_bytes()).unwrap();
    mac.update(payload);
    let result = mac.finalize();
    let expected_hex = hex::encode(result.into_bytes());
    let signature = format!("sha256={}", expected_hex);

    // Act: Validate the signature
    let is_valid = validator
        .validate(payload, &signature)
        .await
        .expect("Validation should not error");

    // Assert: Signature should be valid (Assertion #16)
    assert!(is_valid, "Valid signature should pass validation");
}

#[tokio::test]
async fn test_validate_with_github_example_payload() {
    // Arrange: Real GitHub webhook example
    let secret = "It's a Secret to Everybody";
    let secret_provider = Arc::new(MockSecretProvider::new(secret.to_string()));
    let validator = SignatureValidator::new(secret_provider);

    // Real GitHub ping event payload
    let payload = br#"{"zen":"Design for failure.","hook_id":1}"#;

    // Compute expected signature
    use hmac::{Hmac, KeyInit, Mac};
    use sha2::Sha256;
    type HmacSha256 = Hmac<Sha256>;

    let mut mac = HmacSha256::new_from_slice(secret.as_bytes()).unwrap();
    mac.update(payload);
    let result = mac.finalize();
    let expected_hex = hex::encode(result.into_bytes());
    let signature = format!("sha256={}", expected_hex);

    // Act
    let is_valid = validator
        .validate(payload, &signature)
        .await
        .expect("Validation should not error");

    // Assert
    assert!(is_valid, "GitHub example signature should be valid");
}

// ============================================================================
// Test: Invalid Signature Detection
// ============================================================================

#[tokio::test]
async fn test_validate_with_tampered_payload() {
    // Arrange
    let secret = "test_secret";
    let secret_provider = Arc::new(MockSecretProvider::new(secret.to_string()));
    let validator = SignatureValidator::new(secret_provider);

    let original_payload = br#"{"action":"opened","number":1}"#;
    let tampered_payload = br#"{"action":"closed","number":1}"#; // Changed action

    // Create signature for original payload
    use hmac::{Hmac, KeyInit, Mac};
    use sha2::Sha256;
    type HmacSha256 = Hmac<Sha256>;

    let mut mac = HmacSha256::new_from_slice(secret.as_bytes()).unwrap();
    mac.update(original_payload);
    let result = mac.finalize();
    let signature = format!("sha256={}", hex::encode(result.into_bytes()));

    // Act: Validate tampered payload with original signature
    let is_valid = validator
        .validate(tampered_payload, &signature)
        .await
        .expect("Validation should not error");

    // Assert: Should detect tampering (Assertion #17)
    assert!(!is_valid, "Tampered payload should fail validation");
}

#[tokio::test]
async fn test_validate_with_wrong_secret() {
    // Arrange
    let correct_secret = "correct_secret";
    let wrong_secret = "wrong_secret";

    // Create signature with correct secret
    let payload = br#"{"action":"opened"}"#;
    use hmac::{Hmac, KeyInit, Mac};
    use sha2::Sha256;
    type HmacSha256 = Hmac<Sha256>;

    let mut mac = HmacSha256::new_from_slice(correct_secret.as_bytes()).unwrap();
    mac.update(payload);
    let result = mac.finalize();
    let signature = format!("sha256={}", hex::encode(result.into_bytes()));

    // Validator uses wrong secret
    let secret_provider = Arc::new(MockSecretProvider::new(wrong_secret.to_string()));
    let validator = SignatureValidator::new(secret_provider);

    // Act
    let is_valid = validator
        .validate(payload, &signature)
        .await
        .expect("Validation should not error");

    // Assert: Wrong secret should fail validation
    assert!(!is_valid, "Wrong secret should fail validation");
}

#[tokio::test]
async fn test_validate_with_modified_signature() {
    // Arrange
    let secret = "test_secret";
    let secret_provider = Arc::new(MockSecretProvider::new(secret.to_string()));
    let validator = SignatureValidator::new(secret_provider);

    let payload = br#"{"action":"opened"}"#;

    // Create valid signature
    use hmac::{Hmac, KeyInit, Mac};
    use sha2::Sha256;
    type HmacSha256 = Hmac<Sha256>;

    let mut mac = HmacSha256::new_from_slice(secret.as_bytes()).unwrap();
    mac.update(payload);
    let result = mac.finalize();
    let mut sig_bytes = hex::encode(result.into_bytes());

    // Modify one character in the signature
    if let Some(ch) = sig_bytes.chars().next() {
        let new_ch = if ch == 'a' { 'b' } else { 'a' };
        sig_bytes = format!("{}{}", new_ch, &sig_bytes[1..]);
    }
    let signature = format!("sha256={}", sig_bytes);

    // Act
    let is_valid = validator
        .validate(payload, &signature)
        .await
        .expect("Validation should not error");

    // Assert
    assert!(!is_valid, "Modified signature should fail validation");
}

// ============================================================================
// Test: Signature Format Validation
// ============================================================================

#[tokio::test]
async fn test_validate_with_missing_prefix() {
    // Arrange
    let secret = "test_secret";
    let secret_provider = Arc::new(MockSecretProvider::new(secret.to_string()));
    let validator = SignatureValidator::new(secret_provider);

    let payload = br#"{"action":"opened"}"#;
    let signature = "a1b2c3d4e5f6"; // Missing "sha256=" prefix

    // Act
    let result = validator.validate(payload, signature).await;

    // Assert: Should return error for invalid format
    assert!(result.is_err(), "Missing prefix should return error");
    if let Err(ValidationError::InvalidSignatureFormat { .. }) = result {
        // Expected error type
    } else {
        panic!("Expected InvalidSignatureFormat error");
    }
}

#[tokio::test]
async fn test_validate_with_invalid_hex_encoding() {
    // Arrange
    let secret = "test_secret";
    let secret_provider = Arc::new(MockSecretProvider::new(secret.to_string()));
    let validator = SignatureValidator::new(secret_provider);

    let payload = br#"{"action":"opened"}"#;
    let signature = "sha256=not_valid_hex!!!"; // Invalid hex characters

    // Act
    let result = validator.validate(payload, signature).await;

    // Assert
    assert!(result.is_err(), "Invalid hex should return error");
    if let Err(ValidationError::InvalidSignatureFormat { .. }) = result {
        // Expected error type
    } else {
        panic!("Expected InvalidSignatureFormat error");
    }
}

#[tokio::test]
async fn test_validate_with_empty_signature() {
    // Arrange
    let secret = "test_secret";
    let secret_provider = Arc::new(MockSecretProvider::new(secret.to_string()));
    let validator = SignatureValidator::new(secret_provider);

    let payload = br#"{"action":"opened"}"#;
    let signature = "";

    // Act
    let result = validator.validate(payload, signature).await;

    // Assert
    assert!(result.is_err(), "Empty signature should return error");
}

#[tokio::test]
async fn test_validate_with_wrong_algorithm_prefix() {
    // Arrange
    let secret = "test_secret";
    let secret_provider = Arc::new(MockSecretProvider::new(secret.to_string()));
    let validator = SignatureValidator::new(secret_provider);

    let payload = br#"{"action":"opened"}"#;
    let signature = "sha1=a1b2c3d4e5f6"; // Wrong algorithm (should be sha256)

    // Act
    let result = validator.validate(payload, signature).await;

    // Assert
    assert!(
        result.is_err(),
        "Wrong algorithm prefix should return error"
    );
}

// ============================================================================
// Test: Edge Cases
// ============================================================================

#[tokio::test]
async fn test_validate_with_empty_payload() {
    // Arrange
    let secret = "test_secret";
    let secret_provider = Arc::new(MockSecretProvider::new(secret.to_string()));
    let validator = SignatureValidator::new(secret_provider);

    let payload = b"";

    // Compute signature for empty payload
    use hmac::{Hmac, KeyInit, Mac};
    use sha2::Sha256;
    type HmacSha256 = Hmac<Sha256>;

    let mut mac = HmacSha256::new_from_slice(secret.as_bytes()).unwrap();
    mac.update(payload);
    let result = mac.finalize();
    let signature = format!("sha256={}", hex::encode(result.into_bytes()));

    // Act
    let is_valid = validator
        .validate(payload, &signature)
        .await
        .expect("Validation should not error");

    // Assert: Empty payload should still validate correctly
    assert!(is_valid, "Empty payload with valid signature should pass");
}

#[tokio::test]
async fn test_validate_with_large_payload() {
    // Arrange
    let secret = "test_secret";
    let secret_provider = Arc::new(MockSecretProvider::new(secret.to_string()));
    let validator = SignatureValidator::new(secret_provider);

    // Create a large payload (1MB)
    let large_payload = vec![b'a'; 1024 * 1024];

    // Compute signature
    use hmac::{Hmac, KeyInit, Mac};
    use sha2::Sha256;
    type HmacSha256 = Hmac<Sha256>;

    let mut mac = HmacSha256::new_from_slice(secret.as_bytes()).unwrap();
    mac.update(&large_payload);
    let result = mac.finalize();
    let signature = format!("sha256={}", hex::encode(result.into_bytes()));

    // Act: Should complete within performance requirement
    let start = std::time::Instant::now();
    let is_valid = validator
        .validate(&large_payload, &signature)
        .await
        .expect("Validation should not error");
    let duration = start.elapsed();

    // Assert
    assert!(is_valid, "Large payload should validate correctly");
    // Allow up to 1s on a slow/loaded CI machine; HMAC-SHA256 over 1 MB is
    // CPU-bound and typically completes in <10ms, but wall-clock assertions
    // are inherently environment-sensitive.
    assert!(
        duration.as_millis() < 1000,
        "Validation should complete in <1000ms, took {}ms",
        duration.as_millis()
    );
}

#[tokio::test]
async fn test_validate_with_unicode_in_payload() {
    // Arrange
    let secret = "test_secret";
    let secret_provider = Arc::new(MockSecretProvider::new(secret.to_string()));
    let validator = SignatureValidator::new(secret_provider);

    let payload = r#"{"message":"Hello δΈ–η•Œ 🌍"}"#.as_bytes();

    // Compute signature
    use hmac::{Hmac, KeyInit, Mac};
    use sha2::Sha256;
    type HmacSha256 = Hmac<Sha256>;

    let mut mac = HmacSha256::new_from_slice(secret.as_bytes()).unwrap();
    mac.update(payload);
    let result = mac.finalize();
    let signature = format!("sha256={}", hex::encode(result.into_bytes()));

    // Act
    let is_valid = validator
        .validate(payload, &signature)
        .await
        .expect("Validation should not error");

    // Assert
    assert!(is_valid, "Unicode payload should validate correctly");
}

#[tokio::test]
async fn test_validate_with_special_characters_in_secret() {
    // Arrange: Secret with special characters
    let secret = "my!@#$%^&*()secret_key";
    let secret_provider = Arc::new(MockSecretProvider::new(secret.to_string()));
    let validator = SignatureValidator::new(secret_provider);

    let payload = br#"{"action":"opened"}"#;

    // Compute signature
    use hmac::{Hmac, KeyInit, Mac};
    use sha2::Sha256;
    type HmacSha256 = Hmac<Sha256>;

    let mut mac = HmacSha256::new_from_slice(secret.as_bytes()).unwrap();
    mac.update(payload);
    let result = mac.finalize();
    let signature = format!("sha256={}", hex::encode(result.into_bytes()));

    // Act
    let is_valid = validator
        .validate(payload, &signature)
        .await
        .expect("Validation should not error");

    // Assert
    assert!(
        is_valid,
        "Special characters in secret should work correctly"
    );
}

// ============================================================================
// Test: Constant-Time Comparison
// ============================================================================

#[tokio::test]
async fn test_uses_constant_time_comparison() {
    // This test verifies that the implementation uses the subtle crate's
    // constant-time comparison. The actual timing attack resistance is
    // provided by the subtle::ConstantTimeEq trait, which is used in
    // the constant_time_compare method.
    //
    // We verify this by checking that both valid and invalid signatures
    // go through the full validation flow without early termination.

    let secret = "test_secret";
    let secret_provider = Arc::new(MockSecretProvider::new(secret.to_string()));
    let validator = SignatureValidator::new(secret_provider);

    let payload = br#"{"action":"opened"}"#;

    // Create valid signature
    use hmac::{Hmac, KeyInit, Mac};
    use sha2::Sha256;
    type HmacSha256 = Hmac<Sha256>;

    let mut mac = HmacSha256::new_from_slice(secret.as_bytes()).unwrap();
    mac.update(payload);
    let result = mac.finalize();
    let result_bytes = result.into_bytes();
    let valid_sig = format!("sha256={}", hex::encode(result_bytes));

    // Create invalid signature (completely different)
    let invalid_sig = "sha256=0000000000000000000000000000000000000000000000000000000000000000";

    // Both should process without error (though one returns false)
    let valid_result = validator.validate(payload, &valid_sig).await;
    let invalid_result = validator.validate(payload, invalid_sig).await;

    assert!(valid_result.is_ok() && valid_result.unwrap());
    assert!(invalid_result.is_ok() && !invalid_result.unwrap());

    // The key security property is that we use subtle::ConstantTimeEq
    // in the constant_time_compare method, which provides timing attack
    // resistance at the cryptographic level
} // ============================================================================
  // Test: Debug Output Security
  // ============================================================================

#[test]
fn test_debug_output_does_not_expose_secrets() {
    // Arrange
    let secret = "super_secret_webhook_key";
    let secret_provider = Arc::new(MockSecretProvider::new(secret.to_string()));
    let validator = SignatureValidator::new(secret_provider);

    // Act
    let debug_output = format!("{:?}", validator);

    // Assert: Secret should not appear in debug output
    assert!(
        !debug_output.contains(secret),
        "Debug output should not contain secret"
    );
    assert!(
        debug_output.contains("REDACTED"),
        "Debug output should indicate redaction"
    );
}