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

Support concurrent CSRF cookies by using a prefix of nonce #178

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
25 changes: 22 additions & 3 deletions internal/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,14 @@ func ClearCookie(r *http.Request) *http.Cookie {
}
}

func buildCSRFCookieName(nonce string) string {
return config.CSRFCookieName + "_" + nonce[:6]
}

// MakeCSRFCookie makes a csrf cookie (used during login only)
func MakeCSRFCookie(r *http.Request, nonce string) *http.Cookie {
return &http.Cookie{
Name: config.CSRFCookieName,
Name: buildCSRFCookieName(nonce),
Value: nonce,
Path: "/",
Domain: csrfCookieDomain(r),
Expand All @@ -184,9 +188,9 @@ func MakeCSRFCookie(r *http.Request, nonce string) *http.Cookie {
}

// ClearCSRFCookie makes an expired csrf cookie to clear csrf cookie
func ClearCSRFCookie(r *http.Request) *http.Cookie {
func ClearCSRFCookie(r *http.Request, c *http.Cookie) *http.Cookie {
return &http.Cookie{
Name: config.CSRFCookieName,
Name: c.Name,
Value: "",
Path: "/",
Domain: csrfCookieDomain(r),
Expand All @@ -196,6 +200,21 @@ func ClearCSRFCookie(r *http.Request) *http.Cookie {
}
}

// FindCSRFCookie extracts the CSRF cookie from the request based on state.
func FindCSRFCookie(r *http.Request) (c *http.Cookie, err error) {
state := r.URL.Query().Get("state")
if len(state) < 34 {
return nil, errors.New("Invalid CSRF state value")
}
mwitkow marked this conversation as resolved.
Show resolved Hide resolved

// Check for CSRF cookie
c, err = r.Cookie(buildCSRFCookieName(state))
if err != nil {
return nil, err
}
return c, nil
}

// ValidateCSRFCookie validates the csrf cookie against state
func ValidateCSRFCookie(r *http.Request, c *http.Cookie) (valid bool, provider string, redirect string, err error) {
state := r.URL.Query().Get("state")
Expand Down
4 changes: 3 additions & 1 deletion internal/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,12 @@ func TestAuthMakeCSRFCookie(t *testing.T) {
}

func TestAuthClearCSRFCookie(t *testing.T) {
assert := assert.New(t)
config, _ = NewConfig([]string{})
r, _ := http.NewRequest("GET", "http://example.com", nil)

c := ClearCSRFCookie(r)
c := ClearCSRFCookie(r, &http.Cookie{Name: "someCsrfCookie"})
assert.Equal("someCsrfCookie", c.Name)
if c.Value != "" {
t.Error("ClearCSRFCookie should create cookie with empty value")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (s *Server) AuthCallbackHandler() http.HandlerFunc {
logger := s.logger(r, "AuthCallback", "default", "Handling callback")

// Check for CSRF cookie
c, err := r.Cookie(config.CSRFCookieName)
c, err := FindCSRFCookie(r)
if err != nil {
logger.Info("Missing csrf cookie")
http.Error(w, "Not authorized", 401)
Expand Down Expand Up @@ -153,7 +153,7 @@ func (s *Server) AuthCallbackHandler() http.HandlerFunc {
}

// Clear CSRF cookie
http.SetCookie(w, ClearCSRFCookie(r))
http.SetCookie(w, ClearCSRFCookie(r, c))

// Exchange code for token
token, err := p.ExchangeCode(redirectUri(r), r.URL.Query().Get("code"))
Expand Down
2 changes: 1 addition & 1 deletion internal/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestServerAuthHandlerExpired(t *testing.T) {
// Check for CSRF cookie
var cookie *http.Cookie
for _, c := range res.Cookies() {
if c.Name == config.CSRFCookieName {
if strings.HasPrefix(c.Name, config.CSRFCookieName) {
cookie = c
}
}
Expand Down