[relay] Join page: GET /join hands an invite link opened in a browser to the app
ci / check (pull_request) Has been cancelled
ci / check (pull_request) Has been cancelled
A static page for https://<relay>/join#<invite id>.<secret>, the link messengers make tappable. The fragment never reaches the relay. One button opens amicitia://invite?relay=<origin>#…; its only script and style are named by SHA-256 in a Content-Security-Policy of default-src 'none'; no-store, no-referrer, no cookie, framing refused. PROTOCOL.md §8 lists the route.
This commit is contained in:
@@ -741,3 +741,98 @@ async fn no_random_request_breaks_the_relay() {
|
||||
assert_eq!(status, StatusCode::NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
/// `GET /join` (PROTOCOL.md §8, 2026-11-18): a static page that hands the fragment to the app. It
|
||||
/// must load nothing from elsewhere, run nothing but its own script, and keep nothing.
|
||||
#[tokio::test]
|
||||
async fn the_join_page_is_static_and_loads_nothing_from_elsewhere() {
|
||||
use tower::ServiceExt;
|
||||
let relay = Relay::in_memory();
|
||||
let response = relay
|
||||
.app
|
||||
.clone()
|
||||
.oneshot(Request::get("/join").body(Body::empty()).expect("request"))
|
||||
.await
|
||||
.expect("response");
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let h = response.headers().clone();
|
||||
let get = |name: &str| {
|
||||
h.get(name)
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.unwrap_or("")
|
||||
.to_string()
|
||||
};
|
||||
assert_eq!(get("content-type"), "text/html; charset=utf-8");
|
||||
assert_eq!(get("cache-control"), "no-store");
|
||||
assert_eq!(get("referrer-policy"), "no-referrer");
|
||||
assert_eq!(get("x-content-type-options"), "nosniff");
|
||||
assert!(h.get("set-cookie").is_none());
|
||||
let csp = get("content-security-policy");
|
||||
assert!(
|
||||
csp.starts_with("default-src 'none'; script-src 'sha256-"),
|
||||
"{csp}"
|
||||
);
|
||||
for part in [
|
||||
"base-uri 'none'",
|
||||
"form-action 'none'",
|
||||
"frame-ancestors 'none'",
|
||||
] {
|
||||
assert!(csp.contains(part), "{csp} lacks {part}");
|
||||
}
|
||||
let body = String::from_utf8(
|
||||
response
|
||||
.into_body()
|
||||
.collect()
|
||||
.await
|
||||
.expect("body")
|
||||
.to_bytes()
|
||||
.to_vec(),
|
||||
)
|
||||
.expect("utf-8");
|
||||
// Nothing loaded from anywhere, and nothing the script could send the fragment with or leave
|
||||
// the page by on its own (a custom scheme must wait for the person's tap).
|
||||
for banned in [
|
||||
"http://",
|
||||
"https://",
|
||||
" src=",
|
||||
"<link",
|
||||
"<img",
|
||||
"<iframe",
|
||||
"@import",
|
||||
"url(",
|
||||
"fetch(",
|
||||
"XMLHttpRequest",
|
||||
"sendBeacon",
|
||||
"WebSocket",
|
||||
"location.href",
|
||||
"location.assign",
|
||||
"location.replace",
|
||||
"window.open",
|
||||
] {
|
||||
assert!(!body.contains(banned), "the page contains {banned:?}");
|
||||
}
|
||||
// One script and one style, both named by the policy's hashes; the invite goes to the app.
|
||||
assert_eq!(body.matches("<script>").count(), 1);
|
||||
assert_eq!(body.matches("<style>").count(), 1);
|
||||
assert!(body.contains("\"amicitia://invite?relay=\" + location.origin + \"#\" + invite"));
|
||||
assert!(body.contains("location.hash"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_join_page_policy_hashes_its_own_script_and_style() {
|
||||
use base64::Engine;
|
||||
use sha2::{Digest, Sha256};
|
||||
let page = tabellarius_relay::join::PAGE;
|
||||
let between = |open: &str, close: &str| {
|
||||
let s = page.find(open).expect("the tag opens") + open.len();
|
||||
&page[s..page[s..].find(close).expect("the tag closes") + s]
|
||||
};
|
||||
let csp = tabellarius_relay::join::CONTENT_SECURITY_POLICY.as_str();
|
||||
for text in [
|
||||
between("<script>", "</script>"),
|
||||
between("<style>", "</style>"),
|
||||
] {
|
||||
let b64 = base64::engine::general_purpose::STANDARD.encode(Sha256::digest(text.as_bytes()));
|
||||
assert!(csp.contains(&format!("'sha256-{b64}'")), "{csp}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user