seer-core 0.52.2

Core library for Seer domain name utilities
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
//! Subdomain enumeration via Certificate Transparency logs.
//!
//! CT aggregators are operationally flaky, so enumeration is resilient on two
//! axes: per-source retries that understand crt.sh's transient 404/429/HTML
//! responses, and an ordered chain of independent sources (crt.sh, then
//! certspotter) so a downed primary falls through to a fallback provider.
//!
//! Every request is bounded by the per-request timeout (`timeouts.ct_secs`,
//! 30s by default), and each source — its attempts, backoff, `Retry-After`
//! waits and pages together — by `SOURCE_BUDGET_TIMEOUTS` of them, so the
//! whole chain ends within that many timeouts per source. A paginated source
//! cut short (page cap, a failed later page, the budget) is returned with
//! [`SubdomainResult::truncated`] set.

#[cfg(feature = "cli")]
mod baseline;
mod classify;
mod http;
mod sources;

use std::collections::BTreeSet;
use std::time::Duration;

use serde::{Deserialize, Serialize};
use tokio::time::Instant;
use tracing::{debug, instrument, warn};

#[cfg(feature = "cli")]
pub use baseline::{SubdomainBaseline, SubdomainBaselineDiff, SubdomainBaselines};
pub use classify::{
    classify_subdomains, ClassifiedSubdomain, SubdomainClassification, SubdomainStatus,
};

use crate::error::{Result, SeerError};
use sources::{PaginationSpec, Source};

/// Result of subdomain enumeration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubdomainResult {
    pub domain: String,
    pub subdomains: Vec<String>,
    /// Which CT source actually answered (crt.sh or the fallback).
    pub source: String,
    pub count: usize,
    /// True when the source stopped before its last page (page cap, a failed
    /// later page, or the time budget): names may be missing, so a baseline
    /// recorded from it is incomplete.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub truncated: bool,
}

/// The default per-request CT-log timeout (the `timeouts.ct_secs` default).
const DEFAULT_CT_TIMEOUT: Duration = Duration::from_secs(30);

/// Each source may spend at most this many per-request timeouts in total
/// (attempts, backoff, `Retry-After` and pages), so enumeration is bounded by
/// `SOURCE_BUDGET_TIMEOUTS × timeout × sources` — 3 minutes by default. It
/// used to have no overall bound: 4 attempts × 30s plus 30s `Retry-After`
/// waits, over crt.sh and each of up to 10 certspotter pages.
const SOURCE_BUDGET_TIMEOUTS: u32 = 3;

/// Enumerates subdomains using Certificate Transparency logs.
#[derive(Debug, Clone)]
pub struct SubdomainEnumerator {
    /// Per-request timeout; a source's budget is a multiple of it.
    timeout: Duration,
}

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

impl SubdomainEnumerator {
    /// An enumerator with the default 30s per-request timeout.
    pub fn new() -> Self {
        Self {
            timeout: DEFAULT_CT_TIMEOUT,
        }
    }

    /// Builds an enumerator honoring `~/.seer/config.toml`: the per-request
    /// timeout is `timeouts.ct_secs` (clamped to 1–120s).
    pub fn from_config(config: &crate::config::SeerConfig) -> Self {
        Self::new().with_timeout(config.ct_timeout())
    }

    /// Sets the per-request timeout (each source's budget scales with it).
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Discover subdomains for a domain using Certificate Transparency logs.
    ///
    /// Queries CT-log aggregators (crt.sh first, certspotter as a fallback) to
    /// find certificates issued for subdomains of the given domain. Returns a
    /// deduplicated, sorted list of discovered subdomains.
    ///
    /// # Arguments
    /// * `domain` - The domain name to enumerate subdomains for (e.g., "example.com")
    ///
    /// # Returns
    /// * `Ok(SubdomainResult)` - List of discovered subdomains
    /// * `Err(SeerError)` - If every CT source failed
    #[instrument(skip(self), fields(domain = %domain))]
    pub async fn enumerate(&self, domain: &str) -> Result<SubdomainResult> {
        let domain = crate::validation::normalize_domain(domain)?;
        debug!(domain = %domain, "Enumerating subdomains via CT logs");
        enumerate_with_sources(&domain, &sources::default_sources(), self.timeout).await
    }
}

