tobogganSe connecter

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.

38 lignes · 1030 octetsBrut · Historique
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
5import std/http.{type Request}
6import 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.
11pub 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
30fn safe :: method: String -> Bool
31{
32 method == "GET" || method == "HEAD" || method == "OPTIONS"
33}
34
35fn same_host :: origin: String host: String -> Bool
36{
37 host != "" && (origin == "https://" <> host || origin == "http://" <> host)
38}