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