diff --git a/README.md b/README.md index c74c5c1..2a86150 100644 --- a/README.md +++ b/README.md @@ -537,6 +537,12 @@ router.Route("/auth", func(router *tanukirpc.Router[*Registry]) { By default, after a successful callback, the raw ID token is stored in the session (customizable with `oidc.WithSuccessBehavior`) and the user is redirected back to the page they came from. +The page to return to is decided when the login starts, in this order: + +1. The `return_to` query parameter of the redirect endpoint (e.g. `/auth/redirect?return_to=/items/1`). It must be a path on the same origin; absolute URLs, `//...` and `/\...` are ignored. This is useful for SPAs, which may not send a meaningful `Referer`. +2. The `Referer` header, only if it is on the same origin. The origin is the one given by `oidc.WithReferrerBaseURL`, or the `Host` of the request if not given. +3. The default referrer (`/`, customizable with `oidc.WithDefaultReferrer`). + #### Restricting who can log in With the setup above, anyone who can authenticate with the provider can log in. To restrict this, pass `oidc.WithAllowFunc` to `NewHandlers`. It is called in the callback with the verified ID token and decides whether the user is allowed: diff --git a/auth/oidc/oidc.go b/auth/oidc/oidc.go index e56580f..e287594 100644 --- a/auth/oidc/oidc.go +++ b/auth/oidc/oidc.go @@ -6,6 +6,7 @@ import ( "fmt" "log/slog" "net/http" + "net/url" "strings" "github.com/coreos/go-oidc/v3/oidc" @@ -122,21 +123,56 @@ func NewHandlers[Reg sessions.RegistryWithAccessor](oauth2Config *oauth2.Config, return h } +// referrer returns the path of the Referer header if it is same-origin, or "" otherwise. +// The origin is the one of WithReferrerBaseURL if specified, or the host of the request. func (a *Handlers[Reg]) referrer(req *http.Request) string { - referrer := req.Referer() - if referrer == "" && a.referrerBaseURL != "" { - s := strings.TrimPrefix(referrer, a.referrerBaseURL) - if !strings.HasPrefix("/", s) { - s = "/" + s + ref, err := url.Parse(req.Referer()) + if err != nil || ref.Host == "" { + return "" + } + if a.referrerBaseURL != "" { + base, err := url.Parse(a.referrerBaseURL) + if err != nil || !strings.EqualFold(ref.Scheme, base.Scheme) || !strings.EqualFold(ref.Host, base.Host) { + return "" } - referrer = s + } else if !strings.EqualFold(ref.Host, req.Host) { + return "" } - return referrer + path := ref.RequestURI() + if !isLocalPath(path) { + return "" + } + return path +} + +// returnTo returns the "return_to" query parameter if it is a local path, or "" otherwise. +func returnTo(req *http.Request) string { + path := req.URL.Query().Get("return_to") + if !isLocalPath(path) { + return "" + } + return path +} + +// isLocalPath reports whether path is an absolute path on the same origin. +// It rejects protocol-relative URLs such as "//evil.example" and "/\evil.example", +// and control characters that browsers strip before resolving the URL. +func isLocalPath(path string) bool { + if !strings.HasPrefix(path, "/") || strings.HasPrefix(path, "//") { + return false + } + return !strings.ContainsFunc(path, func(r rune) bool { + return r == '\\' || r < 0x20 || r == 0x7f + }) } func (a *Handlers[Reg]) setReferrer(ctx tanukirpc.Context[Reg], name string) error { req := ctx.Request() - if referrer := a.referrer(req); referrer != "" { + referrer := returnTo(req) + if referrer == "" { + referrer = a.referrer(req) + } + if referrer != "" { if err := ctx.Registry().Session().Set(name, referrer); err != nil { return fmt.Errorf("failed to set referrer: %w", err) } @@ -147,14 +183,21 @@ func (a *Handlers[Reg]) setReferrer(ctx tanukirpc.Context[Reg], name string) err func (a *Handlers[Reg]) getReferrer(ctx tanukirpc.Context[Reg], name string) string { reg := ctx.Registry() referrer, ok := reg.Session().Get(name) - if ok { - reg.Session().Remove(name) - return referrer.(string) + if !ok { + return a.defaultReferrer + } + reg.Session().Remove(name) + // Sessions saved by older versions may hold a cross-origin URL. + if s, ok := referrer.(string); ok && isLocalPath(s) { + return s } return a.defaultReferrer } // Redirect redirects to the OIDC provider. +// After a successful login, Callback redirects back to the "return_to" query parameter +// if it is a local path such as "/dashboard", or to the Referer header if it is same-origin, +// or to the default referrer. func (a *Handlers[Reg]) Redirect(ctx tanukirpc.Context[Reg], _ struct{}) (_resp struct{}, err error) { reg := ctx.Registry() diff --git a/auth/oidc/referrer_test.go b/auth/oidc/referrer_test.go new file mode 100644 index 0000000..95a5c32 --- /dev/null +++ b/auth/oidc/referrer_test.go @@ -0,0 +1,295 @@ +package oidc_test + +import ( + "crypto" + "crypto/rand" + "crypto/rsa" + "crypto/sha256" + "encoding/base64" + "encoding/json" + "fmt" + "math/big" + "net/http" + "net/http/cookiejar" + "net/http/httptest" + "net/url" + "testing" + "time" + + gooidc "github.com/coreos/go-oidc/v3/oidc" + gorillasessions "github.com/gorilla/sessions" + "github.com/mackee/tanukirpc" + "github.com/mackee/tanukirpc/auth/oidc" + tsessions "github.com/mackee/tanukirpc/sessions" + "github.com/mackee/tanukirpc/sessions/gorilla" + "golang.org/x/oauth2" +) + +type flowRegistry struct { + accessor tsessions.Accessor +} + +func (r *flowRegistry) Session() tsessions.Accessor { return r.accessor } + +// fakeIdP is a minimal OIDC provider that issues an ID token carrying the nonce of the last authorize request. +type fakeIdP struct { + *httptest.Server + key *rsa.PrivateKey + nonce string +} + +func newFakeIdP(t *testing.T) *fakeIdP { + t.Helper() + key, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + t.Fatal(err) + } + idp := &fakeIdP{key: key} + mux := http.NewServeMux() + mux.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, r *http.Request) { + _ = json.NewEncoder(w).Encode(map[string]any{ + "issuer": idp.URL, + "authorization_endpoint": idp.URL + "/authorize", + "token_endpoint": idp.URL + "/token", + "jwks_uri": idp.URL + "/jwks", + "id_token_signing_alg_values_supported": []string{"RS256"}, + }) + }) + mux.HandleFunc("/jwks", func(w http.ResponseWriter, r *http.Request) { + enc := base64.RawURLEncoding.EncodeToString + _ = json.NewEncoder(w).Encode(map[string]any{ + "keys": []map[string]any{{ + "kty": "RSA", + "alg": "RS256", + "use": "sig", + "kid": "k1", + "n": enc(key.N.Bytes()), + "e": enc(big.NewInt(int64(key.E)).Bytes()), + }}, + }) + }) + mux.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{ + "access_token": "access", + "token_type": "Bearer", + "expires_in": 3600, + "id_token": idp.signIDToken(t), + }) + }) + idp.Server = httptest.NewServer(mux) + t.Cleanup(idp.Close) + return idp +} + +func (idp *fakeIdP) signIDToken(t *testing.T) string { + enc := base64.RawURLEncoding.EncodeToString + header, _ := json.Marshal(map[string]string{"alg": "RS256", "kid": "k1", "typ": "JWT"}) + payload, _ := json.Marshal(map[string]any{ + "iss": idp.URL, + "sub": "subject", + "aud": "client", + "exp": time.Now().Add(time.Hour).Unix(), + "iat": time.Now().Unix(), + "nonce": idp.nonce, + }) + signingInput := enc(header) + "." + enc(payload) + sum := sha256.Sum256([]byte(signingInput)) + sig, err := rsa.SignPKCS1v15(rand.Reader, idp.key, crypto.SHA256, sum[:]) + if err != nil { + t.Error(err) + } + return signingInput + "." + enc(sig) +} + +func newApp(t *testing.T, idp *fakeIdP, opts func(appURL string) []oidc.HandlersOption[*flowRegistry]) *httptest.Server { + t.Helper() + store, err := gorilla.NewStore(gorillasessions.NewCookieStore([]byte("0123456789abcdef0123456789abcdef"))) + if err != nil { + t.Fatal(err) + } + provider, err := gooidc.NewProvider(t.Context(), idp.URL) + if err != nil { + t.Fatal(err) + } + + app := httptest.NewUnstartedServer(nil) + appURL := "http://" + app.Listener.Addr().String() + oauth2Config := &oauth2.Config{ + ClientID: "client", + ClientSecret: "secret", + RedirectURL: appURL + "/auth/callback", + Endpoint: provider.Endpoint(), + Scopes: []string{gooidc.ScopeOpenID}, + } + h := oidc.NewHandlers(oauth2Config, provider, opts(appURL)...) + + router := tanukirpc.NewRouter( + &flowRegistry{}, + tanukirpc.WithContextFactory(tanukirpc.NewContextHookFactory(func(w http.ResponseWriter, req *http.Request) (*flowRegistry, error) { + accessor, err := store.GetAccessor(req) + if err != nil { + return nil, err + } + return &flowRegistry{accessor: accessor}, nil + })), + ) + router.Get("/auth/redirect", tanukirpc.NewHandler(h.Redirect)) + router.Get("/auth/callback", tanukirpc.NewHandler(h.Callback)) + app.Config.Handler = router + app.Start() + t.Cleanup(app.Close) + return app +} + +// login runs Redirect -> (IdP) -> Callback and returns the Location of the final redirect. +func login(t *testing.T, idp *fakeIdP, app *httptest.Server, referer, returnTo string) string { + t.Helper() + jar, err := cookiejar.New(nil) + if err != nil { + t.Fatal(err) + } + client := &http.Client{ + Jar: jar, + CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }, + } + + redirect := app.URL + "/auth/redirect" + if returnTo != "" { + redirect += "?return_to=" + url.QueryEscape(returnTo) + } + req, err := http.NewRequest(http.MethodGet, redirect, nil) + if err != nil { + t.Fatal(err) + } + if referer != "" { + req.Header.Set("Referer", referer) + } + resp, err := client.Do(req) + if err != nil { + t.Fatal(err) + } + resp.Body.Close() + if resp.StatusCode != http.StatusFound { + t.Fatalf("redirect: status = %d", resp.StatusCode) + } + authorize, err := url.Parse(resp.Header.Get("Location")) + if err != nil { + t.Fatal(err) + } + idp.nonce = authorize.Query().Get("nonce") + + callback := fmt.Sprintf("%s/auth/callback?code=code&state=%s", app.URL, url.QueryEscape(authorize.Query().Get("state"))) + resp, err = client.Get(callback) + if err != nil { + t.Fatal(err) + } + resp.Body.Close() + if resp.StatusCode != http.StatusFound { + t.Fatalf("callback: status = %d", resp.StatusCode) + } + return resp.Header.Get("Location") +} + +func TestLoginReturnsToReferrer(t *testing.T) { + idp := newFakeIdP(t) + + tests := []struct { + name string + withBaseURL bool + referer func(appURL string) string + returnTo string + want string + }{ + { + name: "no referer falls back to default", + withBaseURL: true, + want: "/", + }, + { + name: "same-origin referer is normalized to path", + withBaseURL: true, + referer: func(appURL string) string { return appURL + "/dashboard?x=1" }, + want: "/dashboard?x=1", + }, + { + name: "cross-origin referer is rejected", + withBaseURL: true, + referer: func(string) string { return "https://evil.example/phish" }, + want: "/", + }, + { + name: "cross-origin referer is rejected without base URL", + withBaseURL: false, + referer: func(string) string { return "https://evil.example/phish" }, + want: "/", + }, + { + name: "same-host referer is normalized to path without base URL", + withBaseURL: false, + referer: func(appURL string) string { return appURL + "/dashboard" }, + want: "/dashboard", + }, + { + name: "same-origin referer with protocol-relative path is rejected", + withBaseURL: true, + referer: func(appURL string) string { return appURL + "//evil.example/phish" }, + want: "/", + }, + { + name: "return_to wins over referer", + withBaseURL: true, + referer: func(appURL string) string { return appURL + "/dashboard" }, + returnTo: "/items/1?tab=a", + want: "/items/1?tab=a", + }, + { + name: "absolute return_to is ignored", + withBaseURL: true, + referer: func(appURL string) string { return appURL + "/dashboard" }, + returnTo: "https://evil.example/phish", + want: "/dashboard", + }, + { + name: "protocol-relative return_to is ignored", + withBaseURL: true, + returnTo: "//evil.example/phish", + want: "/", + }, + { + name: "backslash return_to is ignored", + withBaseURL: true, + returnTo: "/\\evil.example/phish", + want: "/", + }, + { + name: "return_to with control characters is ignored", + withBaseURL: true, + returnTo: "/\t/evil.example/phish", + want: "/", + }, + { + name: "relative return_to is ignored", + withBaseURL: true, + returnTo: "evil.example/phish", + want: "/", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + app := newApp(t, idp, func(appURL string) []oidc.HandlersOption[*flowRegistry] { + if !tt.withBaseURL { + return nil + } + return []oidc.HandlersOption[*flowRegistry]{oidc.WithReferrerBaseURL[*flowRegistry](appURL)} + }) + var referer string + if tt.referer != nil { + referer = tt.referer(app.URL) + } + if got := login(t, idp, app, referer, tt.returnTo); got != tt.want { + t.Errorf("redirected to %q after login, want %q", got, tt.want) + } + }) + } +}