Skip to content

Commit 9928e7f

Browse files
committed
fix: returnTo not used when checking where to redirect
1 parent ce89c29 commit 9928e7f

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

selfservice/flow/login/hook.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func (e *HookExecutor) PostLoginHook(w http.ResponseWriter, r *http.Request, a *
8282
// Verify the redirect URL before we do any other processing.
8383
c := e.d.Config(r.Context())
8484
returnTo, err := x.SecureRedirectTo(r, c.SelfServiceBrowserDefaultReturnTo(),
85+
x.SecureRedirectReturnTo(a.ReturnTo),
8586
x.SecureRedirectUseSourceURL(a.RequestURL),
8687
x.SecureRedirectAllowURLs(c.SelfServiceBrowserAllowedReturnToDomains()),
8788
x.SecureRedirectAllowSelfServiceURLs(c.SelfPublicURL()),

selfservice/flow/registration/hook.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ func (e *HookExecutor) PostRegistrationHook(w http.ResponseWriter, r *http.Reque
128128
// Verify the redirect URL before we do any other processing.
129129
c := e.d.Config(r.Context())
130130
returnTo, err := x.SecureRedirectTo(r, c.SelfServiceBrowserDefaultReturnTo(),
131+
x.SecureRedirectReturnTo(a.ReturnTo),
131132
x.SecureRedirectUseSourceURL(a.RequestURL),
132133
x.SecureRedirectAllowURLs(c.SelfServiceBrowserAllowedReturnToDomains()),
133134
x.SecureRedirectAllowSelfServiceURLs(c.SelfPublicURL()),

x/http_secure_redirect.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
type secureRedirectOptions struct {
2121
allowlist []url.URL
2222
defaultReturnTo *url.URL
23+
returnTo string
2324
sourceURL string
2425
}
2526

@@ -40,6 +41,13 @@ func SecureRedirectUseSourceURL(source string) SecureRedirectOption {
4041
}
4142
}
4243

44+
// SecureRedirectReturnTo uses the provided URL to redirect the user to it.
45+
func SecureRedirectReturnTo(returnTo string) SecureRedirectOption {
46+
return func(o *secureRedirectOptions) {
47+
o.returnTo = returnTo
48+
}
49+
}
50+
4351
// SecureRedirectAllowSelfServiceURLs allows the caller to define `?return_to=` values
4452
// which contain the server's URL and `/self-service` path prefix. Useful for redirecting
4553
// to the login endpoint, for example.
@@ -85,9 +93,10 @@ func SecureRedirectTo(r *http.Request, defaultReturnTo *url.URL, opts ...SecureR
8593
}
8694
}
8795

88-
if len(source.Query().Get("return_to")) == 0 {
96+
rawReturnTo := stringsx.Coalesce(o.returnTo, source.Query().Get("return_to"))
97+
if rawReturnTo == "" {
8998
return o.defaultReturnTo, nil
90-
} else if returnTo, err = url.Parse(source.Query().Get("return_to")); err != nil {
99+
} else if returnTo, err = url.Parse(rawReturnTo); err != nil {
91100
return nil, herodot.ErrInternalServerError.WithWrap(err).WithReasonf("Unable to parse the return_to query parameter as an URL: %s", err)
92101
}
93102

0 commit comments

Comments
 (0)