certon 0.2.0

Automatic HTTPS/TLS certificate management via the ACME protocol
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
//! HTTP to HTTPS redirect handler.
//!
//! This module provides a simple HTTP server that redirects all incoming
//! HTTP requests to their HTTPS equivalents. This is a common best practice
//! for production web servers: listen on port 80 only to redirect clients
//! to port 443.
//!
//! # Usage
//!
//! ```ignore
//! use certon::redirect::HttpsRedirectHandler;
//!
//! let handler = HttpsRedirectHandler::new(443)
//!     .with_canonical_host("example.com")?;
//! handler.start("0.0.0.0:80").await?;
//! ```

use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
use tokio::task::JoinHandle;
use tracing::{debug, warn};

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

// ---------------------------------------------------------------------------
// HttpsRedirectHandler
// ---------------------------------------------------------------------------

/// A simple HTTP server that responds to every request with a `301 Moved
/// Permanently` redirect to the HTTPS equivalent of the requested URL.
///
/// This handler also serves ACME HTTP-01 challenge responses if an
/// [`Http01Solver`](crate::solvers::Http01Solver) token map is provided,
/// allowing the same port-80 listener to handle both redirects and ACME
/// validation.
#[derive(Debug, Clone)]
pub struct HttpsRedirectHandler {
    /// The HTTPS port to redirect to. When set to `443` (default), the port
    /// is omitted from the `Location` header.
    pub https_port: u16,

    /// Canonical host to place in the `Location` header. When configured,
    /// the request `Host` header is not reflected.
    canonical_host: Option<String>,

    /// Accepted request hosts when no canonical host is configured.
    allowed_hosts: Vec<String>,
}

impl Default for HttpsRedirectHandler {
    fn default() -> Self {
        Self {
            https_port: 443,
            canonical_host: None,
            allowed_hosts: Vec::new(),
        }
    }
}

impl HttpsRedirectHandler {
    /// Create a new redirect handler that redirects to the given HTTPS port.
    pub fn new(https_port: u16) -> Self {
        Self {
            https_port,
            canonical_host: None,
            allowed_hosts: Vec::new(),
        }
    }

    /// Set the canonical HTTPS host used in every redirect response.
    pub fn with_canonical_host(mut self, host: &str) -> Result<Self> {
        self.canonical_host = Some(normalize_host(host)?);
        Ok(self)
    }

    /// Allow a request Host header when redirects should preserve the host.
    pub fn allow_host(mut self, host: &str) -> Result<Self> {
        self.allowed_hosts.push(normalize_host(host)?);
        Ok(self)
    }

    /// Start the redirect server on the given bind address (e.g.
    /// `"0.0.0.0:80"`).
    ///
    /// Returns a [`JoinHandle`] for the spawned server task. The server runs
    /// until the handle is dropped or aborted.
    pub async fn start(&self, bind_addr: &str) -> Result<JoinHandle<()>> {
        if self.canonical_host.is_none() && self.allowed_hosts.is_empty() {
            return Err(Error::Config(
                "HTTPS redirect requires a canonical host or explicit allowed hosts".to_owned(),
            ));
        }

        let listener = TcpListener::bind(bind_addr).await.map_err(|e| {
            Error::Other(format!(
                "failed to bind HTTP redirect listener on {bind_addr}: {e}"
            ))
        })?;

        let handler = self.clone();

        let handle = tokio::spawn(async move {
            loop {
                match listener.accept().await {
                    Ok((stream, _addr)) => {
                        tokio::spawn(handle_redirect_connection(stream, handler.clone()));
                    }
                    Err(e) => {
                        warn!(error = %e, "failed to accept HTTP connection for redirect");
                    }
                }
            }
        });

        Ok(handle)
    }

    /// Build the HTTPS redirect URL from the request's `Host` header and
    /// path.
    pub fn redirect_url(&self, host: &str, path: &str) -> String {
        let host = self
            .canonical_host
            .clone()
            .or_else(|| normalize_host(host).ok())
            .unwrap_or_else(|| "localhost".to_owned());
        self.format_redirect_url(&host, path)
    }

