1- //! Rocket fairing to configure secure  HTTP headers, such as content security policies 
1+ //! Rocket fairing to inject  HTTP security  headers, such as content security policies, into responses  
22
33use  std:: io; 
44
@@ -10,12 +10,14 @@ use rocket::http::Status;
1010use  rocket:: { Data ,  Request ,  Response } ; 
1111use  tracing:: { Level ,  event} ; 
1212
13+ // Configuration of Fairing 
1314#[ derive( Clone ) ]  
1415pub  struct  SecurityHttpHeaders  { 
1516    pub  config :  crate :: configuration:: config:: CustomAppConfig , 
1617    pub  regex_paths :  regex:: RegexSet , 
1718} 
1819
20+ // Fairing implementation 
1921#[ rocket:: async_trait]  
2022impl  Fairing  for  SecurityHttpHeaders  { 
2123    fn  info ( & self )  -> Info  { 
@@ -25,12 +27,27 @@ impl Fairing for SecurityHttpHeaders {
2527        } 
2628    } 
2729
30+     /// Executed on every request (we do not need it, but it is part of the Fairing trait) 
31+ /// 
32+ /// # Arguments 
33+ /// * `self` - Struct Security HTTP Headers 
34+ /// * `req` - Request object 
35+ /// 
36+ /// 
2837async  fn  on_request ( & self ,  req :  & mut  Request < ' _ > ,  _:  & mut  Data < ' _ > )  { 
2938        // do nothing 
3039    } 
3140
41+     /// Executed on every response. We inject here the HTTP Security Headers 
42+ /// 
43+ /// # Arguments 
44+ /// * `self` - Struct Security HTTP Headers 
45+ /// * `req` - Request object 
46+ /// * `res` - Response object 
47+ /// 
3248async  fn  on_response < ' r > ( & self ,  req :  & ' r  Request < ' _ > ,  res :  & mut  Response < ' r > )  { 
3349        if  ( res. status ( )  == Status :: Ok )  { 
50+             // Configure Content-Security Policy Header and insert nonces 
3451            let  mut  body_bytes = res. body_mut ( ) . to_bytes ( ) . await . unwrap ( ) ; 
3552            match  self . config . clone ( ) . httpheaders . content_security_policy  { 
3653                Some ( csp)  => { 
@@ -95,9 +112,48 @@ impl Fairing for SecurityHttpHeaders {
95112                                "Configuration: Content-Security-Policy: You did not specify a tag to inject a nonce" 
96113                            ) , 
97114                        } 
98- 
99115                        res. set_raw_header ( "Content-Security-Policy" ,  csp_value) ; 
100116                        res. set_sized_body ( body_bytes. len ( ) ,  io:: Cursor :: new ( body_bytes) ) ; 
117+                         // Set other HTTP Security Headers 
118+                         match  self . config . clone ( ) . httpheaders . permissions_policy  { 
119+                             Some ( permissions_policy)  => { 
120+                                 res. set_raw_header ( "Permissions-Policy" ,  permissions_policy) ; 
121+                             } 
122+                             None  => ( ) , 
123+                         } ; 
124+                         match  self . config . clone ( ) . httpheaders . referrer_policy  { 
125+                             Some ( referrer_policy)  => { 
126+                                 res. set_raw_header ( "Referrer-Policy" ,  referrer_policy) ; 
127+                             } 
128+                             None  => ( ) , 
129+                         } ; 
130+                         match  self . config . clone ( ) . httpheaders . cross_origin_embedder_policy  { 
131+                             Some ( cross_origin_embedder_policy)  => { 
132+                                 res. set_raw_header ( 
133+                                     "Cross-Origin-Embedder-Policy" , 
134+                                     cross_origin_embedder_policy, 
135+                                 ) ; 
136+                             } 
137+                             None  => ( ) , 
138+                         } ; 
139+                         match  self . config . clone ( ) . httpheaders . cross_origin_opener_policy  { 
140+                             Some ( cross_origin_opener_policy)  => { 
141+                                 res. set_raw_header ( 
142+                                     "Cross-Origin-Opener-Policy" , 
143+                                     cross_origin_opener_policy, 
144+                                 ) ; 
145+                             } 
146+                             None  => ( ) , 
147+                         } ; 
148+                         match  self . config . clone ( ) . httpheaders . cross_origin_resource_policy  { 
149+                             Some ( cross_origin_resource_policy)  => { 
150+                                 res. set_raw_header ( 
151+                                     "Cross-Origin-Resource-Policy" , 
152+                                     cross_origin_resource_policy, 
153+                                 ) ; 
154+                             } 
155+                             None  => ( ) , 
156+                         } ; 
101157                    } 
102158                } 
103159                None  => ( ) , 
0 commit comments