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
+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 {
|
||||
return nil, err
|
||||
}
|
||||
m, err := w.discover(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// The browser's cookie and the state first: a callback of no sign-in
|
||||
// of this browser never reaches the provider.
|
||||
var c signInCookie
|
||||
cookie, cerr := r.Cookie(w.cookieName())
|
||||
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 == "" {
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -411,3 +411,14 @@ func TestAllowInsecure(t *testing.T) {
|
||||
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