~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
950 lines · 33.4 KB Raw History
//! Requests the server sends to URLs others name (`spec/PROXY.md`, "SSRF
//! protection"): the services a request is forwarded to and the AppView,
//! every `did:web` document the server reads, OAuth clients' documents and
//! JWKS (`spec/OAUTH-CLIENTS.md`), client attestations' issuers, managing
//! apps, the spaces' notifications (`spec/SPACES*.md`), and handles'
//! `/.well-known/atproto-did` (`spec/IDENTITY.md`). Only the directory at
//! `PDS_PLC_URL` is asked without this guard.
//!
//! Unless `PDS_DISABLE_SSRF_PROTECTION` turns it off, a request goes only to
//! an `https` URL whose host is a name, not an IP address and not
//! `localhost`, and which resolves only to public unicast addresses: a
//! request that would reach this machine or its private network is never
//! sent. The resolver itself refuses such a host, so a name cannot resolve
//! to one address when checked and another when connected to.
//!
//! Whether protection is on or off, no redirect is followed here (handle
//! resolution follows one by sending each hop through this guard again), and
//! an answer is read only up to a size its caller gives (`DID_DOC_MAX`, `JSON_DOC_MAX`,
//! `FORWARD_MAX`, `OTHER_MAX`). At most `MAX_IN_FLIGHT` requests are out at
//! once, and `did:web` documents at most `DID_WEB_AT_ONCE` of them, apart
//! from the directory's slots (`spec/LIMITS.md`, "Requests out"); a request
//! that has waited `TURN_WAIT` for its turn is given up as one that got no
//! answer. No lock is held meanwhile. Each goes out through `ops::execute`,
//! as every request the server makes does: its `traceparent`, span, metrics
//! under its `Target`, and failure line.

use crate::ops::Target;
use axum::body::Bytes;
use reqwest::header::HeaderMap;
use reqwest::redirect::Policy;
use serde_json::Value as Json;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{Semaphore, SemaphorePermit};

/// How many requests may be out at once.
const MAX_IN_FLIGHT: usize = 256;
/// How many `did:web` documents may be fetched at once.
const DID_WEB_AT_ONCE: usize = 16;
/// How long a request waits for its turn before it is given up.
const TURN_WAIT: Duration = Duration::from_secs(10);
/// How long a service has to send its answer's headers.
const HEADERS: Duration = Duration::from_secs(10);
/// How long it then has to send the rest of a forwarded answer.
const BODY: Duration = Duration::from_secs(30);
/// How long a request the server sends for itself (a document, a managing
/// app's answer, a notification) has for all of its answer.
pub(crate) const WAIT: Duration = Duration::from_secs(10);
/// The most of a DID document read.
pub(crate) const DID_DOC_MAX: usize = 65536;
/// The most of another JSON document read: a client's metadata, a JWKS, an
/// issuer's metadata, a managing app's answer.
pub(crate) const JSON_DOC_MAX: usize = 524288;
/// The most of a forwarded answer, or of a service's answer to a method the
/// server serves by asking it, read.
pub(crate) const FORWARD_MAX: usize = 16 << 20;
/// The most of any other answer read.
pub(crate) const OTHER_MAX: usize = 65536;

/// Whether SSRF protection is on, as `PDS_DISABLE_SSRF_PROTECTION` says:
/// `true` or `1` turn it off; `false`, `0`, empty or unset leave it on; any
/// other value is an error.
pub(crate) fn protection_from_env() -> Result<bool, String> {
    match std::env::var("PDS_DISABLE_SSRF_PROTECTION") {
        Err(std::env::VarError::NotPresent) => Ok(true),
        Ok(v) => protection(&v),
        Err(std::env::VarError::NotUnicode(v)) => Err(format!(
            "PDS_DISABLE_SSRF_PROTECTION is {v:?}, not true, 1, false or 0"
        )),
    }
}

fn protection(v: &str) -> Result<bool, String> {
    match v {
        "" | "false" | "0" => Ok(true),
        "true" | "1" => Ok(false),
        _ => Err(format!(
            "PDS_DISABLE_SSRF_PROTECTION is {v:?}, not true, 1, false or 0"
        )),
    }
}

