From 81aa854e6e307aca077651b26e0e59a0050d7a36 Mon Sep 17 00:00:00 2001 From: piterxyz Date: Mon, 16 Nov 2026 15:26:20 +0100 Subject: [PATCH] feat(venue): CORS on the info router so browsers can call the mirror The consumer's web app posts JSON to /info straight from the page, which makes the browser send a preflight first. The sim answered it with 405 and no allow-origin header, so every browser call was blocked while curl and the services worked. The info router now answers a preflight (204, echoing the requested headers) and gives every response Access-Control-Allow-Origin: *, like the venue it mirrors. No credentials are allowed, and the control router stays closed to browsers. Bump the workspace version to 0.2.2 (the image tag the staging pipeline derives), with Cargo.lock tracking it. --- Cargo.lock | 22 +++--- Cargo.toml | 2 +- crates/venue/src/api/cors.rs | 137 +++++++++++++++++++++++++++++++++++ crates/venue/src/api/mod.rs | 5 ++ 4 files changed, 154 insertions(+), 12 deletions(-) create mode 100644 crates/venue/src/api/cors.rs diff --git a/Cargo.lock b/Cargo.lock index fe9e51fc..e8cee8e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1498,7 +1498,7 @@ dependencies = [ [[package]] name = "diorama-anvil" -version = "0.2.1" +version = "0.2.2" dependencies = [ "alloy-eips", "alloy-json-rpc", @@ -1518,7 +1518,7 @@ dependencies = [ [[package]] name = "diorama-ast-scope" -version = "0.2.1" +version = "0.2.2" dependencies = [ "proc-macro2", "quote", @@ -1527,7 +1527,7 @@ dependencies = [ [[package]] name = "diorama-cctp" -version = "0.2.1" +version = "0.2.2" dependencies = [ "alloy-chains", "alloy-consensus", @@ -1553,7 +1553,7 @@ dependencies = [ [[package]] name = "diorama-cctp-bridge" -version = "0.2.1" +version = "0.2.2" dependencies = [ "diorama-cctp", "diorama-config", @@ -1561,7 +1561,7 @@ dependencies = [ [[package]] name = "diorama-config" -version = "0.2.1" +version = "0.2.2" dependencies = [ "alloy-primitives", "clap", @@ -1576,7 +1576,7 @@ dependencies = [ [[package]] name = "diorama-devnet" -version = "0.2.1" +version = "0.2.2" dependencies = [ "alloy-consensus", "alloy-eips", @@ -1598,7 +1598,7 @@ dependencies = [ [[package]] name = "diorama-mainnet" -version = "0.2.1" +version = "0.2.2" dependencies = [ "alloy-eips", "alloy-json-rpc", @@ -1625,7 +1625,7 @@ dependencies = [ [[package]] name = "diorama-mainnet-engine" -version = "0.2.1" +version = "0.2.2" dependencies = [ "diorama-config", "diorama-mainnet", @@ -1633,7 +1633,7 @@ dependencies = [ [[package]] name = "diorama-venue" -version = "0.2.1" +version = "0.2.2" dependencies = [ "alloy-consensus", "alloy-primitives", @@ -1658,7 +1658,7 @@ dependencies = [ [[package]] name = "diorama-venue-engine" -version = "0.2.1" +version = "0.2.2" dependencies = [ "diorama-config", "diorama-venue", @@ -1666,7 +1666,7 @@ dependencies = [ [[package]] name = "diorama-venue-sim" -version = "0.2.1" +version = "0.2.2" dependencies = [ "diorama-config", "diorama-venue", diff --git a/Cargo.toml b/Cargo.toml index 80dcb7ea..0f193f5b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ resolver = "2" # The image tag the staging pipeline derives: ghcr.io/herodotusdev/-stg:. # Bump per deploy-worthy change set; the pipeline also creates the matching # git tag (plain `0.2.0`, distinct from the legacy `v0.1.0`). -version = "0.2.1" +version = "0.2.2" edition = "2021" rust-version = "1.99" license = "MIT OR Apache-2.0" diff --git a/crates/venue/src/api/cors.rs b/crates/venue/src/api/cors.rs new file mode 100644 index 00000000..c228ea64 --- /dev/null +++ b/crates/venue/src/api/cors.rs @@ -0,0 +1,137 @@ +//! CORS for the info router. +//! +//! The venue's real API answers browsers from any origin, and the consumer's +//! web app calls the mirror straight from the page (`POST /info`, a JSON body, +//! so the browser sends a preflight first). Without these headers the +//! mirror is unusable from a browser although curl and the services read it +//! fine. The info port is read-only market data plus the order lane's named +//! refusal and takes no credentials, so any origin is allowed and cookies are +//! not (no `Allow-Credentials`). +use axum::extract::Request; +use axum::http::header::{ + ACCESS_CONTROL_ALLOW_HEADERS, ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN, + ACCESS_CONTROL_MAX_AGE, ACCESS_CONTROL_REQUEST_HEADERS, ACCESS_CONTROL_REQUEST_METHOD, +}; +use axum::http::{HeaderValue, Method, StatusCode}; +use axum::middleware::Next; +use axum::response::{IntoResponse, Response}; + +/// How long a browser may reuse one preflight answer (seconds). +const PREFLIGHT_MAX_AGE: &str = "86400"; + +/// The headers a preflight is answered with when the page names none. +const DEFAULT_ALLOWED_HEADERS: &str = "content-type"; + +/// Answer a preflight on the spot; give every other response the allow-origin +/// header so the page may read it. +pub(crate) async fn cors(request: Request, next: Next) -> Response { + let is_preflight = request.method() == Method::OPTIONS + && request + .headers() + .contains_key(ACCESS_CONTROL_REQUEST_METHOD); + + let mut response = if is_preflight { + // The page's own list of request headers is echoed back, so a client + // that adds one (an auth or tracing header) is not refused by the + // mirror; with none named the JSON content type is allowed. + let allowed_headers = request + .headers() + .get(ACCESS_CONTROL_REQUEST_HEADERS) + .cloned() + .unwrap_or_else(|| HeaderValue::from_static(DEFAULT_ALLOWED_HEADERS)); + let mut preflight = StatusCode::NO_CONTENT.into_response(); + let headers = preflight.headers_mut(); + headers.insert( + ACCESS_CONTROL_ALLOW_METHODS, + HeaderValue::from_static("GET, POST, OPTIONS"), + ); + headers.insert(ACCESS_CONTROL_ALLOW_HEADERS, allowed_headers); + headers.insert( + ACCESS_CONTROL_MAX_AGE, + HeaderValue::from_static(PREFLIGHT_MAX_AGE), + ); + preflight + } else { + next.run(request).await + }; + + response + .headers_mut() + .insert(ACCESS_CONTROL_ALLOW_ORIGIN, HeaderValue::from_static("*")); + response +} + +#[cfg(test)] +mod tests { + use super::*; + use axum::routing::post; + use axum::Router; + + /// Serve a router wrapped in the CORS layer on a free port. + async fn serve() -> String { + let router = Router::new() + .route("/info", post(|| async { "ok" })) + .layer(axum::middleware::from_fn(cors)); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let addr = listener.local_addr().unwrap(); + tokio::spawn(async move { axum::serve(listener, router).await }); + format!("http://{addr}") + } + + /// The browser's preflight for a JSON `POST /info` is answered (204) with + /// the origin, the methods and the headers it asked about, although the + /// route itself only takes POST. + #[tokio::test] + async fn a_preflight_is_answered_before_the_route_refuses_the_method() { + let base = serve().await; + let response = reqwest::Client::new() + .request(Method::OPTIONS, format!("{base}/info")) + .header("origin", "http://localhost:3000") + .header("access-control-request-method", "POST") + .header("access-control-request-headers", "content-type, x-trace") + .send() + .await + .unwrap(); + assert_eq!(response.status(), StatusCode::NO_CONTENT); + let headers = response.headers(); + assert_eq!(headers["access-control-allow-origin"], "*"); + assert_eq!( + headers["access-control-allow-methods"], + "GET, POST, OPTIONS" + ); + assert_eq!( + headers["access-control-allow-headers"], + "content-type, x-trace" + ); + assert_eq!(headers["access-control-max-age"], PREFLIGHT_MAX_AGE); + } + + /// The real call carries the allow-origin header, so the page may read + /// the answer. + #[tokio::test] + async fn an_answer_carries_the_allow_origin_header() { + let base = serve().await; + let response = reqwest::Client::new() + .post(format!("{base}/info")) + .header("origin", "http://localhost:3000") + .send() + .await + .unwrap(); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(response.headers()["access-control-allow-origin"], "*"); + } + + /// An OPTIONS that is not a preflight (no request-method header) is the + /// route's own business: the method refusal still carries the header. + #[tokio::test] + async fn a_plain_options_is_left_to_the_route() { + let base = serve().await; + let response = reqwest::Client::new() + .request(Method::OPTIONS, format!("{base}/info")) + .send() + .await + .unwrap(); + assert_eq!(response.status(), StatusCode::METHOD_NOT_ALLOWED); + assert_eq!(response.headers()["access-control-allow-origin"], "*"); + } +} diff --git a/crates/venue/src/api/mod.rs b/crates/venue/src/api/mod.rs index c61ca6ed..e0bce987 100644 --- a/crates/venue/src/api/mod.rs +++ b/crates/venue/src/api/mod.rs @@ -7,6 +7,7 @@ //! mapping keeps the taxonomy's meaning on the wire: control refusals are //! 4xx, a warming market plane is 503, a venue fault is 502. pub(crate) mod control; +pub(crate) mod cors; pub(crate) mod exchange; pub(crate) mod info; pub(crate) mod ws; @@ -155,6 +156,10 @@ pub fn info_router(state: Arc) -> Router { .route("/exchange", axum::routing::post(exchange::exchange)) // The venue's websocket lane ("info + WS" on this port). .route("/ws", axum::routing::get(ws::ws)) + // Browsers call this port straight from the page (a JSON POST, so a + // preflight first): the info surface answers any origin, like the + // venue it mirrors. The control router stays closed to browsers. + .layer(axum::middleware::from_fn(cors::cors)) .with_state(state) }