From 1a02ebb89f6765d827f2fe0086dfe5960eb6e093 Mon Sep 17 00:00:00 2001 From: Ha Anh Vu <75315486+haanhvu@users.noreply.github.com> Date: Mon, 5 Feb 2024 17:11:03 +0700 Subject: [PATCH] Fix `Couldn't parse: falling back to Raw` for empty JSON array (#6619) #### Description fix #6603 #### Checklist - [x] I have updated tests where applicable. - [ ] I have added an entry to the CHANGELOG. --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- mitmproxy/contentviews/graphql.py | 7 ++++++- test/mitmproxy/contentviews/test_graphql.py | 1 + test/mitmproxy/contentviews/test_json.py | 17 +++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/mitmproxy/contentviews/graphql.py b/mitmproxy/contentviews/graphql.py index 5263b75f5..b76b7bf05 100644 --- a/mitmproxy/contentviews/graphql.py +++ b/mitmproxy/contentviews/graphql.py @@ -30,7 +30,12 @@ def is_graphql_query(data): def is_graphql_batch_query(data): - return isinstance(data, list) and isinstance(data[0], dict) and "query" in data[0] + return ( + isinstance(data, list) + and len(data) > 0 + and isinstance(data[0], dict) + and "query" in data[0] + ) class ViewGraphQL(base.View): diff --git a/test/mitmproxy/contentviews/test_graphql.py b/test/mitmproxy/contentviews/test_graphql.py index 89beda814..f10af244f 100644 --- a/test/mitmproxy/contentviews/test_graphql.py +++ b/test/mitmproxy/contentviews/test_graphql.py @@ -19,6 +19,7 @@ def test_render_priority(): assert 0 == v.render_priority( b"""[{"xquery": "query P { \\n }"}]""", content_type="application/json" ) + assert 0 == v.render_priority(b"""[]""", content_type="application/json") assert 0 == v.render_priority(b"}", content_type="application/json") diff --git a/test/mitmproxy/contentviews/test_json.py b/test/mitmproxy/contentviews/test_json.py index 9711a6465..413f19fd5 100644 --- a/test/mitmproxy/contentviews/test_json.py +++ b/test/mitmproxy/contentviews/test_json.py @@ -78,6 +78,22 @@ def test_format_json(): [("text", " "), ("text", "]"), ("text", "")], [("text", ""), ("text", "}")], ] + assert list(json.format_json({"list": []})) == [ + [("text", "{"), ("text", "")], + [ + ("text", " "), + ("Token_Name_Tag", '"list"'), + ("text", ": "), + ("text", "[]"), + ("text", ""), + ], + [("text", ""), ("text", "}")], + ] + assert list(json.format_json(None)) == [[("Token_Keyword_Constant", "null")]] + assert list(json.format_json(True)) == [[("Token_Keyword_Constant", "true")]] + assert list(json.format_json(1)) == [[("Token_Literal_Number", "1")]] + assert list(json.format_json("test")) == [[("Token_Literal_String", '"test"')]] + assert list(json.format_json([])) == [[("text", "[]")]] def test_view_json(): @@ -88,6 +104,7 @@ def test_view_json(): assert v(b"[1, 2, 3, 4, 5]") assert v(b'{"foo" : 3}') assert v(b'{"foo": true, "nullvalue": null}') + assert v(b"[]") @given(binary())