From 588e78075da8dec075c8c4f05ad6e0976f1678cc Mon Sep 17 00:00:00 2001 From: mundur Date: Fri, 16 Oct 2026 21:17:34 +0800 Subject: [PATCH] Don't modify the configured decode algorithms _Config.decode_algorithms appended this application's encode algorithm to the list object stored in app.config["JWT_DECODE_ALGORITHMS"] instead of to a copy of it. That list is held by reference, and app.config.from_object() copies attributes by reference, so applications built from a common config object share a single list. Appending therefore added the algorithm of whichever application touched the property first to the algorithms every other application accepts. For example an application configured with JWT_DECODE_ALGORITHMS = ["RS256"] and JWT_ALGORITHM = "RS256" accepts HS256 tokens once an application sharing that config object encodes with HS256. The widening is permanent: once the algorithm is in the list the "not in" test is false from then on, so resetting JWT_DECODE_ALGORITHMS no longer restores the restriction. Copy the configured list before appending. The behaviour that test_missing_algorithm_in_decode_algorithms pins is unchanged: the encode algorithm is still returned by the property, it is just no longer written back into the configuration. --- flask_jwt_extended/config.py | 9 ++++++--- tests/test_config.py | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/flask_jwt_extended/config.py b/flask_jwt_extended/config.py index 7d25651..05cc98c 100644 --- a/flask_jwt_extended/config.py +++ b/flask_jwt_extended/config.py @@ -222,9 +222,12 @@ def algorithm(self) -> str: @property def decode_algorithms(self) -> List[str]: - algorithms = current_app.config["JWT_DECODE_ALGORITHMS"] - if not algorithms: - return [self.algorithm] + # Copy before appending. The configured list can be shared by + # reference between applications, for example when they are + # built from the same object with ``from_object``, so mutating it + # would add this application's algorithm to the algorithms every + # other application accepts. + algorithms = list(current_app.config["JWT_DECODE_ALGORITHMS"] or []) if self.algorithm not in algorithms: algorithms.append(self.algorithm) return algorithms diff --git a/tests/test_config.py b/tests/test_config.py index 1d01e30..66190e4 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -347,3 +347,17 @@ def test_missing_algorithm_in_decode_algorithms(app): with app.test_request_context(): assert config.decode_algorithms == ["HS512", "RS256"] + + +def test_decode_algorithms_does_not_modify_config(app): + app.config["JWT_ALGORITHM"] = "RS256" + app.config["JWT_DECODE_ALGORITHMS"] = ["HS512"] + + with app.test_request_context(): + assert config.decode_algorithms == ["HS512", "RS256"] + + # The configured list must be left alone. Applications built from + # the same object share it by reference, so modifying it here would + # silently widen the algorithms the other applications accept. + assert app.config["JWT_DECODE_ALGORITHMS"] == ["HS512"] + assert config.decode_algorithms == ["HS512", "RS256"]