Skip to content

Fix IndexError when nested JSON key ends with a trailing backslash#1873

Open
JSap0914 wants to merge 2 commits into
httpie:masterfrom
JSap0914:fix/nested-json-trailing-backslash-crash
Open

Fix IndexError when nested JSON key ends with a trailing backslash#1873
JSap0914 wants to merge 2 commits into
httpie:masterfrom
JSap0914:fix/nested-json-trailing-backslash-crash

Conversation

@JSap0914

Copy link
Copy Markdown

Bug

tokenize() in httpie/cli/nested_json/parse.py guards the backslash branch with can_advance(), which evaluates to cursor < len(source). Because we are already inside the while can_advance(): loop that guard is always True at this point, so when the input string ends with a backslash the code immediately attempts source[cursor + 1] and raises an unhandled exception:

IndexError: string index out of range

Minimal reproducer (no server needed):

from httpie.cli.nested_json.parse import tokenize
list(tokenize('key\\'))  # IndexError

Fix

Replace can_advance() with cursor + 1 < len(source) so the check actually tests for the existence of the next character before dereferencing it. When no next character exists, the backslash falls through to the else branch and is appended to the buffer as a literal character — consistent with the behavior of KeyValueArgType.tokenize() in argtypes.py, which uses next(characters, '') to safely handle the same edge case.

Verification

pytest tests/test_json.py -k 'not httpbin' -v
# 295 passed (includes new regression test)

RED (before fix): tokenize('key\\') raises IndexError: string index out of range
GREEN (after fix): returns [Token(kind=TEXT, value='key\\', ...)] without error

AI-assisted contribution.

tokenize() in httpie/cli/nested_json/parse.py guarded the backslash
branch with `can_advance()`, which tests `cursor < len(source)`.
Because we are already inside the `while can_advance()` loop that
guard is always True, so when the input ends with a backslash the
code immediately tries to read `source[cursor + 1]` and raises

  IndexError: string index out of range

Fix: replace `can_advance()` with `cursor + 1 < len(source)` so
the check actually tests for the existence of the *next* character.
When no next character exists the backslash falls through to the
else-branch and is appended to the buffer as a literal character,
matching the behaviour of KeyValueArgType.tokenize() in argtypes.py.

Adds a regression test that exercises both tokenize() and parse()
with a key ending in a backslash without requiring a live server.
Copilot AI review requested due to automatic review settings June 17, 2026 06:19

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

2 participants