Skip to content

Commit

Permalink
Ignore protected props in create new session
Browse files Browse the repository at this point in the history
  • Loading branch information
nkshah2 committed Sep 12, 2023
1 parent f27831e commit fd9fc48
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changes

- Dashboard APIs now return a status code `403` for all non-GET requests if the currently logged in Dashboard User is not listed in the `admins` array
- Now ignoring protected props in the payload in `CreateNewSession` and `CreateNewSessionWithoutRequestResponse`

## [0.13.2] - 2023-08-28

Expand Down
17 changes: 12 additions & 5 deletions recipe/session/accessTokenVersions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,21 @@ func TestShouldThrowErrorWhenUsingProtectedProps(t *testing.T) {
}
res2, err2 := http.Post(testServer.URL+"/create", "application/json", bytes.NewBuffer(postBody))
if err2 != nil {
t.Error(err.Error())
t.Error(err2.Error())
}

assert.Equal(t, 400, res2.StatusCode)
assert.Equal(t, 200, res2.StatusCode)
cookies := unittesting.ExtractInfoFromResponse(res2)
assert.True(t, cookies["accessTokenFromAny"] == "")
assert.True(t, cookies["refreshTokenFromAny"] == "")
assert.True(t, cookies["frontToken"] == "")
assert.False(t, cookies["accessTokenFromAny"] == "")
assert.False(t, cookies["refreshTokenFromAny"] == "")
assert.False(t, cookies["frontToken"] == "")

parsedToken, err := ParseJWTWithoutSignatureVerification(cookies["accessTokenFromAny"])
if err != nil {
t.Error(err.Error())
}

assert.True(t, parsedToken.Payload["sub"] != "asdf")
}

func TestMergeIntoATShouldHelpMigratingV2TokenUsingProtectedProps(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions recipe/session/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,17 @@ const (
CookieSameSite_LAX = "lax"
CookieSameSite_STRICT = "strict"
)

var JWKCacheMaxAgeInMs int64 = 60000
var JWKRefreshRateLimit = 500
var protectedProps = []string{
"sub",
"iat",
"exp",
"sessionHandle",
"parentRefreshTokenHash1",
"refreshTokenHash1",
"antiCsrfToken",
"rsub",
"tId",
}
4 changes: 4 additions & 0 deletions recipe/session/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func CreateNewSessionWithoutRequestResponse(tenantId string, userID string, acce

finalAccessTokenPayload["iss"] = issuer

for _, protectedProp := range protectedProps {
delete(finalAccessTokenPayload, protectedProp)
}

for _, claim := range claimsAddedByOtherRecipes {
finalAccessTokenPayload, err = claim.Build(userID, tenantId, finalAccessTokenPayload, userContext[0])
if err != nil {
Expand Down
13 changes: 0 additions & 13 deletions recipe/session/recipeImplementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,6 @@ import (
"github.com/supertokens/supertokens-golang/supertokens"
)

var protectedProps = []string{
"sub",
"iat",
"exp",
"sessionHandle",
"parentRefreshTokenHash1",
"refreshTokenHash1",
"antiCsrfToken",
"tId",
}

var JWKCacheMaxAgeInMs int64 = 60000
var JWKRefreshRateLimit = 500
var jwksCache *sessmodels.GetJWKSResult = nil
var mutex sync.RWMutex

Expand Down
4 changes: 4 additions & 0 deletions recipe/session/sessionRequestFunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func CreateNewSessionInRequest(req *http.Request, res http.ResponseWriter, tenan
issuer := appInfo.APIDomain.GetAsStringDangerous() + appInfo.APIBasePath.GetAsStringDangerous()
finalAccessTokenPayload["iss"] = issuer

for _, protectedProp := range protectedProps {
delete(finalAccessTokenPayload, protectedProp)
}

for _, claim := range claimsAddedByOtherRecipes {
_finalAccessTokenPayload, err := claim.Build(userID, tenantId, finalAccessTokenPayload, userContext)
if err != nil {
Expand Down

0 comments on commit fd9fc48

Please sign in to comment.