Make mitmdump handle invalid serialized data gracefully.

This commit is contained in:
Aldo Cortesi 2011-03-11 15:16:31 +13:00
parent 7d85db0da3
commit 9f16a84a9e
3 changed files with 21 additions and 5 deletions

View File

@ -93,7 +93,7 @@ class DumpMaster(flow.FlowMaster):
try: try:
f = file(path, "r") f = file(path, "r")
flows = list(flow.FlowReader(f).stream()) flows = list(flow.FlowReader(f).stream())
except IOError, v: except (IOError, flow.FlowReadError), v:
raise DumpError(v.strerror) raise DumpError(v.strerror)
return flows return flows
@ -182,7 +182,6 @@ class DumpMaster(flow.FlowMaster):
return f return f
# begin nocover # begin nocover
def run(self): def run(self):
try: try:

View File

@ -560,6 +560,10 @@ class FlowWriter:
s = json.dumps(d) s = json.dumps(d)
self.ns.write(s) self.ns.write(s)
class FlowReadError(Exception):
@property
def strerror(self):
return self.args[0]
class FlowReader: class FlowReader:
def __init__(self, fo): def __init__(self, fo):
@ -570,7 +574,10 @@ class FlowReader:
""" """
Yields Flow objects from the dump. Yields Flow objects from the dump.
""" """
for i in self.ns: try:
data = json.loads(i) for i in self.ns:
yield Flow.from_state(data) data = json.loads(i)
yield Flow.from_state(data)
except netstring.DecoderError:
raise FlowReadError("Invalid data format.")

View File

@ -356,6 +356,16 @@ class uSerialize(libpry.AutoTree):
assert len(l) == 1 assert len(l) == 1
assert l[0] == f assert l[0] == f
def test_error(self):
sio = StringIO()
sio.write("bogus")
sio.seek(0)
r = flow.FlowReader(sio)
libpry.raises(flow.FlowReadError, list, r.stream())
f = flow.FlowReadError("foo")
assert f.strerror == "foo"
class uFlowMaster(libpry.AutoTree): class uFlowMaster(libpry.AutoTree):
def test_all(self): def test_all(self):