    /// Build a redirect URL while enforcing the configured Host policy.
    pub fn try_redirect_url(&self, host: &str, path: &str) -> Result<String> {
        let host = if let Some(canonical_host) = &self.canonical_host {
            canonical_host.clone()
        } else {
            let host = normalize_host(host)?;
            if !self.allowed_hosts.iter().any(|allowed| allowed == &host) {
                return Err(Error::Config(format!(
                    "request Host {host:?} is not allowed for HTTPS redirect"
                )));
            }
            host
        };

        Ok(self.format_redirect_url(&host, path))
    }

    fn format_redirect_url(&self, host: &str, path: &str) -> String {
        let path = sanitize_redirect_path(path);
        if self.https_port == 443 {
            format!("https://{host}{path}")
        } else {
            format!("https://{host}:{}{path}", self.https_port)
        }
    }
}

/// Handle a single HTTP connection: parse enough of the request to extract
/// the Host header and path, then send back a 301 redirect to HTTPS.
async fn handle_redirect_connection(
    mut stream: tokio::net::TcpStream,
    handler: HttpsRedirectHandler,
) {
    let mut buf = vec![0u8; 4096];
    let n = match stream.read(&mut buf).await {
        Ok(0) => return,
        Ok(n) => n,
        Err(e) => {
            debug!(error = %e, "error reading HTTP request for redirect");
            return;
        }
    };

    let request = String::from_utf8_lossy(&buf[..n]);

    // Parse the first line to get the request path.
    let path = request
        .lines()
        .next()
        .and_then(|line| line.split_whitespace().nth(1))
        .unwrap_or("/");

    // Parse the Host header.
    let host = request
        .lines()
        .find(|line| line.to_lowercase().starts_with("host:"))
        .map(|line| line[5..].trim())
        .unwrap_or("localhost");

    let location = match handler.try_redirect_url(host, path) {
        Ok(location) => location,
        Err(e) => {
            debug!(error = %e, "rejecting HTTP redirect request with invalid Host");
            let response = "HTTP/1.1 400 Bad Request\r\n\
                            Content-Length: 0\r\n\
                            Connection: close\r\n\
                            \r\n";
            let _ = stream.write_all(response.as_bytes()).await;
            return;
        }
    };

    let response = format!(
        "HTTP/1.1 301 Moved Permanently\r\n\
         Location: {location}\r\n\
         Content-Length: 0\r\n\
         Connection: close\r\n\
         \r\n"
    );

    if let Err(e) = stream.write_all(response.as_bytes()).await {
        debug!(error = %e, "error writing HTTP redirect response");
    }
}

/// Convenience function: start an HTTP→HTTPS redirect server on `bind_addr`.
///
/// For public listeners, prefer [`start_https_redirect_to_host`] or
/// [`HttpsRedirectHandler::with_canonical_host`]. This helper returns a
/// configuration error unless an allowlist is added through the handler API.
pub async fn start_https_redirect(bind_addr: &str) -> Result<JoinHandle<()>> {
    HttpsRedirectHandler::default().start(bind_addr).await
}

/// Convenience function: start an HTTP→HTTPS redirect server on
/// `bind_addr`, redirecting to a custom HTTPS port.
///
/// For public listeners, prefer configuring a canonical host with
/// [`HttpsRedirectHandler::with_canonical_host`].
pub async fn start_https_redirect_with_port(
    bind_addr: &str,
    https_port: u16,
) -> Result<JoinHandle<()>> {
    HttpsRedirectHandler::new(https_port).start(bind_addr).await
}

/// Convenience function: start an HTTP→HTTPS redirect server with a fixed
/// canonical HTTPS host.
pub async fn start_https_redirect_to_host(
    bind_addr: &str,
    canonical_host: &str,
) -> Result<JoinHandle<()>> {
    HttpsRedirectHandler::new(443)
        .with_canonical_host(canonical_host)?
        .start(bind_addr)
        .await
}

