Skip to content

Commit

Permalink
Merge pull request #1360 from gravitational/roman/redirecturl
Browse files Browse the repository at this point in the history
Safe redirect for oidc/saml
  • Loading branch information
r0mant committed Oct 4, 2017
2 parents 357fed3 + a8cf2d3 commit 4bcd590
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
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

0 comments on commit 4bcd590

Please sign in to comment.