/// Whether an address is a public unicast one: none of the unspecified,
/// loopback, private, link-local, shared, documentation, benchmarking,
/// multicast, broadcast and reserved ranges, nor an IPv6 form of one of
/// those IPv4 addresses (mapped, NAT64, 6to4); of IPv6, global unicast
/// alone, less Teredo, ORCHID and documentation.
pub(crate) fn is_public(ip: IpAddr) -> bool {
    match ip {
        IpAddr::V4(a) => is_public_v4(a),
        IpAddr::V6(a) => is_public_v6(a),
    }
}

fn is_public_v4(a: Ipv4Addr) -> bool {
    let [x, y, z, _] = a.octets();
    !(x == 0
        || x == 10
        || x == 127
        || (x == 100 && (64..128).contains(&y)) // shared, 100.64.0.0/10
        || (x == 169 && y == 254)
        || (x == 172 && (16..32).contains(&y))
        || (x == 192 && y == 0 && (z == 0 || z == 2))
        || (x == 192 && y == 88 && z == 99)
        || (x == 192 && y == 168)
        || (x == 198 && (18..20).contains(&y)) // benchmarking, 198.18.0.0/15
        || (x == 198 && y == 51 && z == 100)
        || (x == 203 && y == 0 && z == 113)
        || x >= 224) // multicast, reserved, broadcast
}

fn is_public_v6(a: Ipv6Addr) -> bool {
    let s = a.segments();
    let embedded = |hi: usize| {
        Ipv4Addr::new(
            (s[hi] >> 8) as u8,
            s[hi] as u8,
            (s[hi + 1] >> 8) as u8,
            s[hi + 1] as u8,
        )
    };
    if let Some(v4) = a.to_ipv4_mapped() {
        return is_public_v4(v4);
    }
    // NAT64's well-known prefix, and 6to4, carry an IPv4 address.
    if s[..6] == [0x64, 0xff9b, 0, 0, 0, 0] {
        return is_public_v4(embedded(6));
    }
    if s[0] == 0x2002 {
        return is_public_v4(embedded(1));
    }
    // Global unicast only, less Teredo, ORCHID and documentation.
    (s[0] & 0xe000) == 0x2000
        && !(s[0] == 0x2001 && (s[1] == 0 || (0x10..0x30).contains(&s[1]) || s[1] == 0x0db8))
        && s[0] != 0x3fff
}

/// A resolver that answers only names whose every address is public.
struct PublicOnly;

impl reqwest::dns::Resolve for PublicOnly {
    fn resolve(&self, name: reqwest::dns::Name) -> reqwest::dns::Resolving {
        let host = name.as_str().to_string();
        Box::pin(async move {
            let addrs: Vec<SocketAddr> =
                tokio::net::lookup_host((host.as_str(), 0)).await?.collect();
            if addrs.is_empty() || !addrs.iter().all(|a| is_public(a.ip())) {
                return Err(
                    format!("{host} resolves to an address the server does not send to").into(),
                );
            }
            Ok(Box::new(addrs.into_iter()) as reqwest::dns::Addrs)
        })
    }
}

/// A service's answer, whole.
pub(crate) struct Fetched {
    pub status: u16,
    pub headers: HeaderMap,
    pub body: Bytes,
}

/// A turn of `slots`, once one is free within `wait`.
async fn turn(slots: &Semaphore, wait: Duration) -> Result<SemaphorePermit<'_>, String> {
    match tokio::time::timeout(wait, slots.acquire()).await {
        Ok(Ok(p)) => Ok(p),
        _ => Err(format!("no turn to send a request within {wait:?}")),
    }
}

/// The URL of a `did:web`'s document: `https://<host>/.well-known/did.json`,
/// or with `http` for `localhost` when `protect` is off. None for a
/// `did:web` with a path, or a host that is not one.
pub(crate) fn did_web_url(did: &str, protect: bool) -> Option<url::Url> {
    let id = did.strip_prefix("did:web:")?;
    if id.contains(':') {
        return None;
    }
    let (host, port) = match id.find("%3A").or_else(|| id.find("%3a")) {
        Some(i) => (&id[..i], Some(id[i + 3..].parse::<u16>().ok()?)),
        None => (id, None),
    };
    let ok = !host.is_empty()
        && host
            .split('.')
            .all(|l| !l.is_empty() && l.bytes().all(|c| c.is_ascii_alphanumeric() || c == b'-'));
    if !ok {
        return None;
    }
    let host = host.to_ascii_lowercase();
    let scheme = if host == "localhost" && !protect {
        "http"
    } else {
        "https"
    };
    let port = port.map(|p| format!(":{p}")).unwrap_or_default();
    url::Url::parse(&format!("{scheme}://{host}{port}/.well-known/did.json")).ok()
}

