~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
430 lines · 15.2 KB Raw History
//! Resolving a handle outside the server's domains, as `spec/IDENTITY.md`
//! says under "Resolving a handle": its `_atproto` TXT records in DNS, and
//! its `/.well-known/atproto-did` over HTTPS, both at once, DNS's DID
//! winning. Nothing is kept: the DNS resolver keeps no answers, and each
//! resolution asks again. The fetch goes out through `outbound`, as a
//! request of target `handle`; with `PDS_HANDLE_FETCH`, from its base URL
//! instead.

use crate::Service;
use crate::ops::Target;
use hickory_resolver::TokioResolver;
use std::net::{IpAddr, SocketAddr};
use std::pin::pin;
use std::sync::OnceLock;
use std::time::Duration;
use url::Url;

/// How long the fetch has, its redirects included; and the DNS lookup too.
const WAIT: Duration = Duration::from_secs(3);
/// The most of a `/.well-known/atproto-did` body read.
const BODY_MAX: usize = 8192;
/// The most redirects followed.
const REDIRECTS: usize = 10;
const DNS_VAR: &str = "PDS_HANDLE_DNS_SERVER";
const FETCH_VAR: &str = "PDS_HANDLE_FETCH";

/// The DNS resolver handles are looked up with, made on first use.
#[derive(Default)]
pub(crate) struct Resolver {
    dns: OnceLock<Result<TokioResolver, String>>,
}

/// `PDS_HANDLE_DNS_SERVER`, if set: Err if it does not parse. Set to
/// nothing, it is as unset.
pub(crate) fn dns_server_from_env() -> Result<Option<SocketAddr>, String> {
    match std::env::var(DNS_VAR) {
        Ok(v) if !v.trim().is_empty() => dns_server(v.trim())
            .map(Some)
            .map_err(|e| format!("{DNS_VAR}: {e}")),
        Ok(_) | Err(std::env::VarError::NotPresent) => Ok(None),
        Err(std::env::VarError::NotUnicode(v)) => {
            Err(format!("{DNS_VAR}: {v:?} is not an IP address"))
        }
    }
}

/// An IPv4 or IPv6 address, with an optional port (53 without one): as
/// `127.0.0.1`, `127.0.0.1:5353`, `::1`, `[::1]` or `[::1]:5353`.
fn dns_server(v: &str) -> Result<SocketAddr, String> {
    if let Ok(a) = v.parse::<SocketAddr>() {
        return Ok(a);
    }
    let ip = v
        .strip_prefix('[')
        .and_then(|r| r.strip_suffix(']'))
        .unwrap_or(v);
    ip.parse::<IpAddr>()
        .map(|ip| SocketAddr::new(ip, 53))
        .map_err(|_| format!("{v:?} is not an IP address with an optional port"))
}

/// `PDS_HANDLE_FETCH`, if set: Err if it does not parse. Set to nothing, it
/// is as unset.
pub(crate) fn fetch_from_env() -> Result<Option<String>, String> {
    match std::env::var(FETCH_VAR) {
        Ok(v) if !v.trim().is_empty() => fetch_base(v.trim())
            .map(Some)
            .map_err(|e| format!("{FETCH_VAR}: {e}")),
        Ok(_) | Err(std::env::VarError::NotPresent) => Ok(None),
        Err(std::env::VarError::NotUnicode(v)) => Err(format!("{FETCH_VAR}: {v:?} is not a URL")),
    }
}

/// A base URL: `http://` or `https://`, a host, an optional port and path,
/// no user, query or fragment, and no trailing `/`.
fn fetch_base(base: &str) -> Result<String, String> {
    let b = Url::parse(base).map_err(|e| format!("{base:?}: {e}"))?;
    let plain = b.username().is_empty()
        && b.password().is_none()
        && b.query().is_none()
        && b.fragment().is_none();
    if !["http", "https"].contains(&b.scheme())
        || b.host().is_none()
        || !plain
        || base.ends_with('/')
        || base.contains(['?', '#'])
    {
        return Err(format!(
            "{base:?} is not an http or https URL with a host, an optional port and path, and no trailing /"
        ));
    }
    Ok(base.to_string())
}

