oficina / home Public

Clone
git clone https://amadan.net/oficina/home

Plain git — no account needed to clone.

Download

Download this file

1package home
2
3import (
4 "encoding/xml"
5 "io"
6 "net/http"
7 "net/http/httptest"
8 "strings"
9 "testing"
10)
11
12func TestKeymailSignInContinuesWithoutExternalPostRedirect(t *testing.T) {
13 app := newTestApp(t)
14 const addr = "person@keymail.invalid"
15 stub := newKeymailStub(t, app, addr)
16 policy := app.SecurityPolicy()
17 rec := signIn(t, app, addr)
18 if rec.Code != http.StatusOK {
19 t.Fatalf("POST sign-in must end before external navigation: %d", rec.Code)
20 }
21 if to := doorContinuation(t, rec); !strings.HasPrefix(to, stub.srv.URL+"/") {
22 t.Fatal("missing exact provider continuation")
23 }
24 if app.SecurityPolicy() != policy || strings.Contains(policy, stub.host) {
25 t.Fatal("CSP broadened to provider")
26 }
27 if sessionCookie(rec) != nil {
28 t.Fatal("continuation minted a session")
29 }
30 var pending *http.Cookie
31 for _, c := range rec.Result().Cookies() {
32 if c.Name == pendingCookie {
33 pending = c
34 }
35 }
36 if pending == nil || pending.Value == "" || !pending.HttpOnly || !pending.Secure || pending.SameSite != http.SameSiteLaxMode {
37 t.Fatal("pending cookie lost")
38 }
39}
40
41// doorContinuation is the Keymail authorize URL the door's Continue step
42// (rastrillo's "Taking you to Keymail") hands the browser, checked: a 200
43// that is not cacheable and sends no referrer from the page (the
44// header covers every link on it, as rastrillo's PrepareSigninResponse
45// does), exactly one meta refresh and exactly one primary link, both to
46// that URL, written through attribute escaping, and no script but
47// estilo's busy rule.
48func doorContinuation(t *testing.T, r *httptest.ResponseRecorder) string {
49 t.Helper()
50 h := r.Header()
51 if r.Code != 200 || h.Get("Cache-Control") != "no-store" || h.Get("Referrer-Policy") != "no-referrer" || h.Get("Location") != "" || h.Get("Refresh") != "" {
52 t.Fatal("continuation response", r.Code, h)
53 }
54 body := r.Body.String()
55 var refresh, links []string
56 d := xml.NewDecoder(strings.NewReader(body))
57 d.Strict, d.AutoClose, d.Entity = false, xml.HTMLAutoClose, xml.HTMLEntity
58 for {
59 token, err := d.Token()
60 if err == io.EOF {
61 break
62 }
63 if err != nil {
64 t.Fatal(err)
65 }
66 el, ok := token.(xml.StartElement)
67 if !ok {
68 continue
69 }
70 a := map[string]string{}
71 for _, v := range el.Attr {
72 a[v.Name.Local] = v.Value
73 }
74 if el.Name.Local == "meta" && strings.EqualFold(a["http-equiv"], "refresh") {
75 refresh = append(refresh, a["content"])
76 }
77 if el.Name.Local == "a" && a["rst-btn"] == "primary block" {
78 links = append(links, a["href"])
79 }
80 }
81 if len(links) != 1 || len(refresh) != 1 || refresh[0] != "0;url="+links[0] || strings.Contains(body, "ZgotmplZ") || hasOtherScript(body) ||
82 !strings.Contains(body, "Taking you to Keymail") {
83 t.Fatal(refresh, links, body)
84 }
85 return links[0]
86}
87