From 296034c32311dd55446deae793512c7dadd4ffb5 Mon Sep 17 00:00:00 2001 From: nadiraikido <166383531+nadiraikido@users.noreply.github.com> Date: Fri, 4 Apr 2025 17:54:51 +0200 Subject: [PATCH 1/3] Handle JSON body starting with double quote --- aikido_zen/context/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aikido_zen/context/__init__.py b/aikido_zen/context/__init__.py index 296bc68e5..8234f3e8c 100644 --- a/aikido_zen/context/__init__.py +++ b/aikido_zen/context/__init__.py @@ -113,7 +113,7 @@ def set_body_internal(self, body): self.body = self.body.decode("utf-8") # Decode byte input to string. if not isinstance(self.body, str): return - if self.body.strip()[0] in ["{", "["]: + if self.body.strip()[0] in ["{", "[", "\""]: # Might be JSON, but might not have been parsed correctly by server because of wrong headers parsed_body = json.loads(self.body) if parsed_body: From fe5602fd37afb642fed419ce0939db9613989477 Mon Sep 17 00:00:00 2001 From: nadiraikido <166383531+nadiraikido@users.noreply.github.com> Date: Mon, 7 Apr 2025 18:49:27 +0200 Subject: [PATCH 2/3] Add tests --- aikido_zen/context/init_test.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/aikido_zen/context/init_test.py b/aikido_zen/context/init_test.py index d227d1d2d..bd1989d0b 100644 --- a/aikido_zen/context/init_test.py +++ b/aikido_zen/context/init_test.py @@ -19,7 +19,6 @@ "HTTP_USER_AGENT": "Mozilla/5.0", } - @pytest.fixture(autouse=True) def run_around_tests(): yield @@ -27,7 +26,6 @@ def run_around_tests(): # interfere with other tests current_context.set(None) - def test_get_current_context_no_context(): # Test get_current_context() when no context is set assert get_current_context() is None @@ -208,13 +206,24 @@ def test_set_valid_json_with_spaces(): assert context.body == {"key": [1, 2, 3]} +def test_valid_json_string_with_newlines(): + context = Context(req=basic_wsgi_req, body=None, source="flask") + + context.set_body('\r\n\r\n"hello"\r\n\r\n') + assert context.body == "hello" + +def test_valid_json_string_with_spaces(): + context = Context(req=basic_wsgi_req, body=None, source="flask") + + context.set_body('" hello "') + assert context.body == " hello " + def test_set_valid_json_with_newlines(): context = Context(req=basic_wsgi_req, body=None, source="flask") context.set_body('\r\n\r\n{"key": [1, 2, 3]}\r\n\r\n') assert context.body == {"key": [1, 2, 3]} - def test_set_valid_json_with_spaces_and_array(): context = Context(req=basic_wsgi_req, body=None, source="flask") From 3a84925614e798d1704278065da3f4d7417a3c19 Mon Sep 17 00:00:00 2001 From: nadiraikido <166383531+nadiraikido@users.noreply.github.com> Date: Mon, 7 Apr 2025 18:51:30 +0200 Subject: [PATCH 3/3] Linting --- aikido_zen/context/__init__.py | 2 +- aikido_zen/context/init_test.py | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/aikido_zen/context/__init__.py b/aikido_zen/context/__init__.py index 8234f3e8c..e6f51f10a 100644 --- a/aikido_zen/context/__init__.py +++ b/aikido_zen/context/__init__.py @@ -113,7 +113,7 @@ def set_body_internal(self, body): self.body = self.body.decode("utf-8") # Decode byte input to string. if not isinstance(self.body, str): return - if self.body.strip()[0] in ["{", "[", "\""]: + if self.body.strip()[0] in ["{", "[", '"']: # Might be JSON, but might not have been parsed correctly by server because of wrong headers parsed_body = json.loads(self.body) if parsed_body: diff --git a/aikido_zen/context/init_test.py b/aikido_zen/context/init_test.py index bd1989d0b..4769167ba 100644 --- a/aikido_zen/context/init_test.py +++ b/aikido_zen/context/init_test.py @@ -19,6 +19,7 @@ "HTTP_USER_AGENT": "Mozilla/5.0", } + @pytest.fixture(autouse=True) def run_around_tests(): yield @@ -26,6 +27,7 @@ def run_around_tests(): # interfere with other tests current_context.set(None) + def test_get_current_context_no_context(): # Test get_current_context() when no context is set assert get_current_context() is None @@ -212,18 +214,21 @@ def test_valid_json_string_with_newlines(): context.set_body('\r\n\r\n"hello"\r\n\r\n') assert context.body == "hello" + def test_valid_json_string_with_spaces(): context = Context(req=basic_wsgi_req, body=None, source="flask") context.set_body('" hello "') assert context.body == " hello " - + + def test_set_valid_json_with_newlines(): context = Context(req=basic_wsgi_req, body=None, source="flask") context.set_body('\r\n\r\n{"key": [1, 2, 3]}\r\n\r\n') assert context.body == {"key": [1, 2, 3]} + def test_set_valid_json_with_spaces_and_array(): context = Context(req=basic_wsgi_req, body=None, source="flask")