@@ -3,11 +3,21 @@ use rocket_cors::{AllowedHeaders, AllowedOrigins};
33
44use  crate :: config:: AWConfig ; 
55
6+ const  CHROME_EXTENSION_PREFIX :  & str  = "chrome-extension://" ; 
7+ 
68pub  fn  cors ( config :  & AWConfig )  -> rocket_cors:: Cors  { 
79    let  root_url = format ! ( "http://127.0.0.1:{}" ,  config. port) ; 
810    let  root_url_localhost = format ! ( "http://localhost:{}" ,  config. port) ; 
911    let  mut  allowed_exact_origins = vec ! [ root_url,  root_url_localhost] ; 
10-     allowed_exact_origins. extend ( config. cors . clone ( ) ) ; 
12+     // url with chrome-extension:// is parsed by url crate as Opaque, so it 
13+     // should be used as regex origin 
14+     allowed_exact_origins. extend ( 
15+         config
16+             . cors 
17+             . clone ( ) 
18+             . into_iter ( ) 
19+             . filter ( |c| !c. starts_with ( CHROME_EXTENSION_PREFIX ) ) , 
20+     ) ; 
1121
1222    if  config. testing  { 
1323        allowed_exact_origins. push ( "http://127.0.0.1:27180" . to_string ( ) ) ; 
@@ -22,6 +32,13 @@ pub fn cors(config: &AWConfig) -> rocket_cors::Cors {
2232    if  config. testing  { 
2333        allowed_regex_origins. push ( "chrome-extension://.*" . to_string ( ) ) ; 
2434    } 
35+     allowed_regex_origins. extend ( 
36+         config
37+             . cors 
38+             . clone ( ) 
39+             . into_iter ( ) 
40+             . filter ( |c| c. starts_with ( CHROME_EXTENSION_PREFIX ) ) , 
41+     ) ; 
2542
2643    let  allowed_origins = AllowedOrigins :: some ( & allowed_exact_origins,  & allowed_regex_origins) ; 
2744    let  allowed_methods = vec ! [ Method :: Get ,  Method :: Post ,  Method :: Delete ] 
@@ -39,5 +56,6 @@ pub fn cors(config: &AWConfig) -> rocket_cors::Cors {
3956        ..Default :: default ( ) 
4057    } 
4158    . to_cors ( ) 
59+     . inspect_err ( |e| log:: error!( "failed to setup cors: {e}" ) ) 
4260    . expect ( "Failed to set up CORS" ) 
4361} 
0 commit comments