Repository navigation
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1965.
Root Cause
The issue was in the
parse_prefixed_jsonfunction inhttpie/output/utils.py. The function was usingre.findall(PREFIX_REGEX, data)to find all potential prefixes, then taking the first match without considering whether it started at position 0.Problem Analysis
For the input
'{{\"a\": 1}':PREFIX_REGEXis[^{\\[\"]+which matches sequences of characters that are NOT{,[, or\"re.findallfinds ALL matches including'a'at position 3 (not position 0)'a'and treated it as a prefix'a'and body'{\"a\": 1}'instead of recognizing that there's no valid prefixFix Applied
Changed the
parse_prefixed_jsonfunction to only consider matches that start at position 0:Before:
After:
Result
'{{\"a\": 1}'now correctly returns prefix''and body'{{\"a\": 1}''while(1);'in'while(1);{\"a\": 1}'still work correctly'{\"a\": 1}'continues to work as expectedTests
on fixed/: 3 passed in 0.06s; on orig/: 1 failed, 2 passed in 0.06s
PASS
Test 1: Main issue case
Command:
sh /sandbox/.openclaw/workspace/team/bin/py.sh 1965 orig -c "from httpie.output.utils import load_prefixed_json; print(load_prefixed_json('{{\\\"a\\\": 1}'))"Expected:
('a', JsonDictPreservingDuplicateKeys([('a', 1)]))(buggy behavior)Actual:
('a', JsonDictPreservingDuplicateKeys([('a', 1)]))Command:
sh /sandbox/.openclaw/workspace/team/bin/py.sh 1965 fixed -c "from httpie.output.utils import load_prefixed_json; print(load_prefixed_json('{{\\\"a\\\": 1}'))"Expected:
ValueError: Invalid JSON(correct behavior)Actual:
ValueError: Invalid JSONTest 2: Valid prefix case
Command:
sh /sandbox/.openclaw/workspace/team/bin/py.sh 1965 orig -c "from httpie.output.utils import load_prefixed_json; print(load_prefixed_json('while(1);{\\\"a\\\": 1}'))"Expected:
('while(1);', JsonDictPreservingDuplicateKeys([('a', 1)]))Actual:
('while(1);', JsonDictPreservingDuplicateKeys([('a', 1)]))Command:
sh /sandbox/.openclaw/workspace/team/bin/py.sh 1965 fixed -c "from httpie.output.utils import load_prefixed_json; print(load_prefixed_json('while(1);{\\\"a\\\": 1}'))"Expected:
('while(1);', JsonDictPreservingDuplicateKeys([('a', 1)]))Actual:
('while(1);', JsonDictPreservingDuplicateKeys([('a', 1)]))Test 3: Normal JSON case
Command:
sh /sandbox/.openclaw/workspace/team/bin/py.sh 1965 orig -c "from httpie.output.utils import load_prefixed_json; print(load_prefixed_json('{\\\"a\\\": 1}'))"Expected:
('', JsonDictPreservingDuplicateKeys([('a', 1)]))Actual:
('', JsonDictPreservingDuplicateKeys([('a', 1)]))Command:
sh /sandbox/.openclaw/workspace/team/bin/py.sh 1965 fixed -c "from httpie.output.utils import load_prefixed_json; print(load_prefixed_json('{\\\"a\\\": 1}'))"Expected:
('', JsonDictPreservingDuplicateKeys([('a', 1)]))Actual:
('', JsonDictPreservingDuplicateKeys([('a', 1)]))