sagittarius 0.2.0

A fast, self-hosted DNS sinkhole in a single Rust binary
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
//! Global settings management (SPEC §9, §7, §4).
//!
//! Edits the typed `settings` row (cache bounds, blocking mode + custom sinkhole
//! IPs, blocklist refresh interval, UI theme), writes through to E3.4, and then
//! updates the live [`RuntimeSettings`] snapshot so changes apply where the
//! subsystem supports it:
//!
//! - **min/max TTL** apply immediately: the new bounds are pushed into the
//!   live cache (`DnsCache::set_ttl_bounds`) and clamp the next insert.
//! - **negative-TTL cap, blocking mode/custom IP** apply immediately (read from
//!   the snapshot at synthesis / cache-store time).
//! - **blocklist refresh interval** is re-read by the scheduler at the next
//!   cycle boundary (E7.4).
//! - **cache capacity** is fixed when the moka cache is built, so a change is
//!   persisted but only takes effect after a restart — surfaced in the UI.
//!
//! The session-cookie `Secure` policy is intentionally **not** here: it is an
//! operational CLI/env setting (E1.2) because it depends on deployment topology.

use std::net::{Ipv4Addr, Ipv6Addr};

use askama::Template;
use askama_web::WebTemplate;
use axum::{
    extract::State,
    http::{HeaderMap, StatusCode, Uri, header},
    response::{IntoResponse, Redirect, Response},
};
use serde::Deserialize;

use crate::{
    resolver::state::RuntimeSettings,
    storage::{
        query_log::QueryLogRepository,
        settings::{BlockingMode, SelectionStrategy, Settings, SettingsRepository},
    },
    web::{
        AppState, Chrome,
        auth::CurrentUser,
        render::{WebError, WebResult},
    },
};

impl AppState {
    async fn render_settings(
        &self,
        user: &CurrentUser,
        error: Option<String>,
        notice: Option<String>,
    ) -> WebResult<SettingsPageTemplate> {
        let s = self.db.settings().get().await?;
        Ok(SettingsPageTemplate {
            chrome: self.chrome("settings", user).await,
            cache_min_ttl: s.cache_min_ttl,
            cache_max_ttl: s.cache_max_ttl,
            cache_negative_ttl_cap: s.cache_negative_ttl_cap,
            cache_capacity: s.cache_capacity,
            blocking_mode: s.blocking_mode.as_str(),
            custom_block_ipv4: s
                .custom_block_ipv4
                .map(|i| i.to_string())
                .unwrap_or_default(),
            custom_block_ipv6: s
                .custom_block_ipv6
                .map(|i| i.to_string())
                .unwrap_or_default(),
            blocklist_refresh_interval: s.blocklist_refresh_interval,
            query_log_enabled: s.query_log_enabled,
            query_log_retention_days: s.query_log_retention_days,
            upstream_selection_strategy: s.upstream_selection_strategy.as_str(),
            upstream_parallel_fanout: s.upstream_parallel_fanout,
            error,
            notice,
        })
    }

    /// `GET /settings`.
    pub async fn settings_page(
        user: CurrentUser,
        State(state): State<AppState>,
    ) -> WebResult<Response> {
        Ok(state
            .render_settings(&user, None, None)
            .await?
            .into_response())
    }

    /// `POST /settings`.
    pub async fn settings_save(
        user: CurrentUser,
        State(state): State<AppState>,
        axum::Form(form): axum::Form<SettingsForm>,
    ) -> WebResult<Response> {
        match state.apply_settings(form).await {
            Ok(()) => Ok(state
                .render_settings(&user, None, Some("Settings saved.".to_owned()))
                .await?
                .into_response()),
            // A validation error re-renders the form with the message and a 400.
            Err(WebError::BadRequest(msg)) => {
                let page = state.render_settings(&user, Some(msg), None).await?;
                Ok((StatusCode::BAD_REQUEST, page).into_response())
            }
            Err(e) => Err(e),
        }
    }

    /// `POST /theme/toggle` — flip the UI theme from the topbar sun/moon button.
    ///
    /// Theme is a persisted UI-only setting (read fresh into the page chrome on
    /// every render). This flips it — anything not already `dark` becomes
    /// `dark`, so the initial `auto` resolves to an explicit choice on first
    /// click — and redirects (PRG) back to the page the toggle was clicked on.
    pub async fn theme_toggle(
        _user: CurrentUser,
        State(state): State<AppState>,
        headers: HeaderMap,
    ) -> WebResult<Response> {
        let mut settings = state.db.settings().get().await?;
        settings.ui_theme = if settings.ui_theme == "dark" {
            "light".to_owned()
        } else {
            "dark".to_owned()
        };
        state.db.settings().update(&settings).await?;

        // Return to the originating page; take only the same-origin path from
        // the Referer (never the full URL) so this can't become an open redirect.
        let back = headers
            .get(header::REFERER)
            .and_then(|v| v.to_str().ok())
            .and_then(|s| s.parse::<Uri>().ok())
            .and_then(|u| u.path_and_query().map(|pq| pq.as_str().to_owned()))
            .unwrap_or_else(|| "/".to_owned());
        Ok(Redirect::to(&back).into_response())
    }

