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

View File

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