Skip to content

Commit c2f7d31

Browse files
Merge pull request #449 from supertokens/fix/aws-tldextract
fix: Handle AWS public urls separately when extracting TLDs
2 parents 34887f8 + d6944ce commit c2f7d31

File tree

6 files changed

+101
-3
lines changed

6 files changed

+101
-3
lines changed

CHANGELOG.md

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

99
## [unreleased]
1010

11+
## [0.16.1] - 2023-09-19
12+
- Handle AWS Public URLs (ending with `.amazonaws.com`) separately while extracting TLDs for SameSite attribute.
13+
1114

1215
## [0.16.0] - 2023-09-13
1316

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.16.0",
73+
version="0.16.1",
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.16.0"
17+
VERSION = "0.16.1"
1818
TELEMETRY = "/telemetry"
1919
USER_COUNT = "/users/count"
2020
USER_DELETE = "/user/remove"

supertokens_python/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,13 @@ def get_top_level_domain_for_same_site_resolution(url: str) -> str:
299299

300300
if hostname.startswith("localhost") or is_an_ip_address(hostname):
301301
return "localhost"
302+
302303
parsed_url: Any = extract(hostname, include_psl_private_domains=True)
303304
if parsed_url.domain == "": # type: ignore
305+
# We need to do this because of https://github.com/supertokens/supertokens-python/issues/394
306+
if hostname.endswith(".amazonaws.com") and parsed_url.suffix == hostname:
307+
return hostname
308+
304309
raise Exception(
305310
"Please make sure that the apiDomain and websiteDomain have correct values"
306311
)

tests/test_config.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,3 +736,67 @@ async def test_samesite_invalid_config():
736736
)
737737
else:
738738
assert False, "Exception not raised"
739+
740+
741+
@mark.asyncio
742+
async def test_cookie_samesite_with_ec2_public_url():
743+
start_st()
744+
init(
745+
supertokens_config=SupertokensConfig("http://localhost:3567"),
746+
app_info=InputAppInfo(
747+
app_name="SuperTokens Demo",
748+
api_domain="https://ec2-xx-yyy-zzz-0.compute-1.amazonaws.com:3001",
749+
website_domain="https://blog.supertokens.com",
750+
api_base_path="/",
751+
),
752+
framework="fastapi",
753+
recipe_list=[
754+
session.init(get_token_transfer_method=lambda _, __, ___: "cookie")
755+
],
756+
)
757+
758+
# domain name isn't provided so browser decides to use the same host
759+
# which will be ec2-xx-yyy-zzz-0.compute-1.amazonaws.com
760+
assert SessionRecipe.get_instance().config.cookie_domain is None
761+
assert SessionRecipe.get_instance().config.cookie_same_site == "none"
762+
assert SessionRecipe.get_instance().config.cookie_secure is True
763+
764+
reset()
765+
766+
init(
767+
supertokens_config=SupertokensConfig("http://localhost:3567"),
768+
app_info=InputAppInfo(
769+
app_name="SuperTokens Demo",
770+
api_domain="http://ec2-xx-yyy-zzz-0.compute-1.amazonaws.com:3001",
771+
website_domain="http://ec2-aa-bbb-ccc-0.compute-1.amazonaws.com:3000",
772+
api_base_path="/",
773+
),
774+
framework="fastapi",
775+
recipe_list=[
776+
session.init(get_token_transfer_method=lambda _, __, ___: "cookie")
777+
],
778+
)
779+
780+
assert SessionRecipe.get_instance().config.cookie_domain is None
781+
assert SessionRecipe.get_instance().config.cookie_same_site == "none"
782+
assert SessionRecipe.get_instance().config.cookie_secure is False
783+
784+
reset()
785+
786+
init(
787+
supertokens_config=SupertokensConfig("http://localhost:3567"),
788+
app_info=InputAppInfo(
789+
app_name="SuperTokens Demo",
790+
api_domain="http://ec2-xx-yyy-zzz-0.compute-1.amazonaws.com:3001",
791+
website_domain="http://ec2-xx-yyy-zzz-0.compute-1.amazonaws.com:3000",
792+
api_base_path="/",
793+
),
794+
framework="fastapi",
795+
recipe_list=[
796+
session.init(get_token_transfer_method=lambda _, __, ___: "cookie")
797+
],
798+
)
799+
800+
assert SessionRecipe.get_instance().config.cookie_domain is None
801+
assert SessionRecipe.get_instance().config.cookie_same_site == "lax"
802+
assert SessionRecipe.get_instance().config.cookie_secure is False

tests/test_utils.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
import pytest
44
import threading
55

6-
from supertokens_python.utils import humanize_time, is_version_gte
6+
from supertokens_python.utils import (
7+
humanize_time,
8+
is_version_gte,
9+
get_top_level_domain_for_same_site_resolution,
10+
)
711
from supertokens_python.utils import RWMutex
812

913
from tests.utils import is_subset
@@ -171,3 +175,25 @@ def balance_is_valid():
171175
expected_balance -= 10 * 5 # 10 threads withdrawing 5 each
172176
actual_balance, _ = account.get_stats()
173177
assert actual_balance == expected_balance, "Incorrect account balance"
178+
179+
180+
@pytest.mark.parametrize(
181+
"url,res",
182+
[
183+
("http://localhost:3001", "localhost"),
184+
(
185+
"https://ec2-xx-yyy-zzz-0.compute-1.amazonaws.com",
186+
"ec2-xx-yyy-zzz-0.compute-1.amazonaws.com",
187+
),
188+
(
189+
"https://foo.vercel.com",
190+
"vercel.com",
191+
),
192+
(
193+
"https://blog.supertokens.com",
194+
"supertokens.com",
195+
),
196+
],
197+
)
198+
def test_tld_for_same_site(url: str, res: str):
199+
assert get_top_level_domain_for_same_site_resolution(url) == res

0 commit comments

Comments
 (0)