diff --git a/CHANGELOG.md b/CHANGELOG.md index 0497ac3508..fadc1f0c40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ This document records all notable changes to [HTTPie](https://httpie.io). This project adheres to [Semantic Versioning](https://semver.org/). +## [unreleased](https://github.com/httpie/cli/compare/3.2.4...master) + +- Fixed an unhandled `OverflowError`/`MemoryError` crash when an out-of-range array index was used in the nested JSON syntax (e.g. `item[99999999999999999999999]:=1`); a clear error is now reported instead. + ## [3.2.4](https://github.com/httpie/cli/compare/3.2.3...3.2.4) (2024-11-01) - Fix default certs loading and unpin `requests`. ([#1596](https://github.com/httpie/cli/issues/1596)) diff --git a/httpie/cli/nested_json/interpret.py b/httpie/cli/nested_json/interpret.py index 71fad98acd..659102af3e 100644 --- a/httpie/cli/nested_json/interpret.py +++ b/httpie/cli/nested_json/interpret.py @@ -84,7 +84,15 @@ def object_for(kind: PathAction) -> Any: message='Negative indexes are not supported.', message_kind='Value', ) - cursor.extend([None] * (path.accessor - len(cursor) + 1)) + try: + cursor.extend([None] * (path.accessor - len(cursor) + 1)) + except (OverflowError, MemoryError): + raise NestedJSONSyntaxError( + source=key, + token=path.tokens[1], + message='The index is too large.', + message_kind='Value', + ) from None if next_path.kind is PathAction.SET: cursor[path.accessor] = next_path.accessor break diff --git a/tests/test_json.py b/tests/test_json.py index e758ebe7f4..47e84871c1 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -538,6 +538,18 @@ def test_nested_json_syntax(input_json, expected_json, httpbin): ['foo[-10]:=[1,2]'], 'HTTPie Value Error: Negative indexes are not supported.\nfoo[-10]\n ^^^', ), + ( + ['foo[99999999999999999999999]:=1'], + 'HTTPie Value Error: The index is too large.' + '\nfoo[99999999999999999999999]' + '\n ^^^^^^^^^^^^^^^^^^^^^^^', + ), + ( + ['foo[1000000000000000000]:=1'], + 'HTTPie Value Error: The index is too large.' + '\nfoo[1000000000000000000]' + '\n ^^^^^^^^^^^^^^^^^^^', + ), ( ['foo[0]:=1', 'foo[]:=2', 'foo[\\2]:=3'], "HTTPie Type Error: Cannot perform 'key' based access on 'foo' which has a type of 'array' but this operation requires a type of 'object'.\nfoo[\\2]\n ^^^^",