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 | /// A guard against cross-site request forgery, from the headers browsers |
| 2 | /// send: `Sec-Fetch-Site` when there is one, `Origin` against `Host` |
| 3 | /// otherwise. |
| 4 | |
| 5 | import std/http.{type Request} |
| 6 | import std/option |
| 7 | |
| 8 | /// Whether `req` changes something (any method but GET, HEAD and OPTIONS) |
| 9 | /// on behalf of a page from another site. A request without either header |
| 10 | /// does not come from a browser page, and passes. |
| 11 | pub fn cross_site :: req: Request -> Bool |
| 12 | { |
| 13 | if safe req.method |
| 14 | { |
| 15 | False |
| 16 | } |
| 17 | else |
| 18 | { |
| 19 | match http.header req "Sec-Fetch-Site" |
| 20 | { Some(site) -> site != "same-origin" && site != "none" |
| 21 | , None -> match http.header req "Origin" |
| 22 | { Some(origin) -> |
| 23 | !same_host origin (http.header req "Host" |> option.unwrap_or "") |
| 24 | , None -> False |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | fn safe :: method: String -> Bool |
| 31 | { |
| 32 | method == "GET" || method == "HEAD" || method == "OPTIONS" |
| 33 | } |
| 34 | |
| 35 | fn same_host :: origin: String host: String -> Bool |
| 36 | { |
| 37 | host != "" && (origin == "https://" <> host || origin == "http://" <> host) |
| 38 | } |