~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
390 lines · 13.9 KB Raw History
//! The web app's pages, handed to a fake app on a port of its own, offline:
//! streamed, compressed, their headers forwarded, and HUD's own routes
//! kept HUD's.

use axum::body::{Body, Bytes};
use axum::extract::{Request, State};
use axum::http::{HeaderMap, StatusCode, header};
use axum::response::{IntoResponse, Response};
use futures::StreamExt;
use hud_core::net::Web;
use hud_core::{Config, Hud};
use std::convert::Infallible;
use std::io::{Read, Write};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::sync::Semaphore;
use tower::ServiceExt;

/// What the fake app was asked.
struct Asked {
    method: String,
    path: String,
    headers: HeaderMap,
    body: Bytes,
}

/// The fake app: what it was asked, and the gate a streamed page waits at
/// between its two parts.
struct App {
    asked: Mutex<Vec<Asked>>,
    gate: Semaphore,
}

impl App {
    /// The paths it was asked for, in turn.
    fn paths(&self) -> Vec<String> {
        let asked = self.asked.lock().unwrap();
        asked.iter().map(|a| a.path.clone()).collect()
    }
}

const FIRST: &[u8] = b"<!doctype html><title>Gallery</title><p>the page's head, drawn first";
const REST: &[u8] = b"<p>and its photos, once they're read</p>";

/// A page long enough to compress.
fn page() -> String {
    format!("<!doctype html>{}", "<p>a photo</p>".repeat(200))
}

/// `page()`, gzipped, as the app would send a file it keeps compressed.
fn gzipped() -> Vec<u8> {
    let mut gz = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::fast());
    gz.write_all(page().as_bytes()).unwrap();
    gz.finish().unwrap()
}

async fn answer(State(app): State<Arc<App>>, req: Request) -> Response {
    let (parts, body) = req.into_parts();
    let body = axum::body::to_bytes(body, usize::MAX).await.unwrap();
    let path = parts.uri.path().to_string();
    app.asked.lock().unwrap().push(Asked {
        method: parts.method.to_string(),
        path: path.clone(),
        headers: parts.headers,
        body,
    });
    match path.as_str() {
        "/stream" | "/events" => {
            let kind = match path.as_str() {
                "/events" => "text/event-stream",
                _ => "text/html",
            };
            let first = futures::stream::once(async { Ok::<_, Infallible>(Bytes::from(FIRST)) });
            let rest = futures::stream::once(async move {
                app.gate.acquire().await.unwrap().forget();
                Ok(Bytes::from(REST))
            });
            let body = Body::from_stream(first.chain(rest));
            ([(header::CONTENT_TYPE, kind)], body).into_response()
        }
        "/page" => ([(header::CONTENT_TYPE, "text/html")], page()).into_response(),
        "/gz" => (
            [
                (header::CONTENT_TYPE, "text/html"),
                (header::CONTENT_ENCODING, "gzip"),
            ],
            gzipped(),
        )
            .into_response(),
        "/form" => (
            StatusCode::SEE_OTHER,
            [
                (header::LOCATION, "/elsewhere"),
                (header::SET_COOKIE, "session=two; HttpOnly"),
            ],
        )
            .into_response(),
        _ => format!("page {path}").into_response(),
    }
}

/// The fake app, served: its origin, and what it was asked.
async fn app() -> (String, Arc<App>) {
    let app = Arc::new(App {
        asked: Mutex::new(Vec::new()),
        gate: Semaphore::new(0),
    });
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let origin = format!("http://{}", listener.local_addr().unwrap());
    let routes = axum::Router::new().fallback(answer).with_state(app.clone());
    tokio::spawn(async move { axum::serve(listener, routes).await.unwrap() });
    (origin, app)
}

/// HUD at `https://hud.example`, sign-in on or off.
fn hud(oauth: bool) -> Arc<Hud> {
    let dir = std::env::temp_dir().join(format!("hud-web-{}", atproto::jose::random(6)));
    Hud::new(Config {
        port: 0,
        bind: "::".parse().unwrap(),
        public_origin: "https://hud.example".into(),
        cache_dir: dir.join("cache"),
        data_dir: dir,
        blob_cache_bytes: 0,
        cache_memory_bytes: 1 << 20,
        oauth,
        oauth_scope: hud_core::scope::asked(hud_core::view::views(), true, false),
        oauth_include: None,
        workspace: true,
        spaces: false,
        plc_url: "http://127.0.0.1:9".into(),
        handle_resolver: None,
        allow_private: false,
        backlinks: hud_core::backlinks::Backlinks::None,
        favicons: None,
        image_cdn: None,
        relay: None,
        sign_up_pds: hud_core::config::SIGN_UP_PDS.into(),
        client: Default::default(),
        operators: Vec::new(),
        logs: false,
        logs_keep: hud_core::logs::KEEP,
    })
    .unwrap()
}