/// Try each source in order, returning the first that yields a parseable
/// response. Records the last error so a total failure surfaces a real cause.
/// Each source runs under its own `SOURCE_BUDGET_TIMEOUTS` budget, so a
/// primary that eats its whole budget still leaves the fallback its share.
async fn enumerate_with_sources(
    domain: &str,
    srcs: &[Source],
    timeout: Duration,
) -> Result<SubdomainResult> {
    let mut last_err: Option<SeerError> = None;

    for src in srcs {
        let budget = http::Budget {
            timeout,
            deadline: Instant::now() + timeout * SOURCE_BUDGET_TIMEOUTS,
        };
        let fetched = match &src.paginate {
            Some(spec) => fetch_paginated(domain, &src.base, spec, budget).await,
            None => {
                let url = (src.build_url)(&src.base, domain);
                match http::fetch_with_retry(&url, budget).await {
                    Ok(body) => (src.parse)(&body).map(|names| (names, false)),
                    Err(e) => Err(e),
                }
            }
        };
        match fetched {
            Ok((names, truncated)) => {
                let mut result = build_result(domain, names, src.name);
                result.truncated = truncated;
                return Ok(result);
            }
            Err(e) => {
                warn!(source = src.name, error = %e, "CT source unavailable or unparseable, trying next");
                last_err = Some(e);
            }
        }
    }

    Err(last_err.unwrap_or_else(|| {
        SeerError::HttpError(
            "All Certificate Transparency sources are currently unavailable; try again shortly"
                .into(),
        )
    }))
}

/// Fetches a cursor-paginated source page by page, accumulating DNS names until
/// the cursor is exhausted or `max_pages` is reached, every page within the
/// source's `budget`. A failure on the FIRST page propagates (the source is
/// down — let the chain fall through); a failure on a LATER page returns what
/// was gathered so far rather than discarding the earlier pages. Returns the
/// names and whether pagination stopped before the last page.
async fn fetch_paginated(
    domain: &str,
    base: &str,
    spec: &PaginationSpec,
    budget: http::Budget,
) -> Result<(Vec<String>, bool)> {
    let mut names = Vec::new();
    let mut cursor: Option<String> = None;

    for page_num in 0..spec.max_pages {
        let url = (spec.url_after)(base, domain, cursor.as_deref());
        let page = match http::fetch_with_retry(&url, budget)
            .await
            .and_then(|body| (spec.parse_page)(&body))
        {
            Ok(page) => page,
            Err(e) if page_num == 0 => return Err(e),
            Err(e) => {
                warn!(error = %e, page = page_num, "pagination stopped early; returning partial results");
                return Ok((names, true));
            }
        };

        if page.names.is_empty() {
            return Ok((names, false));
        }
        names.extend(page.names);

        match page.next_cursor {
            Some(next) => cursor = Some(next),
            None => return Ok((names, false)),
        }
    }

    // The page cap was reached with a cursor still pointing at more.
    Ok((names, true))
}

