use axum::{
Router,
http::{HeaderValue, header},
response::{IntoResponse, Response},
routing::get,
};
const PAGE: &str = include_str!("inspector/index.html");
const SCRIPT: &str = include_str!("inspector/app.js");
const STYLE: &str = include_str!("inspector/style.css");
pub const CSP: &str = "default-src 'none'; script-src 'self'; style-src 'self'; \
connect-src 'self'; img-src 'self' data:; base-uri 'none'; \
form-action 'none'; frame-ancestors 'none'";
pub fn routes<S: Clone + Send + Sync + 'static>(base_path: &str) -> Router<S> {
let page = PAGE.replace("__BASE_PATH__", &escape_attribute(base_path));
Router::new()
.route(
"/_inspect",
get(move || async move { asset("text/html; charset=utf-8", page) }),
)
.route(
"/_inspect/app.js",
get(|| async { asset("text/javascript; charset=utf-8", SCRIPT.to_string()) }),
)
.route(
"/_inspect/style.css",
get(|| async { asset("text/css; charset=utf-8", STYLE.to_string()) }),
)
}
fn asset(content_type: &'static str, body: String) -> Response {
let mut response = body.into_response();
let headers = response.headers_mut();
headers.insert(header::CONTENT_TYPE, HeaderValue::from_static(content_type));
headers.insert(
header::CONTENT_SECURITY_POLICY,
HeaderValue::from_static(CSP),
);
headers.insert(
header::X_CONTENT_TYPE_OPTIONS,
HeaderValue::from_static("nosniff"),
);
headers.insert(
header::REFERRER_POLICY,
HeaderValue::from_static("no-referrer"),
);
headers.insert(header::CACHE_CONTROL, HeaderValue::from_static("no-store"));
response
}
fn escape_attribute(text: &str) -> String {
text.replace('&', "&")
.replace('"', """)
.replace('<', "<")
.replace('>', ">")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_script_never_inserts_markup_or_evaluates_text() {
for sink in [
"innerHTML",
"outerHTML",
"insertAdjacentHTML",
"document.write",
"eval(",
"new Function",
] {
assert!(!SCRIPT.contains(sink), "app.js uses {sink}");
}
assert!(
!PAGE.contains("<script>"),
"the page carries an inline script"
);
let inline_handler = PAGE.match_indices(" on").any(|(i, _)| {
let rest = &PAGE[i + 3..];
let name: String = rest.chars().take_while(char::is_ascii_lowercase).collect();
!name.is_empty() && rest[name.len()..].starts_with('=')
});
assert!(!inline_handler, "the page carries an inline event handler");
}
#[test]
fn credentials_are_read_from_the_fragment_and_cleared() {
assert!(SCRIPT.contains("location.hash"));
assert!(SCRIPT.contains("history.replaceState(null, \"\", location.pathname)"));
assert!(
!SCRIPT.contains("location.search"),
"credentials must never ride in a query"
);
}
#[test]
fn the_policy_forbids_inline_script_foreign_origins_and_framing() {
for directive in [
"default-src 'none'",
"script-src 'self'",
"frame-ancestors 'none'",
] {
assert!(CSP.contains(directive), "{directive}");
}
assert!(!CSP.contains("unsafe-inline") && !CSP.contains("unsafe-eval"));
}
#[test]
fn the_base_path_lands_escaped() {
let router_page = PAGE.replace("__BASE_PATH__", &escape_attribute("/x\"><b>"));
assert!(router_page.contains("/x"><b>"));
}
}