Answer#
CORS is a rule that browsers enforce for pages that call another origin. The server only sends headers that say what is allowed. With CorsLayer:
use axum::{
body::Body,
http::{header, HeaderValue, Method, Request},
routing::get,
Router,
};
use tower::ServiceExt;
use tower_http::cors::CorsLayer;
async fn api() -> &'static str { "data" }
#[tokio::main]
async fn main() {
let cors = CorsLayer::new()
.allow_origin("https://app.example".parse::<HeaderValue>().unwrap())
.allow_methods([Method::GET, Method::POST])
.allow_headers([header::CONTENT_TYPE]);
let app = Router::new().route("/api", get(api)).layer(cors);
let preflight = Request::builder().method("OPTIONS").uri("/api")
.header(header::ORIGIN, "https://app.example")
.header(header::ACCESS_CONTROL_REQUEST_METHOD, "POST")
.header(header::ACCESS_CONTROL_REQUEST_HEADERS, "content-type")
.body(Body::empty()).unwrap();
let res = app.clone().oneshot(preflight).await.unwrap();
println!("preflight -> {}", res.status());
for (k, v) in res.headers() { println!(" {k}: {}", v.to_str().unwrap()); }
for origin in ["https://app.example", "https://evil.example"] {
let req = Request::get("/api").header(header::ORIGIN, origin).body(Body::empty()).unwrap();
let res = app.clone().oneshot(req).await.unwrap();
println!("GET from {origin} -> {} allow-origin={:?}", res.status(), res.headers().get(header::ACCESS_CONTROL_ALLOW_ORIGIN));
}
}preflight -> 200 OK
vary: origin, access-control-request-method, access-control-request-headers
access-control-allow-methods: GET,POST
access-control-allow-headers: content-type
access-control-allow-origin: https://app.example
allow: GET,HEAD
content-length: 0
GET from https://app.example -> 200 OK allow-origin=Some("https://app.example")
GET from https://evil.example -> 200 OK allow-origin=Some("https://app.example")Reading the output:
- The preflight request (
OPTIONSwithAccess-Control-Request-Method) was answered by the layer with status 200 and the allowed methods, headers and origin. The route itself has noOPTIONShandler. - A normal
GETfrom the allowed origin gotaccess-control-allow-origin: https://app.example. - A
GETfromhttps://evil.examplealso got status 200 and the body, with the allow-origin header still naminghttps://app.example. The server did not refuse it. The browser compares the header with the page's origin, sees they differ, and hides the response from the page's JavaScript.
So CORS does not protect the server from other callers (curl and other programs ignore it); it protects users' browsers.
tower-http = { version = "0.6.11", features = ["cors"] }Common mistakes#
- Allowing every origin while also sending cookies. Name the exact origins you trust.
- Forgetting a header the page sends (
Authorization,Content-Typewith JSON): list it inallow_headers, or the preflight fails. - Debugging CORS with curl. curl never applies the rule; read the browser console message and compare with the headers above.
Related questions#
Is CORS needed when the page and API share one origin?#
No. The rule applies only to calls to a different origin (scheme, host or port).
Sources#
- tower-http,
cors: https://docs.rs/tower-http/0.6.11/tower_http/cors/index.html (read 2026-12-05) - MDN, CORS: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS (read 2026-12-05)