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

Add CORS headers to the tsh login callback #39696

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Changes from 1 commit
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
27 changes: 20 additions & 7 deletions lib/client/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,14 @@ func (rd *Redirector) callback(w http.ResponseWriter, r *http.Request) (*auth.SS
return nil, trace.NotFound("path not found")
}

if r.URL.Query().Has("err") {
err := r.URL.Query().Get("err")
r.ParseForm()
if r.Form.Has("err") {
err := r.Form.Get("err")
return nil, trace.Errorf("identity provider callback failed with error: %v", err)
}

// Decrypt ciphertext to get login response.
plaintext, err := rd.key.Open([]byte(r.URL.Query().Get("response")))
plaintext, err := rd.key.Open([]byte(r.Form.Get("response")))
if err != nil {
return nil, trace.BadParameter("failed to decrypt response: in %v, err: %v", r.URL.String(), err)
}
Expand All @@ -301,12 +302,27 @@ func (rd *Redirector) Close() error {
// and sends a result to the channel and redirect users to error page
func (rd *Redirector) wrapCallback(fn func(http.ResponseWriter, *http.Request) (*auth.SSHLoginResponse, error)) http.Handler {
clone := *rd.proxyURL
origin := clone.String()
clone.Path = LoginFailedRedirectURL
errorURL := clone.String()
clone.Path = LoginSuccessRedirectURL
successURL := clone.String()

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", origin)
w.Header().Add("Access-Control-Allow-Methods", "GET, POST")
w.Header().Add("Access-Control-Allow-Private-Network", "true")
w.Header().Add("Allow", "GET, OPTIONS, POST")
switch r.Method {
default:
w.WriteHeader(http.StatusMethodNotAllowed)
return
case http.MethodOptions:
w.WriteHeader(http.StatusOK)
return
case http.MethodGet, http.MethodPost:
}

response, err := fn(w, r)
if err != nil {
if trace.IsNotFound(err) {
Expand All @@ -316,18 +332,15 @@ func (rd *Redirector) wrapCallback(fn func(http.ResponseWriter, *http.Request) (
select {
case rd.errorC <- err:
case <-rd.context.Done():
http.Redirect(w, r, errorURL, http.StatusFound)
return
}
http.Redirect(w, r, errorURL, http.StatusFound)
return
}
select {
case rd.responseC <- response:
http.Redirect(w, r, successURL, http.StatusFound)
case <-rd.context.Done():
http.Redirect(w, r, errorURL, http.StatusFound)
return
}
http.Redirect(w, r, successURL, http.StatusFound)
})
}
Loading