im
Clone
https://git.tahinli.com/Tahinli/im.git
0 watching0 forksRustAGPL-3.0542.8 KiBb9f0726Tahinli27 days ago34 commits
| 1 | //! The security headers of every response, the cache policy that goes with them, and the |
| 2 | //! Content-Security-Policy they carry. |
| 3 | //! |
| 4 | //! The policy allows scripts and styles from the origin itself and nothing else. No page may use an |
| 5 | //! inline script, an inline style or an inline event handler: all behaviour lives in |
| 6 | //! `crates/im-web/assets/*.js`. Changing [`CSP`] is a security change, so keep it in step with |
| 7 | //! `docs/conventions.md` section 5. |
| 8 | //! |
| 9 | //! Cache policy by path: pages (everything that is not a static asset or a photo) are `no-store`, |
| 10 | //! so a page with a secret (recovery codes, a TOTP secret) is never kept by a browser or a proxy. |
| 11 | //! Assets set their own long-lived policy. A photo that has its bytes sets its own `private` policy |
| 12 | //! and its own CSP (see `pages/photo.rs`), and this layer leaves both alone. A photo's redirect or |
| 13 | //! error (sign-in, 404) is a page to the browser, so it gets the page headers. |
| 14 | |
| 15 | use Cx; |
| 16 | use uri; |
| 17 | use response_headers; |
| 18 | use ; |
| 19 | |
| 20 | /// The Content-Security-Policy of every response. |
| 21 | pub const CSP: &str = "default-src 'self'; img-src 'self' data:; style-src 'self'; script-src 'self'; \ |
| 22 | form-action 'self'; frame-ancestors 'none'; base-uri 'none'"; |
| 23 | |
| 24 | /// One year, in seconds. Sent only when the public origin is https. |
| 25 | const HSTS: &str = "max-age=31536000"; |
| 26 | |
| 27 | /// The cache policy of every page: nothing is stored, not even for going back in the history. |
| 28 | const NO_STORE: &str = "no-store"; |
| 29 | |
| 30 | /// The referrer policy of the pages that carry a single-use token in their path. The token must never leave |
| 31 | /// the site in a `Referer` header, so these pages send none at all. |
| 32 | const NO_REFERRER: &str = "no-referrer"; |
| 33 | |
| 34 | /// True for the pages whose path holds a token or starts an account link: the reset request and every |
| 35 | /// `/reset/`, `/invite/` and `/email/` page. |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | /// The headers that every response carries, for an instance at `base_url`. |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | /// What kind of answer a path gets, which decides its headers. |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | /// The kind of answer for a request path. |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | /// Adds the security headers to every response, and `Cache-Control: no-store` to every page and every |
| 97 | /// photo answer that is not a photo's bytes. |
| 98 | /// |
| 99 | /// It uses the per-request response headers, which the router applies after the layers have run, |
| 100 | /// so error responses (404, 500, the sign-in redirect) carry them as well. A header the route set |
| 101 | /// itself is never replaced, so a photo with bytes keeps its own CSP and cache policy. |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 |