diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a70493c1..51bdd3634 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ ([#5084](https://github.com/mitmproxy/mitmproxy/pull/5084), @Speedlulu) * Scripts with relative paths are now loaded relative to the config file and not where the command is ran ([#4860](https://github.com/mitmproxy/mitmproxy/pull/4860), @Speedlulu) +* Enhance documentation and add alert log messages when stream_large_bodies and modify_body are set + ([#6514](https://github.com/mitmproxy/mitmproxy/pull/6514), @rosydawn6) ## 14 November 2023: mitmproxy 10.1.5 diff --git a/docs/src/content/overview-features.md b/docs/src/content/overview-features.md index f635436a2..adb948511 100644 --- a/docs/src/content/overview-features.md +++ b/docs/src/content/overview-features.md @@ -205,7 +205,9 @@ if a modify hook is triggered on server response, the replacement is only run on the Response object leaving the Request intact. You control whether the hook triggers on the request, response or both using the filter pattern. If you need finer-grained control than this, it's simple -to create a script using the replacement API on Flow components. +to create a script using the replacement API on Flow components. Body +modifications have no effect on streamed bodies. See +[Streaming]({{< relref "#streaming" >}}) for more detail. #### Examples @@ -359,8 +361,8 @@ indicated manipulations on it, and then send the message on to the other party. This can be problematic when downloading or uploading large files. When streaming is enabled, message bodies are not buffered on the proxy but instead sent directly to the server/client. This currently means that the message body -will not be accessible within mitmproxy. HTTP headers are still fully buffered before -being sent. +will not be accessible within mitmproxy, and body modifications will have no +effect. HTTP headers are still fully buffered before being sent. Request/response streaming is enabled by specifying a size cutoff in the `stream_large_bodies` option. diff --git a/mitmproxy/addons/modifybody.py b/mitmproxy/addons/modifybody.py index 4148c4799..657dbe07c 100644 --- a/mitmproxy/addons/modifybody.py +++ b/mitmproxy/addons/modifybody.py @@ -6,6 +6,9 @@ from mitmproxy import ctx from mitmproxy import exceptions from mitmproxy.addons.modifyheaders import ModifySpec from mitmproxy.addons.modifyheaders import parse_modify_spec +from mitmproxy.log import ALERT + +logger = logging.getLogger(__name__) class ModifyBody: @@ -37,6 +40,18 @@ class ModifyBody: self.replacements.append(spec) + stream_and_modify_conflict = ( + ctx.options.modify_body + and ctx.options.stream_large_bodies + and ("modify_body" in updated or "stream_large_bodies" in updated) + ) + if stream_and_modify_conflict: + logger.log( + ALERT, + "Both modify_body and stream_large_bodies are active. " + "Streamed bodies will not be modified.", + ) + def request(self, flow): if flow.response or flow.error or not flow.live: return diff --git a/mitmproxy/addons/proxyserver.py b/mitmproxy/addons/proxyserver.py index 820a30cc7..1335f28f6 100644 --- a/mitmproxy/addons/proxyserver.py +++ b/mitmproxy/addons/proxyserver.py @@ -150,8 +150,9 @@ class Proxyserver(ServerManager): None, """ Stream data to the client if response body exceeds the given - threshold. If streamed, the body will not be stored in any way. - Understands k/m/g suffixes, i.e. 3m for 3 megabytes. + threshold. If streamed, the body will not be stored in any way, + and such responses cannot be modified. Understands k/m/g + suffixes, i.e. 3m for 3 megabytes. """, ) loader.add_option( diff --git a/test/mitmproxy/addons/test_modifybody.py b/test/mitmproxy/addons/test_modifybody.py index 25f3c3b42..3d5290016 100644 --- a/test/mitmproxy/addons/test_modifybody.py +++ b/test/mitmproxy/addons/test_modifybody.py @@ -1,6 +1,7 @@ import pytest from mitmproxy.addons import modifybody +from mitmproxy.addons import proxyserver from mitmproxy.test import taddons from mitmproxy.test import tflow from mitmproxy.test.tutils import tresp @@ -14,6 +15,13 @@ class TestModifyBody: with pytest.raises(Exception, match="Cannot parse modify_body"): tctx.configure(mb, modify_body=["/"]) + def test_warn_conflict(self, caplog): + caplog.set_level("DEBUG") + mb = modifybody.ModifyBody() + with taddons.context(mb, proxyserver.Proxyserver()) as tctx: + tctx.configure(mb, stream_large_bodies="3m", modify_body=["one/two/three"]) + assert "Streamed bodies will not be modified" in caplog.text + def test_simple(self): mb = modifybody.ModifyBody() with taddons.context(mb) as tctx: