Skip to content

Commit f56a634

Browse files
Merge pull request #464 from supertokens/flask-fix
fix: flask issue with gunicorn
2 parents 095e4b9 + f10fe6c commit f56a634

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

CHANGELOG.md

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

99
## [unreleased]
1010

11+
## [0.18.3] - 2023-12-07
12+
13+
- Fixes security issue with shared `g` objects from gunicorn: https://github.com/supertokens/supertokens-python/issues/463
14+
1115
## [0.18.2] - 2023-12-05
1216

1317
- Updates LinkedIn OAuth implementation as per the latest [changes](https://learn.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/sign-in-with-linkedin-v2?context=linkedin%2Fconsumer%2Fcontext#authenticating-members).

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

supertokens_python/framework/flask/flask_middleware.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ def _(response: Response):
7373

7474
return response_.response
7575

76+
@app.teardown_request
77+
def _(_):
78+
from flask import g
79+
80+
if hasattr(g, "supertokens"):
81+
# this is to ensure there are no shared objects between requests.
82+
# calling any other API with a shared request causes a security issue, resulting in unintentional
83+
# sign-ins. More on this here - https://github.com/supertokens/supertokens-python/issues/463
84+
g.pop("supertokens")
85+
7686
def set_error_handler(self):
7787
app = self.app
7888
from supertokens_python.exceptions import SuperTokensError

tests/Flask/test_flask.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,3 +848,51 @@ def test_that_verify_session_return_401_if_access_token_is_not_sent_and_middlewa
848848
"/verify", headers={"Authorization": "Bearer " + s.get_access_token()}
849849
)
850850
assert res.status_code == 200
851+
852+
853+
@fixture(scope="function")
854+
def flask_app_that_checks_for_supertokens_in_g():
855+
app = Flask(__name__)
856+
857+
app.testing = True
858+
859+
@app.teardown_request
860+
def _(_):
861+
assert hasattr(g, "supertokens") is False
862+
863+
Middleware(app)
864+
865+
init(
866+
supertokens_config=SupertokensConfig("http://localhost:3567"),
867+
app_info=InputAppInfo(
868+
app_name="SuperTokens Demo",
869+
api_domain="http://api.supertokens.io",
870+
website_domain="http://supertokens.io",
871+
api_base_path="/auth",
872+
),
873+
framework="flask",
874+
recipe_list=[
875+
session.init(
876+
anti_csrf="VIA_TOKEN",
877+
cookie_domain="supertokens.io",
878+
get_token_transfer_method=lambda _, __, ___: "cookie",
879+
),
880+
],
881+
)
882+
883+
@app.route("/create-session") # type: ignore
884+
def create_session_api(): # type: ignore
885+
create_new_session(request, "public", "userId", {}, {})
886+
return jsonify({})
887+
888+
return app
889+
890+
891+
def test_that_supertokens_is_not_in_g_if_middleware_is_not_added(
892+
flask_app_that_checks_for_supertokens_in_g: Any,
893+
):
894+
start_st()
895+
896+
client = flask_app_that_checks_for_supertokens_in_g.test_client()
897+
898+
assert client.get("/create-session").status_code == 200

0 commit comments

Comments
 (0)