use super::tests::{authed_request, router_and_sessions, test_auth, test_binding};
use super::*;
use axum::body::Body;
use axum::http::{HeaderValue, StatusCode, header};
use tower::util::ServiceExt;
async fn guarded_router() -> AxumRouter {
let (router, sessions, workstreams) = router_and_sessions().await;
build_axum_router(router, sessions, workstreams, test_binding(), test_auth())
}
fn rpc_create_body() -> Body {
Body::from(
serde_json::json!({
"jsonrpc": "2.0",
"id": 1,
"method": "session.create",
"params": {}
})
.to_string(),
)
}
fn rpc_ping_body() -> Body {
Body::from(serde_json::json!({"jsonrpc": "2.0", "id": 1, "method": "ping"}).to_string())
}
#[tokio::test]
async fn rpc_rejects_cross_origin_write() {
let resp = guarded_router()
.await
.oneshot(
authed_request()
.method("POST")
.uri("/rpc")
.header(header::ORIGIN, "http://evil.example.com")
.header(header::CONTENT_TYPE, "application/json")
.body(rpc_create_body())
.expect("request builds"),
)
.await
.expect("router responds");
assert_eq!(
resp.status(),
StatusCode::FORBIDDEN,
"cross-origin POST /rpc must be rejected by the write guard"
);
}
#[tokio::test]
async fn merged_rest_write_route_rejects_cross_origin() {
let resp = guarded_router()
.await
.oneshot(
authed_request()
.method("POST")
.uri("/tasks")
.header(header::ORIGIN, "https://evil.example.com")
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from("{}"))
.expect("request builds"),
)
.await
.expect("router responds");
assert_eq!(
resp.status(),
StatusCode::FORBIDDEN,
"cross-origin POST /tasks must be rejected — the guard is router-wide, \
so it covers routes merged in after the core ones"
);
}
#[tokio::test]
async fn write_with_non_utf8_origin_is_rejected() {
let resp = guarded_router()
.await
.oneshot(
authed_request()
.method("POST")
.uri("/rpc")
.header(
header::ORIGIN,
HeaderValue::from_bytes(&[0xff, 0xfe]).expect("header value"),
)
.header(header::CONTENT_TYPE, "application/json")
.body(rpc_create_body())
.expect("request builds"),
)
.await
.expect("router responds");
assert_eq!(
resp.status(),
StatusCode::FORBIDDEN,
"an unclassifiable Origin must fail closed, not fall through"
);
}
#[tokio::test]
async fn rpc_allows_same_origin_write() {
for origin in ["http://127.0.0.1:7882", "http://localhost:5174"] {
let resp = guarded_router()
.await
.oneshot(
authed_request()
.method("POST")
.uri("/rpc")
.header(header::ORIGIN, origin)
.header(header::CONTENT_TYPE, "application/json")
.body(rpc_ping_body())
.expect("request builds"),
)
.await
.expect("router responds");
assert_eq!(
resp.status(),
StatusCode::OK,
"loopback origin {origin} must pass the write guard"
);
}
}
#[tokio::test]
async fn rpc_allows_cli_shaped_write_with_no_origin() {
let resp = guarded_router()
.await
.oneshot(
authed_request()
.method("POST")
.uri("/rpc")
.header(header::CONTENT_TYPE, "application/json")
.body(rpc_ping_body())
.expect("request builds"),
)
.await
.expect("router responds");
assert_eq!(
resp.status(),
StatusCode::OK,
"a request with no Origin header is not the CSRF threat model and must pass"
);
}
#[tokio::test]
async fn content_reads_are_not_cors_reflected_to_a_foreign_origin() {
for uri in [
"/sessions/does-not-exist/transcript",
"/sessions/does-not-exist/events",
"/workstreams/does-not-exist/events",
] {
let resp = guarded_router()
.await
.oneshot(
authed_request()
.uri(uri)
.header(header::ORIGIN, "https://evil.example.com")
.body(Body::empty())
.expect("request builds"),
)
.await
.expect("router responds");
assert!(
resp.headers()
.get(header::ACCESS_CONTROL_ALLOW_ORIGIN)
.is_none(),
"{uri} must not reflect a foreign origin — a browser page would \
otherwise read the response body"
);
}
}
#[tokio::test]
async fn content_reads_are_cors_reflected_to_a_same_machine_origin() {
for origin in ["http://127.0.0.1:7882", "http://localhost:5174"] {
let resp = guarded_router()
.await
.oneshot(
authed_request()
.uri("/sessions/does-not-exist/transcript")
.header(header::ORIGIN, origin)
.body(Body::empty())
.expect("request builds"),
)
.await
.expect("router responds");
assert_eq!(
resp.headers()
.get(header::ACCESS_CONTROL_ALLOW_ORIGIN)
.map(|v| v.to_str().expect("ascii header")),
Some(origin),
"a same-machine origin must still be reflected"
);
}
}
#[tokio::test]
async fn ip_prefixed_dns_lookalike_is_neither_reflected_nor_allowed_to_write() {
let lookalike = "http://127.0.0.1.evil.com";
let read = guarded_router()
.await
.oneshot(
authed_request()
.uri("/sessions/does-not-exist/transcript")
.header(header::ORIGIN, lookalike)
.body(Body::empty())
.expect("request builds"),
)
.await
.expect("router responds");
assert!(
read.headers()
.get(header::ACCESS_CONTROL_ALLOW_ORIGIN)
.is_none(),
"a 127-prefixed DNS name must not be reflected"
);
let write = guarded_router()
.await
.oneshot(
authed_request()
.method("POST")
.uri("/rpc")
.header(header::ORIGIN, lookalike)
.header(header::CONTENT_TYPE, "application/json")
.body(rpc_create_body())
.expect("request builds"),
)
.await
.expect("router responds");
assert_eq!(
write.status(),
StatusCode::FORBIDDEN,
"a 127-prefixed DNS name must not pass the write guard"
);
}
#[tokio::test]
async fn health_read_is_unaffected_by_the_guard() {
let resp = guarded_router()
.await
.oneshot(
authed_request()
.uri("/health")
.header(header::ORIGIN, "https://evil.example.com")
.body(Body::empty())
.expect("request builds"),
)
.await
.expect("router responds");
assert_eq!(
resp.status(),
StatusCode::OK,
"the guard is method-gated; GET /health must still answer"
);
}