Skip to content

Commit

Permalink
fix: update csrf token cookie name (#1601)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Jul 28, 2021
1 parent 60d848d commit 64c90bf
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion x/nosurf.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package x

import (
"crypto/sha256"
"encoding/base64"
"fmt"
"net/http"

"github.com/ory/kratos/driver/config"
Expand Down Expand Up @@ -122,7 +124,7 @@ type CSRFHandler interface {
func CSRFCookieName(reg interface {
config.Provider
}, r *http.Request) string {
return base64.RawURLEncoding.EncodeToString([]byte(reg.Config(r.Context()).SelfPublicURL(r).String())) + "_csrf_token"
return "csrf_token_" + fmt.Sprintf("%x", sha256.Sum256([]byte(reg.Config(r.Context()).SelfPublicURL(r).String())))
}

func NosurfBaseCookieHandler(reg interface {
Expand Down
2 changes: 1 addition & 1 deletion x/nosurf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestNosurfBaseCookieHandler(t *testing.T) {
require.NoError(t, conf.Source().Set(config.ViperKeyPublicBaseURL, "http://foo.com/bar"))

cookie := x.NosurfBaseCookieHandler(reg)(httptest.NewRecorder(), httptest.NewRequest("GET", "https://foo/bar", nil))
assert.EqualValues(t, "aHR0cDovL2Zvby5jb20vYmFy_csrf_token", cookie.Name, "base64 representation of http://foo.com/bar")
assert.EqualValues(t, "csrf_token_01c86631efd1537ee34a98e75884a6e21dd8e2d9e944934bca21204106bfd32f", cookie.Name, "base64 representation of http://foo.com/bar")
assert.EqualValues(t, http.SameSiteLaxMode, cookie.SameSite, "is set to lax because https/secure is false - chrome rejects none samesite on non-https")
assert.EqualValues(t, nosurf.MaxAge, cookie.MaxAge)
assert.EqualValues(t, "/", cookie.Path, "cookie path is site root by default")
Expand Down

0 comments on commit 64c90bf

Please sign in to comment.