Skip to content

Commit 365defa

Browse files
committed
feat: allow disabling tldextract HTTP requests
- Workaround till tldextract#233 is implemented - Adds flag to control disabling HTTP requests - Adds `pyfakefs` to use in tests
1 parent 68b9356 commit 365defa

File tree

12 files changed

+99
-12
lines changed

12 files changed

+99
-12
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ __pycache__
88
releasePassword
99
apiPassword
1010
venv/
11-
env/
11+
./env/
1212
.env
1313
.DS_Store
1414
bin/

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.28.1] - 2025-02-17
12+
- Adds option to disable `tldextract` HTTP calls by setting `SUPERTOKENS_TLDEXTRACT_DISABLE_HTTP=1`
13+
1114
## [0.28.0]
1215
- **[Breaking] Updates pre-commit hooks to use `pre-commit`**
1316
- Migration:

dev-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ flask-cors==5.0.0
99
nest-asyncio==1.6.0
1010
pdoc3==0.11.0
1111
pre-commit==3.5.0
12+
pyfakefs==5.7.4
1213
pylint==3.2.7
1314
pyright==1.1.393
1415
python-dotenv==1.0.1

setup.py

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

8484
setup(
8585
name="supertokens_python",
86-
version="0.28.0",
86+
version="0.28.1",
8787
author="SuperTokens",
8888
license="Apache 2.0",
8989
author_email="team@supertokens.com",

supertokens_python/constants.py

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

1717
SUPPORTED_CDI_VERSIONS = ["5.2"]
18-
VERSION = "0.28.0"
18+
VERSION = "0.28.1"
1919
TELEMETRY = "/telemetry"
2020
USER_COUNT = "/users/count"
2121
USER_DELETE = "/user/remove"

supertokens_python/env/__init__.py

Whitespace-only changes.

supertokens_python/env/base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from os import environ
2+
3+
from supertokens_python.env.utils import str_to_bool
4+
5+
6+
def FLAG_tldextract_disable_http():
7+
"""
8+
Disable HTTP calls from `tldextract`.
9+
"""
10+
val = environ.get("SUPERTOKENS_TLDEXTRACT_DISABLE_HTTP", "0")
11+
12+
return str_to_bool(val)

supertokens_python/env/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def str_to_bool(val: str) -> bool:
2+
"""
3+
Convert ENV values to boolean
4+
"""
5+
return val.lower() in ("true", "t", "1")

supertokens_python/utils.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@
3535
from urllib.parse import urlparse
3636

3737
from httpx import HTTPStatusError, Response
38-
from tldextract import extract # type: ignore
38+
from tldextract import TLDExtract
3939

40+
from supertokens_python.env.base import FLAG_tldextract_disable_http
4041
from supertokens_python.framework.django.framework import DjangoFramework
4142
from supertokens_python.framework.fastapi.framework import FastapiFramework
4243
from supertokens_python.framework.flask.framework import FlaskFramework
@@ -288,7 +289,16 @@ def get_top_level_domain_for_same_site_resolution(url: str) -> str:
288289
if hostname.startswith("localhost") or is_an_ip_address(hostname):
289290
return "localhost"
290291

291-
parsed_url: Any = extract(hostname, include_psl_private_domains=True)
292+
extract = TLDExtract(fallback_to_snapshot=True, include_psl_private_domains=True)
293+
# Explicitly disable HTTP calls, use snapshot bundled into library
294+
if FLAG_tldextract_disable_http():
295+
extract = TLDExtract(
296+
suffix_list_urls=(), # Ensures no HTTP calls
297+
fallback_to_snapshot=True,
298+
include_psl_private_domains=True,
299+
)
300+
301+
parsed_url: Any = extract(hostname)
292302
if parsed_url.domain == "": # type: ignore
293303
# We need to do this because of https://github.com/supertokens/supertokens-python/issues/394
294304
if hostname.endswith(".amazonaws.com") and parsed_url.suffix == hostname:

tests/auth-react/fastapi-server/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,7 @@ async def exception_handler(a, b): # type: ignore
11171117
return JSONResponse(status_code=500, content={})
11181118

11191119

1120-
app.add_middleware(ExceptionMiddleware, handlers=app.exception_handlers)
1120+
app.add_middleware(ExceptionMiddleware, handlers=app.exception_handlers) # type: ignore
11211121

11221122

11231123
@app.post("/beforeeach")

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy