~ 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
143 lines · 5.2 KB Raw History
//! HUD's terms, privacy policy and community guidelines: `GET /terms`,
//! `GET /privacy` and `GET /guidelines`, each an HTML fragment of its own
//! (`policies/*.html`) in one page shell, with no script, served with
//! sign-in on or off.

use axum::Router;
use axum::http::{StatusCode, header};
use axum::response::{IntoResponse, Response};
use axum::routing::get;
use hud_core::Hud;
use std::sync::Arc;

/// The pages' CSP: their own inline style, nothing else.
const CSP: &str = "default-src 'none'; style-src 'unsafe-inline'; frame-ancestors 'none'";

/// The policies' routes.
pub fn routes() -> Router<Arc<Hud>> {
    Router::new()
        .route(
            "/terms",
            get(|| async { page("Terms of service", include_str!("policies/terms.html")) }),
        )
        .route(
            "/privacy",
            get(|| async { page("Privacy policy", include_str!("policies/privacy.html")) }),
        )
        .route(
            "/guidelines",
            get(|| async {
                page(
                    "Community guidelines",
                    include_str!("policies/guidelines.html"),
                )
            }),
        )
}

/// A policy's page: `body`, its HTML, in a readable column, light or dark
/// as the reader's system is, linking to the others; kept an hour.
fn page(title: &str, body: &str) -> Response {
    let html = format!(
        r#"<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{title} · HUD</title>
<style>
  :root {{ color-scheme: light dark; font-family: system-ui, sans-serif; }}
  body {{ margin: 0; background: Canvas; color: CanvasText; }}
  main {{ max-width: 42rem; margin: 0 auto; padding: 2rem 1rem 4rem; line-height: 1.55; }}
  nav {{ display: flex; gap: 1rem; font-size: .9rem; margin-bottom: 2rem; }}
  h1 {{ font-size: 1.6rem; margin: 0 0 .4rem; }}
  h2 {{ font-size: 1.15rem; margin: 2rem 0 .5rem; }}
  .updated {{ opacity: .7; font-size: .9rem; margin: 0 0 1.5rem; }}
  p, ul {{ margin: 0 0 .9rem; }}
  ul {{ padding-left: 1.3rem; }}
  li {{ margin-bottom: .3rem; }}
  a {{ color: LinkText; text-underline-offset: .15em; }}
</style>
</head>
<body><main>
<nav><a href="/terms">Terms</a><a href="/privacy">Privacy</a><a href="/guidelines">Guidelines</a></nav>
{body}
</main></body>
</html>
"#
    );
    (
        StatusCode::OK,
        [
            (header::CONTENT_TYPE, "text/html; charset=utf-8"),
            (header::CONTENT_SECURITY_POLICY, CSP),
            (header::CACHE_CONTROL, "public, max-age=3600"),
            (header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
        ],
        html,
    )
        .into_response()
}

#[cfg(test)]
mod tests {
    use hud_core::{Config, Hud};
    use tower::ServiceExt;

    #[tokio::test]
    async fn policies_served_signed_in_or_not() {
        for oauth in [false, true] {
            let data_dir = std::env::temp_dir()
                .join(format!("hud-policies-tests-{}", atproto::jose::random(6)));
            let hud = Hud::new(Config {
                port: 0,
                bind: "::".parse().unwrap(),
                public_origin: "https://hud.example".into(),
                cache_dir: data_dir.join("cache"),
                data_dir,
                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,
                blob_cache_bytes: 0,
                cache_memory_bytes: 1 << 24,
                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();
            for (path, heading) in [
                ("/terms", "Terms of service"),
                ("/privacy", "Privacy policy"),
                ("/guidelines", "Community guidelines"),
            ] {
                let req = axum::http::Request::get(path)
                    .body(axum::body::Body::empty())
                    .unwrap();
                let r = crate::router(hud.clone(), None).oneshot(req).await.unwrap();
                assert_eq!(r.status(), 200, "{path}");
                let h = r.headers();
                assert_eq!(h["content-type"], "text/html; charset=utf-8");
                assert_eq!(h["content-security-policy"], super::CSP);
                assert!(super::CSP.starts_with("default-src 'none'; style-src 'unsafe-inline'"));
                let html = axum::body::to_bytes(r.into_body(), usize::MAX)
                    .await
                    .unwrap();
                let html = String::from_utf8(html.to_vec()).unwrap();
                assert!(html.contains(&format!("<h1>{heading}</h1>")), "{path}");
                assert!(!html.contains("<script"), "{path}");
                assert!(html.contains("href=\"/privacy\""), "{path}");
            }
        }
    }
}