fix(yaiam): check the sign-in cookie and state before discovery
A callback that belongs to no sign-in of this browser is now ErrSignInState without contacting the provider (before, an unreachable provider turned it into a discovery error). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
376ae3c970
commit
76543b23a0
3 files changed
+19
-5
No files matched your search
@@ -16,7 +16,8 @@
|
|||||||
(2026-11-20, first consumer yaemp, replacing go-oidc/oauth2):
|
(2026-11-20, first consumer yaemp, replacing go-oidc/oauth2):
|
||||||
`Web.PublicIssuer` (the browser reaches the provider under another
|
`Web.PublicIssuer` (the browser reaches the provider under another
|
||||||
origin), `Client.AllowInsecure` (http off loopback, test installations
|
origin), `Client.AllowInsecure` (http off loopback, test installations
|
||||||
only), `email_verified` true only as JSON boolean.
|
only), `email_verified` true only as JSON boolean; `Callback` checks
|
||||||
|
the cookie and state before it contacts the provider (v0.5.1).
|
||||||
- [ ] **yaiam: web refresh.** `Web` has no refresh for a confidential
|
- [ ] **yaiam: web refresh.** `Web` has no refresh for a confidential
|
||||||
client (Client.Refresh sends no secret); add when an application keeps
|
client (Client.Refresh sends no secret); add when an application keeps
|
||||||
access tokens beyond the sign-in.
|
access tokens beyond the sign-in.
|
||||||
|
|||||||
+6
-4
@@ -251,10 +251,8 @@ func (w *Web) Callback(ctx context.Context, rw http.ResponseWriter, r *http.Requ
|
|||||||
if err := w.check(); err != nil {
|
if err := w.check(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
m, err := w.discover(ctx)
|
// The browser's cookie and the state first: a callback of no sign-in
|
||||||
if err != nil {
|
// of this browser never reaches the provider.
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var c signInCookie
|
var c signInCookie
|
||||||
cookie, cerr := r.Cookie(w.cookieName())
|
cookie, cerr := r.Cookie(w.cookieName())
|
||||||
http.SetCookie(rw, &http.Cookie{Name: w.cookieName(), Value: "", Path: "/", MaxAge: -1, HttpOnly: true, Secure: w.secure(),
|
http.SetCookie(rw, &http.Cookie{Name: w.cookieName(), Value: "", Path: "/", MaxAge: -1, HttpOnly: true, Secure: w.secure(),
|
||||||
@@ -282,6 +280,10 @@ func (w *Web) Callback(ctx context.Context, rw http.ResponseWriter, r *http.Requ
|
|||||||
if code == "" {
|
if code == "" {
|
||||||
return nil, errors.New("yaiam: the callback has no code")
|
return nil, errors.New("yaiam: the callback has no code")
|
||||||
}
|
}
|
||||||
|
m, err := w.discover(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
tok, idToken, err := w.exchange(ctx, m, code, c.Verifier)
|
tok, idToken, err := w.exchange(ctx, m, code, c.Verifier)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -411,3 +411,14 @@ func TestAllowInsecure(t *testing.T) {
|
|||||||
t.Error("http redirect URL off loopback taken without AllowInsecure")
|
t.Error("http redirect URL off loopback taken without AllowInsecure")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestWebCallbackBeforeProvider: a callback without this browser's
|
||||||
|
// sign-in is refused before the provider is asked (here: unreachable).
|
||||||
|
func TestWebCallbackBeforeProvider(t *testing.T) {
|
||||||
|
w := &Web{Client: Client{Issuer: "http://127.0.0.1:9/t/default", ClientID: "web"}, RedirectURL: "http://localhost:8097/callback"}
|
||||||
|
for _, cookie := range []*http.Cookie{nil, {Name: "yaiam_signin", Value: "!!"}} {
|
||||||
|
if _, _, err := callback(w, "code=c&state=s", cookie); !errors.Is(err, ErrSignInState) {
|
||||||
|
t.Errorf("cookie %v: %v", cookie, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user