/// Filter and normalize raw certificate names into the final subdomain list:
/// keep only names under `domain`, strip a wildcard's `*.` (a certificate for
/// `*.dev.example.com` reveals `dev.example.com`), drop the apex itself, and
/// reject anything that isn't a syntactically valid hostname.
fn build_result(domain: &str, raw_names: Vec<String>, source: &str) -> SubdomainResult {
    let suffix = format!(".{}", domain);
    let mut subdomains = BTreeSet::new();

    for name in raw_names {
        let name = name.trim().to_lowercase();
        let name = name.strip_prefix("*.").unwrap_or(&name);
        if (name.ends_with(&suffix) || name == domain) && !name.contains('*') {
            subdomains.insert(name.to_string());
        }
    }

    // The apex itself is not a subdomain.
    subdomains.remove(domain);

    let subdomains: Vec<String> = subdomains
        .into_iter()
        .filter(|s| {
            !s.is_empty()
                && s.len() <= 253
                // Underscores are valid in DNS names (RFC 8552 service labels,
                // e.g. `_acme-challenge`, `_dmarc`) and appear in CT-log SANs;
                // matches the charset normalize_domain accepts on input, so they
                // are not silently dropped here.
                && s.chars()
                    .all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '-' || c == '_')
                && !s.contains("..")
                && !s.starts_with('.')
                && !s.starts_with('-')
        })
        .collect();

    let count = subdomains.len();
    SubdomainResult {
        domain: domain.to_string(),
        subdomains,
        source: source.to_string(),
        count,
        truncated: false,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use wiremock::matchers::method;
    use wiremock::{Mock, MockServer, ResponseTemplate};

    /// Per-request timeout for the mock-server tests: long enough for the
    /// retry backoff (0.5 + 1 + 2 s) to fit in a source's budget.
    const TEST_TIMEOUT: Duration = Duration::from_secs(5);

    #[test]
    fn test_subdomain_result_serialization() {
        let result = SubdomainResult {
            domain: "example.com".to_string(),
            subdomains: vec![
                "api.example.com".to_string(),
                "mail.example.com".to_string(),
            ],
            source: "crt.sh (Certificate Transparency)".to_string(),
            count: 2,
            truncated: false,
        };
        let json = serde_json::to_string(&result).unwrap();
        assert!(json.contains("api.example.com"));
        assert!(json.contains("mail.example.com"));
        assert!(json.contains("crt.sh"));
    }

    #[test]
    fn test_subdomain_enumerator_default() {
        let _ = SubdomainEnumerator::default();
    }

    #[test]
    fn build_result_filters_and_dedups() {
        let raw = vec![
            "example.com".to_string(),       // apex — dropped
            "API.example.com".to_string(),   // lowercased
            "api.example.com".to_string(),   // dup
            "*.example.com".to_string(),     // wildcard of the apex — dropped
            "*.dev.example.com".to_string(), // wildcard — reveals dev
            "*.evilexample.com".to_string(), // off-domain wildcard — dropped
            "a.*.example.com".to_string(),   // interior wildcard — dropped
            "evil.com".to_string(),          // off-domain — dropped
            "ok.example.com".to_string(),
        ];
        let r = build_result("example.com", raw, "test");
        assert_eq!(
            r.subdomains,
            vec!["api.example.com", "dev.example.com", "ok.example.com"]
        );
        assert_eq!(r.count, 3);
        assert_eq!(r.source, "test");
    }

    #[test]
    fn build_result_keeps_underscore_service_labels() {
        // RFC 8552 service labels (routinely present in CT-log SANs) must not be
        // silently dropped by the hostname-charset filter.
        let raw = vec![
            "_acme-challenge.example.com".to_string(),
            "_dmarc.example.com".to_string(),
            "www.example.com".to_string(),
        ];
        let r = build_result("example.com", raw, "test");
        assert!(r
            .subdomains
            .contains(&"_acme-challenge.example.com".to_string()));
        assert!(r.subdomains.contains(&"_dmarc.example.com".to_string()));
        assert!(r.subdomains.contains(&"www.example.com".to_string()));
        assert_eq!(r.count, 3);
    }

    /// The headline regression: when the primary source rate-limits (429, the
    /// crt.sh failure mode behind the reported `zac.app` 404), enumeration must
    /// fall through to the fallback source instead of erroring out.
    #[tokio::test]
    async fn falls_back_to_second_source_when_primary_rate_limits() {
        let primary = MockServer::start().await;
        Mock::given(method("GET"))
            // Retry-After: 0 keeps the retry budget from sleeping in the test.
            .respond_with(
                ResponseTemplate::new(429).insert_header("retry-after", "0"),
            )
            .mount(&primary)
            .await;

        let fallback = MockServer::start().await;
        let body = r#"[{"dns_names":["api.example.com","example.com"]}]"#;
        Mock::given(method("GET"))
            .respond_with(ResponseTemplate::new(200).set_body_string(body))
            .mount(&fallback)
            .await;

        let srcs = vec![
            Source {
                name: "primary",
                base: primary.uri(),
                build_url: |b, d| format!("{}/?q={}", b, d),
                parse: sources::parse_crtsh,
                paginate: None,
            },
            Source {
                name: "fallback",
                base: fallback.uri(),
                build_url: |b, d| format!("{}/?domain={}", b, d),
                parse: sources::parse_certspotter,
                paginate: None,
            },
        ];

        let result = enumerate_with_sources("example.com", &srcs, TEST_TIMEOUT)
            .await
            .unwrap();
        assert_eq!(result.source, "fallback");
        assert_eq!(result.subdomains, vec!["api.example.com"]);
    }

    /// When every source is down, the error should be the friendly exhaustion
    /// message, not a silent empty success.
    #[tokio::test]
    async fn errors_when_all_sources_unavailable() {
        let down = MockServer::start().await;
        Mock::given(method("GET"))
            .respond_with(ResponseTemplate::new(503))
            .mount(&down)
            .await;

        let srcs = vec![Source {
            name: "only",
            base: down.uri(),
            build_url: |b, d| format!("{}/?q={}", b, d),
            parse: sources::parse_crtsh,
            paginate: None,
        }];

        let err = enumerate_with_sources("example.com", &srcs, TEST_TIMEOUT).await;
        assert!(err.is_err());
    }

    /// A paginated source must follow the `after=` cursor and accumulate names
    /// across pages instead of silently truncating to the first page.
    #[tokio::test]
    async fn paginated_source_follows_cursor_across_pages() {
        use wiremock::matchers::{query_param, query_param_is_missing};

        let server = MockServer::start().await;

        // Page 1 (no cursor): id 100 -> next cursor "100".
        Mock::given(method("GET"))
            .and(query_param_is_missing("after"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_string(r#"[{"id":"100","dns_names":["a.example.com"]}]"#),
            )
            .mount(&server)
            .await;

        // Page 2 (after=100): id 200 -> next cursor "200".
        Mock::given(method("GET"))
            .and(query_param("after", "100"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_string(r#"[{"id":"200","dns_names":["b.example.com"]}]"#),
            )
            .mount(&server)
            .await;

        // Page 3 (after=200): empty -> pagination ends.
        Mock::given(method("GET"))
            .and(query_param("after", "200"))
            .respond_with(ResponseTemplate::new(200).set_body_string("[]"))
            .mount(&server)
            .await;

        let srcs = vec![Source {
            name: "paged",
            base: server.uri(),
            build_url: |b, d| format!("{}/?domain={}", b, d),
            parse: sources::parse_certspotter,
            paginate: Some(PaginationSpec {
                url_after: |base, domain, after| {
                    let mut url = format!("{}/v1/issuances?domain={}", base, domain);
                    if let Some(c) = after {
                        url.push_str("&after=");
                        url.push_str(c);
                    }
                    url
                },
                parse_page: sources::parse_certspotter_page,
                max_pages: 10,
            }),
        }];

        let result = enumerate_with_sources("example.com", &srcs, TEST_TIMEOUT)
            .await
            .unwrap();
        assert_eq!(
            result.subdomains,
            vec!["a.example.com", "b.example.com"],
            "names from both pages must be accumulated"
        );
        assert!(!result.truncated, "pagination ran to the end");
    }

    fn paged_source(base: String, max_pages: usize) -> Source {
        Source {
            name: "paged",
            base,
            build_url: |b, d| format!("{}/?domain={}", b, d),
            parse: sources::parse_certspotter,
            paginate: Some(PaginationSpec {
                url_after: |base, domain, after| match after {
                    Some(c) => format!("{base}/v1/issuances?domain={domain}&after={c}"),
                    None => format!("{base}/v1/issuances?domain={domain}"),
                },
                parse_page: sources::parse_certspotter_page,
                max_pages,
            }),
        }
    }

    /// A source cut short by its page cap (cursor still pointing at more) or
    /// by a failed later page is marked truncated, so a baseline recorded
    /// from it is known to be incomplete.
    #[tokio::test]
    async fn paginated_source_cut_short_is_truncated() {
        use wiremock::matchers::{query_param, query_param_is_missing};

        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(query_param_is_missing("after"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_string(r#"[{"id":"100","dns_names":["a.example.com"]}]"#),
            )
            .mount(&server)
            .await;
        Mock::given(method("GET"))
            .and(query_param("after", "100"))
            .respond_with(ResponseTemplate::new(400))
            .mount(&server)
            .await;

        // Page cap of 1 with a next cursor.
        let srcs = vec![paged_source(server.uri(), 1)];
        let result = enumerate_with_sources("example.com", &srcs, TEST_TIMEOUT)
            .await
            .unwrap();
        assert_eq!(result.subdomains, vec!["a.example.com"]);
        assert!(result.truncated);

        // Page 2 fails (terminal 400): partial names, truncated.
        let srcs = vec![paged_source(server.uri(), 10)];
        let result = enumerate_with_sources("example.com", &srcs, TEST_TIMEOUT)
            .await
            .unwrap();
        assert_eq!(result.subdomains, vec!["a.example.com"]);
        assert!(result.truncated);
    }

    /// Regression: enumeration had no overall bound (4 attempts × 30s plus
    /// 30s Retry-After waits per request). A source that never answers now
    /// gives up after its budget and the chain moves on.
    #[tokio::test]
    async fn a_hanging_source_is_bounded_by_its_budget() {
        let hanging = MockServer::start().await;
        Mock::given(method("GET"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_string("[]")
                    .set_delay(Duration::from_secs(30)),
            )
            .mount(&hanging)
            .await;
        let fallback = MockServer::start().await;
        Mock::given(method("GET"))
            .respond_with(
                ResponseTemplate::new(200).set_body_string(r#"[{"dns_names":["b.example.com"]}]"#),
            )
            .mount(&fallback)
            .await;

        let srcs = vec![
            Source {
                name: "hanging",
                base: hanging.uri(),
                build_url: |b, d| format!("{}/?q={}", b, d),
                parse: sources::parse_crtsh,
                paginate: None,
            },
            Source {
                name: "fallback",
                base: fallback.uri(),
                build_url: |b, d| format!("{}/?domain={}", b, d),
                parse: sources::parse_certspotter,
                paginate: None,
            },
        ];
        let timeout = Duration::from_millis(200);
        let started = std::time::Instant::now();
        let result = enumerate_with_sources("example.com", &srcs, timeout)
            .await
            .unwrap();
        let elapsed = started.elapsed();
        assert_eq!(result.source, "fallback");
        // The hanging source's budget is 3 timeouts (600ms) plus backoff that
        // fits in it; far below a single 30s response.
        assert!(
            elapsed < Duration::from_secs(5),
            "enumeration took {elapsed:?}"
        );
    }

    #[test]
    fn from_config_takes_the_ct_timeout() {
        let mut config = crate::config::SeerConfig::default();
        config.timeouts.ct_secs = 45;
        assert_eq!(
            SubdomainEnumerator::from_config(&config).timeout,
            Duration::from_secs(45)
        );
        assert_eq!(SubdomainEnumerator::new().timeout, DEFAULT_CT_TIMEOUT);
    }
}