/// HUD, sign-in on, served with the app at `web` on a port of its own, as
/// `hud serve` serves it: its origin.
async fn served(web: &str) -> String {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let origin = format!("http://{}", listener.local_addr().unwrap());
    let web = Some(Web::new(web.to_string()));
    let stop = std::future::pending();
    tokio::spawn(crate::serve(listener, hud(true), web, stop));
    origin
}

/// A client as a browser is, but following no redirect and decoding
/// nothing, so a test reads what HUD sent.
fn browser() -> reqwest::Client {
    reqwest::Client::builder()
        .redirect(reqwest::redirect::Policy::none())
        .no_gzip()
        .no_brotli()
        .build()
        .unwrap()
}

/// `bytes` decoded as `encoding` says.
fn decoded(encoding: Option<&str>, bytes: &[u8]) -> Vec<u8> {
    let mut out = Vec::new();
    match encoding {
        Some("gzip") => flate2::read::GzDecoder::new(bytes).read_to_end(&mut out),
        Some("br") => brotli::Decompressor::new(bytes, 4096).read_to_end(&mut out),
        _ => return bytes.to_vec(),
    }
    .unwrap();
    out
}

/// A page the app streams reaches the browser part by part: its first part
/// before the app has sent the rest, compressed or not, and an event
/// stream as well.
#[tokio::test]
async fn pages_stream_as_the_app_draws_them() {
    let (web, app) = app().await;
    let hud = served(&web).await;
    let cases = [
        ("/stream", None),
        ("/stream", Some("br")),
        ("/stream", Some("gzip")),
        ("/events", Some("br")),
    ];
    for (path, encoding) in cases {
        let mut asked = browser().get(format!("{hud}{path}"));
        if let Some(e) = encoding {
            asked = asked.header(header::ACCEPT_ENCODING, e);
        }
        let mut res = asked.send().await.unwrap();
        assert_eq!(res.status(), StatusCode::OK);
        // The event stream is never compressed.
        let encoded = match path {
            "/events" => None,
            _ => encoding,
        };
        let got = res.headers().get(header::CONTENT_ENCODING);
        assert_eq!(
            got.map(|v| v.to_str().unwrap()),
            encoded,
            "{path} {encoding:?}"
        );
        // The app waits at its gate until this part has come.
        let wait = Duration::from_secs(5);
        let first = tokio::time::timeout(wait, res.chunk()).await;
        let first = first.expect("the first part comes before the rest is sent");
        let mut body = first.unwrap().unwrap().to_vec();
        assert!(!body.is_empty());
        app.gate.add_permits(1);
        while let Some(part) = res.chunk().await.unwrap() {
            body.extend_from_slice(&part);
        }
        assert_eq!(
            decoded(encoded, &body),
            [FIRST, REST].concat(),
            "{path} {encoding:?}"
        );
    }
}

/// The app's answers are compressed as the browser takes them, Brotli or
/// gzip, or not at all; what the app compressed itself goes on as it is.
#[tokio::test]
async fn pages_compressed_as_the_browser_takes_them() {
    let (web, _) = app().await;
    let hud = served(&web).await;
    for encoding in ["br", "gzip", "identity"] {
        let res = browser()
            .get(format!("{hud}/page"))
            .header(header::ACCEPT_ENCODING, encoding)
            .send()
            .await
            .unwrap();
        let got = res.headers().get(header::CONTENT_ENCODING);
        let got = got.map(|v| v.to_str().unwrap().to_string());
        let expected = (encoding != "identity").then(|| encoding.to_string());
        assert_eq!(got, expected);
        let body = res.bytes().await.unwrap();
        assert_eq!(decoded(got.as_deref(), &body), page().as_bytes());
    }
    let res = browser()
        .get(format!("{hud}/gz"))
        .header(header::ACCEPT_ENCODING, "br, gzip")
        .send()
        .await
        .unwrap();
    assert_eq!(res.headers()[header::CONTENT_ENCODING], "gzip");
    assert_eq!(res.bytes().await.unwrap(), gzipped());
}

