modo-rs 0.8.0

Rust web framework for small monolithic apps
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
//! Domain ownership verification via DNS TXT and CNAME record lookups.

use std::sync::Arc;
use std::time::Duration;

use crate::error::{Error, Result};

use super::config::DnsConfig;
use super::error::DnsError;
use super::resolver::{DnsResolver, UdpDnsResolver};

/// Result of a domain verification check.
///
/// Returned by [`DomainVerifier::verify_domain`]. Both checks run
/// concurrently; a field is `true` only when the corresponding record was
/// found and matched exactly.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DomainStatus {
    /// Whether the TXT record at `{txt_prefix}.{domain}` matched the
    /// expected token.
    pub txt_verified: bool,
    /// Whether the CNAME record at `domain` pointed to the expected target.
    pub cname_verified: bool,
}

pub(crate) struct Inner {
    pub(crate) resolver: Arc<dyn DnsResolver>,
    pub(crate) txt_prefix: String,
}

/// DNS-based domain ownership verification service.
///
/// Checks TXT record ownership and CNAME routing via raw UDP DNS queries.
/// Construct with [`DomainVerifier::from_config`]. The struct is cheap to
/// clone because it wraps an `Arc` internally.
///
/// # Example
///
/// ```rust,no_run
/// # {
/// use modo::dns::{DnsConfig, DomainVerifier, generate_verification_token};
///
/// let config = DnsConfig::new("8.8.8.8:53");
/// let verifier = DomainVerifier::from_config(&config).unwrap();
/// let token = generate_verification_token();
///
/// // Ask the user to create: _modo-verify.example.com TXT "<token>"
/// // Then verify:
/// // let ok = verifier.check_txt("example.com", &token).await?;
/// # }
/// ```
pub struct DomainVerifier {
    inner: Arc<Inner>,
}

impl Clone for DomainVerifier {
    fn clone(&self) -> Self {
        Self {
            inner: Arc::clone(&self.inner),
        }
    }
}

impl DomainVerifier {
    /// Create a new verifier from [`DnsConfig`].
    ///
    /// Parses the nameserver address and builds a UDP resolver.
    ///
    /// # Errors
    ///
    /// Returns an error if the nameserver string is not a valid IP address.
    pub fn from_config(config: &DnsConfig) -> Result<Self> {
        let nameserver = config.parse_nameserver()?;
        let timeout = Duration::from_millis(config.timeout_ms);
        let resolver = UdpDnsResolver::new(nameserver, timeout);

        Ok(Self {
            inner: Arc::new(Inner {
                resolver: Arc::new(resolver),
                txt_prefix: config.txt_prefix.clone(),
            }),
        })
    }

    /// Create a verifier with a custom resolver and TXT record prefix.
    ///
    /// Used by other in-crate modules to build a `DomainVerifier` backed by a
    /// mock resolver for testing. Only called from `#[cfg(test)]` blocks in
    /// other modules, so it has zero callers on the lib target — hence
    /// `allow(dead_code)`. Cannot use `#[cfg(test)]` here because that would
    /// make it invisible to other modules' test blocks.
    #[allow(dead_code)]
    pub(crate) fn with_resolver(
        resolver: impl DnsResolver + 'static,
        txt_prefix: impl Into<String>,
    ) -> Self {
        Self {
            inner: Arc::new(Inner {
                resolver: Arc::new(resolver),
                txt_prefix: txt_prefix.into(),
            }),
        }
    }

    /// Check whether a TXT record matches the expected verification token.
    ///
    /// Looks up `{txt_prefix}.{domain}` and returns `true` if any TXT record
    /// value equals `expected_token` exactly (case-sensitive). Returns `false`
    /// when the record exists but no value matches, or when no TXT records
    /// exist (NXDOMAIN is treated as an empty record set, not an error).
    ///
    /// # Errors
    ///
    /// Returns [`crate::Error`] with status 400 when `domain` or
    /// `expected_token` is empty, or a gateway error on network/DNS failure.
    pub async fn check_txt(&self, domain: &str, expected_token: &str) -> Result<bool> {
        if domain.is_empty() {
            return Err(Error::bad_request("domain must not be empty")
                .chain(DnsError::InvalidInput)
                .with_code(DnsError::InvalidInput.code()));
        }
        if expected_token.is_empty() {
            return Err(Error::bad_request("token must not be empty")
                .chain(DnsError::InvalidInput)
                .with_code(DnsError::InvalidInput.code()));
        }

        let lookup_domain = format!("{}.{}", self.inner.txt_prefix, domain);
        let records = self.inner.resolver.resolve_txt(&lookup_domain).await?;

        Ok(records.iter().any(|r| r == expected_token))
    }

