| @@ -4,6 +4,7 @@ import security |
| 4 | 4 | import std/bytes |
| 5 | 5 | import std/html.{type Html, Node, Text} |
| 6 | 6 | import std/http.{type Response} |
| 7 | +import std/list |
| 7 | 8 | import std/option |
| 8 | 9 | import std/process.{type proc} |
| 9 | 10 | import std/random.{type random} |
| @@ -21,7 +22,12 @@ fn page :: content: List<Html> -> Response |
| 21 | 22 | |
| 22 | 23 | fn settings :: -> Settings |
| 23 | 24 | { |
| 24 | | − Settings(timeout_ms: 1000, fallback: Text("unavailable")) |
| 25 | + Settings(timeout_ms: 1000, fallback: Text("unavailable"), nonce: nonce ()) |
| 26 | +} |
| 27 | + |
| 28 | +fn nonce :: -> String |
| 29 | +{ |
| 30 | + "bm9uY2Utb2YtdGhpcy1yZXF1ZXN0" |
| 25 | 31 | } |
| 26 | 32 | |
| 27 | 33 | fn text :: resp: Response -> String / <io, proc, clock, random, ffi> |
| @@ -66,13 +72,11 @@ fn a_part_streams_after_the_page_and_before_its_end :: |
| 66 | 72 | parts.start started (Text("…")) fn { Node("ul", [], [Text("labels")]) } |
| 67 | 73 | let whole = page [shown] |
| 68 | 74 | let resp = parts.respond started (settings ()) whole |
| 69 | | − let nonce = nonce_of resp |
| 70 | | − assert (string.length nonce >= 16) "a fresh nonce: {nonce}" |
| 71 | 75 | let rendered = |
| 72 | 76 | option.unwrap_or |
| 73 | 77 | (http.whole_body whole |> option.map bytes.to_string_lossy) |
| 74 | 78 | "" |
| 75 | | − match swap.opened rendered nonce |
| 79 | + match swap.opened rendered (nonce ()) |
| 76 | 80 | { Some((first, last)) -> |
| 77 | 81 | assert_equal |
| 78 | 82 | (text resp) |
| @@ -159,3 +163,47 @@ fn a_page_of_any_status_streams_its_parts :: |
| 159 | 163 | let body = text resp |
| 160 | 164 | assert (string.contains body (swap.part (id_of shown) "labels")) body |
| 161 | 165 | } |
| 166 | + |
| 167 | +@test |
| 168 | +fn the_streamed_page_carries_the_nonce_of_its_settings :: |
| 169 | +{ |
| 170 | + let started = parts.new () |
| 171 | + let shown = parts.start started (Text("…")) fn { Text("labels") } |
| 172 | + let resp = parts.respond started (settings ()) (page [shown]) |
| 173 | + assert_equal (nonce_of resp) (nonce ()) |
| 174 | + let body = text resp |
| 175 | + assert (string.contains body (swap.script (nonce ()))) body |
| 176 | +} |
| 177 | + |
| 178 | +@test |
| 179 | +fn a_page_that_allows_the_nonce_already_allows_it_once :: |
| 180 | +{ |
| 181 | + let started = parts.new () |
| 182 | + let shown = parts.start started (Text("…")) fn { Text("labels") } |
| 183 | + let allowed = security.with_script_nonce (page [shown]) (nonce ()) |
| 184 | + let resp = parts.respond started (settings ()) allowed |
| 185 | + assert_equal |
| 186 | + (header resp "Content-Security-Policy") |
| 187 | + (header allowed "Content-Security-Policy") |
| 188 | + let _ = text resp |
| 189 | + () |
| 190 | +} |
| 191 | + |
| 192 | +@test |
| 193 | +fn a_page_whose_head_holds_the_script_streams_it_once :: |
| 194 | +{ |
| 195 | + let started = parts.new () |
| 196 | + let shown = parts.start started (Text("…")) fn { Text("labels") } |
| 197 | + let script = swap.script (nonce ()) |
| 198 | + let document = "<html><head>{script}</head><body>{html.render shown}</body></html>" |
| 199 | + let resp = |
| 200 | + parts.respond |
| 201 | + started |
| 202 | + (settings ()) |
| 203 | + ( security.harden |
| 204 | + (http.response 200 "text/html" (bytes.from_string document)) |
| 205 | + ) |
| 206 | + let body = text resp |
| 207 | + assert_equal (list.length (string.split body script)) 2 |
| 208 | + assert (string.contains body (swap.part (id_of shown) "labels")) body |
| 209 | +} |