@@ -3,11 +3,21 @@ use rocket_cors::{AllowedHeaders, AllowedOrigins};
3
3
4
4
use crate :: config:: AWConfig ;
5
5
6
+ const CHROME_EXTENSION_PREFIX : & str = "chrome-extension://" ;
7
+
6
8
pub fn cors ( config : & AWConfig ) -> rocket_cors:: Cors {
7
9
let root_url = format ! ( "http://127.0.0.1:{}" , config. port) ;
8
10
let root_url_localhost = format ! ( "http://localhost:{}" , config. port) ;
9
11
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
+ ) ;
11
21
12
22
if config. testing {
13
23
allowed_exact_origins. push ( "http://127.0.0.1:27180" . to_string ( ) ) ;
@@ -22,6 +32,13 @@ pub fn cors(config: &AWConfig) -> rocket_cors::Cors {
22
32
if config. testing {
23
33
allowed_regex_origins. push ( "chrome-extension://.*" . to_string ( ) ) ;
24
34
}
35
+ allowed_regex_origins. extend (
36
+ config
37
+ . cors
38
+ . clone ( )
39
+ . into_iter ( )
40
+ . filter ( |c| c. starts_with ( CHROME_EXTENSION_PREFIX ) ) ,
41
+ ) ;
25
42
26
43
let allowed_origins = AllowedOrigins :: some ( & allowed_exact_origins, & allowed_regex_origins) ;
27
44
let allowed_methods = vec ! [ Method :: Get , Method :: Post , Method :: Delete ]
@@ -39,5 +56,6 @@ pub fn cors(config: &AWConfig) -> rocket_cors::Cors {
39
56
..Default :: default ( )
40
57
}
41
58
. to_cors ( )
59
+ . inspect_err ( |e| log:: error!( "failed to setup cors: {e}" ) )
42
60
. expect ( "Failed to set up CORS" )
43
61
}
0 commit comments