    /// `POST /settings/clear-log` — delete all persisted query-log history.
    pub async fn settings_clear_log(
        user: CurrentUser,
        State(state): State<AppState>,
    ) -> WebResult<Response> {
        state.db.query_log().clear_all().await?;
        Ok(state
            .render_settings(&user, None, Some("Query log cleared.".to_owned()))
            .await?
            .into_response())
    }

    /// Validate and persist a settings form, then refresh the live snapshot.
    async fn apply_settings(&self, form: SettingsForm) -> WebResult<()> {
        // The theme is owned by the topbar toggle, not this form; preserve the
        // stored value across an otherwise-unrelated settings save.
        let ui_theme = self.db.settings().get().await?.ui_theme;
        let settings = form.into_settings(ui_theme)?;
        self.db.settings().update(&settings).await?;
        // Apply to the live snapshot (capacity needs a restart; see module doc).
        self.resolver
            .store_settings(RuntimeSettings::from(&settings));
        // Push the min/max-TTL clamp bounds into the live cache so they take
        // effect on the next insert without a restart (capacity, fixed at moka
        // build time, still needs one).
        self.resolver
            .cache()
            .set_ttl_bounds(settings.cache_min_ttl, settings.cache_max_ttl);
        // The selection strategy / fan-out is read when (re)building the pool,
        // so rebuild it now to apply the change immediately (E15.5).
        self.rebuild_upstream_pool().await?;
        Ok(())
    }
}

/// Settings form payload.
#[derive(Debug, Deserialize)]
pub struct SettingsForm {
    cache_min_ttl: u32,
    cache_max_ttl: u32,
    cache_negative_ttl_cap: u32,
    cache_capacity: u64,
    blocking_mode: String,
    #[serde(default)]
    custom_block_ipv4: String,
    #[serde(default)]
    custom_block_ipv6: String,
    blocklist_refresh_interval: u32,
    /// HTML checkbox: present (any value) when ticked, absent when unticked.
    #[serde(default)]
    query_log_enabled: Option<String>,
    query_log_retention_days: u32,
    upstream_selection_strategy: String,
    upstream_parallel_fanout: u32,
}

impl SettingsForm {
    /// Validate the raw form into a typed [`Settings`].
    ///
    /// The UI theme is no longer part of this form — it is toggled from the
    /// topbar (see [`AppState::theme_toggle`]) — so the caller passes the
    /// currently-stored `ui_theme` through to preserve it across a save.
    fn into_settings(self, ui_theme: String) -> WebResult<Settings> {
        if self.cache_min_ttl > self.cache_max_ttl {
            return Err(WebError::bad_request(
                "Minimum cache TTL must not exceed the maximum.",
            ));
        }
        if self.cache_capacity == 0 {
            return Err(WebError::bad_request("Cache capacity must be at least 1."));
        }
        let blocking_mode: BlockingMode = self
            .blocking_mode
            .parse()
            .map_err(|_| WebError::bad_request("Invalid blocking mode."))?;

        // Custom IPs only matter in Custom mode, where the inputs are visible
        // and validated strictly. In other modes the inputs are hidden, so we
        // keep whatever still parses (preserving previously-saved IPs) and
        // silently drop anything else — a stale value can never block saving.
        let (custom_block_ipv4, custom_block_ipv6) = if blocking_mode == BlockingMode::Custom {
            (
                parse_opt_ip::<Ipv4Addr>(&self.custom_block_ipv4, "IPv4")?,
                parse_opt_ip::<Ipv6Addr>(&self.custom_block_ipv6, "IPv6")?,
            )
        } else {
            (
                self.custom_block_ipv4.trim().parse::<Ipv4Addr>().ok(),
                self.custom_block_ipv6.trim().parse::<Ipv6Addr>().ok(),
            )
        };

        if self.query_log_retention_days == 0 {
            return Err(WebError::bad_request(
                "Query-log retention must be at least 1 day.",
            ));
        }

        let upstream_selection_strategy: SelectionStrategy = self
            .upstream_selection_strategy
            .parse()
            .map_err(|_| WebError::bad_request("Invalid upstream selection strategy."))?;
        if self.upstream_parallel_fanout < 1 {
            return Err(WebError::bad_request(
                "Parallel fan-out must be at least 1.",
            ));
        }

        Ok(Settings {
            cache_min_ttl: self.cache_min_ttl,
            cache_max_ttl: self.cache_max_ttl,
            cache_negative_ttl_cap: self.cache_negative_ttl_cap,
            cache_capacity: self.cache_capacity,
            blocking_mode,
            custom_block_ipv4,
            custom_block_ipv6,
            blocklist_refresh_interval: self.blocklist_refresh_interval,
            ui_theme,
            query_log_enabled: self.query_log_enabled.is_some(),
            query_log_retention_days: self.query_log_retention_days,
            upstream_selection_strategy,
            upstream_parallel_fanout: self.upstream_parallel_fanout,
        })
    }
}

