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

A middleware that adjusts request.host and request.scheme according to the Forwarded header (rfc7239) #175

Closed
wants to merge 7 commits into from

Conversation

faassen
Copy link

@faassen faassen commented Nov 20, 2014

As discussed on issue #174, implement the Forwarded header as a tween.

If you know you have a trusted proxy that sets the Forwarded header, you can use this Tween to affect request.host and request.scheme in WebOb (and thus all code that generates links based on this information).

This is a more secure alternative than doing it inside of WebOb as in pull request #160, as web applications should not accept the Forwarded header from untrusted sources as this opens them up to various attacks. In addition this tween also deals with the scheme bit, not just the host.

This does not support older forwarded headers like X_FORWARDED_HOST and X_FORWARDED_PROTO. We should do this in a similar tween along the same lines.

…ing to

the Forwarded header as described by rfc7239.
@digitalresistor
Copy link
Member

I would argue for this to be under webob.middleware rather than at the top-level at webob.*

@faassen
Copy link
Author

faassen commented Nov 24, 2014

I realized over the weekend I need to catch the exception raised and return 400 Bad Request if it's raised. I also need to make available the WSGI version, not just the handler. So stand by. :)

@faassen faassen changed the title A tween that adjusts request.host and request.scheme according to the Forwarded header (rfc7239) A middleware that adjusts request.host and request.scheme according to the Forwarded header (rfc7239) Nov 24, 2014
@faassen
Copy link
Author

faassen commented Nov 24, 2014

I now convert a parsing error of the Forwarded error into a 400 bad request (as per spec). I've added an actual WSGI middleware. I also made sure the coverage is 100%.

We could instead use the wsgify.middleware decorator to create the middleware. In that case I'd still like to expose a function that turns this middleware back into the original handle(request) -> response wrapper as that's the format in which I'd like to use this myself.

assert_raises(forwarded.ForwardedError,
forwarded.parse, 'for= _something')
assert_raises(forwarded.ForwardedError,
forwarded.parse, 'for=_something; by=192.51.100.17')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that whitespaces are not allowed after a semicolon?
According to the RFC this header field is valid:
Forwarded: for=192.0.2.43, for="[2001:db8:cafe::17]", for=unknown
but are the following valid?

Forwarded: for=192.0.2.43 , for="[2001:db8:cafe::17]" , for=unknown
Forwarded: for=192.0.2.43 ,for="[2001:db8:cafe::17]" ,for=unknown
Forwarded: for=192.0.2.60; proto=http; by=203.0.113.43
Forwarded: for=192.0.2.60 ; proto=http ; by=203.0.113.43
Forwarded: for=192.0.2.60 ;proto=http ;by=203.0.113.43

Copy link
Author

@faassen faassen May 31, 2015 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reading the spec I'm not sure either, it's not very clear. I don't even know where to ask this.

@faassen faassen closed this Jul 1, 2021
@piotr-dobrogost
Copy link

@faassen
Close? What's the reason?

@faassen
Copy link
Author

faassen commented Jul 1, 2021 via email

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

4 participants