forge
Explore
Log in

Don't you dare go Hollow

iz

Clone
★ 0

0 watching0 forksRustAGPL-3.0567.3 KiB3950aceTahinli27 days ago34 commits

security.rs87 lines, 3.2 KiBHistoryRawDownload
1//! The security headers of every response, and the Content-Security-Policy they carry.
2//!
3//! The policy allows scripts and styles from the origin itself and nothing else. No page may use an
4//! inline script, an inline style or an inline event handler: all behaviour lives in
5//! `crates/iz-web/assets/*.js`. Changing [`CSP`] is a security change, so keep it in step with
6//! `docs/conventions.md` section 5.
7
8use topcoat::context::Cx;
9use topcoat::router::response::response_headers;
10use topcoat::router::{Body, HeaderMap, HeaderValue, Layer, LayerFuture, Next, Path, header};
11
12/// The Content-Security-Policy of every response.
13pub const CSP: &str = "default-src 'self'; img-src 'self' data:; style-src 'self'; script-src 'self'; \
14 form-action 'self'; frame-ancestors 'none'; base-uri 'none'";
15
16/// One year, in seconds. Sent only when the public origin is https.
17const HSTS: &str = "max-age=31536000";
18
19/// The headers that every response carries, for an instance at `base_url`.
20pub fn headers_for(base_url: &str) -> HeaderMap {
21 let mut headers = HeaderMap::new();
22 headers.insert(
23 header::X_CONTENT_TYPE_OPTIONS,
24 HeaderValue::from_static("nosniff"),
25 );
26 headers.insert(header::X_FRAME_OPTIONS, HeaderValue::from_static("DENY"));
27 headers.insert(
28 header::REFERRER_POLICY,
29 HeaderValue::from_static("same-origin"),
30 );
31 headers.insert(
32 header::CONTENT_SECURITY_POLICY,
33 HeaderValue::from_static(CSP),
34 );
35 if base_url.starts_with("https://") {
36 headers.insert(
37 header::STRICT_TRANSPORT_SECURITY,
38 HeaderValue::from_static(HSTS),
39 );
40 }
41 headers
42}
43
44/// Adds a fixed set of headers to every response of the router.
45///
46/// A header that the route already set is kept: a byte response carries its own policy. Errors take the
47/// per-request response headers, which the router applies after the layers have run.
48pub struct SecurityHeaders {
49 headers: HeaderMap,
50}
51
52impl SecurityHeaders {
53 /// A layer that adds `headers` to every response.
54 pub fn new(headers: HeaderMap) -> Self {
55 Self { headers }
56 }
57}
58
59impl Layer for SecurityHeaders {
60 fn path(&self) -> Option<&Path> {
61 None
62 }
63
64 fn handle<'a>(&'a self, cx: &'a Cx, body: Body, next: Next<'a>) -> LayerFuture<'a> {
65 Box::pin(async move {
66 match next.run(cx, body).await {
67 // A header the route set itself wins: byte responses carry their own sandboxed
68 // policy, and two policies would apply both (the page one forbids framing).
69 Ok(mut response) => {
70 let headers = response.headers_mut();
71 for (name, value) in &self.headers {
72 if !headers.contains_key(name) {
73 headers.insert(name.clone(), value.clone());
74 }
75 }
76 Ok(response)
77 }
78 // Errors become responses after the layers have run; the per-request slot gives
79 // them the headers too.
80 Err(error) => {
81 response_headers(cx).extend(self.headers.clone());
82 Err(error)
83 }
84 }
85 })
86 }
87}