Test hand-off escaping of a hostile state and the final referrer

Review follow-up. A state full of quotes, tags, entities, unicode and
a second url= must come back intact and stay inside the hand-off URL,
for both approve and deny, with a registered redirect that carries its
own query and a semicolon in its path. The browser test now also
checks the third origin receives no Referer, since the client's URL
held the code.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev a35ef36962bf66ec9b02aa9858bf169243b1f81c parent e16eb11
2 files changed, +36 −5
  • connection/consent_browser_test.go +9 −5
  • grant/consent_test.go +27 −0
diff --git a/connection/consent_browser_test.go b/connection/consent_browser_test.go
index 015e485..938d321 100644
--- a/connection/consent_browser_test.go
+++ b/connection/consent_browser_test.go
@@ -90,7 +90,7 @@ type consentRig struct {
// finalOrigin, when set, is where the client's callback redirects on to,
// the way Equipo hands a person on to Base after connecting.
finalOrigin string
- finals []url.Values
+ finals []clientCall
}
type clientCall struct {
@@ -155,7 +155,7 @@ func newConsentRigAt(t *testing.T, clientHost, finalHost string) *consentRig {
return
}
rig.mu.Lock()
- rig.finals = append(rig.finals, r.URL.Query())
+ rig.finals = append(rig.finals, clientCall{r.URL.Query(), r.Header.Get("Referer")})
rig.mu.Unlock()
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
_, _ = io.WriteString(w, "final page")
@@ -379,11 +379,15 @@ func (b *consentBrowser) landOnFinal() clientCall {
b.t.Helper()
b.landOn(b.rig.finalOrigin+"/done", "final page")
b.rig.mu.Lock()
- finals := len(b.rig.finals)
+ finals := append([]clientCall{}, b.rig.finals...)
b.rig.mu.Unlock()
calls := b.rig.clientCalls()
- if len(calls) != 1 || finals != 1 {
- b.t.Fatalf("client saw %d callbacks and the final page %d visits, want 1 each", len(calls), finals)
+ if len(calls) != 1 || len(finals) != 1 {
+ b.t.Fatalf("client saw %d callbacks and the final page %d visits, want 1 each", len(calls), len(finals))
+ }
+ // The code was in the client's URL; the page after it must not learn it.
+ if finals[0].referer != "" {
+ b.t.Fatalf("final page received a referrer: %q", finals[0].referer)
}
return calls[0]
}
diff --git a/grant/consent_test.go b/grant/consent_test.go
index f039e10..56815b5 100644
--- a/grant/consent_test.go
+++ b/grant/consent_test.go
@@ -265,6 +265,33 @@ func TestConsentAnswersHandTheBrowserOnRatherThanRedirect(t *testing.T) {
}
}
+// A state can hold anything the client likes, and a registered redirect can
+// carry its own query; neither may change where the hand-off page goes.
+func TestHandoffKeepsAHostileStateInsideTheURL(t *testing.T) {
+ const redirect = "https://other.example/oauth/done;v=1?tenant=a"
+ const state = `quote"'<b>&amp;%zz ünï;url=https://evil.example/ + 0;url=x`
+ clients := []Client{{ID: "other", Name: "Other Host", Redirects: []string{redirect}}}
+ for _, decision := range []string{"approve", "deny"} {
+ t.Run(decision, func(t *testing.T) {
+ _, ts := consentServer(t, clients, true)
+ resp, body := fetchAs(t, ts, authorizeURL(ts.URL, url.Values{"client_id": {"other"}, "redirect_uri": {redirect}, "state": {state}}), "fresh")
+ m := regexp.MustCompile(`name="ticket" value="([^"]+)"`).FindStringSubmatch(body)
+ if resp.StatusCode != 200 || len(m) != 2 {
+ t.Fatalf("consent page %d", resp.StatusCode)
+ }
+ resp, body = answer{ticket: m[1], decision: decision, session: "fresh", origins: []string{ts.URL}, cookies: resp.Cookies()}.post(t, ts)
+ dest := wantHandoff(t, resp, body)
+ q := dest.Query()
+ if dest.Scheme != "https" || dest.Host != "other.example" || dest.Path != "/oauth/done;v=1" || q.Get("state") != state || q.Get("tenant") != "a" {
+ t.Fatalf("sent to %q, query %v", dest, q)
+ }
+ if strings.Contains(body, "<b>") || strings.Contains(body, `quote"`) {
+ t.Fatalf("state reached the page unescaped: %s", body)
+ }
+ })
+ }
+}
+
func TestApproveRefusesAnyOtherOrigin(t *testing.T) {
_, ts := consentServer(t, consentClients, true)
ticket, cookies := consentPage(t, ts, "other", otherRedirect, "fresh")