    /// Check whether a CNAME record points to the expected target.
    ///
    /// Normalizes both the resolved target and `expected_target` before
    /// comparing: both are lowercased and any trailing dot is stripped.
    /// Returns `false` when no CNAME record is present.
    ///
    /// # Errors
    ///
    /// Returns [`crate::Error`] with status 400 when `domain` or
    /// `expected_target` is empty, or a gateway error on network/DNS failure.
    pub async fn check_cname(&self, domain: &str, expected_target: &str) -> Result<bool> {
        if domain.is_empty() {
            return Err(Error::bad_request("domain must not be empty")
                .chain(DnsError::InvalidInput)
                .with_code(DnsError::InvalidInput.code()));
        }
        if expected_target.is_empty() {
            return Err(Error::bad_request("target must not be empty")
                .chain(DnsError::InvalidInput)
                .with_code(DnsError::InvalidInput.code()));
        }

        let target = self.inner.resolver.resolve_cname(domain).await?;

        match target {
            Some(resolved) => {
                let normalized_resolved = normalize_domain(&resolved);
                let normalized_expected = normalize_domain(expected_target);
                Ok(normalized_resolved == normalized_expected)
            }
            None => Ok(false),
        }
    }

    /// Check both TXT ownership and CNAME routing concurrently.
    ///
    /// Runs [`check_txt`](Self::check_txt) and
    /// [`check_cname`](Self::check_cname) in parallel via `tokio::join!`.
    /// Returns [`DomainStatus`] with individual results.
    ///
    /// # Errors
    ///
    /// If either check returns a hard error (e.g. network failure) the error
    /// is propagated and the other result is discarded.
    pub async fn verify_domain(
        &self,
        domain: &str,
        expected_token: &str,
        expected_cname: &str,
    ) -> Result<DomainStatus> {
        let (txt_result, cname_result) = tokio::join!(
            self.check_txt(domain, expected_token),
            self.check_cname(domain, expected_cname),
        );

        Ok(DomainStatus {
            txt_verified: txt_result?,
            cname_verified: cname_result?,
        })
    }
}