/// Whether `did` is a `did:web` as `atproto/did.md` allows one: a host
/// name, no path, and a port only on `localhost`. The port is read from the
/// identifier, as a URL drops a port that is its scheme's default.
pub(crate) fn atproto_did_web(did: &str) -> bool {
    let id = did
        .strip_prefix("did:web:")
        .unwrap_or_default()
        .to_ascii_lowercase();
    did_web_url(did, false).is_some() && (!id.contains("%3a") || id.starts_with("localhost%3a"))
}

/// A service's endpoint, as the spaces send to one (`spec/SPACES-SYNC.md`,
/// "Environment"): its `serviceEndpoint` when that is a string starting
/// with `http://` or `https://` that parses as a URL and has no user name
/// or password, reduced to its origin; its path, query and fragment are
/// not used. None for any other.
pub(crate) fn origin(endpoint: &Json) -> Option<url::Url> {
    let e = endpoint
        .as_str()
        .filter(|e| e.starts_with("http://") || e.starts_with("https://"))?;
    let u = url::Url::parse(e).ok()?;
    if !u.username().is_empty() || u.password().is_some() || u.host().is_none() {
        return None;
    }
    url::Url::parse(&u.origin().ascii_serialization()).ok()
}

/// The client every request to a service goes out through.
pub(crate) struct Outbound {
    protect: bool,
    client: reqwest::Client,
    permits: Semaphore,
    did_web: Semaphore,
    turn_wait: Duration,
}

impl Outbound {
    /// The client, with SSRF protection if `protect`. It follows no redirect.
    pub fn new(protect: bool) -> Result<Outbound, String> {
        Outbound::with(protect, MAX_IN_FLIGHT, DID_WEB_AT_ONCE, TURN_WAIT)
    }

    fn with(
        protect: bool,
        in_flight: usize,
        did_web: usize,
        turn_wait: Duration,
    ) -> Result<Outbound, String> {
        let mut b = reqwest::Client::builder()
            .no_proxy()
            .redirect(Policy::none())
            .connect_timeout(Duration::from_secs(5))
            // A service may close a connection it holds idle (Node's HTTP
            // server does after 5 seconds) just as a request goes out on it.
            .pool_idle_timeout(Duration::from_secs(2));
        if protect {
            b = b.dns_resolver(Arc::new(PublicOnly));
        }
        let client = b.build().map_err(|e| format!("an HTTP client: {e}"))?;
        Ok(Outbound {
            protect,
            client,
            permits: Semaphore::new(in_flight),
            did_web: Semaphore::new(did_web),
            turn_wait,
        })
    }

    /// The client to build requests with; `fetch` and the others send them.
    pub fn client(&self) -> &reqwest::Client {
        &self.client
    }

    /// Whether the server may send a request to `url`: `https`, or `http`
    /// too without protection; and with it, a host that is a name, not an
    /// IP address and not `localhost`. A name's addresses are checked as it
    /// is resolved.
    pub fn allows(&self, url: &url::Url) -> bool {
        match url.scheme() {
            "https" => {}
            "http" if !self.protect => {}
            _ => return false,
        }
        match url.host() {
            None => false,
            Some(_) if !self.protect => true,
            Some(url::Host::Domain(d)) => {
                let d = d.trim_end_matches('.').to_ascii_lowercase();
                d != "localhost" && !d.ends_with(".localhost")
            }
            Some(url::Host::Ipv4(_) | url::Host::Ipv6(_)) => false,
        }
    }

