leaktor 0.4.1

A secrets scanner with pattern matching, entropy analysis, and live validation
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
//! Configuration-driven API validator for services with known validation endpoints.
//!
//! This validator makes HTTP requests to service APIs to verify if a detected
//! secret is actually valid (active). It supports ~100+ services organized by
//! authentication type (Bearer token, API key header, Basic auth, etc.).
//!
//! Each service is configured with:
//! - API endpoint URL
//! - Authentication method
//! - Expected success/failure status codes

use crate::models::{Secret, SecretType};
use crate::validators::Validator;
use anyhow::Result;
use reqwest::{Client, StatusCode};

pub struct ServiceValidator {
    client: Client,
}

impl ServiceValidator {
    pub fn new() -> Self {
        Self {
            client: Client::builder()
                .timeout(std::time::Duration::from_secs(5))
                .user_agent("Leaktor-Secret-Scanner")
                .build()
                .unwrap(),
        }
    }
}

impl Default for ServiceValidator {
    fn default() -> Self {
        Self::new()
    }
}

/// How to authenticate with the service API
enum AuthMethod {
    /// Authorization: Bearer {token}
    Bearer,
    /// Authorization: token {token}
    TokenAuth,
    /// Custom header name with the token as value
    Header(&'static str),
    /// Custom header with prefix: {prefix} {token}
    HeaderWithPrefix(&'static str, &'static str),
    /// HTTP Basic auth with token as password
    BasicAuthPassword,
    /// Token appended as query parameter
    QueryParam(&'static str),
}

/// Configuration for validating a service via API
struct ServiceConfig {
    url: &'static str,
    auth: AuthMethod,
    /// Additional headers to include
    extra_headers: &'static [(&'static str, &'static str)],
}

/// Get the API validation config for a secret type, if available
fn get_config(secret_type: &SecretType) -> Option<ServiceConfig> {
    match secret_type {
        // ══════════════════════════════════════════════
        // Cloud & Infrastructure
        // ══════════════════════════════════════════════
        SecretType::CloudflareApiToken => Some(ServiceConfig {
            url: "https://api.cloudflare.com/client/v4/user/tokens/verify",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::VercelToken => Some(ServiceConfig {
            url: "https://api.vercel.com/v2/user",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::NetlifyPat | SecretType::NetlifyToken => Some(ServiceConfig {
            url: "https://api.netlify.com/api/v1/user",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::HerokuApiKey => Some(ServiceConfig {
            url: "https://api.heroku.com/account",
            auth: AuthMethod::Bearer,
            extra_headers: &[("Accept", "application/vnd.heroku+json; version=3")],
        }),
        SecretType::RenderApiKey => Some(ServiceConfig {
            url: "https://api.render.com/v1/owners",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::HetznerApiToken => Some(ServiceConfig {
            url: "https://api.hetzner.cloud/v1/servers",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::VultrApiKey => Some(ServiceConfig {
            url: "https://api.vultr.com/v2/account",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::LinodeApiToken => Some(ServiceConfig {
            url: "https://api.linode.com/v4/profile",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::ScalewayApiKey => Some(ServiceConfig {
            url: "https://api.scaleway.com/account/v3/projects",
            auth: AuthMethod::Header("X-Auth-Token"),
            extra_headers: &[],
        }),

        // ══════════════════════════════════════════════
        // AI / ML Platforms
        // ══════════════════════════════════════════════
        SecretType::CohereApiKey => Some(ServiceConfig {
            url: "https://api.cohere.ai/v1/models",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::ReplicateApiKey => Some(ServiceConfig {
            url: "https://api.replicate.com/v1/account",
            auth: AuthMethod::TokenAuth,
            extra_headers: &[],
        }),
        SecretType::GroqApiKey => Some(ServiceConfig {
            url: "https://api.groq.com/openai/v1/models",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::MistralApiKey => Some(ServiceConfig {
            url: "https://api.mistral.ai/v1/models",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::PerplexityApiKey => Some(ServiceConfig {
            url: "https://api.perplexity.ai/chat/completions",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::DeepgramApiKey => Some(ServiceConfig {
            url: "https://api.deepgram.com/v1/projects",
            auth: AuthMethod::TokenAuth,
            extra_headers: &[],
        }),
        SecretType::AssemblyAiApiKey => Some(ServiceConfig {
            url: "https://api.assemblyai.com/v2/transcript",
            auth: AuthMethod::Header("authorization"),
            extra_headers: &[],
        }),
        SecretType::ElevenLabsApiKey => Some(ServiceConfig {
            url: "https://api.elevenlabs.io/v1/user",
            auth: AuthMethod::Header("xi-api-key"),
            extra_headers: &[],
        }),
        SecretType::WandBApiKey => Some(ServiceConfig {
            url: "https://api.wandb.ai/graphql",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),

        // ══════════════════════════════════════════════
        // Communication & Email
        // ══════════════════════════════════════════════
        SecretType::MailgunApiKey => Some(ServiceConfig {
            url: "https://api.mailgun.net/v3/domains",
            auth: AuthMethod::BasicAuthPassword,
            extra_headers: &[],
        }),
        SecretType::PostmarkApiToken => Some(ServiceConfig {
            url: "https://api.postmarkapp.com/server",
            auth: AuthMethod::Header("X-Postmark-Server-Token"),
            extra_headers: &[("Accept", "application/json")],
        }),
        SecretType::BrevoApiKey => Some(ServiceConfig {
            url: "https://api.brevo.com/v3/account",
            auth: AuthMethod::Header("api-key"),
            extra_headers: &[],
        }),
        SecretType::ResendApiKey => Some(ServiceConfig {
            url: "https://api.resend.com/domains",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::MailerSendApiKey => Some(ServiceConfig {
            url: "https://api.mailersend.com/v1/api-quota",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::MandrillApiKey => Some(ServiceConfig {
            url: "https://mandrillapp.com/api/1.0/users/info.json",
            auth: AuthMethod::Bearer, // Actually uses POST with key in body, but Bearer works for validation
            extra_headers: &[],
        }),
        SecretType::SparkPostApiKey => Some(ServiceConfig {
            url: "https://api.sparkpost.com/api/v1/account",
            auth: AuthMethod::Header("Authorization"),
            extra_headers: &[],
        }),
        SecretType::VonageApiKey => Some(ServiceConfig {
            url: "https://rest.nexmo.com/account/get-balance",
            auth: AuthMethod::QueryParam("api_key"),
            extra_headers: &[],
        }),
        SecretType::MessageBirdApiKey => Some(ServiceConfig {
            url: "https://rest.messagebird.com/balance",
            auth: AuthMethod::HeaderWithPrefix("AccessKey", ""),
            extra_headers: &[],
        }),

        // ══════════════════════════════════════════════
        // SaaS & Productivity
        // ══════════════════════════════════════════════
        SecretType::NotionApiKey => Some(ServiceConfig {
            url: "https://api.notion.com/v1/users/me",
            auth: AuthMethod::Bearer,
            extra_headers: &[("Notion-Version", "2022-06-28")],
        }),
        SecretType::AirtableApiKey | SecretType::AirtableOAuthToken => Some(ServiceConfig {
            url: "https://api.airtable.com/v0/meta/whoami",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::AsanaSecret | SecretType::AsanaClientId => Some(ServiceConfig {
            url: "https://app.asana.com/api/1.0/users/me",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::ClickUpPersonalToken => Some(ServiceConfig {
            url: "https://api.clickup.com/api/v2/user",
            auth: AuthMethod::Header("Authorization"),
            extra_headers: &[],
        }),
        SecretType::FigmaPat => Some(ServiceConfig {
            url: "https://api.figma.com/v1/me",
            auth: AuthMethod::Header("X-FIGMA-TOKEN"),
            extra_headers: &[],
        }),
        SecretType::TodoistApiKey => Some(ServiceConfig {
            url: "https://api.todoist.com/rest/v2/projects",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::TypeformApiToken => Some(ServiceConfig {
            url: "https://api.typeform.com/me",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::IntercomAccessToken => Some(ServiceConfig {
            url: "https://api.intercom.io/me",
            auth: AuthMethod::Bearer,
            extra_headers: &[("Accept", "application/json")],
        }),
        SecretType::FreshdeskApiKey => Some(ServiceConfig {
            url: "https://support.freshdesk.com/api/v2/agents/me",
            auth: AuthMethod::BasicAuthPassword,
            extra_headers: &[],
        }),
        SecretType::WrikeApiToken => Some(ServiceConfig {
            url: "https://www.wrike.com/api/v4/contacts?me=true",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),

        // ══════════════════════════════════════════════
        // DevTools & CI/CD
        // ══════════════════════════════════════════════
        SecretType::BitbucketAppPassword | SecretType::BitbucketToken => Some(ServiceConfig {
            url: "https://api.bitbucket.org/2.0/user",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::CircleCiToken | SecretType::CircleCiPersonalToken => Some(ServiceConfig {
            url: "https://circleci.com/api/v2/me",
            auth: AuthMethod::Header("Circle-Token"),
            extra_headers: &[],
        }),
        SecretType::TravisCiApiToken | SecretType::TravisCiToken => Some(ServiceConfig {
            url: "https://api.travis-ci.com/user",
            auth: AuthMethod::TokenAuth,
            extra_headers: &[("Travis-API-Version", "3")],
        }),
        SecretType::BuildKiteApiToken => Some(ServiceConfig {
            url: "https://api.buildkite.com/v2/organizations",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::PostmanApiToken => Some(ServiceConfig {
            url: "https://api.getpostman.com/me",
            auth: AuthMethod::Header("X-Api-Key"),
            extra_headers: &[],
        }),
        SecretType::CodecovAccessToken => Some(ServiceConfig {
            url: "https://codecov.io/api/v2/",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::SnykApiToken => Some(ServiceConfig {
            url: "https://api.snyk.io/rest/self",
            auth: AuthMethod::TokenAuth,
            extra_headers: &[("Content-Type", "application/vnd.api+json")],
        }),
        SecretType::SonarQubeToken => Some(ServiceConfig {
            url: "https://sonarcloud.io/api/authentication/validate",
            auth: AuthMethod::BasicAuthPassword,
            extra_headers: &[],
        }),

        // ══════════════════════════════════════════════
        // Monitoring & Observability
        // ══════════════════════════════════════════════
        SecretType::PagerDutyApiKey => Some(ServiceConfig {
            url: "https://api.pagerduty.com/users?limit=1",
            auth: AuthMethod::HeaderWithPrefix("Authorization", "Token token="),
            extra_headers: &[("Content-Type", "application/json")],
        }),
        // SentryDsn is a URL, not an API token — no API validation possible
        SecretType::PosthogApiKey => Some(ServiceConfig {
            url: "https://app.posthog.com/api/projects/",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::BugSnagApiKey => Some(ServiceConfig {
            url: "https://api.bugsnag.com/user",
            auth: AuthMethod::Header("Authorization"),
            extra_headers: &[],
        }),
        SecretType::RollbarApiKey => Some(ServiceConfig {
            url: "https://api.rollbar.com/api/1/status/ping",
            auth: AuthMethod::Header("X-Rollbar-Access-Token"),
            extra_headers: &[],
        }),
        SecretType::HoneycombApiKey => Some(ServiceConfig {
            url: "https://api.honeycomb.io/1/auth",
            auth: AuthMethod::Header("X-Honeycomb-Team"),
            extra_headers: &[],
        }),

        // ══════════════════════════════════════════════
        // Marketing & CRM
        // ══════════════════════════════════════════════
        SecretType::HubSpotApiKey | SecretType::HubSpotPrivateAppToken => Some(ServiceConfig {
            url: "https://api.hubapi.com/account-info/v3/api-usage/daily/private",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::ConvertKitApiKey => Some(ServiceConfig {
            url: "https://api.convertkit.com/v3/account",
            auth: AuthMethod::QueryParam("api_key"),
            extra_headers: &[],
        }),
        SecretType::KlaviyoApiKey => Some(ServiceConfig {
            url: "https://a.klaviyo.com/api/accounts/",
            auth: AuthMethod::HeaderWithPrefix("Authorization", "Klaviyo-API-Key "),
            extra_headers: &[("revision", "2023-12-15")],
        }),

        // ══════════════════════════════════════════════
        // Maps & Location
        // ══════════════════════════════════════════════
        SecretType::MapboxToken => Some(ServiceConfig {
            url: "https://api.mapbox.com/tokens/v2",
            auth: AuthMethod::QueryParam("access_token"),
            extra_headers: &[],
        }),

        // ══════════════════════════════════════════════
        // Database & Data
        // ══════════════════════════════════════════════
        SecretType::FaunaDbApiKey => Some(ServiceConfig {
            url: "https://graphql.fauna.com/graphql",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::PlanetScaleToken | SecretType::PlanetScalePassword => Some(ServiceConfig {
            url: "https://api.planetscale.com/v1/organizations",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::NeonApiKey => Some(ServiceConfig {
            url: "https://console.neon.tech/api/v2/projects",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::AivenApiToken => Some(ServiceConfig {
            url: "https://api.aiven.io/v1/me",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::InfluxDbToken => Some(ServiceConfig {
            url: "https://cloud2.influxdata.com/api/v2/me",
            auth: AuthMethod::TokenAuth,
            extra_headers: &[],
        }),

        // ══════════════════════════════════════════════
        // Payment
        // ══════════════════════════════════════════════
        SecretType::SquareAccessToken | SecretType::SquareOAuthToken => Some(ServiceConfig {
            url: "https://connect.squareup.com/v2/locations",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::CoinbaseAccessToken => Some(ServiceConfig {
            url: "https://api.coinbase.com/v2/user",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::GoCardlessApiToken => Some(ServiceConfig {
            url: "https://api.gocardless.com/creditors",
            auth: AuthMethod::Bearer,
            extra_headers: &[("GoCardless-Version", "2019-11-07")],
        }),
        SecretType::PaddleApiKey => Some(ServiceConfig {
            url: "https://vendors.paddle.com/api/2.0/subscription/users",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::LemonSqueezyApiKey => Some(ServiceConfig {
            url: "https://api.lemonsqueezy.com/v1/users/me",
            auth: AuthMethod::Bearer,
            extra_headers: &[("Accept", "application/vnd.api+json")],
        }),
        SecretType::RecurlyApiKey => Some(ServiceConfig {
            url: "https://v3.recurly.com/sites",
            auth: AuthMethod::Bearer,
            extra_headers: &[("Accept", "application/vnd.recurly.v2021-02-25+json")],
        }),

        // ══════════════════════════════════════════════
        // Content & CMS
        // ══════════════════════════════════════════════
        SecretType::ContentfulApiToken => Some(ServiceConfig {
            url: "https://cdn.contentful.com/spaces",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::StoryblokApiToken => Some(ServiceConfig {
            url: "https://api.storyblok.com/v2/cdn/spaces/me",
            auth: AuthMethod::QueryParam("token"),
            extra_headers: &[],
        }),
        SecretType::WebflowApiToken => Some(ServiceConfig {
            url: "https://api.webflow.com/v2/token/introspect",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),

        // ══════════════════════════════════════════════
        // Security & Identity
        // ══════════════════════════════════════════════
        SecretType::LaunchDarklyKey => Some(ServiceConfig {
            url: "https://app.launchdarkly.com/api/v2/caller-identity",
            auth: AuthMethod::Header("Authorization"),
            extra_headers: &[],
        }),
        SecretType::FastlyApiToken => Some(ServiceConfig {
            url: "https://api.fastly.com/current_user",
            auth: AuthMethod::Header("Fastly-Key"),
            extra_headers: &[],
        }),

        // ══════════════════════════════════════════════
        // Miscellaneous Services
        // ══════════════════════════════════════════════
        SecretType::AlgoliaApiKey => Some(ServiceConfig {
            url: "https://analytics.algolia.com/2/searches",
            auth: AuthMethod::Header("X-Algolia-API-Key"),
            extra_headers: &[],
        }),
        SecretType::TwilioAuthToken => Some(ServiceConfig {
            url: "https://api.twilio.com/2010-04-01/Accounts.json",
            auth: AuthMethod::BasicAuthPassword,
            extra_headers: &[],
        }),
        SecretType::BrowserStackAccessKey => Some(ServiceConfig {
            url: "https://api.browserstack.com/automate/plan.json",
            auth: AuthMethod::BasicAuthPassword,
            extra_headers: &[],
        }),
        SecretType::ShodanApiKey => Some(ServiceConfig {
            url: "https://api.shodan.io/api-info",
            auth: AuthMethod::QueryParam("key"),
            extra_headers: &[],
        }),
        SecretType::FullStoryApiKey => Some(ServiceConfig {
            url: "https://api.fullstory.com/operations/v1",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::SalesforceApiToken => Some(ServiceConfig {
            url: "https://login.salesforce.com/services/oauth2/userinfo",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::JFrogIdentityToken => Some(ServiceConfig {
            url: "https://jfrog.com/api/system/ping",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::DatabricksToken => Some(ServiceConfig {
            url: "https://accounts.cloud.databricks.com/api/2.0/preview/scim/v2/Me",
            auth: AuthMethod::Bearer,
            extra_headers: &[],
        }),
        SecretType::SourcegraphAccessToken => Some(ServiceConfig {
            url: "https://sourcegraph.com/.api/graphql",
            auth: AuthMethod::TokenAuth,
            extra_headers: &[],
        }),

        _ => None,
    }
}

#[async_trait::async_trait]
impl Validator for ServiceValidator {
    async fn validate(&self, secret: &Secret) -> Result<bool> {
        let config = match get_config(&secret.secret_type) {
            Some(c) => c,
            None => return Ok(false),
        };

        let mut request = self.client.get(config.url);

        // Apply authentication
        match config.auth {
            AuthMethod::Bearer => {
                request = request.header("Authorization", format!("Bearer {}", secret.value));
            }
            AuthMethod::TokenAuth => {
                request = request.header("Authorization", format!("token {}", secret.value));
            }
            AuthMethod::Header(name) => {
                request = request.header(name, &secret.value);
            }
            AuthMethod::HeaderWithPrefix(header, prefix) => {
                request =
                    request.header(header, format!("{}{}", prefix, secret.value));
            }
            AuthMethod::BasicAuthPassword => {
                request = request.basic_auth("api", Some(&secret.value));
            }
            AuthMethod::QueryParam(param) => {
                request = request.query(&[(param, &secret.value)]);
            }
        }

        // Apply extra headers
        for (name, value) in config.extra_headers {
            request = request.header(*name, *value);
        }

        // Send request and check response
        let response = request.send().await?;
        match response.status() {
            StatusCode::OK | StatusCode::CREATED | StatusCode::NO_CONTENT => Ok(true),
            StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => Ok(false),
            StatusCode::TOO_MANY_REQUESTS => {
                // Propagate 429 as an error so the rate-limiter retry logic kicks in
                anyhow::bail!("429 Too Many Requests from {}", config.url)
            }
            status if status.is_server_error() => {
                // 5xx errors are transient — propagate so retry logic can handle them
                anyhow::bail!("Server error {} from {}", status.as_u16(), config.url)
            }
            _ => Ok(false),
        }
    }

    fn supports(&self, secret_type: &SecretType) -> bool {
        get_config(secret_type).is_some()
    }
}

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

    #[test]
    fn test_supports_known_services() {
        let validator = ServiceValidator::new();
        assert!(validator.supports(&SecretType::CloudflareApiToken));
        assert!(validator.supports(&SecretType::NotionApiKey));
        assert!(validator.supports(&SecretType::CohereApiKey));
        assert!(validator.supports(&SecretType::SquareAccessToken));
        assert!(validator.supports(&SecretType::CircleCiToken));
    }

    #[test]
    fn test_does_not_support_unknown() {
        let validator = ServiceValidator::new();
        // Types already handled by existing dedicated validators
        assert!(!validator.supports(&SecretType::GitHubPat));
        assert!(!validator.supports(&SecretType::AwsAccessKey));
        assert!(!validator.supports(&SecretType::SlackToken));
        // Generic types
        assert!(!validator.supports(&SecretType::GenericApiKey));
        assert!(!validator.supports(&SecretType::HighEntropyString));
    }

    #[tokio::test]
    async fn test_invalid_cloudflare_token() {
        let validator = ServiceValidator::new();
        let secret = Secret::new(
            SecretType::CloudflareApiToken,
            "invalid_token_12345678901234567890123".to_string(),
            4.0,
            Severity::High,
            0.9,
        );
        let result = validator.validate(&secret).await;
        assert!(result.is_ok());
        // Should return false for invalid token
        assert!(!result.unwrap());
    }

    #[tokio::test]
    async fn test_invalid_notion_token() {
        let validator = ServiceValidator::new();
        let secret = Secret::new(
            SecretType::NotionApiKey,
            "ntn_invalidtokeninvalidtokeninvalidtokeninva".to_string(),
            4.0,
            Severity::High,
            0.9,
        );
        let result = validator.validate(&secret).await;
        assert!(result.is_ok());
        assert!(!result.unwrap());
    }
}