Sign-ins finish only in the browser that started them

A Core sign-in, and a posting account's or Search Console's OAuth sign-in,
now sets a short-lived cookie whose hash the flow keeps on the server. The
callback refuses one without it (constant-time compare), clears the cookie
either way, and the flow is used up regardless. This stops login CSRF: a
callback URL brought to another browser signs nobody in.

The cookie uses the __Host- prefix over https, so its Path is "/".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-04 08:07:21 -07:00
co-authored by Claude Opus 5.5
parent 005c4ac800
commit cd2dc6145b
9 changed files with 240 additions and 9 deletions
+37 -3
View File
@@ -350,15 +350,49 @@ func TestCallbackURLs(t *testing.T) {
func TestOAuthErrorIsShownOnce(t *testing.T) {
s, h := newTestServer(t, nil)
_ = s.Secrets.Set(map[string]string{secrets.LinkedInClientID: "cid", secrets.LinkedInClientSecret: "cs"})
loc, _ := url.Parse(postForm(h, "/auth/linkedin/start", nil).Header().Get("Location"))
start := postForm(h, "/auth/linkedin/start", nil)
loc, _ := url.Parse(start.Header().Get("Location"))
state := loc.Query().Get("state")
body := do(h, http.MethodGet, "/auth/linkedin/callback?state="+state+"&error=unauthorized_scope_error&error_description="+
url.QueryEscape("Scope &quot;openid&quot; is not authorized"), nil, nil).Body.String()
body := getWith(h, "/auth/linkedin/callback?state="+state+"&error=unauthorized_scope_error&error_description="+
url.QueryEscape("Scope &quot;openid&quot; is not authorized"), cookieOf(start, oauthFlowCookie)).Body.String()
if !strings.Contains(body, "Scope &#34;openid&#34; is not authorized") || strings.Contains(body, "&amp;quot;") {
t.Errorf("error page:\n%s", body)
}
}
// A posting account's sign-in finishes only in the browser that started it.
func TestOAuthCallbackNeedsItsBrowser(t *testing.T) {
s, h := newTestServer(t, nil)
_ = s.Secrets.Set(map[string]string{secrets.LinkedInClientID: "cid", secrets.LinkedInClientSecret: "cs"})
start := func() (string, *http.Cookie) {
rec := postForm(h, "/auth/linkedin/start", nil)
loc, _ := url.Parse(rec.Header().Get("Location"))
return loc.Query().Get("state"), cookieOf(rec, oauthFlowCookie)
}
refused := func(rec *httptest.ResponseRecorder) bool {
return rec.Code == http.StatusSeeOther && strings.Contains(rec.Header().Get("Location"), "oauth-expired")
}
state, c := start()
if c == nil || !c.HttpOnly || c.SameSite != http.SameSiteLaxMode || c.Path != "/" || c.MaxAge != int(oauthTTL/time.Second) {
t.Fatalf("cookie %+v", c)
}
cb := "/auth/linkedin/callback?error=access_denied&state="
if rec := getWith(h, cb+state); !refused(rec) {
t.Errorf("without the cookie: %d %s", rec.Code, rec.Header().Get("Location"))
}
if rec := getWith(h, cb+state, c); !refused(rec) {
t.Errorf("tried again after a refusal: %d", rec.Code)
}
state2, _ := start()
if rec := getWith(h, cb+state2, c); !refused(rec) {
t.Errorf("with another flow's cookie: %d", rec.Code)
}
state3, c3 := start()
if rec := getWith(h, cb+state3, c3); refused(rec) {
t.Errorf("its own browser: %d %s", rec.Code, rec.Header().Get("Location"))
}
}
func TestOAuthCallbackRejectsUnknownState(t *testing.T) {
_, h := newTestServer(t, nil)
rec := do(h, http.MethodGet, "/auth/x/callback?state=nope&code=c", nil, nil)