/// The DNS resolver: the system's, or one asking only `server`; either
/// keeping no answer.
fn resolver(server: Option<SocketAddr>) -> Result<TokioResolver, String> {
    use hickory_resolver::config::{ConnectionConfig, NameServerConfig, ResolverConfig};
    use hickory_resolver::net::runtime::TokioRuntimeProvider;
    let mut builder = match server {
        Some(addr) => {
            let mut udp = ConnectionConfig::udp();
            udp.port = addr.port();
            let config = ResolverConfig::from_name_servers(vec![NameServerConfig::new(
                addr.ip(),
                true,
                vec![udp],
            )]);
            TokioResolver::builder_with_config(config, TokioRuntimeProvider::default())
        }
        None => TokioResolver::builder_tokio().map_err(|e| format!("a DNS resolver: {e}"))?,
    };
    let opts = builder.options_mut();
    opts.cache_size = 0;
    opts.positive_max_ttl = Some(Duration::ZERO);
    opts.negative_max_ttl = Some(Duration::ZERO);
    opts.timeout = WAIT;
    opts.attempts = 1;
    builder.build().map_err(|e| format!("a DNS resolver: {e}"))
}

/// The DID `handle` (lowercased) resolves to, if any: DNS's when it finds
/// one, else the HTTPS fetch's, both sought at once.
pub(super) async fn resolve(s: &Service, handle: &str) -> Option<String> {
    let mut dns = pin!(by_dns(s, handle));
    let mut https = pin!(by_https(s, handle));
    tokio::select! {
        d = &mut dns => match d {
            Some(d) => Some(d),
            None => https.await,
        },
        h = &mut https => dns.await.or(h),
    }
}

/// The DID of `_atproto.<handle>`'s one `did=` TXT record.
async fn by_dns(s: &Service, handle: &str) -> Option<String> {
    use hickory_resolver::proto::rr::RData;
    let dns = s
        .handle_resolver
        .dns
        .get_or_init(|| resolver(s.config.handle_dns))
        .as_ref()
        .ok()?;
    let name = format!("_atproto.{handle}.");
    let found = tokio::time::timeout(WAIT, dns.txt_lookup(name.as_str()))
        .await
        .ok()?
        .ok()?;
    did_of_records(found.answers().iter().filter_map(|r| match &r.data {
        RData::TXT(t) => Some(t.txt_data.concat()),
        _ => None,
    }))
}

/// Of TXT records, each its strings joined, what follows `did=` in the one
/// that starts so: none if none does, or more than one.
fn did_of_records(records: impl IntoIterator<Item = Vec<u8>>) -> Option<String> {
    let mut dids = records
        .into_iter()
        .filter_map(|r| r.strip_prefix(b"did=").map(<[u8]>::to_vec));
    let did = dids.next()?;
    if dids.next().is_some() {
        return None;
    }
    String::from_utf8(did).ok()
}

/// The DID `https://<handle>/.well-known/atproto-did` answers, redirects
/// followed, within `WAIT`.
async fn by_https(s: &Service, handle: &str) -> Option<String> {
    let first = match &s.config.handle_fetch {
        Some(base) => format!("{base}/{handle}/.well-known/atproto-did"),
        None => format!("https://{handle}/.well-known/atproto-did"),
    };
    let mut url = Url::parse(&first).ok()?;
    let body = tokio::time::timeout(WAIT, async {
        for _ in 0..=REDIRECTS {
            let out = &s.outbound;
            let got = out
                .fetch_head(
                    Target::Handle,
                    out.client().get(url.clone()),
                    BODY_MAX,
                    WAIT,
                )
                .await
                .ok()?;
            if matches!(got.status, 301 | 302 | 303 | 307 | 308) {
                let to = got.headers.get(reqwest::header::LOCATION)?.to_str().ok()?;
                url = url.join(to).ok()?;
                continue;
            }
            return (got.status == 200).then_some(got.body);
        }
        None
    })
    .await
    .ok()??;
    did_of_body(&body)
}

