@@ -3,8 +3,9 @@ use axum::{
33	middleware:: { self ,  Next } , 
44	response:: { Redirect ,  Response } , 
55} ; 
6- use  reqwest:: header:: { AUTHORIZATION ,   HeaderMap } ; 
6+ use  reqwest:: header:: { HeaderMap ,   AUTHORIZATION } ; 
77use  rivet_api_builder:: { create_router,  extract:: FailedExtraction } ; 
8+ use  tower_http:: cors:: CorsLayer ; 
89use  utoipa:: OpenApi ; 
910
1011use  crate :: { actors,  ctx,  datacenters,  namespaces,  runner_configs,  runners,  ui} ; 
@@ -83,6 +84,14 @@ pub async fn router(
8384			. route ( "/ui/" ,  axum:: routing:: get ( ui:: serve_index) ) 
8485			. route ( "/ui/{*path}" ,  axum:: routing:: get ( ui:: serve_ui) ) 
8586			// MARK: Middleware (must go after all routes) 
87+ 			// Add CORS layer that mirrors the request origin 
88+ 			. layer ( 
89+ 				CorsLayer :: new ( ) 
90+ 					. allow_origin ( tower_http:: cors:: AllowOrigin :: mirror_request ( ) ) 
91+ 					. allow_methods ( tower_http:: cors:: AllowMethods :: mirror_request ( ) ) 
92+ 					. allow_headers ( tower_http:: cors:: AllowHeaders :: mirror_request ( ) ) 
93+ 					. allow_credentials ( true ) , 
94+ 			) 
8695			. layer ( middleware:: from_fn ( auth_middleware) ) 
8796	} ) 
8897	. await 
@@ -110,13 +119,15 @@ async fn auth_middleware(
110119	let  ctx = ctx:: ApiCtx :: new ( ctx. clone ( ) ,  token) ; 
111120	req. extensions_mut ( ) . insert ( ctx. clone ( ) ) ; 
112121
122+ 	let  method = req. method ( ) . clone ( ) ; 
113123	let  path = req. uri ( ) . path ( ) . to_string ( ) ; 
114124
115125	// Run endpoint 
116126	let  res = next. run ( req) . await ; 
117127
118128	// Verify auth was handled 
119129	if  res. extensions ( ) . get :: < FailedExtraction > ( ) . is_none ( ) 
130+ 		&& method != reqwest:: Method :: OPTIONS 
120131		&& path != "/" 
121132		&& path != "/ui" 
122133		&& !path. starts_with ( "/ui/" ) 
0 commit comments