diff --git a/mitmproxy/addons/intercept.py b/mitmproxy/addons/intercept.py index 14ef7410a..a016025ff 100644 --- a/mitmproxy/addons/intercept.py +++ b/mitmproxy/addons/intercept.py @@ -19,11 +19,10 @@ class Intercept: def process_flow(self, f): if self.filt: - should_intercept = all( + should_intercept = all([ self.filt(f), not f.request.is_replay, - f.reply.state == "handled" - ) + ]) if should_intercept: f.intercept(ctx.master) diff --git a/mitmproxy/controller.py b/mitmproxy/controller.py index c5e125ce7..868d58418 100644 --- a/mitmproxy/controller.py +++ b/mitmproxy/controller.py @@ -174,10 +174,10 @@ class Reply: class DummyReply(Reply): """ - A reply object that is not connected to anything. In contrast to regular Reply objects, - DummyReply objects are reset to "unhandled" at the end of an handler so that they can be used - multiple times. Useful when we need an object to seem like it has a channel, - and during testing. + A reply object that is not connected to anything. In contrast to regular + Reply objects, DummyReply objects are reset to "unhandled" at the end of an + handler so that they can be used multiple times. Useful when we need an + object to seem like it has a channel, and during testing. """ def __init__(self): super().__init__(None) diff --git a/mitmproxy/test/taddons.py b/mitmproxy/test/taddons.py index a22fd5e24..819d58abe 100644 --- a/mitmproxy/test/taddons.py +++ b/mitmproxy/test/taddons.py @@ -1,6 +1,7 @@ import mitmproxy.master import mitmproxy.options from mitmproxy import proxy +from mitmproxy import events class context: @@ -26,6 +27,19 @@ class context: self.wrapped = None return False + def cycle(self, addon, f): + """ + Cycles the flow through the events for the flow. Stops if a reply + is taken (as in flow interception). + """ + f.reply._state = "handled" + for evt, arg in events.event_sequence(f): + h = getattr(addon, evt, None) + if h: + h(arg) + if f.reply.state == "taken": + return + def configure(self, addon, **kwargs): """ A helper for testing configure methods. Modifies the registered diff --git a/test/mitmproxy/addons/test_intercept.py b/test/mitmproxy/addons/test_intercept.py index 19828a629..f389ff8ed 100644 --- a/test/mitmproxy/addons/test_intercept.py +++ b/test/mitmproxy/addons/test_intercept.py @@ -3,6 +3,7 @@ from mitmproxy import options from mitmproxy import exceptions from mitmproxy.test import taddons from mitmproxy.test import tutils +from mitmproxy.test import tflow class Options(options.Options): @@ -23,3 +24,13 @@ def test_simple(): r, intercept="~~" ) + + tctx.configure(r, intercept="~s") + + f = tflow.tflow(resp=True) + tctx.cycle(r, f) + assert f.intercepted + + f = tflow.tflow(resp=False) + tctx.cycle(r, f) + assert not f.intercepted