From 8ec7fea0a40bedb4ca78178c1ef2c16234d171de Mon Sep 17 00:00:00 2001 From: Camille Barneaud <1693643+gadcam@users.noreply.github.com> Date: Fri, 30 Jun 2023 19:51:01 +0200 Subject: [PATCH] [HAR] Heuristic - Fix and improve support for Firefox - Add a bigger weight to the HAR extension - Fix the ASCII detection by removing EOL characters - Fix the first character detection that was not working because would compare with an int - Add a pattern to detect Firefox export --- mitmproxy2swagger/har_capture_reader.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/mitmproxy2swagger/har_capture_reader.py b/mitmproxy2swagger/har_capture_reader.py index 9255a88..302e0db 100644 --- a/mitmproxy2swagger/har_capture_reader.py +++ b/mitmproxy2swagger/har_capture_reader.py @@ -11,23 +11,21 @@ def har_archive_heuristic(file_path: str) -> int: val = 0 # if has the har extension if file_path.endswith(".har"): - val += 15 + val += 25 # read the first 2048 bytes with open(file_path, "rb") as f: data = f.read(2048) - # if file contains only ascii characters - if data.decode("utf-8", "ignore").isprintable() is True: + # if file contains only ascii characters after remove EOL characters + if data.decode("utf-8", "ignore").replace("\r", "").replace("\n", "").isprintable() is True: val += 25 - # if first character is a '{' - if data[0] == "{": + # sign of a JSON file + if data[0:1] == b'{': val += 23 - # if it contains the word '"WebInspector"' - if b'"WebInspector"' in data: + # sign of Chrome OR Firefox export + if b'"WebInspector"' in data or b'"Firefox"' in data: val += 15 - # if it contains the word '"entries"' if b'"entries"' in data: val += 15 - # if it contains the word '"version"' if b'"version"' in data: val += 15 return val