Skip to content

Commit

Permalink
Merge pull request #363 from supertokens/ignore-protected-props
Browse files Browse the repository at this point in the history
refactor: Ignore protected props in create new session
  • Loading branch information
rishabhpoddar authored Sep 12, 2023
2 parents d86ddb8 + e31dd3d commit 93083c9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 26 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
29 changes: 16 additions & 13 deletions recipe/session/accessTokenVersions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,27 +171,30 @@ func TestShouldThrowErrorWhenUsingProtectedProps(t *testing.T) {
testServer.Close()
}()

appSub := "asdf"
body := map[string]map[string]*string{
"payload": {
"sub": &appSub,
},
sessionResponse, err := CreateNewSessionWithoutRequestResponse("public", "testing", map[string]interface{}{
"customProps": "custom",
}, map[string]interface{}{}, nil)

if err != nil {
t.Error(err.Error())
}

postBody, err := json.Marshal(body)
newSession, err := CreateNewSessionWithoutRequestResponse("public", "testing2", sessionResponse.GetAccessTokenPayload(), map[string]interface{}{}, nil)

if err != nil {
t.Error(err.Error())
}
res2, err2 := http.Post(testServer.URL+"/create", "application/json", bytes.NewBuffer(postBody))
if err2 != nil {

accessToken := newSession.GetAccessToken()

parsedToken, err := ParseJWTWithoutSignatureVerification(accessToken)
if err != nil {
t.Error(err.Error())
}

assert.Equal(t, 400, res2.StatusCode)
cookies := unittesting.ExtractInfoFromResponse(res2)
assert.True(t, cookies["accessTokenFromAny"] == "")
assert.True(t, cookies["refreshTokenFromAny"] == "")
assert.True(t, cookies["frontToken"] == "")
assert.True(t, parsedToken.Payload["customProps"] == "custom")
// This makes sure it does not reuse the sub from the old payload
assert.True(t, parsedToken.Payload["sub"] == "testing2")
}

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 93083c9

Please sign in to comment.