    /// Send a request and read its answer whole, or say why there is none: a
    /// URL the server does not send to, no turn within `TURN_WAIT`, a service
    /// that cannot be reached or refuses the connection, one too slow to
    /// answer, or an answer longer than `max` bytes. A redirect is answered
    /// as it is, not followed.
    pub async fn fetch(
        &self,
        target: Target,
        req: reqwest::RequestBuilder,
        max: usize,
    ) -> Result<Fetched, String> {
        let req = self.request(req)?;
        let url = req.url().to_string();
        let _turn = turn(&self.permits, self.turn_wait).await?;
        let res =
            match tokio::time::timeout(HEADERS, crate::ops::execute(target, &self.client, req))
                .await
            {
                Err(_) => return Err(format!("{url} sent no answer within {HEADERS:?}")),
                Ok(Err(e)) => return Err(format!("{url}: {e}")),
                Ok(Ok(res)) => res,
            };
        tokio::time::timeout(BODY, read(res, max, &url))
            .await
            .unwrap_or_else(|_| Err(format!("{url} sent no whole answer within {BODY:?}")))
    }

    /// Send a request and read its answer whole, as `fetch` does, but all of
    /// it within `within`.
    pub async fn fetch_limited(
        &self,
        target: Target,
        req: reqwest::RequestBuilder,
        max: usize,
        within: Duration,
    ) -> Result<Fetched, String> {
        self.limited(target, req, max, within, true).await
    }

    /// Send a request and read its answer as `fetch_limited` does, but of a
    /// body longer than `max` bytes only its first `max`: the rest is not read.
    pub async fn fetch_head(
        &self,
        target: Target,
        req: reqwest::RequestBuilder,
        max: usize,
        within: Duration,
    ) -> Result<Fetched, String> {
        self.limited(target, req, max, within, false).await
    }

    async fn limited(
        &self,
        target: Target,
        req: reqwest::RequestBuilder,
        max: usize,
        within: Duration,
        whole: bool,
    ) -> Result<Fetched, String> {
        let req = self.request(req)?;
        let url = req.url().to_string();
        let _turn = turn(&self.permits, self.turn_wait).await?;
        let got = async {
            let res = crate::ops::execute(target, &self.client, req)
                .await
                .map_err(|e| format!("{url}: {e}"))?;
            if whole {
                read(res, max, &url).await
            } else {
                read_head(res, max, &url).await
            }
        };
        tokio::time::timeout(within, got)
            .await
            .unwrap_or_else(|_| Err(format!("{url} sent no whole answer within {within:?}")))
    }

    /// A `did:web`'s document (`spec/PROXY.md`, `spec/SPACES-CREDENTIALS.md`,
    /// "Issuers' keys"): from its host, through this guard, in the pool kept
    /// for `did:web` documents, at most `DID_DOC_MAX` bytes, within `WAIT`.
    /// A redirect is as no answer; any other status is the caller's to judge.
    pub async fn did_web(&self, did: &str) -> Result<Fetched, String> {
        let url = did_web_url(did, self.protect)
            .ok_or_else(|| format!("{did} is no did:web the server reads"))?;
        let _turn = turn(&self.did_web, self.turn_wait).await?;
        let got = self
            .fetch_limited(
                Target::DidWeb,
                self.client
                    .get(url)
                    .header(reqwest::header::ACCEPT, "application/json"),
                DID_DOC_MAX,
                WAIT,
            )
            .await?;
        if (300..400).contains(&got.status) {
            return Err(format!("{did}'s document is a redirect"));
        }
        Ok(got)
    }

    /// Send a request and wait for its answer's status, within `within`; at
    /// most `OTHER_MAX` bytes of its body are read and thrown away.
    pub async fn send(
        &self,
        target: Target,
        req: reqwest::RequestBuilder,
        within: Duration,
    ) -> Result<u16, String> {
        self.fetch_limited(target, req, OTHER_MAX, within)
            .await
            .map(|got| got.status)
    }

    /// A request built, if it is to a URL the server sends to.
    fn request(&self, req: reqwest::RequestBuilder) -> Result<reqwest::Request, String> {
        let req = req.build().map_err(|e| format!("a request: {e}"))?;
        if !self.allows(req.url()) {
            return Err(format!("{} is not a URL the server sends to", req.url()));
        }
        Ok(req)
    }
}

