Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include sent headers with CONNECT request to upstream proxy #6328

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Yopi
Copy link
Contributor

@Yopi Yopi commented Aug 20, 2023

Description

Includes the request headers from the client to the upstream proxy in the CONNECT request. Looking at #5802, and the relevant events, I don't believe that there is a way today to access the headers that are sent from the client and override the empty headers sent to the upstream proxy.

I don't know if this is the best way to tackle it, or if there is a reason to strip all the headers in the CONNECT request, but for my specific use case of the day, it would be nice to be able to handle the traffic differently based on the user agent of the caller.

Checklist

  • I have updated tests where applicable.
  • I have added an entry to the CHANGELOG.

@mhils
Copy link
Member

mhils commented Aug 24, 2023

Thanks for the PR! I see how this can be useful, but forwarding headers by default can also lead to unexpected leaks. For example, you may not want to forward your proxy authentication headers to an upstream proxy, or you do not want to forward the true user agent.

I'd be happy to have this as an off-by-default option (something like forward_connect_headers?). Ideally this is implemented in an isolated addon that uses a WeakKeyDictionary[Client, Headers] (see https://github.com/mitmproxy/mitmproxy/blob/10.0.0/mitmproxy/addons/proxyauth.py#L28-L30) to store the headers in http_connect and then set them in http_connect_upstream if the option is set. Let me know if that doesn't make sense!

I don't believe that there is a way today to access the headers that are sent from the client and override the empty headers sent to the upstream proxy.

The upstream_auth option is already doing that I believe, see https://github.com/mitmproxy/mitmproxy/blob/main/mitmproxy/addons/upstream_auth.py.

@Yopi
Copy link
Contributor Author

Yopi commented Aug 24, 2023

Thank you for the feedback. That makes sense, that it might accidentally leak content.

I'll have a look if we can make it as an addon, but when I was playing with the different events it didn't seem like we could actually access the headers sent from the client to mitmproxy on the connect call. What upstream_auth is doing, if I'm not mistaken, is adding headers from mitmproxy to the upstream proxy on the request.

Since I had to make the changes that I did in this PR for the connection between mitmproxy to the upstream proxy for any headers to be available in the flow in this event:

    def http_connect_upstream(self, f: http.HTTPFlow):

@mhils
Copy link
Member

mhils commented Aug 24, 2023

I'll have a look if we can make it as an addon, but when I was playing with the different events it didn't seem like we could actually access the headers sent from the client to mitmproxy on the connect call.

Take a look at the proxyauth addons - that one accesses them. http_connect to access, http_connect_upstream to set, and a weak key dict in the meantime to remember them.

@Yopi Yopi force-pushed the include-connection-headers-with-connect-request branch from 07fa747 to caea19b Compare August 26, 2023 16:02
@Yopi
Copy link
Contributor Author

Yopi commented Aug 26, 2023

Yes, you were completely right. Looks like it wasn't too difficult to achieve!

I'll add some tests as well, but I'm not sure what your general policy is on addons. Do you merge them if they are general enough, or are they better left to each user to have on their side?

Similar question to whether or not it should be a default addon or not?

@mhils
Copy link
Member

mhils commented Aug 26, 2023

An forward_connect_headers option (even if off by default) with a corresponding addon seems to be generic enough to be in mitmproxy by default. We don't have non-default addons. For things that cater very specific use cases or incur significant complexity, examples/contrib is a great place to share them. :)

@mhils
Copy link
Member

mhils commented Oct 31, 2023

Any update on this? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants