//! 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> { 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#" {title} ยท HUD
{body}
"# ); ( 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!("

{heading}

")), "{path}"); assert!(!html.contains("