Skip to content

Commit ce8416e

Browse files
Merge pull request #271 from supertokens/fix/flask-response
fix: Make flask response consistent with other frameworks
2 parents 5ac99fb + 927d18f commit ce8416e

File tree

2 files changed

+15
-45
lines changed

2 files changed

+15
-45
lines changed

CHANGELOG.md

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

88
## unreleased
99

10+
- Add missing `original` attribute to flask response and remove logic for cases where `response` is `None`
11+
1012
## [0.11.12] - 2022-12-27
1113
- Fix django cookie expiry time format to make it consistent with other frameworks: https://github.com/supertokens/supertokens-python/issues/267
1214

supertokens_python/framework/flask/flask_response.py

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class FlaskResponse(BaseResponse):
2323
def __init__(self, response: Response):
2424
super().__init__({})
2525
self.response = response
26+
self.original = response
2627
self.headers: List[Any] = []
2728
self.response_sent = False
2829
self.status_set = False
@@ -44,53 +45,22 @@ def set_cookie(
4445
httponly: bool = False,
4546
samesite: str = "lax",
4647
):
47-
from werkzeug.http import dump_cookie
48-
49-
if self.response is None:
50-
cookie = dump_cookie(
51-
key,
52-
value=value,
53-
expires=int(expires / 1000),
54-
path=path,
55-
domain=domain,
56-
secure=secure,
57-
httponly=httponly,
58-
samesite=samesite,
59-
)
60-
self.headers.append(("Set-Cookie", cookie))
61-
else:
62-
self.response.set_cookie(
63-
key,
64-
value=value,
65-
expires=expires / 1000,
66-
path=path,
67-
domain=domain,
68-
secure=secure,
69-
httponly=httponly,
70-
samesite=samesite,
71-
)
48+
self.response.set_cookie(
49+
key,
50+
value=value,
51+
expires=expires / 1000,
52+
path=path,
53+
domain=domain,
54+
secure=secure,
55+
httponly=httponly,
56+
samesite=samesite,
57+
)
7258

7359
def set_header(self, key: str, value: str):
74-
if self.response is None:
75-
# TODO in the future the headrs must be validated..
76-
# if not isinstance(value, str):
77-
# raise TypeError("Value should be unicode.")
78-
if "\n" in value or "\r" in value:
79-
raise ValueError(
80-
"Detected newline in header value. This is "
81-
"a potential security problem"
82-
)
83-
self.headers.append((key, value))
84-
else:
85-
self.response.headers.add(key, value)
60+
self.response.headers.add(key, value)
8661

8762
def get_header(self, key: str) -> Union[None, str]:
88-
if self.response is not None:
89-
return self.response.headers.get(key)
90-
for value in self.headers:
91-
if value[0] == key:
92-
return value[1]
93-
return None
63+
return self.response.headers.get(key)
9464

9565
def set_status_code(self, status_code: int):
9666
if not self.status_set:
@@ -99,8 +69,6 @@ def set_status_code(self, status_code: int):
9969
self.status_set = True
10070

10171
def get_headers(self):
102-
if self.response is None:
103-
return self.headers
10472
return self.response.headers
10573

10674
def set_json_content(self, content: Dict[str, Any]):

0 commit comments

Comments
 (0)