Coverage / crates/hub/src/headers.rs 94.3%
116 of 123 lines run, 29 of 49 functions · run #530 on main · run missed
| 1 | //! Security headers on every response (oxygenhub#222), and the endpoint | |
| 2 | //! the browser reports Content-Security-Policy violations to. | |
| 3 | //! | |
| 4 | //! The policy is enforced (#227): the pages carry no inline scripts or | |
| 5 | //! handlers, and in the week of reports before it was switched on the only | |
| 6 | //! thing it would block was Cloudflare's analytics beacon, which the | |
| 7 | //! policy now allows. Violations are still reported and logged; going back | |
| 8 | //! to report-only is one constant ([`ENFORCE`]). | |
| 9 | //! Responses that set their own policy (report frames, #127; a job's | |
| 10 | //! HTML artifact) keep theirs, and may be framed as they say. | |
| 11 | //! Strict-Transport-Security is sent only when `[server] hsts_max_age` is | |
| 12 | //! set (oxygenhub#231), raised in stages: browsers keep it. | |
| 13 | | |
| 14 | use ; | |
| 15 | use Response; | |
| 16 | use HashMap; | |
| 17 | use Mutex; | |
| 18 | ||
| 19 | /// Where violation reports go. | |
| 20 | pub const REPORT_PATH: &str = "/csp-report"; | |
| 21 | ||
| 22 | /// Enforce the policy rather than only report it. | |
| 23 | const ENFORCE: bool = true; | |
| 24 | ||
| 25 | /// What the pages may load: their own scripts (and WebAssembly, for the | |
| 26 | /// backdrop), styles (inline too: style attributes are everywhere), images | |
| 27 | /// from anywhere over https (READMEs show badges), and forms posted only to | |
| 28 | /// the hub (sign-in providers are reached by links, not forms). Cloudflare, | |
| 29 | /// in front of oxygenhub.io, adds its Web Analytics beacon to pages: its | |
| 30 | /// script and where it reports are allowed, nothing else of theirs. | |
| 31 | const POLICY: &str = "default-src 'self'; script-src 'self' 'wasm-unsafe-eval' https://static.cloudflareinsights.com; \ | |
| 32 | style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self'; \ | |
| 33 | connect-src 'self' https://cloudflareinsights.com; media-src 'self'; object-src 'none'; \ | |
| 34 | base-uri 'self'; form-action 'self'; frame-ancestors 'none'; report-uri /csp-report; report-to csp"; | |
| 35 | ||
| 36 | const PERMISSIONS: &str = "camera=(), microphone=(), geolocation=(), payment=(), usb=(), serial=(), bluetooth=(), \ | |
| 37 | midi=(), magnetometer=(), gyroscope=(), accelerometer=(), display-capture=(), browsing-topics=(), interest-cohort=()"; | |
| 38 | ||
| 39 | /// The Strict-Transport-Security value for a site, if it sends one | |
| 40 | /// (oxygenhub#231): only for an https address and a max-age above 0. | |
| 41 | 2845× | |
| 42 | 2845× | |
| 43 | 2845× | |
| 44 | ||
| 45 | /// Adds the headers a response does not already carry. | |
| 46 | 3861× | pub async |
| 47 | 3861× | |
| 48 | 3861× | |
| 49 | 3861× | |
| 50 | 3861× | |
| 51 | 3861× | |
| 52 | 3861× | |
| 53 | 3861× | |
| 54 | 0× | |
| 55 | 3861× | |
| 56 | 3861× | |
| 57 | 23136× | |
| 58 | 23136× | |
| 59 | 23136× | |
| 60 | 3861× | |
| 61 | 3861× | |
| 62 | 3861× | |
| 63 | 3861× | |
| 64 | 3851× | |
| 65 | 3851× | |
| 66 | 3851× | |
| 67 | 3851× | |
| 68 | 10× | |
| 69 | 3861× | |
| 70 | 3861× | |
| 71 | ||
| 72 | /// One violation, as either report format gives it. | |
| 73 | | |
| 74 | | |
| 75 | | |
| 76 | | |
| 77 | | |
| 78 | | |
| 79 | | |
| 80 | ||
| 81 | /// The violations in a report body: the older `application/csp-report` | |
| 82 | /// (`{"csp-report": {...}}`) or the Reporting API's list. | |
| 83 | 4× | |
| 84 | 4× | |
| 85 | 15× | |
| 86 | 17× | |
| 87 | 15× | |
| 88 | 3× | |
| 89 | 3× | |
| 90 | 3× | |
| 91 | 3× | |
| 92 | 3× | |
| 93 | 1× | |
| 94 | 0× | |
| 95 | 2× | |
| 96 | | |
| 97 | 3× | |
| 98 | 3× | |
| 99 | 2× | |
| 100 | 2× | |
| 101 | 2× | |
| 102 | 2× | |
| 103 | 2× | |
| 104 | 2× | |
| 105 | 2× | |
| 106 | 2× | |
| 107 | 1× | |
| 108 | 1× | |
| 109 | 1× | |
| 110 | 1× | |
| 111 | 2× | |
| 112 | 1× | |
| 113 | 1× | |
| 114 | 1× | |
| 115 | 1× | |
| 116 | 1× | |
| 117 | 4× | |
| 118 | ||
| 119 | /// Each distinct violation is logged once an hour, so a busy page does not | |
| 120 | /// flood the log. | |
| 121 | static SEEN: = new; | |
| 122 | const LOG_EVERY_MS: i64 = 60 * 60_000; | |
| 123 | ||
| 124 | 4× | |
| 125 | 4× | |
| 126 | 4× | |
| 127 | 4× | |
| 128 | 0× | |
| 129 | 4× | |
| 130 | 4× | |
| 131 | 2× | |
| 132 | | |
| 133 | 3× | |
| 134 | 3× | |
| 135 | | |
| 136 | | |
| 137 | 4× | |
| 138 | ||
| 139 | /// `POST /csp-report`: log what the policy would block. | |
| 140 | 1× | pub async |
| 141 | 1× | |
| 142 | 1× | |
| 143 | | |
| 144 | 1× | |
| 145 | 1× | |
| 146 | 1× | |
| 147 | 1× | |
| 148 | 0× | |
| 149 | | |
| 150 | 1× | |
| 151 | 1× | |
| 152 | ||
| 153 | | |
| 154 | | |
| 155 | | |
| 156 | | |
| 157 | | |
| 158 | 1× | |
| 159 | 1× | |
| 160 | 1× | |
| 161 | 1× | |
| 162 | 1× | |
| 163 | 1× | |
| 164 | 1× | |
| 165 | 1× | |
| 166 | 1× | |
| 167 | 1× | |
| 168 | | |
| 169 | 1× | |
| 170 | 1× | |
| 171 | 1× | |
| 172 | 1× | |
| 173 | 1× | |
| 174 | 1× | |
| 175 | | |
| 176 | | |
| 177 | 1× | |
| 178 | 3× | |
| 179 | 1× | |
| 180 | 1× | |
| 181 | 1× | |
| 182 | 1× | |
| 183 | |