Skip to content

Fix #1965: A JSON body that starts with a brace can be parsed from the middle - #1997

Draft
vinayK34 wants to merge 3 commits into
httpie:masterfrom
vinayK34:fix/1965-a-json-body-that-starts
Draft

vinayK34 wants to merge 3 commits into
httpie:masterfrom
vinayK34:fix/1965-a-json-body-that-starts

Conversation

@vinayK34

@vinayK34 vinayK34 commented Nov 3, 2026

Copy link
Copy Markdown

Fixes #1965.

Root Cause

The issue was in the parse_prefixed_json function in httpie/output/utils.py. The function was using re.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}':

  1. PREFIX_REGEX is [^{\\[\"]+ which matches sequences of characters that are NOT {, [, or \"
  2. re.findall finds ALL matches including 'a' at position 3 (not position 0)
  3. The code took the first match 'a' and treated it as a prefix
  4. This resulted in returning prefix 'a' and body '{\"a\": 1}' instead of recognizing that there's no valid prefix

Fix Applied

Changed the parse_prefixed_json function to only consider matches that start at position 0:

Before:

matches = re.findall(PREFIX_REGEX, data)
data_prefix = matches[0] if matches else ''

After:

# Find matches that start at the beginning of the string
matches = []
for match in re.finditer(PREFIX_REGEX, data):
    if match.start() == 0:
        matches.append(match.group())
    else:
        break

data_prefix = matches[0] if matches else ''

Result

  • Input '{{\"a\": 1}' now correctly returns prefix '' and body '{{\"a\": 1}'
  • Valid prefixes like 'while(1);' in 'while(1);{\"a\": 1}' still work correctly
  • Normal JSON input like '{\"a\": 1}' continues to work as expected

Tests

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 JSON

Test 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)]))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A JSON body that starts with a brace can be parsed from the middle

1 participant