| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "html" |
| 5 | "io" |
| 6 | "log/slog" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "net/url" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "github.com/keymaildev/signin" |
| 14 | ) |
| 15 | |
| 16 | // Fichas' tests for Docs' continuation, ported with it. |
| 17 | |
| 18 | const navOrigin = "https://paulca.mapa.example.test" |
| 19 | |
| 20 | func navApp() *app { |
| 21 | return &app{config: Config{Origin: navOrigin}, |
| 22 | logger: slog.New(slog.NewTextHandler(io.Discard, nil))} |
| 23 | } |
| 24 | |
| 25 | func keymailURL(remember bool) string { |
| 26 | keymail := &signin.Keymail{Base: "https://keymail.example.test", |
| 27 | Origin: navOrigin} |
| 28 | return keymail.AuthorizeURL("state", "challenge", remember) |
| 29 | } |
| 30 | |
| 31 | func beginPost() *http.Request { |
| 32 | return httptest.NewRequest(http.MethodPost, "/auth/begin", nil) |
| 33 | } |
| 34 | |
| 35 | // The trap itself: Begin answers with a 303 to keymail, the CSP says |
| 36 | // form-action 'self', and the browser refuses. The continuation ends the |
| 37 | // POST with a page, leaves the CSP alone, and keeps the pending cookie. |
| 38 | func TestKeymailContinuation(t *testing.T) { |
| 39 | destination := keymailURL(true) |
| 40 | handler := navApp().keymailContinuation(func(writer http.ResponseWriter, |
| 41 | request *http.Request) { |
| 42 | http.SetCookie(writer, &http.Cookie{Name: "__Host-pending", |
| 43 | Value: "opaque", Path: "/", Secure: true, HttpOnly: true, |
| 44 | SameSite: http.SameSiteLaxMode}) |
| 45 | http.Redirect(writer, request, destination, http.StatusSeeOther) |
| 46 | }) |
| 47 | recorder := httptest.NewRecorder() |
| 48 | policy := "default-src 'self'; form-action 'self'" |
| 49 | recorder.Header().Set("Content-Security-Policy", policy) |
| 50 | handler(recorder, beginPost()) |
| 51 | headers := recorder.Header() |
| 52 | if recorder.Code != http.StatusOK || headers.Get("Location") != "" { |
| 53 | t.Fatalf("status %d location %q: want a page, not a redirect", |
| 54 | recorder.Code, headers.Get("Location")) |
| 55 | } |
| 56 | if headers.Get("Content-Security-Policy") != policy { |
| 57 | t.Fatal("the CSP changed; widening form-action is what this avoids") |
| 58 | } |
| 59 | if headers.Get("Cache-Control") != "no-store" || |
| 60 | headers.Get("Referrer-Policy") != "no-referrer" { |
| 61 | t.Fatal("the continuation must not cache or leak as a Referer") |
| 62 | } |
| 63 | cookies := recorder.Result().Cookies() |
| 64 | if len(cookies) != 1 || cookies[0].Value != "opaque" || |
| 65 | !cookies[0].Secure || !cookies[0].HttpOnly { |
| 66 | t.Fatalf("lost the pending cookie: %v", cookies) |
| 67 | } |
| 68 | body := html.UnescapeString(recorder.Body.String()) |
| 69 | for _, part := range []string{`http-equiv="refresh"`, |
| 70 | `content="0;url=` + destination + `"`, |
| 71 | `href="` + destination + `"`} { |
| 72 | if !strings.Contains(body, part) { |
| 73 | t.Fatalf("the continuation is missing %q", part) |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // The one place Mapa turns a URL into a link: anything but keymail's |
| 79 | // authorize URL for this origin is refused. |
| 80 | func TestKeymailDestinationRefusals(t *testing.T) { |
| 81 | valid := keymailURL(false) |
| 82 | if !keymailDestination(valid, navOrigin) { |
| 83 | t.Fatalf("refused keymail's own URL: %s", valid) |
| 84 | } |
| 85 | foreign := url.QueryEscape("https://foreign.example.test") |
| 86 | for _, target := range []string{ |
| 87 | "javascript:alert(1)", |
| 88 | "//keymail.example.test/oauth/authorize", |
| 89 | strings.Replace(valid, "https:", "http:", 1), |
| 90 | strings.Replace(valid, "keymail.example", "u@keymail.example", 1), |
| 91 | valid + "#fragment", |
| 92 | valid + "&state=duplicate", |
| 93 | strings.Replace(valid, "/oauth/authorize", "/other", 1), |
| 94 | strings.ReplaceAll(valid, url.QueryEscape(navOrigin), foreign), |
| 95 | strings.Replace(valid, "S256", "plain", 1), |
| 96 | } { |
| 97 | if keymailDestination(target, navOrigin) { |
| 98 | t.Errorf("accepted %q", target) |
| 99 | } |
| 100 | } |
| 101 | recorder := httptest.NewRecorder() |
| 102 | navApp().keymailContinuation(func(writer http.ResponseWriter, |
| 103 | request *http.Request) { |
| 104 | http.Redirect(writer, request, "https://foreign.example.test", |
| 105 | http.StatusSeeOther) |
| 106 | })(recorder, beginPost()) |
| 107 | if recorder.Code != http.StatusBadGateway || |
| 108 | recorder.Header().Get("Location") != "" { |
| 109 | t.Fatalf("off-keymail: status %d, location %q", recorder.Code, |
| 110 | recorder.Header().Get("Location")) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Every other outcome of Begin passes through untouched. |
| 115 | func TestNonKeymailOutcomesPassThrough(t *testing.T) { |
| 116 | recorder := httptest.NewRecorder() |
| 117 | navApp().keymailContinuation(func(writer http.ResponseWriter, |
| 118 | request *http.Request) { |
| 119 | http.Redirect(writer, request, "/signin?sent=1", |
| 120 | http.StatusSeeOther) |
| 121 | })(recorder, beginPost()) |
| 122 | if recorder.Code != http.StatusSeeOther || |
| 123 | recorder.Header().Get("Location") != "/signin?sent=1" { |
| 124 | t.Fatalf("a local redirect: status %d, location %q", |
| 125 | recorder.Code, recorder.Header().Get("Location")) |
| 126 | } |
| 127 | } |
| 128 | |