Skip to content

Commit 84c1946

Browse files
authored
Do not check JWT_TOKEN_LOCATION when setting csrf value in a jwt (#538)
Previously, we would only include the csrf double submit value in a jwt if `JWT_COOKIE_CSRF_PROTECT` was true (the default) AND `JWT_TOKEN_LOCATION` was configured to use cookies. However, since we allow overwriting `locations` on a per-route basis instead of only globally for he whole application, we could create a situation where a single route was configured to use cookies when the rest of the app was not, and csrf checks were not happening against that endpoint. This change makes it so that any jwts will be encoded with a csrf value when `JWT_COOKIE_CSRF_PROTECT` is true, regardless of if the app is globally configured to use cookies. It will also verify the csrf double submit token on any route that uses cookies when `JWT_COOKIE_CSRF_PROTECT` is true, regardless of if that is set globally in the application or on an individual route. As a result of this change, you might notice that using jwts without cookies now include a csrf value. This will not change the behavior of non-jwt based endpoints at all, your jwts will just be a little bigger. You can remove that key from the jwt by explicitly setting `JWT_COOKIE_CSRF_PROTECT` to False, if you are not using cookies.
1 parent f65202f commit 84c1946

File tree

5 files changed

+12
-18
lines changed

5 files changed

+12
-18
lines changed

flask_jwt_extended/config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ def refresh_json_key(self) -> str:
135135
return current_app.config["JWT_REFRESH_JSON_KEY"]
136136

137137
@property
138-
def csrf_protect(self) -> bool:
139-
return self.jwt_in_cookies and current_app.config["JWT_COOKIE_CSRF_PROTECT"]
138+
def cookie_csrf_protect(self) -> bool:
139+
return current_app.config["JWT_COOKIE_CSRF_PROTECT"]
140140

141141
@property
142142
def csrf_request_methods(self) -> Iterable[str]:

flask_jwt_extended/jwt_manager.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ def _encode_jwt_from_config(
516516
algorithm=config.algorithm,
517517
audience=config.encode_audience,
518518
claim_overrides=claim_overrides,
519-
csrf=config.csrf_protect,
519+
csrf=config.cookie_csrf_protect,
520520
expires_delta=expires_delta,
521521
fresh=fresh,
522522
header_overrides=header_overrides,

flask_jwt_extended/utils.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def set_access_cookies(
305305
samesite=config.cookie_samesite,
306306
)
307307

308-
if config.csrf_protect and config.csrf_in_cookies:
308+
if config.cookie_csrf_protect and config.csrf_in_cookies:
309309
response.set_cookie(
310310
config.access_csrf_cookie_name,
311311
value=get_csrf_token(encoded_access_token),
@@ -358,7 +358,7 @@ def set_refresh_cookies(
358358
samesite=config.cookie_samesite,
359359
)
360360

361-
if config.csrf_protect and config.csrf_in_cookies:
361+
if config.cookie_csrf_protect and config.csrf_in_cookies:
362362
response.set_cookie(
363363
config.refresh_csrf_cookie_name,
364364
value=get_csrf_token(encoded_refresh_token),
@@ -408,7 +408,7 @@ def unset_access_cookies(response: Response, domain: Optional[str] = None) -> No
408408
samesite=config.cookie_samesite,
409409
)
410410

411-
if config.csrf_protect and config.csrf_in_cookies:
411+
if config.cookie_csrf_protect and config.csrf_in_cookies:
412412
response.set_cookie(
413413
config.access_csrf_cookie_name,
414414
value="",
@@ -446,7 +446,7 @@ def unset_refresh_cookies(response: Response, domain: Optional[str] = None) -> N
446446
samesite=config.cookie_samesite,
447447
)
448448

449-
if config.csrf_protect and config.csrf_in_cookies:
449+
if config.cookie_csrf_protect and config.csrf_in_cookies:
450450
response.set_cookie(
451451
config.refresh_csrf_cookie_name,
452452
value="",

flask_jwt_extended/view_decorators.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def _decode_jwt_from_cookies(refresh: bool) -> Tuple[str, Optional[str]]:
244244
if not encoded_token:
245245
raise NoAuthorizationError('Missing cookie "{}"'.format(cookie_key))
246246

247-
if config.csrf_protect and request.method in config.csrf_request_methods:
247+
if config.cookie_csrf_protect and request.method in config.csrf_request_methods:
248248
csrf_value = request.headers.get(csrf_header_key, None)
249249
if not csrf_value and config.csrf_check_form:
250250
csrf_value = request.form.get(csrf_field_key, None)

tests/test_config.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def test_default_configs(app):
5151
assert config.json_key == "access_token"
5252
assert config.refresh_json_key == "refresh_token"
5353

54-
assert config.csrf_protect is False
54+
assert config.cookie_csrf_protect is True
5555
assert config.csrf_request_methods == ["POST", "PUT", "PATCH", "DELETE"]
5656
assert config.csrf_in_cookies is True
5757
assert config.access_csrf_cookie_name == "csrf_access_token"
@@ -142,7 +142,7 @@ def test_override_configs(app, delta_func):
142142
assert config.session_cookie is False
143143
assert config.cookie_samesite == "Strict"
144144

145-
assert config.csrf_protect is True
145+
assert config.cookie_csrf_protect is True
146146
assert config.csrf_request_methods == ["GET"]
147147
assert config.csrf_in_cookies is False
148148
assert config.access_csrf_cookie_name == "access_csrf_cookie"
@@ -333,17 +333,11 @@ def test_jwt_token_locations_config(app):
333333

334334
def test_csrf_protect_config(app):
335335
with app.test_request_context():
336-
app.config["JWT_TOKEN_LOCATION"] = ["headers"]
337336
app.config["JWT_COOKIE_CSRF_PROTECT"] = True
338-
assert config.csrf_protect is False
337+
assert config.cookie_csrf_protect is True
339338

340-
app.config["JWT_TOKEN_LOCATION"] = ["cookies"]
341-
app.config["JWT_COOKIE_CSRF_PROTECT"] = True
342-
assert config.csrf_protect is True
343-
344-
app.config["JWT_TOKEN_LOCATION"] = ["cookies"]
345339
app.config["JWT_COOKIE_CSRF_PROTECT"] = False
346-
assert config.csrf_protect is False
340+
assert config.cookie_csrf_protect is False
347341

348342

349343
def test_missing_algorithm_in_decode_algorithms(app):

0 commit comments

Comments
 (0)