aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Kriechbaumer <thomas@kriechbaumer.name>2017-05-24 14:09:41 +0200
committerThomas Kriechbaumer <thomas@kriechbaumer.name>2017-05-24 14:09:41 +0200
commitae7e9efb5ca014291e879694414bbffc982f2a0d (patch)
tree6ada273eb6d6f0cc9cf04a9d002ee8481d9cce41
parent2faaa0b2a23089b9ffccf2d46dc42328edb76ddd (diff)
downloadmitmproxy-ae7e9efb5ca014291e879694414bbffc982f2a0d.tar.gz
mitmproxy-ae7e9efb5ca014291e879694414bbffc982f2a0d.tar.bz2
mitmproxy-ae7e9efb5ca014291e879694414bbffc982f2a0d.zip
fix various fd/socket leaks
-rw-r--r--mitmproxy/addons/view.py11
-rw-r--r--mitmproxy/net/tcp.py2
-rw-r--r--pathod/language/generators.py4
-rw-r--r--test/mitmproxy/addons/test_clientplayback.py7
-rw-r--r--test/mitmproxy/addons/test_save.py5
-rw-r--r--test/mitmproxy/addons/test_serverplayback.py7
-rw-r--r--test/mitmproxy/addons/test_view.py7
-rw-r--r--test/mitmproxy/contentviews/test_protobuf.py4
-rw-r--r--test/mitmproxy/net/test_tcp.py10
-rw-r--r--test/mitmproxy/platform/test_pf.py5
-rw-r--r--test/pathod/language/test_base.py6
-rw-r--r--test/pathod/language/test_generators.py4
-rw-r--r--test/pathod/protocols/test_http2.py2
13 files changed, 45 insertions, 29 deletions
diff --git a/mitmproxy/addons/view.py b/mitmproxy/addons/view.py
index 13a17c56..aa3e11ed 100644
--- a/mitmproxy/addons/view.py
+++ b/mitmproxy/addons/view.py
@@ -339,11 +339,12 @@ class View(collections.Sequence):
"""
Load flows into the view, without processing them with addons.
"""
- for i in io.FlowReader(open(path, "rb")).stream():
- # Do this to get a new ID, so we can load the same file N times and
- # get new flows each time. It would be more efficient to just have a
- # .newid() method or something.
- self.add([i.copy()])
+ with open(path, "rb") as f:
+ for i in io.FlowReader(f).stream():
+ # Do this to get a new ID, so we can load the same file N times and
+ # get new flows each time. It would be more efficient to just have a
+ # .newid() method or something.
+ self.add([i.copy()])
@command.command("view.go")
def go(self, dst: int) -> None:
diff --git a/mitmproxy/net/tcp.py b/mitmproxy/net/tcp.py
index f17943c1..cdac4cd5 100644
--- a/mitmproxy/net/tcp.py
+++ b/mitmproxy/net/tcp.py
@@ -676,6 +676,8 @@ class TCPClient(_Connection):
sock.setsockopt(socket.SOL_IP, socket.IP_TRANSPARENT, 1) # pragma: windows no cover pragma: osx no cover
except Exception as e:
# socket.IP_TRANSPARENT might not be available on every OS and Python version
+ if sock is not None:
+ sock.close()
raise exceptions.TcpException(
"Failed to spoof the source address: " + str(e)
)
diff --git a/pathod/language/generators.py b/pathod/language/generators.py
index d716804d..93db3014 100644
--- a/pathod/language/generators.py
+++ b/pathod/language/generators.py
@@ -91,3 +91,7 @@ class FileGenerator:
def __repr__(self):
return "<%s" % self.path
+
+ def close(self):
+ self.map.close()
+ self.fp.close()
diff --git a/test/mitmproxy/addons/test_clientplayback.py b/test/mitmproxy/addons/test_clientplayback.py
index 7ffda317..6089b2d5 100644
--- a/test/mitmproxy/addons/test_clientplayback.py
+++ b/test/mitmproxy/addons/test_clientplayback.py
@@ -10,9 +10,10 @@ from mitmproxy.test import taddons
def tdump(path, flows):
- w = io.FlowWriter(open(path, "wb"))
- for i in flows:
- w.add(i)
+ with open(path, "wb") as f:
+ w = io.FlowWriter(f)
+ for i in flows:
+ w.add(i)
class MockThread():
diff --git a/test/mitmproxy/addons/test_save.py b/test/mitmproxy/addons/test_save.py
index 85c2a398..a4e425cd 100644
--- a/test/mitmproxy/addons/test_save.py
+++ b/test/mitmproxy/addons/test_save.py
@@ -26,8 +26,9 @@ def test_configure(tmpdir):
def rd(p):
- x = io.FlowReader(open(p, "rb"))
- return list(x.stream())
+ with open(p, "rb") as f:
+ x = io.FlowReader(f)
+ return list(x.stream())
def test_tcp(tmpdir):
diff --git a/test/mitmproxy/addons/test_serverplayback.py b/test/mitmproxy/addons/test_serverplayback.py
index 3ceab3fa..7605a5d9 100644
--- a/test/mitmproxy/addons/test_serverplayback.py
+++ b/test/mitmproxy/addons/test_serverplayback.py
@@ -11,9 +11,10 @@ from mitmproxy import io
def tdump(path, flows):
- w = io.FlowWriter(open(path, "wb"))
- for i in flows:
- w.add(i)
+ with open(path, "wb") as f:
+ w = io.FlowWriter(f)
+ for i in flows:
+ w.add(i)
def test_load_file(tmpdir):
diff --git a/test/mitmproxy/addons/test_view.py b/test/mitmproxy/addons/test_view.py
index 6da13650..d5a3a456 100644
--- a/test/mitmproxy/addons/test_view.py
+++ b/test/mitmproxy/addons/test_view.py
@@ -132,9 +132,10 @@ def test_filter():
def tdump(path, flows):
- w = io.FlowWriter(open(path, "wb"))
- for i in flows:
- w.add(i)
+ with open(path, "wb") as f:
+ w = io.FlowWriter(f)
+ for i in flows:
+ w.add(i)
def test_create():
diff --git a/test/mitmproxy/contentviews/test_protobuf.py b/test/mitmproxy/contentviews/test_protobuf.py
index 31e382ec..71e51576 100644
--- a/test/mitmproxy/contentviews/test_protobuf.py
+++ b/test/mitmproxy/contentviews/test_protobuf.py
@@ -17,7 +17,9 @@ def test_view_protobuf_request():
m.configure_mock(**attrs)
n.return_value = m
- content_type, output = v(open(p, "rb").read())
+ with open(p, "rb") as f:
+ data = f.read()
+ content_type, output = v(data)
assert content_type == "Protobuf"
assert output[0] == [('text', b'1: "3bbc333c-e61c-433b-819a-0b9a8cc103b8"')]
diff --git a/test/mitmproxy/net/test_tcp.py b/test/mitmproxy/net/test_tcp.py
index 81d51888..234e8afb 100644
--- a/test/mitmproxy/net/test_tcp.py
+++ b/test/mitmproxy/net/test_tcp.py
@@ -34,7 +34,7 @@ class ClientCipherListHandler(tcp.BaseHandler):
sni = None
def handle(self):
- self.wfile.write("%s" % self.connection.get_cipher_list())
+ self.wfile.write(str(self.connection.get_cipher_list()).encode())
self.wfile.flush()
@@ -391,14 +391,15 @@ class TestSNI(tservers.ServerTestBase):
class TestServerCipherList(tservers.ServerTestBase):
handler = ClientCipherListHandler
ssl = dict(
- cipher_list='AES256-GCM-SHA384'
+ cipher_list=b'AES256-GCM-SHA384'
)
def test_echo(self):
c = tcp.TCPClient(("127.0.0.1", self.port))
with c.connect():
c.convert_to_ssl(sni="foo.com")
- assert c.rfile.readline() == b"['AES256-GCM-SHA384']"
+ expected = b"['AES256-GCM-SHA384']"
+ assert c.rfile.read(len(expected) + 2) == expected
class TestServerCurrentCipher(tservers.ServerTestBase):
@@ -424,7 +425,7 @@ class TestServerCurrentCipher(tservers.ServerTestBase):
class TestServerCipherListError(tservers.ServerTestBase):
handler = ClientCipherListHandler
ssl = dict(
- cipher_list='bogus'
+ cipher_list=b'bogus'
)
def test_echo(self):
@@ -632,6 +633,7 @@ class TestTCPServer:
with s.handler_counter:
with pytest.raises(exceptions.Timeout):
s.wait_for_silence()
+ s.shutdown()
class TestFileLike:
diff --git a/test/mitmproxy/platform/test_pf.py b/test/mitmproxy/platform/test_pf.py
index f644bcc5..3292d345 100644
--- a/test/mitmproxy/platform/test_pf.py
+++ b/test/mitmproxy/platform/test_pf.py
@@ -9,10 +9,11 @@ class TestLookup:
def test_simple(self):
if sys.platform == "freebsd10":
p = tutils.test_data.path("mitmproxy/data/pf02")
- d = open(p, "rb").read()
else:
p = tutils.test_data.path("mitmproxy/data/pf01")
- d = open(p, "rb").read()
+ with open(p, "rb") as f:
+ d = f.read()
+
assert pf.lookup("192.168.1.111", 40000, d) == ("5.5.5.5", 80)
with pytest.raises(Exception, match="Could not resolve original destination"):
pf.lookup("192.168.1.112", 40000, d)
diff --git a/test/pathod/language/test_base.py b/test/pathod/language/test_base.py
index ec460b07..910d298a 100644
--- a/test/pathod/language/test_base.py
+++ b/test/pathod/language/test_base.py
@@ -202,12 +202,14 @@ class TestMisc:
e.parseString("m@1")
s = base.Settings(staticdir=str(tmpdir))
- tmpdir.join("path").write_binary(b"a" * 20, ensure=True)
+ with open(str(tmpdir.join("path")), 'wb') as f:
+ f.write(b"a" * 20)
v = e.parseString("m<path")[0]
with pytest.raises(Exception, match="Invalid value length"):
v.values(s)
- tmpdir.join("path2").write_binary(b"a" * 4, ensure=True)
+ with open(str(tmpdir.join("path2")), 'wb') as f:
+ f.write(b"a" * 4)
v = e.parseString("m<path2")[0]
assert v.values(s)
diff --git a/test/pathod/language/test_generators.py b/test/pathod/language/test_generators.py
index dc15aaa1..6a67ab72 100644
--- a/test/pathod/language/test_generators.py
+++ b/test/pathod/language/test_generators.py
@@ -23,9 +23,7 @@ def test_filegenerator(tmpdir):
assert len(g[1:10]) == 9
assert len(g[10000:10001]) == 0
assert repr(g)
- # remove all references to FileGenerator instance to close the file
- # handle.
- del g
+ g.close()
def test_transform_generator():
diff --git a/test/pathod/protocols/test_http2.py b/test/pathod/protocols/test_http2.py
index 1c074197..c16a6d40 100644
--- a/test/pathod/protocols/test_http2.py
+++ b/test/pathod/protocols/test_http2.py
@@ -202,7 +202,7 @@ class TestApplySettings(net_tservers.ServerTestBase):
def handle(self):
# check settings acknowledgement
assert self.rfile.read(9) == codecs.decode('000000040100000000', 'hex_codec')
- self.wfile.write("OK")
+ self.wfile.write(b"OK")
self.wfile.flush()
self.rfile.safe_read(9) # just to keep the connection alive a bit longer