oficina / meet Public

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

Plain git — no account needed to clone.

Download

Download this file

1package callstest
2
3import (
4 "io"
5 "log/slog"
6 "net/http"
7 "net/http/httptest"
8 "net/url"
9 "strings"
10 "testing"
11
12 "amadan.net/rastrillo/rastrillo"
13
14 "amadan.net/oficina/calls/internal/calls"
15)
16
17// TestEmailedLinkOpensTheConfirmDoor walks the magic-link sign-in end to
18// end: begin, the mail, GET of the emailed link twice (the confirm door,
19// which spends nothing), a cross-site POST that is refused without
20// spending it, the door's POST, a session that opens the list, and the
21// spent link answering ?err=expired.
22//
23// GET /auth/verify used to sign in on sight, so a mail-security gateway
24// that fetches every link in an inbound message (Safe Links, Proofpoint)
25// spent it before the person clicked, and they were told it had expired.
26// rastrillo now confirms on GET and redeems on POST; this pins that the
27// confirm step is Calls' own door with one Sign in button, not
28// rastrillo's plain English fallback, and that the POST is mounted.
29func TestEmailedLinkOpensTheConfirmDoor(t *testing.T) {
30 x := newHarness(t, "paul@example.com")
31 c := x.browser(t)
32 if rec := c.post("/signin", url.Values{"address": {"paul@example.com"}}); rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/signin?sent=1" {
33 t.Fatalf("POST /signin = %d %q, want 303 /signin?sent=1", rec.Code, rec.Header().Get("Location"))
34 }
35 _, link := x.mail.last()
36 u, err := url.Parse(link)
37 if err != nil || link == "" {
38 t.Fatalf("emailed link %q: %v", link, err)
39 }
40
41 // Opened twice, as a scanner and then the person would. Each is a
42 // fresh browser, so a cookie one GET set could not help the next.
43 for _, who := range []string{"scanner", "person"} {
44 rec := x.browser(t).get(u.RequestURI())
45 if rec.Code != http.StatusOK || len(rec.Result().Cookies()) != 0 {
46 t.Fatalf("%s's GET = %d with %d cookies, want 200 and nothing minted", who, rec.Code, len(rec.Result().Cookies()))
47 }
48 body := rec.Body.String()
49 for _, want := range []string{"estilo-door", "estilo-signin-form", "<span>Calls</span>", "Select Sign in to finish signing in.", `name="token" value="` + u.Query().Get("token") + `"`, `action="/auth/verify"`} {
50 if !strings.Contains(body, want) {
51 t.Errorf("%s: confirm door lacks %q:\n%s", who, want, body)
52 }
53 }
54 if n := strings.Count(body, "<button"); n != 1 || !strings.Contains(body, `type="submit" autofocus>Sign in</button>`) {
55 t.Errorf("%s: confirm door has %d buttons, want exactly one Sign in:\n%s", who, n, body)
56 }
57 if strings.Contains(body, `name="address"`) {
58 t.Errorf("%s: confirm door asks for an address again", who)
59 }
60 // auth sets these before RenderConfirm; render must not undo them.
61 for header, want := range map[string]string{
62 "Cache-Control": "no-store",
63 "Referrer-Policy": "strict-origin",
64 "X-Frame-Options": "DENY",
65 } {
66 if got := rec.Header().Get(header); got != want {
67 t.Errorf("%s: %s = %q, want %q", who, header, got, want)
68 }
69 }
70 }
71 if n := x.authLinks(t); n != 1 {
72 t.Fatalf("auth_links after two GETs: %d, want the one link still unspent", n)
73 }
74
75 // A post without a browser's same-origin evidence is refused and
76 // spends nothing.
77 action, token := c.open(link)
78 cross := httptest.NewRequest(http.MethodPost, action, strings.NewReader(url.Values{"token": {token}}.Encode()))
79 cross.Header.Set("Content-Type", "application/x-www-form-urlencoded")
80 cross.Header.Set("Sec-Fetch-Site", "cross-site")
81 if rec := c.do(cross); rec.Code != http.StatusForbidden || len(rec.Result().Cookies()) != 0 {
82 t.Fatalf("cross-site POST = %d with %d cookies, want 403 and nothing minted", rec.Code, len(rec.Result().Cookies()))
83 }
84 if n := x.authLinks(t); n != 1 {
85 t.Fatalf("auth_links after a refused cross-site POST: %d, want the link still unspent", n)
86 }
87
88 // Sign in: the POST spends the link and mints the session.
89 signed := c.press(action, token)
90 if signed.Code != http.StatusSeeOther || signed.Header().Get("Location") != "/continue" {
91 t.Fatalf("POST %s = %d %q, want 303 /continue", action, signed.Code, signed.Header().Get("Location"))
92 }
93 if rec := c.get("/continue"); rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/" {
94 t.Fatalf("GET /continue = %d %q, want 303 /", rec.Code, rec.Header().Get("Location"))
95 }
96 if rec := c.get("/"); rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "Meetings") {
97 t.Fatalf("GET / with the new session = %d", rec.Code)
98 }
99
100 // Spent: the door still opens (it looks nothing up), but its button
101 // now says the link expired, and mints nothing.
102 again := x.browser(t)
103 replay := again.press(again.open(link))
104 if len(replay.Result().Cookies()) != 0 || replay.Header().Get("Location") != "/signin?err=expired" {
105 t.Fatalf("second POST = %d %q, want /signin?err=expired and no session", replay.Code, replay.Header().Get("Location"))
106 }
107}
108
109// productionHandler is the app behind rastrillo's serving stack, built
110// from the same calls.ServeOptions main hands to rastrillo.Serve:
111// security headers, CSP and all. The bare mux carries no CSP, so a test
112// that serves only the mux cannot see what a browser is told.
113func productionHandler(t *testing.T, x *harness) http.Handler {
114 t.Helper()
115 h, closeFn, err := rastrillo.Handler(calls.ServeOptions(rastrillo.Options{Logger: slog.New(slog.NewTextHandler(io.Discard, nil))}, x.h.(*http.ServeMux), x.ctl.Runtime()))
116 if err != nil {
117 t.Fatalf("rastrillo.Handler: %v", err)
118 }
119 t.Cleanup(func() { _ = closeFn() })
120 return h
121}
122
123// frameworkDefaultCSP is rastrillo's own policy, read off a handler
124// built with no CSP and no app.
125func frameworkDefaultCSP(t *testing.T) string {
126 t.Helper()
127 bare, closeFn, err := rastrillo.Handler(rastrillo.Options{Mux: http.NewServeMux(), Logger: slog.New(slog.NewTextHandler(io.Discard, nil))})
128 if err != nil {
129 t.Fatal(err)
130 }
131 defer closeFn()
132 rec := httptest.NewRecorder()
133 bare.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/healthz", nil))
134 policy := rec.Header().Get("Content-Security-Policy")
135 if policy == "" {
136 t.Fatal("rastrillo set no default CSP")
137 }
138 return policy
139}
140
141// TestProductionServesRastrillosDefaultCSP: standalone, every page Calls
142// serves through the production stack carries exactly rastrillo's
143// default policy, the one that refuses inline styles (serving.go says
144// why Calls needs nothing looser). A bump that changes the default is
145// followed, not overridden; a change here that widened style-src would
146// fail.
147func TestProductionServesRastrillosDefaultCSP(t *testing.T) {
148 want := frameworkDefaultCSP(t)
149 if strings.Contains(want, "'unsafe-inline'") {
150 t.Fatalf("rastrillo's default allows inline again (%q): revisit serving.go", want)
151 }
152 x, sqid, _ := memberWithMeeting(t)
153 h := productionHandler(t, x)
154 member := x.browser(t)
155 member.signIn("paul@example.com")
156 for _, path := range []string{"/signin", "/", "/m/" + sqid + "/join", "/m/" + sqid, "/settings", "/help/open-source"} {
157 req := httptest.NewRequest(http.MethodGet, path, nil)
158 if path != "/signin" {
159 for _, ck := range member.cookies {
160 req.AddCookie(ck)
161 }
162 }
163 rec := httptest.NewRecorder()
164 h.ServeHTTP(rec, req)
165 if rec.Code != http.StatusOK {
166 t.Fatalf("GET %s through the production stack = %d", path, rec.Code)
167 }
168 if got := rec.Header().Values("Content-Security-Policy"); len(got) != 1 || got[0] != want {
169 t.Errorf("GET %s: CSP = %q, want exactly rastrillo's default %q", path, got, want)
170 }
171 if strings.Contains(rec.Body.String(), " style=\"") {
172 t.Errorf("GET %s: the page carries a style attribute, which this policy refuses", path)
173 }
174 }
175}
176
177// TestTheConfirmDoorKeepsEveryPolicy: the confirm door through the
178// production stack carries rastrillo's policy and auth's added
179// frame-ancestors 'none' (browsers enforce every CSP header, so the
180// second only narrows), and auth's no-store, strict-origin referrer and
181// frame denial survive the render.
182func TestTheConfirmDoorKeepsEveryPolicy(t *testing.T) {
183 x := newHarness(t, "paul@example.com")
184 h := productionHandler(t, x)
185 x.browser(t).post("/signin", url.Values{"address": {"paul@example.com"}})
186 _, link := x.mail.last()
187 u, err := url.Parse(link)
188 if err != nil {
189 t.Fatal(err)
190 }
191 rec := httptest.NewRecorder()
192 h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, u.RequestURI(), nil))
193 if rec.Code != http.StatusOK || !confirmForm.MatchString(rec.Body.String()) {
194 t.Fatalf("confirm door through the production stack = %d, or no confirm form", rec.Code)
195 }
196 got := rec.Header().Values("Content-Security-Policy")
197 if want := frameworkDefaultCSP(t); len(got) != 2 || got[0] != want || got[1] != "frame-ancestors 'none'" {
198 t.Fatalf("confirm door CSP = %q, want rastrillo's default then frame-ancestors 'none'", got)
199 }
200 for header, want := range map[string]string{"X-Frame-Options": "DENY", "Referrer-Policy": "strict-origin", "Cache-Control": "no-store"} {
201 if v := rec.Header().Get(header); v != want {
202 t.Errorf("%s = %q, want %q", header, v, want)
203 }
204 }
205}
206