use pagetop::prelude::*; use web::http::StatusCode; // Paths seen by the extension middleware: a rejected request must not reach it (it would load // the session). static SEEN_PATHS: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); fn middleware_saw(path: &str) -> bool { SEEN_PATHS .lock() .unwrap_or_else(std::sync::PoisonError::into_inner) .iter() .any(|seen| seen == path) } struct ActionExtension; #[async_trait] impl Extension for ActionExtension { fn configure_router(&self, routes: Routes) -> Routes { routes.public_route( "/action/{name}", web::get(|| async { "read" }).post(|| async { "done" }), ) } fn configure_middleware(&self, layers: Layers) -> Layers { layers.layer(web::middleware::from_fn(record_path)) } } async fn record_path(request: web::Request, next: web::middleware::Next) -> Response { SEEN_PATHS .lock() .unwrap_or_else(std::sync::PoisonError::into_inner) .push(request.uri().path().to_owned()); next.run(request).await } async fn send(request: web::test::TestRequest) -> Response { let app = web::test::init_router(Application::prepare(&ActionExtension).await.test()); web::test::send_request(&app, request.to_request()).await } fn post(path: &str, headers: &[(&str, &str)]) -> web::test::TestRequest { headers.iter().fold( web::test::TestRequest::post().uri(path), |req, (name, value)| req.header(name, value), ) } #[pagetop::test] async fn same_origin_requests_are_allowed() { let resp = send(post("/action/same", &[("sec-fetch-site", "same-origin")])).await; assert_eq!(resp.status(), StatusCode::OK); assert_eq!(web::test::read_body_text(resp).await, "done"); let origin = [("origin", "http://example.com"), ("host", "example.com")]; assert_eq!( send(post("/action/old", &origin)).await.status(), StatusCode::OK ); } #[pagetop::test] async fn cross_origin_requests_are_rejected_before_the_middleware() { let resp = send(post("/action/cross", &[("sec-fetch-site", "cross-site")])).await; assert_eq!(resp.status(), StatusCode::FORBIDDEN); // The theme renders the 403 page. assert!(web::test::read_body_text(resp).await.contains("