Skip to content

Commit

Permalink
fix: auth: don't delete the csrf cookie (#1435)
Browse files Browse the repository at this point in the history
Signed-off-by: Grant Linville <[email protected]>
  • Loading branch information
g-linville authored Jan 24, 2025
1 parent 6e29944 commit 8691e85
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ func (pm *Manager) AuthenticateRequest(req *http.Request) (*authenticator.Respon
}

// Overwrite the cookie with just the token.
cookie.Value = contents.Token
req.Header.Del("Cookie")
req.AddCookie(cookie)
if err := replaceTokenCookie(contents.Token, req); err != nil {
return nil, false, err
}

// Reset the cookie value after authenticating.
defer func() {
cookie.Value = cookieOriginalValue
req.Header.Del("Cookie")
req.AddCookie(cookie)
if err := replaceTokenCookie(cookieOriginalValue, req); err != nil {
log.Errorf("failed to reset cookie value: %v", err)
}
}()

return proxy.authenticateRequest(req)
Expand Down Expand Up @@ -113,15 +113,9 @@ func (pm *Manager) ServeHTTP(w http.ResponseWriter, r *http.Request) {
provider = contents.AuthProvider

// Update the cookie to just be the token, which is what the auth provider expects.
cookie.Value = contents.Token
allCookies := r.Cookies()
r.Header.Del("Cookie")
for _, c := range allCookies {
if c.Name != ObotAccessTokenCookie {
r.AddCookie(c)
}
if err := replaceTokenCookie(contents.Token, r); err != nil {
http.Error(w, fmt.Sprintf("failed to replace token cookie: %v", err), http.StatusInternalServerError)
}
r.AddCookie(cookie)
}
}

Expand Down Expand Up @@ -327,3 +321,28 @@ func (p *Proxy) authenticateRequest(req *http.Request) (*authenticator.Response,
User: u,
}, true, nil
}

func replaceTokenCookie(token string, req *http.Request) error {
tokenCookie, err := req.Cookie(ObotAccessTokenCookie)
if err != nil {
return fmt.Errorf("failed to get token cookie: %w", err)
}

tokenCookie.Value = token

otherCookies := make([]http.Cookie, 0, len(req.Cookies()))
for _, c := range req.Cookies() {
if c.Name != ObotAccessTokenCookie {
otherCookies = append(otherCookies, *c)
}
}

req.Header.Del("Cookie")

for _, c := range otherCookies {
req.AddCookie(&c)
}
req.AddCookie(tokenCookie)

return nil
}

0 comments on commit 8691e85

Please sign in to comment.