/// Normalize a domain name: lowercase, strip trailing dot.
fn normalize_domain(domain: &str) -> String {
    domain.to_lowercase().trim_end_matches('.').to_owned()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use std::future::Future;
    use std::pin::Pin;

    struct MockResolver {
        txt_records: HashMap<String, Vec<String>>,
        cname_records: HashMap<String, String>,
    }

    impl MockResolver {
        fn new() -> Self {
            Self {
                txt_records: HashMap::new(),
                cname_records: HashMap::new(),
            }
        }

        fn with_txt(mut self, domain: &str, records: Vec<&str>) -> Self {
            self.txt_records.insert(
                domain.to_owned(),
                records.into_iter().map(|s| s.to_owned()).collect(),
            );
            self
        }

        fn with_cname(mut self, domain: &str, target: &str) -> Self {
            self.cname_records
                .insert(domain.to_owned(), target.to_owned());
            self
        }
    }

    impl DnsResolver for MockResolver {
        fn resolve_txt(
            &self,
            domain: &str,
        ) -> Pin<Box<dyn Future<Output = Result<Vec<String>>> + Send + '_>> {
            let records = self.txt_records.get(domain).cloned().unwrap_or_default();
            Box::pin(async move { Ok(records) })
        }

        fn resolve_cname(
            &self,
            domain: &str,
        ) -> Pin<Box<dyn Future<Output = Result<Option<String>>> + Send + '_>> {
            let target = self.cname_records.get(domain).cloned();
            Box::pin(async move { Ok(target) })
        }
    }

    fn verifier_with_mock(resolver: MockResolver) -> DomainVerifier {
        DomainVerifier {
            inner: Arc::new(Inner {
                resolver: Arc::new(resolver),
                txt_prefix: "_modo-verify".into(),
            }),
        }
    }

    // -- check_txt tests --

    #[tokio::test]
    async fn check_txt_matching_token_returns_true() {
        let mock = MockResolver::new().with_txt("_modo-verify.example.com", vec!["abc123"]);
        let v = verifier_with_mock(mock);
        assert!(v.check_txt("example.com", "abc123").await.unwrap());
    }

    #[tokio::test]
    async fn check_txt_no_match_returns_false() {
        let mock = MockResolver::new().with_txt("_modo-verify.example.com", vec!["wrong"]);
        let v = verifier_with_mock(mock);
        assert!(!v.check_txt("example.com", "abc123").await.unwrap());
    }

    #[tokio::test]
    async fn check_txt_multiple_records_one_matches() {
        let mock = MockResolver::new().with_txt(
            "_modo-verify.example.com",
            vec!["spf-record", "abc123", "other"],
        );
        let v = verifier_with_mock(mock);
        assert!(v.check_txt("example.com", "abc123").await.unwrap());
    }

    #[tokio::test]
    async fn check_txt_no_records_returns_false() {
        let mock = MockResolver::new();
        let v = verifier_with_mock(mock);
        assert!(!v.check_txt("example.com", "abc123").await.unwrap());
    }

    #[tokio::test]
    async fn check_txt_prefix_is_prepended() {
        let mock = MockResolver::new().with_txt("_modo-verify.test.io", vec!["token1"]);
        let v = verifier_with_mock(mock);
        assert!(v.check_txt("test.io", "token1").await.unwrap());
    }

    #[tokio::test]
    async fn check_txt_case_sensitive() {
        let mock = MockResolver::new().with_txt("_modo-verify.example.com", vec!["ABC123"]);
        let v = verifier_with_mock(mock);
        assert!(!v.check_txt("example.com", "abc123").await.unwrap());
    }

    #[tokio::test]
    async fn check_txt_empty_domain_returns_bad_request() {
        let mock = MockResolver::new();
        let v = verifier_with_mock(mock);
        let err = v.check_txt("", "abc123").await.unwrap_err();
        assert_eq!(err.status(), http::StatusCode::BAD_REQUEST);
    }

    #[tokio::test]
    async fn check_txt_empty_token_returns_bad_request() {
        let mock = MockResolver::new();
        let v = verifier_with_mock(mock);
        let err = v.check_txt("example.com", "").await.unwrap_err();
        assert_eq!(err.status(), http::StatusCode::BAD_REQUEST);
    }

    // -- check_cname tests --

    #[tokio::test]
    async fn check_cname_matching_target_returns_true() {
        let mock = MockResolver::new().with_cname("custom.example.com", "app.myservice.com");
        let v = verifier_with_mock(mock);
        assert!(
            v.check_cname("custom.example.com", "app.myservice.com")
                .await
                .unwrap()
        );
    }

    #[tokio::test]
    async fn check_cname_trailing_dot_normalized() {
        let mock = MockResolver::new().with_cname("custom.example.com", "app.myservice.com.");
        let v = verifier_with_mock(mock);
        assert!(
            v.check_cname("custom.example.com", "app.myservice.com")
                .await
                .unwrap()
        );
    }

    #[tokio::test]
    async fn check_cname_case_insensitive() {
        let mock = MockResolver::new().with_cname("custom.example.com", "App.MyService.COM");
        let v = verifier_with_mock(mock);
        assert!(
            v.check_cname("custom.example.com", "app.myservice.com")
                .await
                .unwrap()
        );
    }

    #[tokio::test]
    async fn check_cname_no_record_returns_false() {
        let mock = MockResolver::new();
        let v = verifier_with_mock(mock);
        assert!(
            !v.check_cname("custom.example.com", "app.myservice.com")
                .await
                .unwrap()
        );
    }

    #[tokio::test]
    async fn check_cname_no_match_returns_false() {
        let mock = MockResolver::new().with_cname("custom.example.com", "other.service.com");
        let v = verifier_with_mock(mock);
        assert!(
            !v.check_cname("custom.example.com", "app.myservice.com")
                .await
                .unwrap()
        );
    }

    #[tokio::test]
    async fn check_cname_empty_domain_returns_bad_request() {
        let mock = MockResolver::new();
        let v = verifier_with_mock(mock);
        let err = v.check_cname("", "app.myservice.com").await.unwrap_err();
        assert_eq!(err.status(), http::StatusCode::BAD_REQUEST);
    }

    #[tokio::test]
    async fn check_cname_empty_target_returns_bad_request() {
        let mock = MockResolver::new();
        let v = verifier_with_mock(mock);
        let err = v.check_cname("example.com", "").await.unwrap_err();
        assert_eq!(err.status(), http::StatusCode::BAD_REQUEST);
    }

    // -- verify_domain tests --

    #[tokio::test]
    async fn verify_domain_both_pass() {
        let mock = MockResolver::new()
            .with_txt("_modo-verify.example.com", vec!["token1"])
            .with_cname("example.com", "app.myservice.com");
        let v = verifier_with_mock(mock);
        let status = v
            .verify_domain("example.com", "token1", "app.myservice.com")
            .await
            .unwrap();
        assert!(status.txt_verified);
        assert!(status.cname_verified);
    }

    #[tokio::test]
    async fn verify_domain_txt_pass_cname_fail() {
        let mock = MockResolver::new().with_txt("_modo-verify.example.com", vec!["token1"]);
        let v = verifier_with_mock(mock);
        let status = v
            .verify_domain("example.com", "token1", "app.myservice.com")
            .await
            .unwrap();
        assert!(status.txt_verified);
        assert!(!status.cname_verified);
    }

    #[tokio::test]
    async fn verify_domain_both_fail() {
        let mock = MockResolver::new();
        let v = verifier_with_mock(mock);
        let status = v
            .verify_domain("example.com", "token1", "app.myservice.com")
            .await
            .unwrap();
        assert!(!status.txt_verified);
        assert!(!status.cname_verified);
    }

    #[tokio::test]
    async fn verify_domain_dns_error_propagates() {
        struct FailingResolver;
        impl DnsResolver for FailingResolver {
            fn resolve_txt(
                &self,
                _domain: &str,
            ) -> Pin<Box<dyn Future<Output = Result<Vec<String>>> + Send + '_>> {
                Box::pin(async {
                    Err(Error::bad_gateway("dns server failure")
                        .chain(DnsError::ServerFailure)
                        .with_code(DnsError::ServerFailure.code()))
                })
            }
            fn resolve_cname(
                &self,
                _domain: &str,
            ) -> Pin<Box<dyn Future<Output = Result<Option<String>>> + Send + '_>> {
                Box::pin(async { Ok(None) })
            }
        }

        let v = DomainVerifier {
            inner: Arc::new(Inner {
                resolver: Arc::new(FailingResolver),
                txt_prefix: "_modo-verify".into(),
            }),
        };
        let err = v
            .verify_domain("example.com", "token1", "app.myservice.com")
            .await
            .unwrap_err();
        assert_eq!(err.status(), http::StatusCode::BAD_GATEWAY);
    }

    // -- from_config tests --

    #[test]
    fn from_config_valid() {
        let config = DnsConfig {
            nameserver: "8.8.8.8:53".into(),
            txt_prefix: "_myapp-verify".into(),
            timeout_ms: 3000,
        };
        let v = DomainVerifier::from_config(&config).unwrap();
        assert_eq!(v.inner.txt_prefix, "_myapp-verify");
    }

    #[test]
    fn from_config_invalid_nameserver_fails() {
        let config = DnsConfig {
            nameserver: "not-valid".into(),
            txt_prefix: "_modo-verify".into(),
            timeout_ms: 5000,
        };
        let err = DomainVerifier::from_config(&config).err().unwrap();
        assert_eq!(err.status(), http::StatusCode::INTERNAL_SERVER_ERROR);
    }
}