oficina / meet Public

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

Plain git — no account needed to clone.

Download

Download this file

1package calls
2
3// The return memory across the door (signin_return.go): the page a sign-in
4// began from, kept Calls' way so /continue (a pending meeting join first)
5// is never skipped, with every protection the earlier reviews asked for.
6
7import (
8 "net/http"
9 "net/http/httptest"
10 "net/url"
11 "strings"
12 "testing"
13 "time"
14)
15
16// The door's form posts return_to; Begin never sees it; the sign-in lands
17// on the page through /continue.
18func TestTheDoorLandsOnThePageItBeganFrom(t *testing.T) {
19 doorModes(t, func(t *testing.T, f *homeRouterFixture) {
20 page := f.get("/signin?return_to=%2Fsettings")
21 // The one way in carries the page: the email form standalone, and
22 // Continue with Oficina ID with Home.
23 if body := page.Body.String(); !strings.Contains(body, `name="return_to" value="/settings"`) && !strings.Contains(body, `/signin/oficina?return_to=%2Fsettings`) {
24 t.Fatalf("the door does not carry the page:\n%s", door(t, body))
25 }
26 if got := f.emailSignin(t, url.Values{"address": {"alice@example.test"}, "return_to": {"/settings"}}); got != "/settings" {
27 t.Fatalf("landed on %q, want /settings", got)
28 }
29 })
30}
31
32// Begin's redirect to the door gets return_to back, so the next form posts
33// it again; and Begin itself is never handed it (rastrillo would land the
34// session there, skipping /continue).
35func TestBeginNeverSeesReturnToButTheDoorKeepsIt(t *testing.T) {
36 doorModes(t, func(t *testing.T, f *homeRouterFixture) {
37 sent := f.do(http.MethodPost, "/signin", url.Values{"address": {"alice@example.test"}, "return_to": {"/settings"}}.Encode())
38 loc := sent.Header().Get("Location")
39 if u, _ := url.Parse(loc); u == nil || u.Query().Get("return_to") != "/settings" || u.Query().Get("sent") != "1" {
40 t.Fatalf("Sent = %q, want the page carried", loc)
41 }
42 if f.returnPath() != "/settings" {
43 t.Fatalf("the return memory holds %q", f.returnPath())
44 }
45 // rastrillo's own kept destination is never set.
46 for _, c := range sent.Result().Cookies() {
47 if strings.Contains(c.Name, "rastrillo_return") && c.MaxAge >= 0 && c.Value != "" {
48 t.Fatalf("Begin kept a destination of its own: %s", c.Name)
49 }
50 }
51 bad := f.do(http.MethodPost, "/signin", url.Values{"address": {"not-an-email@"}, "return_to": {"/settings"}}.Encode())
52 if u, _ := url.Parse(bad.Header().Get("Location")); u == nil || u.Query().Get("err") != "address" || u.Query().Get("return_to") != "/settings" {
53 t.Fatalf("a bad address = %q, want the problem with the page carried", bad.Header().Get("Location"))
54 }
55 })
56}
57
58// A post without return_to is a sign-in for /continue's own landing: a page
59// an earlier, abandoned attempt kept does not win.
60func TestAPostWithoutReturnToForgetsTheKeptPage(t *testing.T) {
61 doorModes(t, func(t *testing.T, f *homeRouterFixture) {
62 f.get("/signin?return_to=%2Fsettings")
63 if got := f.emailSignin(t, url.Values{"address": {"alice@example.test"}}); got != "/" {
64 t.Fatalf("landed on %q, want /", got)
65 }
66 })
67}
68
69// Off the form even when the body does not parse: nothing reaches Begin,
70// and nothing is kept.
71func TestReturnToComesOffEvenOnAParseError(t *testing.T) {
72 doorModes(t, func(t *testing.T, f *homeRouterFixture) {
73 f.get("/signin?return_to=%2Fsettings")
74 w := f.do(http.MethodPost, "/signin", "return_to=%2Fsettings&address=%zz")
75 if strings.Contains(w.Header().Get("Location"), "return_to") || f.returnPath() != "" {
76 t.Fatalf("a malformed post kept the page: %q, memory %q", w.Header().Get("Location"), f.returnPath())
77 }
78 })
79}
80
81// A plain /signin forgets a kept page; a plain /signin/local (where connect
82// sends people mid-way through a sign-in) keeps it and carries it on its
83// form.
84func TestAPlainSigninLocalCarriesTheKeptPage(t *testing.T) {
85 f := newHomeRouterFixture(t)
86 f.get("/signin?return_to=%2Fsettings")
87 page := f.get("/signin/local")
88 if !strings.Contains(page.Body.String(), `name="return_to" value="/settings"`) || f.returnPath() != "/settings" {
89 t.Fatalf("/signin/local lost the page (memory %q):\n%s", f.returnPath(), door(t, page.Body.String()))
90 }
91 f.get("/signin")
92 if f.returnPath() != "" {
93 t.Fatalf("a plain /signin kept %q", f.returnPath())
94 }
95}
96
97// Only a same-origin page is ever kept: anything else, and the signed-out
98// page, read as no page.
99func TestOnlyASafePageIsKept(t *testing.T) {
100 doorModes(t, func(t *testing.T, f *homeRouterFixture) {
101 for _, bad := range []string{"//evil.example/x", "https://evil.example/", `/\evil.example`, "/signed-out", "/signed-out?x=1", "/signin", "/auth/verify", "/j/sometoken"} {
102 br := f.fresh()
103 br.get("/signin?return_to=" + url.QueryEscape(bad))
104 if br.returnPath() != "" {
105 t.Errorf("GET /signin?return_to=%s kept %q", bad, br.returnPath())
106 }
107 br.do(http.MethodPost, "/signin", url.Values{"address": {"alice@example.test"}, "return_to": {bad}}.Encode())
108 if br.returnPath() != "" {
109 t.Errorf("POST return_to=%s kept %q", bad, br.returnPath())
110 }
111 }
112 })
113}
114
115// A tampered, expired or duplicated memory reads as none.
116func TestABrokenMemoryReadsAsNone(t *testing.T) {
117 f := newStandaloneDoor(t)
118 w := httptest.NewRecorder()
119 f.app.rememberReturn(w, "/settings")
120 kept := w.Result().Cookies()[0]
121 read := func(cookies ...*http.Cookie) string {
122 r := httptest.NewRequest(http.MethodGet, homeTestOrigin+"/", nil)
123 for _, c := range cookies {
124 r.AddCookie(c)
125 }
126 p, _ := f.app.rememberedReturn(r)
127 return p
128 }
129 if read(kept) != "/settings" {
130 t.Fatal("the memory does not read back")
131 }
132 tampered := *kept
133 tampered.Value = kept.Value[:len(kept.Value)-2] + "AA"
134 if read(&tampered) != "" || read(kept, kept) != "" {
135 t.Fatal("a tampered or duplicated memory was read")
136 }
137 f.app.store.clk.set(func() time.Time { return time.Now().Add(time.Hour) })
138 if read(kept) != "" {
139 t.Fatal("an expired memory was read")
140 }
141}
142
143// Forget and Sign out end a kept page: it was this person's, not the next
144// one's.
145func TestForgetAndSignOutClearTheKeptPage(t *testing.T) {
146 doorModes(t, func(t *testing.T, f *homeRouterFixture) {
147 f.get("/signin?return_to=%2Fsettings")
148 w := f.do(http.MethodPost, signinForgetPath, "")
149 if !clears(w, f.app.returnCookieName()) {
150 t.Fatal("Forget kept the page")
151 }
152 f.get("/signin?return_to=%2Fsettings")
153 f.session(t, "alice@example.test", "magiclink", time.Now())
154 w = f.do(http.MethodPost, "/signout", "")
155 if !clears(w, f.app.returnCookieName()) {
156 t.Fatal("Sign out kept the page")
157 }
158 })
159}
160
161// The link step-up end to end: a stale local session asked to connect its
162// Oficina account goes to the email-only door with reauth=1, a bad address
163// and Sent both keep it, and the fresh sign-in lands back on the link
164// page.
165func TestTheLinkStepUpStaysLocalEndToEnd(t *testing.T) {
166 f := newHomeRouterFixture(t)
167 f.session(t, "alice@example.test", "magiclink", time.Now().Add(-time.Hour))
168 // The persons row the first page makes.
169 if w := f.get("/"); w.Code != http.StatusOK {
170 t.Fatalf("alice's first page: %d", w.Code)
171 }
172 step := f.get("/account/home")
173 loc := step.Header().Get("Location")
174 if step.Code != http.StatusSeeOther || !strings.HasPrefix(loc, "/signin/local?") || !strings.Contains(loc, "reauth=1") {
175 t.Fatalf("a stale session at /account/home = %d %q, want the local door with reauth=1", step.Code, loc)
176 }
177 card := door(t, f.get(loc).Body.String())
178 if strings.Contains(card, oficinaButton) || !strings.Contains(card, `name="reauth" value="1"`) {
179 t.Fatalf("the step-up door:\n%s", card)
180 }
181 for _, address := range []string{"not-an-email@", "alice@example.test"} {
182 w := f.do(http.MethodPost, "/signin", url.Values{"address": {address}, "reauth": {"1"}, "return_to": {"/account/home"}}.Encode())
183 u, _ := url.Parse(w.Header().Get("Location"))
184 if u == nil || u.Query().Get("reauth") != "1" || u.Query().Get("return_to") != "/account/home" {
185 t.Fatalf("POST %s = %q, want reauth=1 and the page kept", address, w.Header().Get("Location"))
186 }
187 if card := door(t, f.get(w.Header().Get("Location")).Body.String()); strings.Contains(card, oficinaButton) {
188 t.Fatalf("the step-up's next door offers Oficina ID:\n%s", card)
189 }
190 }
191 link := signinLink.FindString(f.mailBody())
192 if link == "" {
193 t.Fatal("no link")
194 }
195 next := homeRedirect(t, f.followLink(t, link))
196 for range 3 {
197 if next == "/account/home" {
198 break
199 }
200 next = homeRedirect(t, f.get(next))
201 }
202 if next != "/account/home" {
203 t.Fatalf("the fresh sign-in landed on %q", next)
204 }
205 if w := f.get("/account/home"); w.Code != http.StatusOK {
206 t.Fatalf("the link page after the step-up = %d", w.Code)
207 }
208}
209