1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
import glob
import json
import os
from contextlib import contextmanager
from mitmproxy import utils, script
from mitmproxy.proxy import config
from netlib import tutils as netutils
from netlib.http import Headers
from . import tservers, tutils
example_dir = utils.Data(__name__).path("../../examples")
class DummyContext(object):
"""Emulate script.ScriptContext() functionality."""
contentview = None
def log(self, *args, **kwargs):
pass
def add_contentview(self, view_obj):
self.contentview = view_obj
def remove_contentview(self, view_obj):
self.contentview = None
@contextmanager
def example(command):
command = os.path.join(example_dir, command)
ctx = DummyContext()
with script.Script(command, ctx) as s:
yield s
def test_load_scripts():
scripts = glob.glob("%s/*.py" % example_dir)
tmaster = tservers.TestMaster(config.ProxyConfig())
for f in scripts:
if "har_extractor" in f:
continue
if "flowwriter" in f:
f += " -"
if "iframe_injector" in f:
f += " foo" # one argument required
if "filt" in f:
f += " ~a"
if "modify_response_body" in f:
f += " foo bar" # two arguments required
s = script.Script(f, script.ScriptContext(tmaster))
try:
s.load()
except Exception as v:
if "ImportError" not in str(v):
raise
else:
s.unload()
def test_add_header():
flow = tutils.tflow(resp=netutils.tresp())
with example("add_header.py") as ex:
ex.run("response", flow)
assert flow.response.headers["newheader"] == "foo"
def test_custom_contentviews():
with example("custom_contentviews.py") as ex:
pig = ex.ctx.contentview
_, fmt = pig("<html>test!</html>")
assert any('esttay!' in val[0][1] for val in fmt)
assert not pig("gobbledygook")
def test_iframe_injector():
with tutils.raises(script.ScriptException):
with example("iframe_injector.py") as ex:
pass
flow = tutils.tflow(resp=netutils.tresp(content="<html>mitmproxy</html>"))
with example("iframe_injector.py http://example.org/evil_iframe") as ex:
ex.run("response", flow)
content = flow.response.content
assert 'iframe' in content and 'evil_iframe' in content
def test_modify_form():
form_header = Headers(content_type="application/x-www-form-urlencoded")
flow = tutils.tflow(req=netutils.treq(headers=form_header))
with example("modify_form.py") as ex:
ex.run("request", flow)
assert flow.request.urlencoded_form["mitmproxy"] == ["rocks"]
def test_modify_querystring():
flow = tutils.tflow(req=netutils.treq(path="/search?q=term"))
with example("modify_querystring.py") as ex:
ex.run("request", flow)
assert flow.request.query["mitmproxy"] == ["rocks"]
def test_modify_response_body():
with tutils.raises(script.ScriptException):
with example("modify_response_body.py"):
assert True
flow = tutils.tflow(resp=netutils.tresp(content="I <3 mitmproxy"))
with example("modify_response_body.py mitmproxy rocks") as ex:
assert ex.ctx.old == "mitmproxy" and ex.ctx.new == "rocks"
ex.run("response", flow)
assert flow.response.content == "I <3 rocks"
def test_redirect_requests():
flow = tutils.tflow(req=netutils.treq(host="example.org"))
with example("redirect_requests.py") as ex:
ex.run("request", flow)
assert flow.request.host == "mitmproxy.org"
def test_har_extractor():
with tutils.raises(script.ScriptException):
with example("har_extractor.py"):
pass
times = dict(
timestamp_start=746203272,
timestamp_end=746203272,
)
flow = tutils.tflow(
req=netutils.treq(**times),
resp=netutils.tresp(**times)
)
with example("har_extractor.py -") as ex:
ex.run("response", flow)
with open(tutils.test_data.path("data/har_extractor.har")) as fp:
test_data = json.load(fp)
assert json.loads(ex.ctx.HARLog.json()) == test_data["test_response"]
|