@@ -10,6 +10,7 @@ languages:
1010 - JavaScript
1111 - TypeScript
1212 - Python
13+ - Rust
1314pcx_content_type : example
1415title : Set security headers
1516sidebar :
@@ -250,4 +251,75 @@ async def on_fetch(request):
250251 return Response.new(res.body, status = res.status, statusText = res.statusText, headers = new_headers)
251252```
252253
253- </TabItem > </Tabs >
254+ </TabItem > <TabItem label = " Rust" icon = " seti:rust" >
255+ ``` rs
256+ use std :: collections :: HashMap ;
257+ use worker :: * ;
258+
259+ #[event(fetch)]
260+ async fn fetch (req : Request , _env : Env , _ctx : Context ) -> Result <Response > {
261+ let default_security_headers = HashMap :: from ([
262+ // Secure your application with Content-Security-Policy headers.
263+ // Enabling these headers will permit content from a trusted domain and all its subdomains.
264+ // @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
265+ (
266+ " Content-Security-Policy" ,
267+ " default-src 'self' example.com *.example.com" ,
268+ ),
269+ // You can also set Strict-Transport-Security headers.
270+ // These are not automatically set because your website might get added to Chrome's HSTS preload list.
271+ // Here's the code if you want to apply it:
272+ (
273+ " Strict-Transport-Security" ,
274+ " max-age=63072000; includeSubDomains; preload" ,
275+ ),
276+ // Permissions-Policy header provides the ability to allow or deny the use of browser features, such as opting out of FLoC - which you can use below:
277+ (" Permissions-Policy" , " interest-cohort=()" ),
278+ // X-XSS-Protection header prevents a page from loading if an XSS attack is detected.
279+ // @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection
280+ (" X-XSS-Protection" , " 0" ),
281+ // X-Frame-Options header prevents click-jacking attacks.
282+ // @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options
283+ (" X-Frame-Options" , " DENY" ),
284+ // X-Content-Type-Options header prevents MIME-sniffing.
285+ // @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options
286+ (" X-Content-Type-Options" , " nosniff" ),
287+ (" Referrer-Policy" , " strict-origin-when-cross-origin" ),
288+ (
289+ " Cross-Origin-Embedder-Policy" ,
290+ " require-corp; report-to='default';" ,
291+ ),
292+ (
293+ " Cross-Origin-Opener-Policy" ,
294+ " same-site; report-to='default';" ,
295+ ),
296+ (" Cross-Origin-Resource-Policy" , " same-site" ),
297+ ]);
298+ let blocked_headers = [" Public-Key-Pins" , " X-Powered-By" , " X-AspNet-Version" ];
299+ let tls = req . cf (). unwrap (). tls_version ();
300+ let res = Fetch :: Request (req ). send (). await ? ;
301+ let mut new_headers = res . headers (). clone ();
302+
303+ // This sets the headers for HTML responses
304+ if Some (String :: from (" text/html" )) == new_headers . get (" Content-Type" )? {
305+ return Ok (Response :: from_body (res . body (). clone ())?
306+ . with_headers (new_headers )
307+ . with_status (res . status_code ()));
308+ }
309+ for (k , v ) in default_security_headers {
310+ new_headers . set (k , v )? ;
311+ }
312+
313+ for k in blocked_headers {
314+ new_headers . delete (k )? ;
315+ }
316+
317+ if ! vec! [" TLSv1.2" , " TLSv1.3" ]. contains (& tls . as_str ()) {
318+ return Response :: error (" You need to use TLS version 1.2 or higher." , 400 );
319+ }
320+ Ok (Response :: from_body (res . body (). clone ())?
321+ . with_headers (new_headers )
322+ . with_status (res . status_code ()))
323+ }
324+ ```
325+ </TabItem > </Tabs >
0 commit comments