/// Parse an optional IP string (empty → `None`).
fn parse_opt_ip<T: std::str::FromStr>(s: &str, label: &str) -> Result<Option<T>, WebError> {
    let s = s.trim();
    if s.is_empty() {
        return Ok(None);
    }
    s.parse::<T>()
        .map(Some)
        .map_err(|_| WebError::bad_request(format!("'{s}' is not a valid {label} address.")))
}

/// The settings page.
#[derive(Template, WebTemplate)]
#[template(path = "settings.html")]
struct SettingsPageTemplate {
    chrome: Chrome,
    cache_min_ttl: u32,
    cache_max_ttl: u32,
    cache_negative_ttl_cap: u32,
    cache_capacity: u64,
    blocking_mode: &'static str,
    custom_block_ipv4: String,
    custom_block_ipv6: String,
    blocklist_refresh_interval: u32,
    query_log_enabled: bool,
    query_log_retention_days: u32,
    upstream_selection_strategy: &'static str,
    upstream_parallel_fanout: u32,
    error: Option<String>,
    notice: Option<String>,
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::codec::synth::BlockMode;
    use tempfile::TempDir;

    async fn state() -> (TempDir, AppState) {
        let (dir, db) = crate::test_support::temp_db().await;
        (dir, AppState::for_test(db).await)
    }

    fn base_form() -> SettingsForm {
        SettingsForm {
            cache_min_ttl: 10,
            cache_max_ttl: 3600,
            cache_negative_ttl_cap: 300,
            cache_capacity: 50_000,
            blocking_mode: "nxdomain".to_owned(),
            custom_block_ipv4: String::new(),
            custom_block_ipv6: String::new(),
            blocklist_refresh_interval: 7200,
            query_log_enabled: Some("on".to_owned()),
            query_log_retention_days: 30,
            upstream_selection_strategy: "random".to_owned(),
            upstream_parallel_fanout: 2,
        }
    }

    #[tokio::test]
    async fn apply_settings_persists_and_updates_snapshot() {
        let (_d, st) = state().await;
        st.apply_settings(base_form()).await.expect("apply");

        // Persisted.
        let s = st.db.settings().get().await.unwrap();
        assert_eq!(s.cache_max_ttl, 3600);
        assert_eq!(s.blocking_mode, BlockingMode::NxDomain);
        // The theme is owned by the topbar toggle now, so a settings save
        // leaves the stored value (the fresh-install default) untouched.
        assert_eq!(s.ui_theme, "auto");

        // Live snapshot updated (blocking mode applies immediately).
        assert_eq!(st.resolver.settings().block_mode, BlockMode::NxDomain);
        assert_eq!(st.resolver.settings().cache_max_ttl, 3600);
    }

    #[tokio::test]
    async fn parallel_strategy_rebuilds_pool_in_parallel_mode() {
        let (_d, st) = state().await;

        // A fresh test pool is sequential.
        assert_eq!(st.upstream_pool.load().parallel_fanout(), None);

        let mut f = base_form();
        f.upstream_selection_strategy = "parallel".to_owned();
        f.upstream_parallel_fanout = 3;
        st.apply_settings(f).await.expect("apply");

        // Persisted + live snapshot reflect the strategy …
        let s = st.db.settings().get().await.unwrap();
        assert_eq!(s.upstream_selection_strategy, SelectionStrategy::Parallel);
        assert_eq!(
            st.resolver.settings().upstream_selection_strategy,
            SelectionStrategy::Parallel
        );
        // … and the pool was rebuilt in parallel mode with the configured fan-out.
        assert_eq!(st.upstream_pool.load().parallel_fanout(), Some(3));
    }