fn normalize_host(host: &str) -> Result<String> {
    let host = host.trim();
    if host.is_empty() {
        return Err(Error::Config("Host header is empty".to_owned()));
    }
    if host
        .bytes()
        .any(|b| b.is_ascii_control() || matches!(b, b' ' | b'\t' | b'/' | b'\\' | b'@'))
    {
        return Err(Error::Config(
            "Host header contains invalid characters".to_owned(),
        ));
    }

    if let Some(rest) = host.strip_prefix('[') {
        let Some(end) = rest.find(']') else {
            return Err(Error::Config(
                "IPv6 Host header is missing closing bracket".to_owned(),
            ));
        };
        let addr = &rest[..end];
        let tail = &rest[end + 1..];
        if !tail.is_empty()
            && !tail
                .strip_prefix(':')
                .is_some_and(|port| !port.is_empty() && port.chars().all(|c| c.is_ascii_digit()))
        {
            return Err(Error::Config(
                "IPv6 Host header has invalid port".to_owned(),
            ));
        }
        if addr.is_empty() || addr.bytes().any(|b| b.is_ascii_control()) {
            return Err(Error::Config("IPv6 Host header is invalid".to_owned()));
        }
        return Ok(format!("[{}]", addr.to_ascii_lowercase()));
    }

    let host = if let Some(colon) = host.rfind(':') {
        let after = &host[colon + 1..];
        if !after.is_empty() && after.chars().all(|c| c.is_ascii_digit()) {
            &host[..colon]
        } else {
            host
        }
    } else {
        host
    };

    if host.contains(':') {
        return Err(Error::Config(
            "IPv6 Host headers must use bracket notation".to_owned(),
        ));
    }
    if host.is_empty()
        || host.starts_with('.')
        || host.ends_with('.')
        || !host
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '.'))
    {
        return Err(Error::Config("Host header is invalid".to_owned()));
    }

    Ok(host.to_ascii_lowercase())
}

fn sanitize_redirect_path(path: &str) -> &str {
    if path.starts_with('/') && !path.bytes().any(|b| matches!(b, b'\r' | b'\n')) {
        path
    } else {
        "/"
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_redirect_url_default_port() {
        let handler = HttpsRedirectHandler::new(443);
        assert_eq!(
            handler.redirect_url("example.com", "/path"),
            "https://example.com/path"
        );
    }

    #[test]
    fn test_redirect_url_custom_port() {
        let handler = HttpsRedirectHandler::new(8443);
        assert_eq!(
            handler.redirect_url("example.com", "/path"),
            "https://example.com:8443/path"
        );
    }

    #[test]
    fn test_redirect_url_strips_port_from_host() {
        let handler = HttpsRedirectHandler::new(443);
        assert_eq!(
            handler.redirect_url("example.com:80", "/"),
            "https://example.com/"
        );
    }

    #[test]
    fn test_redirect_url_root_path() {
        let handler = HttpsRedirectHandler::new(443);
        assert_eq!(
            handler.redirect_url("example.com", "/"),
            "https://example.com/"
        );
    }

    #[test]
    fn test_try_redirect_url_uses_canonical_host() {
        let handler = HttpsRedirectHandler::new(443)
            .with_canonical_host("example.com")
            .unwrap();
        assert_eq!(
            handler
                .try_redirect_url("attacker.example", "/login")
                .unwrap(),
            "https://example.com/login"
        );
    }

    #[test]
    fn test_try_redirect_url_rejects_unallowed_host() {
        let handler = HttpsRedirectHandler::new(443)
            .allow_host("example.com")
            .unwrap();
        assert!(handler.try_redirect_url("attacker.example", "/").is_err());
    }

    #[test]
    fn test_try_redirect_url_accepts_allowed_host_without_port() {
        let handler = HttpsRedirectHandler::new(8443)
            .allow_host("example.com")
            .unwrap();
        assert_eq!(
            handler.try_redirect_url("example.com:80", "/").unwrap(),
            "https://example.com:8443/"
        );
    }

    #[test]
    fn test_try_redirect_url_rejects_invalid_host_characters() {
        let handler = HttpsRedirectHandler::new(443)
            .allow_host("example.com")
            .unwrap();
        assert!(
            handler
                .try_redirect_url("example.com\r\nx: y", "/")
                .is_err()
        );
        assert!(handler.try_redirect_url("example.com/path", "/").is_err());
    }

    #[tokio::test]
    async fn test_start_requires_host_policy() {
        let err = HttpsRedirectHandler::new(443)
            .start("127.0.0.1:0")
            .await
            .unwrap_err();
        assert!(err.to_string().contains("canonical host"));
    }
}