From a7f27259a7316e9080ebd0cd98868f80eabde9fe Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Thu, 12 Aug 2021 10:21:06 +0200 Subject: [PATCH] contentviews: ignore empty data --- mitmproxy/contentviews/css.py | 2 +- mitmproxy/contentviews/graphql.py | 2 +- mitmproxy/contentviews/javascript.py | 2 +- mitmproxy/contentviews/json.py | 2 ++ mitmproxy/contentviews/msgpack.py | 2 +- mitmproxy/contentviews/multipart.py | 2 +- mitmproxy/contentviews/protobuf.py | 2 +- mitmproxy/contentviews/urlencoded.py | 2 +- mitmproxy/contentviews/wbxml.py | 2 +- mitmproxy/contentviews/xml_html.py | 4 +++- 10 files changed, 13 insertions(+), 9 deletions(-) diff --git a/mitmproxy/contentviews/css.py b/mitmproxy/contentviews/css.py index cc162eb4a..92a855a8d 100644 --- a/mitmproxy/contentviews/css.py +++ b/mitmproxy/contentviews/css.py @@ -58,7 +58,7 @@ class ViewCSS(base.View): return "CSS", base.format_text(beautified) def render_priority(self, data: bytes, *, content_type: Optional[str] = None, **metadata) -> float: - return float(content_type == "text/css") + return float(bool(data) and content_type == "text/css") if __name__ == "__main__": # pragma: no cover diff --git a/mitmproxy/contentviews/graphql.py b/mitmproxy/contentviews/graphql.py index 26407ed98..cbf56e3d8 100644 --- a/mitmproxy/contentviews/graphql.py +++ b/mitmproxy/contentviews/graphql.py @@ -45,7 +45,7 @@ class ViewGraphQL(base.View): return "GraphQL", base.format_text(format_query_list(data)) def render_priority(self, data: bytes, *, content_type: typing.Optional[str] = None, **metadata) -> float: - if content_type != "application/json": + if content_type != "application/json" or not data: return 0 data = parse_json(data) diff --git a/mitmproxy/contentviews/javascript.py b/mitmproxy/contentviews/javascript.py index bad7fc2a8..875e2faa8 100644 --- a/mitmproxy/contentviews/javascript.py +++ b/mitmproxy/contentviews/javascript.py @@ -59,4 +59,4 @@ class ViewJavaScript(base.View): return "JavaScript", base.format_text(res) def render_priority(self, data: bytes, *, content_type: Optional[str] = None, **metadata) -> float: - return float(content_type in self.__content_types) + return float(bool(data) and content_type in self.__content_types) diff --git a/mitmproxy/contentviews/json.py b/mitmproxy/contentviews/json.py index 04d911d39..9425629aa 100644 --- a/mitmproxy/contentviews/json.py +++ b/mitmproxy/contentviews/json.py @@ -47,6 +47,8 @@ class ViewJSON(base.View): return "JSON", format_json(data) def render_priority(self, data: bytes, *, content_type: typing.Optional[str] = None, **metadata) -> float: + if not data: + return 0 if content_type in ( "application/json", "application/json-rpc", diff --git a/mitmproxy/contentviews/msgpack.py b/mitmproxy/contentviews/msgpack.py index 975440f41..ab8ac3ab6 100644 --- a/mitmproxy/contentviews/msgpack.py +++ b/mitmproxy/contentviews/msgpack.py @@ -50,4 +50,4 @@ class ViewMsgPack(base.View): return "MsgPack", format_msgpack(data) def render_priority(self, data: bytes, *, content_type: typing.Optional[str] = None, **metadata) -> float: - return float(content_type in self.__content_types) + return float(bool(data) and content_type in self.__content_types) diff --git a/mitmproxy/contentviews/multipart.py b/mitmproxy/contentviews/multipart.py index 7f2994498..4b103f31f 100644 --- a/mitmproxy/contentviews/multipart.py +++ b/mitmproxy/contentviews/multipart.py @@ -21,4 +21,4 @@ class ViewMultipart(base.View): return "Multipart form", self._format(v) def render_priority(self, data: bytes, *, content_type: Optional[str] = None, **metadata) -> float: - return float(content_type == "multipart/form-data") + return float(bool(data) and content_type == "multipart/form-data") diff --git a/mitmproxy/contentviews/protobuf.py b/mitmproxy/contentviews/protobuf.py index c470d077c..a47c7a90a 100644 --- a/mitmproxy/contentviews/protobuf.py +++ b/mitmproxy/contentviews/protobuf.py @@ -80,4 +80,4 @@ class ViewProtobuf(base.View): return "Protobuf", base.format_text(decoded) def render_priority(self, data: bytes, *, content_type: Optional[str] = None, **metadata) -> float: - return float(content_type in self.__content_types) + return float(bool(data) and content_type in self.__content_types) diff --git a/mitmproxy/contentviews/urlencoded.py b/mitmproxy/contentviews/urlencoded.py index 22b1ba21b..b1d367924 100644 --- a/mitmproxy/contentviews/urlencoded.py +++ b/mitmproxy/contentviews/urlencoded.py @@ -16,4 +16,4 @@ class ViewURLEncoded(base.View): return "URLEncoded form", base.format_pairs(d) def render_priority(self, data: bytes, *, content_type: Optional[str] = None, **metadata) -> float: - return float(content_type == "application/x-www-form-urlencoded") + return float(bool(data) and content_type == "application/x-www-form-urlencoded") diff --git a/mitmproxy/contentviews/wbxml.py b/mitmproxy/contentviews/wbxml.py index c13124f33..836fec787 100644 --- a/mitmproxy/contentviews/wbxml.py +++ b/mitmproxy/contentviews/wbxml.py @@ -21,4 +21,4 @@ class ViewWBXML(base.View): return None def render_priority(self, data: bytes, *, content_type: Optional[str] = None, **metadata) -> float: - return float(content_type in self.__content_types) + return float(bool(data) and content_type in self.__content_types) diff --git a/mitmproxy/contentviews/xml_html.py b/mitmproxy/contentviews/xml_html.py index 509cb55f2..90eb566a3 100644 --- a/mitmproxy/contentviews/xml_html.py +++ b/mitmproxy/contentviews/xml_html.py @@ -235,8 +235,10 @@ class ViewXmlHtml(base.View): return t, pretty def render_priority(self, data: bytes, *, content_type: Optional[str] = None, **metadata) -> float: + if not data: + return 0 if content_type in self.__content_types: return 1 elif strutils.is_xml(data): return 0.4 - return float(content_type in self.__content_types) + return 0