/// A body's first line, trimmed of white space, if it starts `did:`.
fn did_of_body(body: &[u8]) -> Option<String> {
    let line = body.split(|&b| b == b'\n').next()?;
    let line = String::from_utf8_lossy(line);
    let line = line.trim();
    line.starts_with("did:").then(|| line.to_string())
}

#[cfg(test)]
pub(crate) mod stub {
    //! A DNS server and a web, one each for the whole test process, that
    //! the tests' configuration resolves handles with: each answers only
    //! the handles a test served.

    use std::collections::HashMap;
    use std::net::SocketAddr;
    use std::sync::{LazyLock, Mutex};

    #[derive(Default)]
    struct Served {
        /// TXT records by name asked, each record its strings.
        txt: HashMap<String, Vec<Vec<String>>>,
        /// The body of each handle's `/.well-known/atproto-did`.
        well_known: HashMap<String, String>,
    }

    static SERVED: LazyLock<Mutex<Served>> = LazyLock::new(Default::default);
    /// The DNS server's address, and the web's base URL: started on first use.
    static ADDRS: LazyLock<(SocketAddr, String)> = LazyLock::new(start);

    fn served() -> std::sync::MutexGuard<'static, Served> {
        SERVED
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    /// Serve `handle`: `dns`, its `_atproto` TXT records, each its strings;
    /// `https`, the body of its `/.well-known/atproto-did`. Whatever was
    /// served for it before goes.
    pub fn serve(handle: &str, dns: &[&[&str]], https: Option<&str>) {
        let mut s = served();
        let name = format!("_atproto.{handle}");
        s.txt.remove(&name);
        s.well_known.remove(handle);
        if !dns.is_empty() {
            let records = dns
                .iter()
                .map(|r| r.iter().map(|t| t.to_string()).collect())
                .collect();
            s.txt.insert(name, records);
        }
        if let Some(body) = https {
            s.well_known.insert(handle.to_string(), body.to_string());
        }
    }

    /// The DNS server's address, and the web's base URL.
    pub fn addrs() -> (SocketAddr, String) {
        ADDRS.clone()
    }

    fn start() -> (SocketAddr, String) {
        let (tx, rx) = std::sync::mpsc::channel();
        std::thread::spawn(move || {
            let rt = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .unwrap();
            rt.block_on(async move {
                let udp = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
                let web = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
                tx.send((
                    udp.local_addr().unwrap(),
                    format!("http://{}", web.local_addr().unwrap()),
                ))
                .unwrap();
                tokio::spawn(async move {
                    let mut buf = [0u8; 1500];
                    loop {
                        let Ok((n, from)) = udp.recv_from(&mut buf).await else {
                            continue;
                        };
                        if let Some(a) = answer(&buf[..n]) {
                            let _ = udp.send_to(&a, from).await;
                        }
                    }
                });
                let app = axum::Router::new().route(
                    "/{handle}/.well-known/atproto-did",
                    axum::routing::get(
                        |axum::extract::Path(h): axum::extract::Path<String>| async move {
                            match served().well_known.get(&h.to_lowercase()).cloned() {
                                Some(body) => (axum::http::StatusCode::OK, body),
                                None => {
                                    (axum::http::StatusCode::NOT_FOUND, "not found".to_string())
                                }
                            }
                        },
                    ),
                );
                axum::serve(web, app).await.unwrap();
            });
        });
        rx.recv().unwrap()
    }

