Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safe redirect for oidc/saml #1360

Merged
merged 2 commits into from
Oct 4, 2017
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
10 changes: 10 additions & 0 deletions lib/httplib/httplib.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,13 @@ func RewritePaths(next http.Handler, rewrites ...RewritePair) http.Handler {
next.ServeHTTP(w, req)
})
}

// SafeRedirect performs a relative redirect to the URI part of the provided redirect URL
func SafeRedirect(w http.ResponseWriter, r *http.Request, redirectURL string) error {
parsedURL, err := url.Parse(redirectURL)
if err != nil {
return trace.Wrap(err)
}
http.Redirect(w, r, parsedURL.RequestURI(), http.StatusFound)
return nil
}
2 changes: 1 addition & 1 deletion lib/web/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ func (h *Handler) oidcCallback(w http.ResponseWriter, r *http.Request, p httprou
if err := SetSession(w, response.Username, response.Session.GetName()); err != nil {
return nil, trace.Wrap(err)
}
http.Redirect(w, r, response.Req.ClientRedirectURL, http.StatusFound)
httplib.SafeRedirect(w, r, response.Req.ClientRedirectURL)
return nil, nil
}
log.Infof("oidcCallback redirecting to console login")
Expand Down
2 changes: 1 addition & 1 deletion lib/web/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ func (s *WebSuite) TestSAMLSuccess(c *C) {
// we have got valid session
c.Assert(authRe.Headers().Get("Set-Cookie"), Not(Equals), "")
// we are being redirected to orignal URL
c.Assert(authRe.Headers().Get("Location"), Equals, "http://localhost/after")
c.Assert(authRe.Headers().Get("Location"), Equals, "/after")
}

type authPack struct {
Expand Down
2 changes: 1 addition & 1 deletion lib/web/saml.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (m *Handler) samlACS(w http.ResponseWriter, r *http.Request, p httprouter.P
if err := SetSession(w, response.Username, response.Session.GetName()); err != nil {
return nil, trace.Wrap(err)
}
http.Redirect(w, r, response.Req.ClientRedirectURL, http.StatusFound)
httplib.SafeRedirect(w, r, response.Req.ClientRedirectURL)
return nil, nil
}
l.Debugf("samlCallback redirecting to console login")
Expand Down