Search code examples
mitmproxy

mitmproxy: follow 3xx redirect without letting the client know?


What's the idiomatic way to handle 3XX responses in an addon without letting the client know?

Say a client issues a request to example.com. We pass the request to the server and receive a 301, Location: https://www.example.com. We don't want the client to see this, so we issue a new request to this new location while the client is waiting. Only when we receive a 200 response do we want to send the response back to the client.

The first thing that comes to mind is the responseheaders(flow) hook. However, there doesn't seem to be a way to queue a new flow to be run after the present flow completes without resetting the client connection.

The request hook has the possibility to construct a flow.response and the handler will never send anything to the server. But none of the response hooks have a similar option.

Even if we interrupt the current flow and use the ClientPlayback to send a new request, we still have to eventually return to the current flow as it doesn't know anything about the ClientPlayback. If we kill it, interrupted or not, the connection to the client will be closed.

A few suggestions to similar questions all focus on modifying the client request in the request hook before it is sent; but what if we can't predict where the 301 will send us, or even if there will be several redirects before we get to content?

Curious how to handle this scenario?


Solution

  • Generally speaking this is something mitmproxy does not have a great answer to at the moment. One approach that comes to mind is to use a library like httpx in an async def request hook to populate flow.response, but that's quite a bit of manual setup.