Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion httpie/cli/nested_json/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def can_advance() -> bool:
if index in OPERATORS:
yield from send_buffer()
yield Token(OPERATORS[index], index, cursor, cursor + 1)
elif index == BACKSLASH and can_advance():
elif index == BACKSLASH and cursor + 1 < len(source):
if source[cursor + 1] in SPECIAL_CHARS:
backslashes += 1
else:
Expand Down
19 changes: 19 additions & 0 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,22 @@ def test_nested_json_errors(input_json, expected_error, httpbin):
def test_nested_json_sparse_array(httpbin_both):
r = http(httpbin_both + '/post', 'test[0]:=1', 'test[100]:=1')
assert len(r.json['json']['test']) == 101


def test_nested_json_trailing_backslash_no_crash():
"""A backslash at the very end of a key must not crash with IndexError.

Regression test for: tokenize() accessed source[cursor + 1] without
first verifying cursor + 1 < len(source).
"""
from httpie.cli.nested_json.parse import tokenize, parse

trailing_bs = 'key' + chr(92) # 'key\'

# tokenize must not raise IndexError
tokens = list(tokenize(trailing_bs))
assert tokens # produced at least one token

# parse must also be safe
paths = list(parse(trailing_bs))
assert paths
Loading