aboutsummaryrefslogtreecommitdiffstats
path: root/mitmproxy/stateobject.py
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@corte.si>2018-06-17 10:22:17 +1200
committerAldo Cortesi <aldo@corte.si>2018-06-17 10:22:17 +1200
commit77b49aa8de7ce6695b31b7d6dd4e0b62e766a504 (patch)
treeeb9c5ca6cad5d1e4f80d03c21f0781a9f8f90b2e /mitmproxy/stateobject.py
parent3227f67d883de2b77d83e77d7bc25afb04bc375f (diff)
downloadmitmproxy-77b49aa8de7ce6695b31b7d6dd4e0b62e766a504.tar.gz
mitmproxy-77b49aa8de7ce6695b31b7d6dd4e0b62e766a504.tar.bz2
mitmproxy-77b49aa8de7ce6695b31b7d6dd4e0b62e766a504.zip
Add typechecking of Any values for state object
An ugly solution for an ugly little problem. This patch uses JSON's type checker to validate Any values in stateobject, in order to avoid a circular import. Fixes #3180
Diffstat (limited to 'mitmproxy/stateobject.py')
-rw-r--r--mitmproxy/stateobject.py11
1 files changed, 9 insertions, 2 deletions
diff --git a/mitmproxy/stateobject.py b/mitmproxy/stateobject.py
index 26468ccc..2c16dcda 100644
--- a/mitmproxy/stateobject.py
+++ b/mitmproxy/stateobject.py
@@ -1,6 +1,7 @@
import typing
from typing import Any # noqa
from typing import MutableMapping # noqa
+import json
from mitmproxy.coretypes import serializable
from mitmproxy.utils import typecheck
@@ -77,8 +78,14 @@ def _process(typeinfo: typecheck.Type, val: typing.Any, make: bool) -> typing.An
for k, v in val.items()
}
elif typename.startswith("typing.Any"):
- # FIXME: Remove this when we remove flow.metadata
- assert isinstance(val, (int, str, bool, bytes))
+ # This requires a bit of explanation. We can't import our IO layer here,
+ # because it causes a circular import. Rather than restructuring the
+ # code for this, we use JSON serialization, which has similar primitive
+ # type restrictions as tnetstring, to check for conformance.
+ try:
+ json.dumps(val)
+ except TypeError:
+ raise ValueError(f"Data not serializable: {val}")
return val
else:
return typeinfo(val)