oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1package mapa
2
3import (
4 "bytes"
5 "net/http"
6 "net/url"
7 "strings"
8)
9
10// keymailContinuation ends the sign-in POST with a page rather than a
11// redirect when rastrillo sends the browser on to keymail.
12//
13// **The keymail CSP trap.** When the submitted address's domain delegates
14// to keymail, auth.Begin answers the form POST with a 303 to keymail's
15// /oauth/authorize. The page's CSP says `form-action 'self'`, and
16// browsers enforce form-action through a form's whole redirect chain, so
17// the navigation is refused and the button spins for ever. Docs' first
18// deployed sign-in hit exactly this, and Fichas' did again because it
19// ported the fix late. Spec §10: Mapa ports it from the first commit.
20//
21// Docs' fix (internal/docs/auth_navigation.go), ported unchanged in
22// substance: capture Begin's response and, if it is a 303 to a keymail
23// authorize URL that validates, end the POST with a document carrying a
24// meta refresh and a plain link. A document navigating is not the form's
25// redirect chain, so the CSP stays exactly as strict as it was.
26func (app *app) keymailContinuation(next http.HandlerFunc) http.HandlerFunc {
27 return func(writer http.ResponseWriter, request *http.Request) {
28 result := &capturedResponse{header: make(http.Header)}
29 next(result, request)
30 destination := result.header.Get("Location")
31 external := destination != "" && !strings.HasPrefix(destination, "/")
32 if result.status != http.StatusSeeOther || !external {
33 result.replay(writer)
34 return
35 }
36 // The one place Mapa turns a URL into a link, so the one place
37 // an open redirect could be born.
38 if !keymailDestination(destination, app.config.Origin) {
39 app.logger.Error("sign-in redirected somewhere that is " +
40 "not keymail's authorize endpoint")
41 http.Error(writer, "sign-in could not continue",
42 http.StatusBadGateway)
43 return
44 }
45 // The pending cookie is what the callback checks.
46 for _, cookie := range result.header.Values("Set-Cookie") {
47 writer.Header().Add("Set-Cookie", cookie)
48 }
49 writer.Header().Set("Cache-Control", "no-store")
50 // The URL carries state and a PKCE challenge: no Referer.
51 writer.Header().Set("Referrer-Policy", "no-referrer")
52 render(writer, request, "signin", signinPage{Theme: themeAttr(request),
53 Continue: &signinContinue{Door: doorHead(request),
54 ContinueURL: destination}})
55 }
56}
57
58// keymailDestination accepts only the authorize URL keymail's plugin
59// builds for THIS origin: https, no userinfo, no fragment, the exact
60// path, each required parameter once, and client_id, redirect_uri, scope
61// and the PKCE method pinned. Docs' predicate, unchanged.
62func keymailDestination(destination, origin string) bool {
63 parsed, err := url.Parse(destination)
64 if err != nil || parsed.Scheme != "https" || parsed.Host == "" ||
65 parsed.User != nil || parsed.Fragment != "" ||
66 parsed.Path != "/oauth/authorize" || parsed.RawPath != "" {
67 return false
68 }
69 query, err := url.ParseQuery(parsed.RawQuery)
70 if err != nil {
71 return false
72 }
73 for _, key := range []string{"client_id", "redirect_uri", "scope",
74 "code_challenge_method", "state", "code_challenge"} {
75 if len(query[key]) != 1 || query.Get(key) == "" {
76 return false
77 }
78 }
79 origin = strings.TrimSuffix(origin, "/")
80 return query.Get("client_id") == origin &&
81 query.Get("redirect_uri") == origin+"/auth/callback" &&
82 query.Get("scope") == "identify" &&
83 query.Get("code_challenge_method") == "S256"
84}
85
86// capturedResponse records what a handler wrote so the wrapper can decide
87// whether to pass it through or replace it.
88type capturedResponse struct {
89 header http.Header
90 status int
91 body bytes.Buffer
92}
93
94func (captured *capturedResponse) Header() http.Header {
95 return captured.header
96}
97
98func (captured *capturedResponse) WriteHeader(status int) {
99 if captured.status == 0 {
100 captured.status = status
101 }
102}
103
104func (captured *capturedResponse) Write(data []byte) (int, error) {
105 captured.WriteHeader(http.StatusOK)
106 return captured.body.Write(data)
107}
108
109// replay passes the captured response through untouched.
110func (captured *capturedResponse) replay(writer http.ResponseWriter) {
111 for name, values := range captured.header {
112 for _, value := range values {
113 writer.Header().Add(name, value)
114 }
115 }
116 captured.WriteHeader(http.StatusOK)
117 writer.WriteHeader(captured.status)
118 _, _ = writer.Write(captured.body.Bytes())
119}
120