Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
65 changes: 54 additions & 11 deletions auth/oidc/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log/slog"
"net/http"
"net/url"
"strings"

"github.com/coreos/go-oidc/v3/oidc"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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()

Expand Down
Loading
Loading