diff --git a/mimic/sources/har.py b/mimic/sources/har.py index 7c446c2..e83c272 100644 --- a/mimic/sources/har.py +++ b/mimic/sources/har.py @@ -6,6 +6,7 @@ flow dicts so the rest of the pipeline (extract, hosts, endpoints, codegen) works unchanged, with no mitmproxy or iPhone setup at all. """ import base64 +import binascii import json from urllib.parse import urlencode, urlparse @@ -108,6 +109,6 @@ def _content_bytes(content): if content.get("encoding") == "base64": try: return base64.b64decode(text) - except ValueError: + except (binascii.Error, ValueError): return text.encode("utf-8") return text.encode("utf-8") diff --git a/tests/test_har.py b/tests/test_har.py index db9a025..c5cb2c4 100644 --- a/tests/test_har.py +++ b/tests/test_har.py @@ -80,3 +80,8 @@ def test_base64_response_body(): assert '"ok"' in msg["response_body"] # base64 "eyJvayI6IHRydWV9" -> {"ok": true} assert "true" in msg["response_body"] assert '"to"' in msg["request_body"] # postData decoded too + + +def test_invalid_base64_response_body_falls_back_to_text(): + content = {"text": "not valid base64!", "encoding": "base64"} + assert har._content_bytes(content) == b"not valid base64!"