packages
The Louise packages, each released on its own
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#28 · main · 3 min 34 s
0Étoiles
0Forks
3Issues
| 1 | import requests.{header} |
| 2 | import security.{type Policy, Policy} |
| 3 | import std/http |
| 4 | import std/list |
| 5 | import std/string |
| 6 | import std/test.{assert, assert_equal} |
| 7 | |
| 8 | @test |
| 9 | fn every_response_gets_a_strict_content_security_policy :: |
| 10 | { |
| 11 | let resp = security.harden (http.text 200 "ok") |
| 12 | let policy = header resp "Content-Security-Policy" |
| 13 | assert (string.contains policy "default-src 'self'") policy |
| 14 | assert (string.contains policy "script-src 'self';") policy |
| 15 | assert (string.contains policy "frame-ancestors 'none'") policy |
| 16 | assert (!string.contains policy "unsafe") policy |
| 17 | } |
| 18 | |
| 19 | @test |
| 20 | fn every_response_refuses_sniffing_and_framing :: |
| 21 | { |
| 22 | let resp = security.harden (http.text 200 "ok") |
| 23 | assert_equal (header resp "X-Content-Type-Options") "nosniff" |
| 24 | assert_equal (header resp "Referrer-Policy") "same-origin" |
| 25 | assert_equal (header resp "Cross-Origin-Opener-Policy") "same-origin" |
| 26 | } |
| 27 | |
| 28 | @test |
| 29 | fn a_nonce_allows_the_scripts_that_carry_it :: |
| 30 | { |
| 31 | let resp = |
| 32 | security.with_script_nonce (security.harden (http.text 200 "ok")) "r4nd0m" |
| 33 | let policy = header resp "Content-Security-Policy" |
| 34 | assert (string.contains policy "script-src 'self' 'nonce-r4nd0m';") policy |
| 35 | assert (string.contains policy "style-src 'self';") policy |
| 36 | assert (!string.contains policy "unsafe") policy |
| 37 | } |
| 38 | |
| 39 | @test |
| 40 | fn a_nonce_changes_only_the_policy :: |
| 41 | { |
| 42 | let hardened = security.harden (http.text 200 "ok") |
| 43 | let resp = security.with_script_nonce hardened "r4nd0m" |
| 44 | assert_equal |
| 45 | (list.filter resp.headers fn h { h.0 != "Content-Security-Policy" }) |
| 46 | (list.filter hardened.headers fn h { h.0 != "Content-Security-Policy" }) |
| 47 | assert_equal (list.length resp.headers) (list.length hardened.headers) |
| 48 | } |
| 49 | |
| 50 | @test |
| 51 | fn a_nonce_allowed_already_is_allowed_once :: |
| 52 | { |
| 53 | let once = |
| 54 | security.with_script_nonce (security.harden (http.text 200 "ok")) "r4nd0m" |
| 55 | let twice = security.with_script_nonce once "r4nd0m" |
| 56 | assert_equal twice.headers once.headers |
| 57 | } |
| 58 | |
| 59 | fn policy :: -> Policy |
| 60 | { |
| 61 | Policy(nonce: "bm9uY2U", trusted_types: ["htmx", "pebble-jsx"], https: True) |
| 62 | } |
| 63 | |
| 64 | @test |
| 65 | fn a_policy_pins_every_header :: |
| 66 | { |
| 67 | let resp = security.harden_with (policy ()) (http.text 200 "ok") |
| 68 | assert_equal |
| 69 | (list.drop resp.headers 1) |
| 70 | [ ( "Content-Security-Policy" |
| 71 | , "default-src 'self'; script-src 'self' 'nonce-bm9uY2U'; style-src 'self'; img-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'none'; form-action 'self'; frame-ancestors 'none'; require-trusted-types-for 'script'; trusted-types htmx pebble-jsx" |
| 72 | ) |
| 73 | , ("Cache-Control", "no-store, no-transform") |
| 74 | , ("Referrer-Policy", "no-referrer") |
| 75 | , ("X-Content-Type-Options", "nosniff") |
| 76 | , ( "Permissions-Policy" |
| 77 | , "camera=(), microphone=(), geolocation=(), payment=(), usb=()" |
| 78 | ) |
| 79 | , ("Cross-Origin-Opener-Policy", "same-origin") |
| 80 | , ("Cross-Origin-Resource-Policy", "same-origin") |
| 81 | , ("Strict-Transport-Security", "max-age=31536000; includeSubDomains") |
| 82 | ] |
| 83 | } |
| 84 | |
| 85 | @test |
| 86 | fn a_site_over_http_gets_no_hsts :: |
| 87 | { |
| 88 | let resp = |
| 89 | security.harden_with Policy(..policy (), https: False) (http.text 200 "ok") |
| 90 | assert_equal (header resp "Strict-Transport-Security") "" |
| 91 | assert_equal (header resp "Cross-Origin-Resource-Policy") "same-origin" |
| 92 | } |
| 93 | |
| 94 | @test |
| 95 | fn without_trusted_type_names_trusted_types_are_not_required :: |
| 96 | { |
| 97 | let resp = |
| 98 | security.harden_with |
| 99 | Policy(..policy (), trusted_types: []) |
| 100 | (http.text 200 "ok") |
| 101 | assert_equal |
| 102 | (header resp "Content-Security-Policy") |
| 103 | "default-src 'self'; script-src 'self' 'nonce-bm9uY2U'; style-src 'self'; img-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'none'; form-action 'self'; frame-ancestors 'none'" |
| 104 | } |
| 105 | |
| 106 | @test |
| 107 | fn a_response_that_sets_its_cache_control_keeps_it :: |
| 108 | { |
| 109 | let cached = |
| 110 | http.with_header (http.text 200 "ok") "cache-control" "max-age=60" |
| 111 | let resp = security.harden_with (policy ()) cached |
| 112 | assert_equal |
| 113 | (list.filter resp.headers fn h { string.to_lower h.0 == "cache-control" }) |
| 114 | [("cache-control", "max-age=60")] |
| 115 | } |
| 116 | |
| 117 | @test |
| 118 | fn a_policy_s_nonce_is_allowed_once :: |
| 119 | { |
| 120 | let hardened = security.harden_with (policy ()) (http.text 200 "ok") |
| 121 | assert_equal |
| 122 | (security.with_script_nonce hardened "bm9uY2U").headers |
| 123 | hardened.headers |
| 124 | } |