/// The app is told the host, scheme and client the browser asked as,
/// gets its cookies and body, and its redirect and cookies go back to the
/// browser as they are, the redirect not followed.
#[tokio::test]
async fn pages_asked_as_the_browser_asked() {
    let (web, app) = app().await;
    let hud = served(&web).await;
    let res = browser()
        .post(format!("{hud}/form?next=1"))
        .header(header::HOST, "grain.example")
        .header(header::COOKIE, "session=one")
        .header(header::CONTENT_TYPE, "application/x-www-form-urlencoded")
        .body("title=Fog")
        .send()
        .await
        .unwrap();
    assert_eq!(res.status(), StatusCode::SEE_OTHER);
    assert_eq!(res.headers()[header::LOCATION], "/elsewhere");
    assert_eq!(res.headers()[header::SET_COOKIE], "session=two; HttpOnly");
    {
        let asked = app.asked.lock().unwrap();
        let [form] = &asked[..] else {
            panic!("asked once, the redirect not followed: {:?}", app.paths());
        };
        assert_eq!(form.method, "POST");
        assert_eq!(&form.body[..], b"title=Fog");
        let h = &form.headers;
        assert_eq!(h[header::HOST], "grain.example");
        assert_eq!(h["x-forwarded-host"], "grain.example");
        // HUD's own scheme, where no proxy in front of it said one.
        assert_eq!(h["x-forwarded-proto"], "https");
        assert_eq!(h["x-forwarded-for"], "127.0.0.1");
        assert_eq!(h[header::COOKIE], "session=one");
    }
    // Behind a proxy: its scheme, and the client it named first.
    browser()
        .get(format!("{hud}/profile/x"))
        .header("x-forwarded-proto", "http")
        .header("x-forwarded-for", "203.0.113.7")
        .send()
        .await
        .unwrap();
    let asked = app.asked.lock().unwrap();
    let h = &asked[1].headers;
    assert_eq!(h["x-forwarded-proto"], "http");
    assert_eq!(h["x-forwarded-for"], "203.0.113.7, 127.0.0.1");
    assert!(h.get(header::CONTENT_LENGTH).is_none());
}

/// HUD's routes stay HUD's, and the paths under them it has no route
/// for; `/`, the icons and every other page are the app's, logged as
/// `web`.
#[tokio::test]
async fn pages_are_what_hud_does_not_answer() {
    let (web, app) = app().await;
    let hud = hud(true);
    let router = crate::router(hud.clone(), Some(Web::new(web)));
    let get = |path: &str| {
        let req = Request::get(path).body(Body::empty()).unwrap();
        let router = router.clone();
        async move {
            let res = router.oneshot(req).await.unwrap();
            let status = res.status();
            let body = axum::body::to_bytes(res.into_body(), usize::MAX).await;
            (status, String::from_utf8(body.unwrap().to_vec()).unwrap())
        }
    };
    assert_eq!(get("/_health").await, (StatusCode::OK, "ok".into()));
    let (status, _) = get("/.well-known/oauth-authorization-server").await;
    assert_eq!(status, StatusCode::OK);
    for own in [
        "/mcp",
        "/xrpc/place.hud.nothing",
        "/oauth/nowhere",
        "/workspace/x",
    ] {
        let (status, body) = get(own).await;
        assert!(!body.starts_with("page "), "{own}: {status} {body}");
    }
    assert!(app.paths().is_empty(), "{:?}", app.paths());
    let pages = [
        "/",
        "/favicon.svg",
        "/favicon.ico",
        "/profile/x/gallery/y",
        "/_app/immutable/x.js",
    ];
    for path in pages {
        assert_eq!(get(path).await, (StatusCode::OK, format!("page {path}")));
    }
    assert_eq!(app.paths(), pages);
    // Asked with no host and from no address: none forwarded.
    let asked = app.asked.lock().unwrap();
    assert!(asked[0].headers.get("x-forwarded-host").is_none());
    assert!(asked[0].headers.get("x-forwarded-for").is_none());
    let text = hud_core::telemetry::metrics(&hud.lookups());
    assert!(text.contains("http_route=\"web\""), "{text}");
}

/// An app that doesn't answer is a 502, saying so.
#[tokio::test]
async fn pages_of_an_app_that_is_down() {
    let closed = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let web = format!("http://{}", closed.local_addr().unwrap());
    drop(closed);
    let router = crate::router(hud(false), Some(Web::new(web)));
    let req = Request::get("/").body(Body::empty()).unwrap();
    let res = router.oneshot(req).await.unwrap();
    assert_eq!(res.status(), StatusCode::BAD_GATEWAY);
    let body = axum::body::to_bytes(res.into_body(), usize::MAX).await;
    assert_eq!(&body.unwrap()[..], b"The pages can't be reached now.\n");
}

/// Without the app, `/` is the workspace's with sign-in on, nothing with
/// it off, and the icon HUD's.
#[tokio::test]
async fn without_the_app_home_is_the_workspace() {
    let ask = |oauth: bool, path: &str| {
        let req = Request::get(path).body(Body::empty()).unwrap();
        crate::router(hud(oauth), None).oneshot(req)
    };
    let res = ask(true, "/").await.unwrap();
    assert_eq!(res.status(), StatusCode::SEE_OTHER);
    assert_eq!(res.headers()[header::LOCATION], "/workspace");
    assert_eq!(
        ask(false, "/").await.unwrap().status(),
        StatusCode::NOT_FOUND
    );
    let res = ask(false, "/favicon.svg").await.unwrap();
    assert_eq!(res.headers()[header::CONTENT_TYPE], "image/svg+xml");
}