Skip to content

Commit 3e69c66

Browse files
Merge pull request #443 from supertokens/fix/protected-prop-createsession
fix: ignore protected props in create_new_session functions
2 parents 387077c + a51d242 commit 3e69c66

File tree

8 files changed

+46
-3
lines changed

8 files changed

+46
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## [unreleased]
1010

11+
12+
## [0.16.0] - 2023-09-13
13+
14+
1115
### Added
1216

1317
- The Dashboard recipe now accepts a new `admins` property which can be used to give Dashboard Users write privileges for the user dashboard.
1418

1519
### Changes
1620

1721
- 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
22+
- Now ignoring protected props in the payload in `create_new_session` and `create_new_session_without_request_response`
1823

1924
## [0.15.3] - 2023-09-24
2025

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070

7171
setup(
7272
name="supertokens_python",
73-
version="0.15.3",
73+
version="0.16.0",
7474
author="SuperTokens",
7575
license="Apache 2.0",
7676
author_email="[email protected]",

supertokens_python/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from __future__ import annotations
1515

1616
SUPPORTED_CDI_VERSIONS = ["3.0"]
17-
VERSION = "0.15.3"
17+
VERSION = "0.16.0"
1818
TELEMETRY = "/telemetry"
1919
USER_COUNT = "/users/count"
2020
USER_DELETE = "/user/remove"

supertokens_python/recipe/session/asyncio/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
get_session_from_request,
4242
refresh_session_in_request,
4343
)
44+
from ..constants import protected_props
4445
from ..utils import get_required_claim_validators
4546

4647
from supertokens_python.recipe.multitenancy.constants import DEFAULT_TENANT_ID
@@ -106,6 +107,10 @@ async def create_new_session_without_request_response(
106107

107108
final_access_token_payload = {**access_token_payload, "iss": issuer}
108109

110+
for prop in protected_props:
111+
if prop in final_access_token_payload:
112+
del final_access_token_payload[prop]
113+
109114
for claim in claims_added_by_other_recipes:
110115
update = await claim.build(user_id, tenant_id, user_context)
111116
final_access_token_payload = {**final_access_token_payload, **update}

supertokens_python/recipe/session/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,6 @@
4242
"parentRefreshTokenHash1",
4343
"refreshTokenHash1",
4444
"antiCsrfToken",
45+
"rsub",
4546
"tId",
4647
]

supertokens_python/recipe/session/recipe_implementation.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
from supertokens_python import AppInfo
4848

4949
from .interfaces import SessionContainer
50+
from .constants import protected_props
5051
from supertokens_python.querier import Querier
5152
from supertokens_python.recipe.multitenancy.constants import DEFAULT_TENANT_ID
5253

@@ -378,8 +379,13 @@ async def merge_into_access_token_payload(
378379
if session_info is None:
379380
return False
380381

382+
new_access_token_payload = session_info.custom_claims_in_access_token_payload
383+
for k in protected_props:
384+
if k in new_access_token_payload:
385+
del new_access_token_payload[k]
386+
381387
new_access_token_payload = {
382-
**session_info.custom_claims_in_access_token_payload,
388+
**new_access_token_payload,
383389
**access_token_payload_update,
384390
}
385391
for k in access_token_payload_update.keys():

supertokens_python/recipe/session/session_request_functions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
set_request_in_user_context_if_not_defined,
6161
)
6262
from supertokens_python.supertokens import Supertokens
63+
from .constants import protected_props
6364

6465
if TYPE_CHECKING:
6566
from supertokens_python.recipe.session.recipe import SessionRecipe
@@ -240,6 +241,10 @@ async def create_new_session_in_request(
240241

241242
final_access_token_payload = {**access_token_payload, "iss": issuer}
242243

244+
for prop in protected_props:
245+
if prop in final_access_token_payload:
246+
del final_access_token_payload[prop]
247+
243248
for claim in claims_added_by_other_recipes:
244249
update = await claim.build(user_id, tenant_id, user_context)
245250
final_access_token_payload = {**final_access_token_payload, **update}

tests/sessions/test_access_token_version.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,27 @@ async def test_should_validate_v3_tokens_with_check_database_enabled(app: TestCl
203203
}
204204

205205

206+
async def test_ignore_protected_props_in_create_session():
207+
init(**get_st_init_args([session.init()]))
208+
start_st()
209+
210+
s = await create_new_session_without_request_response(
211+
"public",
212+
"user1",
213+
{"foo": "bar"},
214+
)
215+
payload = parse_jwt_without_signature_verification(s.access_token).payload
216+
assert payload["foo"] == "bar"
217+
assert payload["sub"] == "user1"
218+
219+
s2 = await create_new_session_without_request_response(
220+
"public", "user2", s.get_access_token_payload()
221+
)
222+
payload = parse_jwt_without_signature_verification(s2.access_token).payload
223+
assert payload["foo"] == "bar"
224+
assert payload["sub"] == "user2"
225+
226+
206227
async def test_validation_logic_with_keys_that_can_use_json_nulls_values_in_claims():
207228
"""We want to make sure that for access token claims that can be null, the SDK does not fail access token validation if the
208229
core does not send them as part of the payload. For this we verify that validation passes when the keys are None, empty,

0 commit comments

Comments
 (0)