/// An answer's status, headers and body, the body up to `max` bytes: a
/// longer one, by its `Content-Length` or as it comes, is none, and the rest
/// is not read.
async fn read(mut res: reqwest::Response, max: usize, url: &str) -> Result<Fetched, String> {
    let over = || format!("{url} answered over {max} bytes");
    if res.content_length().is_some_and(|n| n > max as u64) {
        return Err(over());
    }
    let (status, headers) = (res.status().as_u16(), res.headers().clone());
    let mut body = Vec::new();
    while let Some(chunk) = res.chunk().await.map_err(|e| format!("{url}: {e}"))? {
        if body.len() + chunk.len() > max {
            return Err(over());
        }
        body.extend_from_slice(&chunk);
    }
    Ok(Fetched {
        status,
        headers,
        body: body.into(),
    })
}

/// An answer's status, headers and the first `max` bytes of its body; the
/// rest is not read.
async fn read_head(mut res: reqwest::Response, max: usize, url: &str) -> Result<Fetched, String> {
    let (status, headers) = (res.status().as_u16(), res.headers().clone());
    let mut body = Vec::new();
    while body.len() < max {
        let Some(chunk) = res.chunk().await.map_err(|e| format!("{url}: {e}"))? else {
            break;
        };
        let room = max - body.len();
        body.extend_from_slice(&chunk[..chunk.len().min(room)]);
    }
    Ok(Fetched {
        status,
        headers,
        body: body.into(),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::response::IntoResponse;
    use serde_json::json;
    use std::sync::atomic::{AtomicUsize, Ordering};

    #[test]
    fn the_variable() {
        for (v, want) in [
            ("", Ok(true)),
            ("false", Ok(true)),
            ("0", Ok(true)),
            ("true", Ok(false)),
            ("1", Ok(false)),
        ] {
            assert_eq!(protection(v), want, "{v:?}");
        }
        for v in ["yes", "TRUE", "False", "2", " true", "off"] {
            assert!(protection(v).is_err(), "{v:?}");
        }
    }

    #[test]
    fn public_addresses() {
        for ip in [
            "8.8.8.8",
            "1.1.1.1",
            "1.2.3.4",
            "2606:4700:4700::1111",
            "::ffff:8.8.8.8",
            "64:ff9b::808:808",
            "2002:808:808::1",
        ] {
            assert!(is_public(ip.parse().unwrap()), "{ip}");
        }
        for ip in [
            "127.0.0.1",
            "10.1.2.3",
            "172.16.0.1",
            "172.31.255.255",
            "192.168.1.1",
            "169.254.169.254",
            "0.0.0.0",
            "224.0.0.1",
            "255.255.255.255",
            "100.64.0.1",
            "240.0.0.1",
            "192.0.2.1",
            "198.18.0.1",
            "198.51.100.7",
            "203.0.113.9",
            "::1",
            "::",
            "fe80::1",
            "fc00::1",
            "fd12::1",
            "ff02::1",
            "2001:db8::1",
            "2001::1",
            "::ffff:127.0.0.1",
            "::ffff:10.0.0.1",
            "64:ff9b::7f00:1",
            "2002:7f00:1::1",
            "::127.0.0.1",
        ] {
            assert!(!is_public(ip.parse().unwrap()), "{ip}");
        }
    }

    #[test]
    fn addresses() {
        for ok in [
            "93.184.216.34",
            "8.8.8.8",
            "2606:4700::1111",
            "::ffff:8.8.8.8",
        ] {
            assert!(is_public(ok.parse().unwrap()), "{ok}");
        }
        for bad in [
            "127.0.0.1",
            "0.0.0.0",
            "10.1.2.3",
            "172.16.0.1",
            "192.168.1.1",
            "169.254.169.254",
            "100.64.0.1",
            "224.0.0.1",
            "255.255.255.255",
            "240.0.0.1",
            "192.0.2.1",
            "198.18.0.1",
            "::1",
            "::",
            "fc00::1",
            "fd12::1",
            "fe80::1",
            "ff02::1",
            "::ffff:127.0.0.1",
            "::ffff:10.0.0.1",
            "2001:db8::1",
        ] {
            assert!(!is_public(bad.parse().unwrap()), "{bad}");
        }
    }

    #[test]
    fn which_urls() {
        let url = |u: &str| url::Url::parse(u).unwrap();
        let on = Outbound::new(true).unwrap();
        assert!(
            on.allows(&url("https://api.bsky.app/xrpc/x"))
                && on.allows(&url("https://api.bsky.app:8443/"))
        );
        for u in [
            "http://api.bsky.app/",
            "https://127.0.0.1/",
            "https://[::1]/",
            "https://10.0.0.1:443/",
            "ftp://example.com/",
            "https://8.8.8.8/",
            "https://[2606:4700::1111]/",
            "https://localhost/",
            "https://LOCALHOST:8443/",
            "https://a.localhost/",
            "https://localhost./",
        ] {
            assert!(!on.allows(&url(u)), "{u}");
        }
        let off = Outbound::new(false).unwrap();
        assert!(
            off.allows(&url("http://127.0.0.1:2583/"))
                && off.allows(&url("https://[::1]/"))
                && !off.allows(&url("ftp://example.com/"))
        );
    }

    /// With protection, a name that resolves to this machine is not sent to:
    /// the check is the resolver's, not only the URL's.
    #[tokio::test]
    async fn names_of_this_machine_are_refused() {
        let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0))
            .await
            .unwrap();
        let port = listener.local_addr().unwrap().port();
        tokio::spawn(async move {
            let app = axum::Router::new().route("/", axum::routing::get(|| async { "here" }));
            axum::serve(listener, app).await.unwrap()
        });
        let on = Outbound::new(true).unwrap();
        assert!(
            on.fetch(
                Target::Service,
                on.client().get(format!("https://localhost:{port}/")),
                OTHER_MAX
            )
            .await
            .is_err()
        );
        assert!(
            on.fetch(
                Target::Service,
                on.client().get(format!("http://localhost:{port}/")),
                OTHER_MAX
            )
            .await
            .is_err()
        );
        let off = Outbound::new(false).unwrap();
        let got = off
            .fetch(
                Target::Service,
                off.client().get(format!("http://127.0.0.1:{port}/")),
                OTHER_MAX,
            )
            .await
            .unwrap();
        assert_eq!((got.status, &got.body[..]), (200, &b"here"[..]));
    }

    #[test]
    fn urls() {
        let (on, off) = (Outbound::new(true).unwrap(), Outbound::new(false).unwrap());
        // `url`, parsed, if the guard allows it.
        trait Permits {
            fn permits(&self, url: &str) -> Option<url::Url>;
        }
        impl Permits for Outbound {
            fn permits(&self, url: &str) -> Option<url::Url> {
                url::Url::parse(url).ok().filter(|u| self.allows(u))
            }
        }
        for url in ["https://example.com/x", "https://example.com:8443/"] {
            assert!(
                on.permits(url).is_some() && off.permits(url).is_some(),
                "{url}"
            );
        }
        for url in [
            "http://example.com/",
            "https://127.0.0.1/",
            "https://[::1]:8080/",
            "https://10.0.0.1/",
            "http://localhost:2583/",
            "https://8.8.8.8/",
        ] {
            assert!(on.permits(url).is_none(), "{url}");
        }
        for url in ["http://localhost:2583/", "http://127.0.0.1/"] {
            assert!(off.permits(url).is_some(), "{url}");
        }
        for url in ["ftp://example.com/", "not a url", "file:///etc/passwd"] {
            assert!(
                on.permits(url).is_none() && off.permits(url).is_none(),
                "{url}"
            );
        }
    }

    /// With protection, a name that resolves to loopback is refused as the
    /// request connects; without, it is reached.
    #[tokio::test]
    async fn names_are_checked_as_they_resolve() {
        let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0))
            .await
            .unwrap();
        let port = listener.local_addr().unwrap().port();
        let app = axum::Router::new().route("/", axum::routing::get(|| async { "ok" }));
        tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
        let off = Outbound::new(false).unwrap();
        assert_eq!(
            off.client()
                .get(format!("http://localhost:{port}/"))
                .send()
                .await
                .unwrap()
                .status(),
            200
        );
        let resolved =
            reqwest::dns::Resolve::resolve(&PublicOnly, "localhost".parse().unwrap()).await;
        assert!(resolved.is_err());
        let on = Outbound::new(true).unwrap();
        assert!(
            on.client()
                .get(format!("https://localhost:{port}/"))
                .send()
                .await
                .is_err()
        );
    }

    #[test]
    fn did_web_urls() {
        let at = |did: &str, protect: bool| did_web_url(did, protect).map(|u| u.to_string());
        assert_eq!(
            at("did:web:example.com", true).as_deref(),
            Some("https://example.com/.well-known/did.json")
        );
        assert_eq!(
            at("did:web:Example.com%3A8443", true).as_deref(),
            Some("https://example.com:8443/.well-known/did.json")
        );
        assert_eq!(
            at("did:web:localhost%3A2583", false).as_deref(),
            Some("http://localhost:2583/.well-known/did.json")
        );
        assert_eq!(
            at("did:web:localhost%3A2583", true).as_deref(),
            Some("https://localhost:2583/.well-known/did.json")
        );
        for bad in [
            "did:web:example.com:a",
            "did:web:",
            "did:web:a..b",
            "did:web:a%3Ax",
            "did:plc:abc",
            "did:web:a/b",
        ] {
            assert_eq!(at(bad, false), None, "{bad}");
        }
    }

    #[test]
    fn endpoints_by_their_origin() {
        let o = |e: Json| origin(&e).map(|u| u.to_string());
        assert_eq!(
            o(json!("https://app.example/base?x=")).as_deref(),
            Some("https://app.example/")
        );
        assert_eq!(
            o(json!("https://app.example:8443/a/b#frag")).as_deref(),
            Some("https://app.example:8443/")
        );
        assert_eq!(
            o(json!("http://localhost:2583")).as_deref(),
            Some("http://localhost:2583/")
        );
        for bad in [
            json!("https://user:pw@app.example/"),
            json!("https://user@app.example/"),
            json!("ftp://app.example/"),
            json!("app.example"),
            json!({ "a": 1 }),
            json!("https://"),
        ] {
            assert_eq!(o(bad.clone()), None, "{bad}");
        }
        let at = origin(&json!("https://syncer.example/base?x="))
            .unwrap()
            .join("/xrpc/com.atproto.space.notifySpaceUpdate")
            .unwrap();
        assert_eq!(
            at.as_str(),
            "https://syncer.example/xrpc/com.atproto.space.notifySpaceUpdate"
        );
    }

    /// A listener on 127.0.0.1 answering every request with `answer`, after
    /// `delay`, counting the requests it has at once and at most.
    async fn serving(
        answer: fn(&str) -> axum::response::Response,
        delay: Duration,
    ) -> (u16, Arc<(AtomicUsize, AtomicUsize, AtomicUsize)>) {
        let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0))
            .await
            .unwrap();
        let port = listener.local_addr().unwrap().port();
        let counts = Arc::new((
            AtomicUsize::new(0),
            AtomicUsize::new(0),
            AtomicUsize::new(0),
        ));
        let c = counts.clone();
        let app = axum::Router::new().fallback(move |uri: axum::http::Uri| {
            let c = c.clone();
            async move {
                c.2.fetch_add(1, Ordering::SeqCst);
                let now = c.0.fetch_add(1, Ordering::SeqCst) + 1;
                c.1.fetch_max(now, Ordering::SeqCst);
                tokio::time::sleep(delay).await;
                c.0.fetch_sub(1, Ordering::SeqCst);
                answer(uri.path())
            }
        });
        tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
        (port, counts)
    }

    /// The 256 slots of requests out, and the 10 s wait for one: with every
    /// slot taken, a request is given up once it has waited its time, and
    /// sent once one is free.
    #[tokio::test]
    async fn requests_out_wait_for_a_slot() {
        let real = Outbound::new(false).unwrap();
        assert_eq!(
            (
                real.permits.available_permits(),
                real.did_web.available_permits(),
                real.turn_wait
            ),
            (256, 16, Duration::from_secs(10))
        );
        let (port, counts) = serving(|_| "ok".into_response(), Duration::ZERO).await;
        let o = Outbound::with(false, 4, 2, Duration::from_millis(300)).unwrap();
        let url = format!("http://127.0.0.1:{port}/");
        let held = o.permits.acquire_many(4).await.unwrap();
        let started = std::time::Instant::now();
        assert!(
            o.fetch(Target::Service, o.client().get(&url), OTHER_MAX)
                .await
                .is_err()
        );
        assert!(
            o.send(Target::Service, o.client().get(&url), WAIT)
                .await
                .is_err()
        );
        assert!(
            o.did_web(&format!("did:web:localhost%3A{port}"))
                .await
                .is_err()
        );
        assert!(started.elapsed() >= Duration::from_millis(900));
        assert_eq!(counts.2.load(Ordering::SeqCst), 0, "nothing was sent");
        drop(held);
        assert_eq!(
            o.fetch(Target::Service, o.client().get(&url), OTHER_MAX)
                .await
                .unwrap()
                .status,
            200
        );
        // At most so many at once: the rest wait their turn.
        let (port, counts) = serving(|_| "ok".into_response(), Duration::from_millis(200)).await;
        let o = Arc::new(Outbound::with(false, 4, 2, Duration::from_secs(10)).unwrap());
        let url = format!("http://127.0.0.1:{port}/");
        let all: Vec<_> = (0..12)
            .map(|_| {
                let (o, url) = (o.clone(), url.clone());
                tokio::spawn(async move {
                    o.fetch(Target::Service, o.client().get(&url), OTHER_MAX)
                        .await
                        .unwrap()
                        .status
                })
            })
            .collect();
        for t in all {
            assert_eq!(t.await.unwrap(), 200);
        }
        assert_eq!(counts.1.load(Ordering::SeqCst), 4);
    }

    /// `did:web` documents: at most 16 fetched at once, in a pool of their
    /// own (the directory's slots are `crate::plc`'s, which `did_web` never
    /// takes); a redirect is not followed, and a document past 65536 bytes
    /// is none.
    #[tokio::test]
    async fn did_web_documents() {
        fn doc(path: &str) -> axum::response::Response {
            match path {
                "/.well-known/did.json" => axum::Json(json!({ "id": "did:web:x" })).into_response(),
                _ => axum::http::StatusCode::NOT_FOUND.into_response(),
            }
        }
        let (port, counts) = serving(doc, Duration::from_millis(300)).await;
        let o = Arc::new(Outbound::new(false).unwrap());
        let did = format!("did:web:localhost%3A{port}");
        let all: Vec<_> = (0..40)
            .map(|_| {
                let (o, did) = (o.clone(), did.clone());
                tokio::spawn(async move { o.did_web(&did).await.unwrap().status })
            })
            .collect();
        for t in all {
            assert_eq!(t.await.unwrap(), 200);
        }
        assert_eq!(counts.1.load(Ordering::SeqCst), DID_WEB_AT_ONCE);
        // A redirect is as no answer, its target never asked; a document past the size is none.
        fn odd(path: &str) -> axum::response::Response {
            match path {
                "/.well-known/did.json" => {
                    axum::response::Redirect::temporary("/elsewhere").into_response()
                }
                _ => "x".repeat(DID_DOC_MAX + 1).into_response(),
            }
        }
        let (port, counts) = serving(odd, Duration::ZERO).await;
        assert!(
            o.did_web(&format!("did:web:localhost%3A{port}"))
                .await
                .is_err()
        );
        assert_eq!(counts.2.load(Ordering::SeqCst), 1);
        let long = o
            .fetch_limited(
                Target::DidWeb,
                o.client().get(format!("http://127.0.0.1:{port}/big")),
                DID_DOC_MAX,
                WAIT,
            )
            .await;
        assert!(long.is_err());
        assert_eq!(
            o.fetch_limited(
                Target::DidWeb,
                o.client().get(format!("http://127.0.0.1:{port}/big")),
                DID_DOC_MAX + 1,
                WAIT
            )
            .await
            .unwrap()
            .body
            .len(),
            DID_DOC_MAX + 1
        );
        // With protection, a did:web on localhost or an IP address is never fetched.
        let on = Outbound::new(true).unwrap();
        for did in [
            format!("did:web:localhost%3A{port}"),
            format!("did:web:127.0.0.1%3A{port}"),
        ] {
            assert!(on.did_web(&did).await.is_err(), "{did}");
        }
        assert_eq!(counts.2.load(Ordering::SeqCst), 3);
    }
}