From 15f921545c8bb3a05f8f7b05827fea2a35af101d Mon Sep 17 00:00:00 2001 From: Sasha Mitchell Date: Thu, 1 Oct 2026 19:35:27 +0700 Subject: [PATCH] Only strip a JSON prefix that starts at the front. findall() treated a match inside the body as the prefix and cut that many characters off the front. --- httpie/output/utils.py | 6 ++++-- tests/test_json.py | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/httpie/output/utils.py b/httpie/output/utils.py index 875e885586..da9e129d6e 100644 --- a/httpie/output/utils.py +++ b/httpie/output/utils.py @@ -31,7 +31,9 @@ def parse_prefixed_json(data: str) -> Tuple[str, str]: Return a tuple (data prefix, actual JSON body). """ - matches = re.findall(PREFIX_REGEX, data) - data_prefix = matches[0] if matches else '' + # Only a leading run is a prefix. findall() would take a match + # from inside the body and cut that many characters off the front. + match = re.match(PREFIX_REGEX, data) + data_prefix = match.group(0) if match else '' body = data[len(data_prefix):] return data_prefix, body diff --git a/tests/test_json.py b/tests/test_json.py index e758ebe7f4..af65a03a9a 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -7,6 +7,7 @@ from httpie.cli.exceptions import ParseError from httpie.cli.nested_json import NestedJSONSyntaxError from httpie.output.formatters.colors import ColorFormatter +from httpie.output.utils import load_prefixed_json from httpie.utils import JsonDictPreservingDuplicateKeys from .fixtures import ( @@ -55,6 +56,14 @@ }""" +def test_prefixed_json_does_not_chop_inside_the_body(): + with pytest.raises(ValueError): + load_prefixed_json('{{"a": 1}') + prefix, obj = load_prefixed_json('while(1);{"a": 1}') + assert prefix == 'while(1);' + assert list(obj.items()) == [('a', 1)] + + @pytest.mark.parametrize('data_prefix', TEST_JSON_XXSI_PREFIXES) @pytest.mark.parametrize('json_data', TEST_JSON_VALUES) @pytest.mark.parametrize('pretty', PRETTY_MAP.keys())