    #[tokio::test]
    async fn invalid_fanout_is_rejected() {
        let (_d, st) = state().await;
        let mut f = base_form();
        f.upstream_parallel_fanout = 0;
        assert!(matches!(
            st.apply_settings(f).await,
            Err(WebError::BadRequest(_))
        ));
    }

    #[tokio::test]
    async fn min_greater_than_max_is_rejected() {
        let (_d, st) = state().await;
        let mut f = base_form();
        f.cache_min_ttl = 5000;
        f.cache_max_ttl = 100;
        assert!(matches!(
            st.apply_settings(f).await,
            Err(WebError::BadRequest(_))
        ));
    }

    #[tokio::test]
    async fn custom_mode_with_ips_round_trips() {
        let (_d, st) = state().await;
        let mut f = base_form();
        f.blocking_mode = "custom".to_owned();
        f.custom_block_ipv4 = "203.0.113.1".to_owned();
        st.apply_settings(f).await.expect("apply custom");
        let s = st.db.settings().get().await.unwrap();
        assert_eq!(s.blocking_mode, BlockingMode::Custom);
        assert_eq!(s.custom_block_ipv4, Some("203.0.113.1".parse().unwrap()));
    }

    #[tokio::test]
    async fn invalid_custom_ip_in_custom_mode_is_rejected() {
        let (_d, st) = state().await;
        let mut f = base_form();
        f.blocking_mode = "custom".to_owned();
        f.custom_block_ipv4 = "not-an-ip".to_owned();
        assert!(matches!(
            st.apply_settings(f).await,
            Err(WebError::BadRequest(_))
        ));
    }

    #[tokio::test]
    async fn invalid_custom_ip_ignored_when_mode_not_custom() {
        let (_d, st) = state().await;
        let mut f = base_form(); // nxdomain
        // A stale/invalid value in the (hidden) custom field must not block the
        // save — it is simply dropped.
        f.custom_block_ipv4 = "not-an-ip".to_owned();
        st.apply_settings(f).await.expect("apply");
        let s = st.db.settings().get().await.unwrap();
        assert_eq!(s.custom_block_ipv4, None);
    }

    #[tokio::test]
    async fn valid_custom_ip_preserved_across_non_custom_save() {
        let (_d, st) = state().await;
        // A valid custom IP survives a save made while in a non-custom mode, so
        // switching modes back and forth does not lose it.
        let mut f = base_form(); // nxdomain
        f.custom_block_ipv4 = "203.0.113.9".to_owned();
        st.apply_settings(f).await.expect("apply");
        let s = st.db.settings().get().await.unwrap();
        assert_eq!(s.custom_block_ipv4, Some("203.0.113.9".parse().unwrap()));
    }

    #[tokio::test]
    async fn query_log_fields_round_trip() {
        let (_d, st) = state().await;
        let mut f = base_form();
        // Unticked checkbox → None → disabled; custom retention window.
        f.query_log_enabled = None;
        f.query_log_retention_days = 7;
        st.apply_settings(f).await.expect("apply");

        let s = st.db.settings().get().await.unwrap();
        assert!(!s.query_log_enabled, "unticked checkbox disables logging");
        assert_eq!(s.query_log_retention_days, 7);

        // The live snapshot is updated too, so the writer gate sees it at once.
        assert!(!st.resolver.settings().query_log_enabled);
        assert_eq!(st.resolver.settings().query_log_retention_days, 7);
    }

    #[tokio::test]
    async fn zero_retention_is_rejected() {
        let (_d, st) = state().await;
        let mut f = base_form();
        f.query_log_retention_days = 0;
        assert!(matches!(
            st.apply_settings(f).await,
            Err(WebError::BadRequest(_))
        ));
    }

    #[tokio::test]
    async fn clear_log_empties_the_table() {
        use crate::{
            resolver::pipeline::Outcome,
            storage::query_log::{QueryLogRecord, QueryLogRepository},
        };

        let (_d, st) = state().await;
        let repo = st.db.query_log();
        repo.insert_batch(&[QueryLogRecord {
            id: 0,
            ts: 1,
            client: "10.0.0.1".to_owned(),
            qname: "x.test.".to_owned(),
            qtype: "A".to_owned(),
            outcome: Outcome::Forwarded,
            rcode: Some(0),
            upstream: None,
            latency_ms: 1,
            blocklist_id: None,
        }])
        .await
        .expect("seed a row");

        repo.clear_all().await.expect("clear");
        assert!(repo.page(None, 10).await.expect("page").is_empty());
    }
}