    /// The answer to a query: the TXT records of a name served, none for a
    /// name served asked of another type, NXDOMAIN for any other name.
    fn answer(query: &[u8]) -> Option<Vec<u8>> {
        let mut at = 12;
        let mut labels = Vec::new();
        while *query.get(at)? != 0 {
            let len = usize::from(query[at]);
            labels.push(String::from_utf8_lossy(query.get(at + 1..at + 1 + len)?).to_lowercase());
            at += 1 + len;
        }
        let question = query.get(12..at + 5)?;
        let qtype = u16::from_be_bytes([*query.get(at + 1)?, *query.get(at + 2)?]);
        let s = served();
        let records = s.txt.get(&labels.join("."));
        let answers: &[Vec<String>] = match records {
            Some(r) if qtype == 16 => r,
            _ => &[],
        };
        let mut out = query[..2].to_vec();
        out.extend(if records.is_some() {
            [0x81, 0x80]
        } else {
            [0x81, 0x83]
        });
        out.extend([0, 1]);
        out.extend((answers.len() as u16).to_be_bytes());
        out.extend([0, 0, 0, 0]);
        out.extend(question);
        for strings in answers {
            let rdata: Vec<u8> = strings
                .iter()
                .flat_map(|t| std::iter::once(t.len() as u8).chain(t.bytes()))
                .collect();
            out.extend([0xc0, 12, 0, 16, 0, 1, 0, 0, 0, 0]);
            out.extend((rdata.len() as u16).to_be_bytes());
            out.extend(rdata);
        }
        Some(out)
    }
}

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

    #[test]
    fn the_variables() {
        for (v, want) in [
            ("127.0.0.1", "127.0.0.1:53"),
            ("127.0.0.1:5353", "127.0.0.1:5353"),
            ("::1", "[::1]:53"),
            ("[::1]", "[::1]:53"),
            ("[::1]:5353", "[::1]:5353"),
        ] {
            assert_eq!(dns_server(v), Ok(want.parse().unwrap()), "{v}");
        }
        for bad in [
            "localhost",
            "127.0.0.1:",
            "127.0.0.1:99999",
            "::1:5353x",
            "[::1",
            "1.2.3",
        ] {
            assert!(dns_server(bad).is_err(), "{bad}");
        }
        for ok in [
            "http://127.0.0.1:4100",
            "https://handles.example.com/base/path",
        ] {
            assert_eq!(fetch_base(ok).as_deref(), Ok(ok));
        }
        for bad in [
            "127.0.0.1:4100",
            "ftp://127.0.0.1",
            "http://127.0.0.1/",
            "http://127.0.0.1/p?q",
            "http://127.0.0.1#f",
            "http://u@127.0.0.1",
            "http://",
        ] {
            assert!(fetch_base(bad).is_err(), "{bad}");
        }
    }

    #[test]
    fn txt_records() {
        let r =
            |rs: &[&str]| -> Vec<Vec<u8>> { rs.iter().map(|r| r.as_bytes().to_vec()).collect() };
        let a = "did:plc:aaaaaaaaaaaaaaaaaaaaaaaa";
        assert_eq!(did_of_records(r(&[&format!("did={a}")])), Some(a.into()));
        assert_eq!(
            did_of_records(r(&["v=spf1 -all", &format!("did={a}")])),
            Some(a.into())
        );
        assert_eq!(did_of_records(r(&["v=spf1 -all"])), None);
        assert_eq!(did_of_records(r(&[])), None);
        assert_eq!(
            did_of_records(r(&[&format!("did={a}"), "did=did:plc:other"])),
            None
        );
        // Not trimmed nor otherwise read: it must be the DID exactly.
        assert_eq!(did_of_records(r(&[" did=x"])), None);
        assert_eq!(did_of_records(r(&["did= x"])), Some(" x".into()));
    }

    #[test]
    fn well_known_bodies() {
        let a = "did:plc:aaaaaaaaaaaaaaaaaaaaaaaa";
        for (body, want) in [
            (a.to_string(), Some(a)),
            (format!("{a}\n"), Some(a)),
            (format!("  {a}  \r\nmore"), Some(a)),
            (format!("\n{a}"), None),
            ("not a did".into(), None),
            ("".into(), None),
            ("did:".into(), Some("did:")),
        ] {
            assert_eq!(did_of_body(body.as_bytes()).as_deref(), want, "{body:?}");
        }
    }
}