oficina / home Public

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

Plain git — no account needed to clone.

Download

Download this file

1package home
2
3import (
4 "net/http"
5 "net/url"
6
7 "amadan.net/rastrillo/rastrillo/csrf"
8 "golang.org/x/net/publicsuffix"
9)
10
11// Each origin revokes and lands locally. A consumer may separately offer
12// this credential-origin POST as an optional sign-out hop; existing callers
13// using that hop remain admitted by exact registration and origin evidence.
14// There is no cross-origin or global session-revocation promise.
15const signoutPath = "/signout"
16
17// handleCredentialSignout ends the credential-origin session.
18//
19// It sits outside csrf.Protect because a consumer's hand-off is same-site
20// (a team instance, or the directory) or cross-site (a preview), never
21// same-origin, and Protect refuses both. In its place is a narrower
22// check, signoutReturn, which also names the only place the browser may
23// go next.
24//
25// A refusal is the branded "Finish signing out" page, status 403, never
26// bare error text: by then the consumer has already ended its own
27// session, so a plain error would leave the browser half signed out with
28// no way on. The page's Sign out button is a same-origin POST, which
29// needs no Referer, so the browsers and extensions that strip one still
30// have a way through.
31func (a *App) handleCredentialSignout(w http.ResponseWriter, r *http.Request) {
32 w.Header().Set("Cache-Control", "no-store")
33 back, verdict := a.signoutReturn(r)
34 switch verdict {
35 case signoutRefused:
36 a.render(w, r, http.StatusForbidden, "signout_confirm.html", nil)
37 return
38 case signoutConfirm:
39 a.setConsumerPolicy(w, back)
40 // Admitted, but the id.* cookie did not ride this hop, so there is
41 // no row to revoke yet. Clearing the cookie now would leave the
42 // row alive behind it; keep it, and let the same-origin button
43 // (which does carry the cookie) end the session.
44 a.render(w, r, http.StatusOK, "signout_confirm.html", nil)
45 return
46 }
47 a.sessions.SignOut(w, r)
48 http.Redirect(w, r, back+"/signed-out", http.StatusSeeOther)
49}
50
51// handleCredentialSignoutPage is GET /signout: the same page, doing
52// nothing until its button is pressed. Sign-out is never a GET, because
53// a GET is one any link or <img> can trigger.
54func (a *App) handleCredentialSignoutPage(w http.ResponseWriter, r *http.Request) {
55 w.Header().Set("Cache-Control", "no-store")
56 a.render(w, r, http.StatusOK, "signout_confirm.html", nil)
57}
58
59// signoutReturn admits a sign-out POST from this origin's own pages or
60// from an origin registered in HOME_CONSUMERS (the directory is one), and
61// nothing else: not an unregistered team instance on the same site, not
62// any other origin, not a request with no evidence. It returns the origin
63// the browser goes back to, which is always the matched registration
64// (this credential origin for its own button), and never anything the
65// request supplied. So there is no open redirect to close.
66//
67// HOME_CONSUMERS is the allowlist that already decides who may receive an
68// assertion, so admitting its origins here adds no party that could not
69// already take part in sign-in.
70//
71// Origin alone cannot carry the hand-off. Chromium and WebKit send
72// "Origin: null" on a POST that a 307 has carried across origins
73// (verified in real browsers, polish-audit B2; the Fetch spec's tainted-
74// origin rule would keep the consumer's, but the browsers do not), so the
75// hop that matters arrives with a null Origin. Referer survives the
76// redirect, and under Referrer-Policy strict-origin-when-cross-origin
77// (rastrillo's default, and what connect's 307 sets) it is exactly the
78// consumer's origin. So:
79//
80// - Origin, when it names an origin, must be a registered one (or pass
81// the ordinary same-origin check for this one); a Referer cannot
82// overrule it;
83// - a null or absent Origin falls back to Referer, whose origin must be
84// exactly a registered one;
85// - either way, Sec-Fetch-Site must agree with where that origin is:
86// same-site for one on the suite's site, cross-site for one off it (a
87// preview), or absent. A suite origin arriving cross-site passed
88// through another site on the way, and a preview is never same-site.
89//
90// A preview is admitted but not signed out on the spot (signoutConfirm).
91// Its hop is cross-site, and the id.* session cookie is SameSite=Lax, so
92// the POST arrives without it: there is no row to revoke, and clearing
93// the cookie in the browser would leave a live row behind a copy of it.
94// It gets the "Finish signing out" page, whose same-origin POST carries
95// the cookie and revokes the row (the #74 review, finding 1).
96//
97// A page elsewhere cannot set Referer to a consumer's origin. What is
98// left, and accepted (spec §9.5), is logout CSRF from a page ON a
99// consumer origin: any of them can post here. That needs a user-injected
100// form on a page the victim visits, and a consumer that renders one must
101// not do it on a registered origin.
102type signoutVerdict int
103
104const (
105 signoutRefused signoutVerdict = iota
106 signoutAdmitted
107 signoutConfirm
108)
109
110func (a *App) signoutReturn(r *http.Request) (string, signoutVerdict) {
111 if csrf.SameOrigin(r, a.origins.Credential) {
112 return a.origins.Credential, signoutAdmitted
113 }
114 claimed := r.Header.Get("Origin")
115 if claimed == "" || claimed == "null" {
116 ref, err := url.Parse(r.Header.Get("Referer"))
117 if err != nil || ref.User != nil || ref.Scheme == "" || ref.Host == "" {
118 return "", signoutRefused
119 }
120 claimed = ref.Scheme + "://" + ref.Host
121 }
122 matched, ok := a.consumerOrigin(claimed)
123 if !ok {
124 return "", signoutRefused
125 }
126 suite := a.onSuiteSite(matched)
127 switch r.Header.Get("Sec-Fetch-Site") {
128 case "":
129 case "same-site":
130 if !suite {
131 return "", signoutRefused
132 }
133 case "cross-site":
134 if suite {
135 return "", signoutRefused
136 }
137 default:
138 return "", signoutRefused
139 }
140 if !suite {
141 return matched, signoutConfirm
142 }
143 return matched, signoutAdmitted
144}
145
146// consumerOrigin returns the registered consumer origin exactly equal to
147// origin. Registrations are validated at boot and on every registry reload
148// (assertion.NewVerifier), so an exact string match is a match on scheme,
149// host and port. A retired table consumer is not in the union.
150func (a *App) consumerOrigin(origin string) (string, bool) {
151 if a.runtime == nil {
152 return "", false
153 }
154 for _, registered := range a.consumers() {
155 if registered == origin {
156 return registered, true
157 }
158 }
159 return "", false
160}
161
162// onSuiteSite compares schemeful sites with the credential origin. Configured
163// directory origins may be cross-site, and their Lax ID cookie is then absent.
164func (a *App) onSuiteSite(origin string) bool {
165 consumer, err := url.Parse(origin)
166 if err != nil {
167 return false
168 }
169 issuer, err := url.Parse(a.origins.Credential)
170 if err != nil || consumer.Scheme != issuer.Scheme {
171 return false
172 }
173 if consumer.Hostname() == issuer.Hostname() {
174 return true
175 }
176 site, err := publicsuffix.EffectiveTLDPlusOne(issuer.Hostname())
177 if err != nil {
178 return false
179 }
180 other, err := publicsuffix.EffectiveTLDPlusOne(consumer.Hostname())
181 return err == nil && site == other
182}
183