diff --git a/src/router/layers.rs b/src/router/layers.rs index 2e69dda38..3d4b3aa79 100644 --- a/src/router/layers.rs +++ b/src/router/layers.rs @@ -15,7 +15,6 @@ use opentelemetry_instrumentation_tower::http::server::LayerBuilder as OtelHttpL use tower::ServiceBuilder; use tower_http::{ catch_panic::CatchPanicLayer, - cors::{self, CorsLayer}, sensitive_headers::SetSensitiveHeadersLayer, set_header::SetResponseHeaderLayer, timeout::{RequestBodyTimeoutLayer, ResponseBodyTimeoutLayer, TimeoutLayer}, @@ -101,7 +100,6 @@ pub(crate) fn build(services: &Arc) -> Result<(Router, Guard)> { header::CONTENT_SECURITY_POLICY, HeaderValue::from_str(&CONDUWUIT_CSP.join(";"))?, )) - .layer(cors_layer(server)) .layer(body_limit_layer(server)) .layer(CatchPanicLayer::custom(move |panic| catch_panic(panic, services_.clone()))); @@ -147,32 +145,6 @@ fn compression_layer(server: &Server) -> tower_http::compression::CompressionLay compression_layer } -fn cors_layer(_server: &Server) -> CorsLayer { - const METHODS: [Method; 7] = [ - Method::GET, - Method::HEAD, - Method::PATCH, - Method::POST, - Method::PUT, - Method::DELETE, - Method::OPTIONS, - ]; - - let headers: [HeaderName; 5] = [ - header::ORIGIN, - HeaderName::from_lowercase(b"x-requested-with").unwrap(), - header::CONTENT_TYPE, - header::ACCEPT, - header::AUTHORIZATION, - ]; - - CorsLayer::new() - .allow_origin(cors::Any) - .allow_methods(METHODS) - .allow_headers(headers) - .max_age(Duration::from_hours(24)) -} - /// Methods outside the standard set can never match a route; record them as /// `_OTHER`, as the semantic conventions ask, so clients cannot create new /// metric attribute values. diff --git a/src/router/router.rs b/src/router/router.rs index 9267a6ce0..3e7b37a49 100644 --- a/src/router/router.rs +++ b/src/router/router.rs @@ -1,10 +1,11 @@ -use std::sync::Arc; +use std::{sync::Arc, time::Duration}; use axum::{Router, response::IntoResponse}; use conduwuit::Error; use conduwuit_service::{Services, state, state::Guard}; -use http::{StatusCode, Uri}; +use http::{HeaderName, Method, StatusCode, Uri, header}; use ruma::api::error::ErrorKind; +use tower_http::cors::{self, CorsLayer}; pub(crate) fn build(services: &Arc) -> (Router, Guard) { let router = Router::::new(); @@ -14,6 +15,7 @@ pub(crate) fn build(services: &Arc) -> (Router, Guard) { // is dropped. let (state, guard) = unsafe { state::create(services.clone()) }; let router = conduwuit_api::router::build(router, state) + .layer(api_cors_layer()) .merge(conduwuit_web::build(services)) .fallback(not_found) .with_state(state); @@ -24,3 +26,29 @@ pub(crate) fn build(services: &Arc) -> (Router, Guard) { async fn not_found(_uri: Uri) -> impl IntoResponse { Error::Request(ErrorKind::Unrecognized, "not found :(".into(), StatusCode::NOT_FOUND) } + +fn api_cors_layer() -> CorsLayer { + const METHODS: [Method; 7] = [ + Method::GET, + Method::HEAD, + Method::PATCH, + Method::POST, + Method::PUT, + Method::DELETE, + Method::OPTIONS, + ]; + + let headers: [HeaderName; 5] = [ + header::ORIGIN, + HeaderName::from_lowercase(b"x-requested-with").unwrap(), + header::CONTENT_TYPE, + header::ACCEPT, + header::AUTHORIZATION, + ]; + + CorsLayer::new() + .allow_origin(cors::Any) + .allow_methods(METHODS) + .allow_headers(headers) + .max_age(Duration::from_hours(24)) +}