Skip to content

Commit 93083c9

Browse files
Merge pull request #363 from supertokens/ignore-protected-props
refactor: Ignore protected props in create new session
2 parents d86ddb8 + e31dd3d commit 93083c9

File tree

6 files changed

+39
-26
lines changed

6 files changed

+39
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
### Changes
1717

1818
- 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
19+
- Now ignoring protected props in the payload in `CreateNewSession` and `CreateNewSessionWithoutRequestResponse`
1920

2021
## [0.13.2] - 2023-08-28
2122

recipe/session/accessTokenVersions_test.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -171,27 +171,30 @@ func TestShouldThrowErrorWhenUsingProtectedProps(t *testing.T) {
171171
testServer.Close()
172172
}()
173173

174-
appSub := "asdf"
175-
body := map[string]map[string]*string{
176-
"payload": {
177-
"sub": &appSub,
178-
},
174+
sessionResponse, err := CreateNewSessionWithoutRequestResponse("public", "testing", map[string]interface{}{
175+
"customProps": "custom",
176+
}, map[string]interface{}{}, nil)
177+
178+
if err != nil {
179+
t.Error(err.Error())
179180
}
180181

181-
postBody, err := json.Marshal(body)
182+
newSession, err := CreateNewSessionWithoutRequestResponse("public", "testing2", sessionResponse.GetAccessTokenPayload(), map[string]interface{}{}, nil)
183+
182184
if err != nil {
183185
t.Error(err.Error())
184186
}
185-
res2, err2 := http.Post(testServer.URL+"/create", "application/json", bytes.NewBuffer(postBody))
186-
if err2 != nil {
187+
188+
accessToken := newSession.GetAccessToken()
189+
190+
parsedToken, err := ParseJWTWithoutSignatureVerification(accessToken)
191+
if err != nil {
187192
t.Error(err.Error())
188193
}
189194

190-
assert.Equal(t, 400, res2.StatusCode)
191-
cookies := unittesting.ExtractInfoFromResponse(res2)
192-
assert.True(t, cookies["accessTokenFromAny"] == "")
193-
assert.True(t, cookies["refreshTokenFromAny"] == "")
194-
assert.True(t, cookies["frontToken"] == "")
195+
assert.True(t, parsedToken.Payload["customProps"] == "custom")
196+
// This makes sure it does not reuse the sub from the old payload
197+
assert.True(t, parsedToken.Payload["sub"] == "testing2")
195198
}
196199

197200
func TestMergeIntoATShouldHelpMigratingV2TokenUsingProtectedProps(t *testing.T) {

recipe/session/constants.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,17 @@ const (
3131
CookieSameSite_LAX = "lax"
3232
CookieSameSite_STRICT = "strict"
3333
)
34+
35+
var JWKCacheMaxAgeInMs int64 = 60000
36+
var JWKRefreshRateLimit = 500
37+
var protectedProps = []string{
38+
"sub",
39+
"iat",
40+
"exp",
41+
"sessionHandle",
42+
"parentRefreshTokenHash1",
43+
"refreshTokenHash1",
44+
"antiCsrfToken",
45+
"rsub",
46+
"tId",
47+
}

recipe/session/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ func CreateNewSessionWithoutRequestResponse(tenantId string, userID string, acce
6464

6565
finalAccessTokenPayload["iss"] = issuer
6666

67+
for _, protectedProp := range protectedProps {
68+
delete(finalAccessTokenPayload, protectedProp)
69+
}
70+
6771
for _, claim := range claimsAddedByOtherRecipes {
6872
finalAccessTokenPayload, err = claim.Build(userID, tenantId, finalAccessTokenPayload, userContext[0])
6973
if err != nil {

recipe/session/recipeImplementation.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,6 @@ import (
3232
"github.com/supertokens/supertokens-golang/supertokens"
3333
)
3434

35-
var protectedProps = []string{
36-
"sub",
37-
"iat",
38-
"exp",
39-
"sessionHandle",
40-
"parentRefreshTokenHash1",
41-
"refreshTokenHash1",
42-
"antiCsrfToken",
43-
"tId",
44-
}
45-
46-
var JWKCacheMaxAgeInMs int64 = 60000
47-
var JWKRefreshRateLimit = 500
4835
var jwksCache *sessmodels.GetJWKSResult = nil
4936
var mutex sync.RWMutex
5037

recipe/session/sessionRequestFunctions.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ func CreateNewSessionInRequest(req *http.Request, res http.ResponseWriter, tenan
4343
issuer := appInfo.APIDomain.GetAsStringDangerous() + appInfo.APIBasePath.GetAsStringDangerous()
4444
finalAccessTokenPayload["iss"] = issuer
4545

46+
for _, protectedProp := range protectedProps {
47+
delete(finalAccessTokenPayload, protectedProp)
48+
}
49+
4650
for _, claim := range claimsAddedByOtherRecipes {
4751
_finalAccessTokenPayload, err := claim.Build(userID, tenantId, finalAccessTokenPayload, userContext)
4852
if err != nil {

0 commit comments

Comments
 (0)