oficina / home Public

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

Plain git — no account needed to clone.

Download

Download this file

1package connect
2
3import (
4 "amadan.net/oficina/home/handoff"
5 "html/template"
6 "net/http"
7 "net/http/httptest"
8 "strings"
9 "testing"
10)
11
12func TestRouteGuardExemptsOnlyCallbackAndPreservesPolicy(t *testing.T) {
13 b := &Bridge{render: func(w http.ResponseWriter, r *http.Request, state, label string, status int) { w.WriteHeader(status) }}
14 h := &Routes{bridge: b, cfg: RoutesConfig{Origin: "https://slides.example.com", Issuer: "https://home.example.com", SessionCookie: "__Host-session", FormPolicy: "default-src 'none'; form-action 'self' https://home.example.com", Protect: func(next http.Handler) http.Handler {
15 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
16 if r.Method == "POST" {
17 w.WriteHeader(403)
18 return
19 }
20 next.ServeHTTP(w, r)
21 })
22 }}}
23 called := 0
24 handler := h.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { called++; w.WriteHeader(204) }))
25 for _, tc := range []struct {
26 method, origin, path string
27 dup bool
28 want int
29 }{{"POST", h.cfg.Origin, handoff.CallbackPath, false, 204}, {"POST", h.cfg.Origin, handoff.CallbackPath + "/", false, 403}, {"POST", h.cfg.Origin, "/account/home", false, 403}, {"GET", "https://wrong.example.com", handoff.CallbackPath, false, 404}, {"GET", h.cfg.Origin, "/", true, 400}, {"GET", h.cfg.Origin, "/", false, 204}} {
30 r := httptest.NewRequest(tc.method, tc.origin+tc.path, nil)
31 if tc.dup {
32 r.AddCookie(&http.Cookie{Name: h.cfg.SessionCookie, Value: "a"})
33 r.AddCookie(&http.Cookie{Name: h.cfg.SessionCookie, Value: "b"})
34 }
35 w := httptest.NewRecorder()
36 handler.ServeHTTP(w, r)
37 if w.Code != tc.want {
38 t.Fatalf("%s %s: %d", tc.method, tc.path, w.Code)
39 }
40 if w.Header().Get("Content-Security-Policy") != h.cfg.FormPolicy {
41 t.Fatal("app policy replaced")
42 }
43 }
44 if called != 2 {
45 t.Fatal("guard bypass", called)
46 }
47 localCalls := 0
48 local := h.LocalGuard(func(w http.ResponseWriter, r *http.Request) { localCalls++ })
49 r := httptest.NewRequest("GET", h.cfg.Origin+"/auth/verify", nil)
50 r.AddCookie(&http.Cookie{Name: "__Host-rastrillo_pending", Value: "a"})
51 r.AddCookie(&http.Cookie{Name: "__Host-rastrillo_pending", Value: "b"})
52 w := httptest.NewRecorder()
53 local(w, r)
54 if w.Code != 400 || localCalls != 0 {
55 t.Fatal("ambiguous local pending reached plugin")
56 }
57}
58
59// TestSignoutEndsLocallyAndClearsHandoffPointers: with no issuer (the
60// standalone shape) sign-out ends here, as it always did.
61func TestSignoutEndsLocallyAndClearsHandoffPointers(t *testing.T) {
62 revoked := false
63 h := &Routes{cfg: RoutesConfig{SignOut: func(http.ResponseWriter, *http.Request) { revoked = true }}}
64 w := httptest.NewRecorder()
65 h.Signout(w, httptest.NewRequest("POST", "https://slides.example.com/signout", nil))
66 if !revoked || w.Code != 303 || w.Header().Get("Location") != "/signed-out" {
67 t.Fatal("signout restarted Home exchange")
68 }
69 assertSignoutCleared(t, w)
70}
71
72// TestSignoutChainsToTheIssuer is spec shared-chrome §9.5: an app's Sign
73// out cleared only the app's session, so its "Sign in" passed silently
74// through the live 30-day id.* session. With Home configured it now ends
75// the app session and hands the same POST on to {Issuer}/signout, which
76// ends the id.* session and returns to this origin's /signed-out.
77//
78// 307, not 303, so the hop stays a POST: a GET sign-out is one any <img>
79// can trigger. The Referrer-Policy is set on the 307 itself because the
80// browser nulls Origin on the redirected POST and Referer, which this
81// policy makes exactly the app's origin, is the evidence id.* admits it
82// by. FormPolicy already names the issuer in form-action, which browsers
83// enforce on a form's redirect target.
84func TestSignoutChainsToTheIssuer(t *testing.T) {
85 revoked := false
86 h := &Routes{cfg: RoutesConfig{Origin: "https://blue.slides.oficina.works", Issuer: "https://id.oficina.works", SignOut: func(http.ResponseWriter, *http.Request) { revoked = true }}}
87 w := httptest.NewRecorder()
88 h.Signout(w, httptest.NewRequest("POST", "https://blue.slides.oficina.works/signout?next=https://evil.example", nil))
89 if !revoked {
90 t.Fatal("app session survived")
91 }
92 if w.Code != http.StatusTemporaryRedirect || w.Header().Get("Location") != "https://id.oficina.works/signout" {
93 t.Fatalf("sign-out not chained: %d %q", w.Code, w.Header().Get("Location"))
94 }
95 if w.Header().Get("Referrer-Policy") != "strict-origin-when-cross-origin" {
96 t.Fatal("chained hop may lose its only evidence", w.Header().Get("Referrer-Policy"))
97 }
98 assertSignoutCleared(t, w)
99 if !strings.Contains(FormPolicy(h.cfg.Issuer), "form-action 'self' https://id.oficina.works") {
100 t.Fatal("form-action would block the chained hop")
101 }
102}
103
104func assertSignoutCleared(t *testing.T, w *httptest.ResponseRecorder) {
105 t.Helper()
106 if w.Header().Get("Cache-Control") != "no-store" {
107 t.Fatal("sign-out cacheable")
108 }
109 if len(w.Result().Cookies()) != 7 {
110 t.Fatal("handoff pointers left behind")
111 }
112 choice := false
113 for _, c := range w.Result().Cookies() {
114 if c.MaxAge != -1 || !c.Secure || !c.HttpOnly {
115 t.Fatal("cookie not cleared safely")
116 }
117 choice = choice || c.Name == homeChoiceCookie
118 }
119 if !choice {
120 t.Fatal("a pending member choice survived sign-out")
121 }
122}
123
124func TestStaleSessionStillRefusesAndOffersExplicitLocalRecovery(t *testing.T) {
125 f := newBridgeFixture(t)
126 templates := template.Must(template.ParseFS(Templates(), "templates/*.html"))
127 f.b.render = func(w http.ResponseWriter, r *http.Request, state, label string, status int) {
128 w.WriteHeader(status)
129 if err := templates.ExecuteTemplate(w, "home-connect", Page(state, label)); err != nil {
130 t.Fatal(err)
131 }
132 }
133 token, err := f.sessions.Mint(Session{Subject: "retired-session", Method: "magiclink"})
134 if err != nil {
135 t.Fatal(err)
136 }
137 delete(f.sessions.values, token)
138 r := bridgeRequest("GET", "/auth/start", []*http.Cookie{{Name: f.sessions.cookie, Value: token}})
139 w := httptest.NewRecorder()
140 f.b.Start(w, r)
141 if w.Code != 403 || w.Header().Get("Location") != "" {
142 t.Fatal("stale cookie guard relaxed", w.Code)
143 }
144 if !strings.Contains(w.Body.String(), `href="/signin/local"`) || !strings.Contains(w.Body.String(), English["local_action"]) {
145 t.Fatal("no explicit local recovery", w.Body.String())
146 }
147 if len(w.Result().Cookies()) != 0 || len(f.sessions.values) != 0 {
148 t.Fatal("failure changed session state")
149 }
150}
151