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

Add Handshake::forwarded_addr() to get a trusted forwarded address #295

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

Conversation

zuckschwerdt
Copy link

The X-Forwarded-For header is implicitly untrustworthy, the need may arise to get the last trusted address in a reverse proxy chain.

This adds Handshake::forwarded_addr(trusted) and Request::forwarded_addr(trusted)
to get a address at exactly specified trusted proxy depth.

E.g. in the common example of a service behind a Apache or Nginx reverse proxy use forwarded_addr(1) to get the rightmost X-Forwarded-For header address.
Using forwarded_addr(0) will always return the direct peer address.

I'm currently using this as extension trait and it's confirmed to work well.

The X-Forwarded-For header is implicitly untrustworthy,
this adds `Handshake::forwarded_addr()` and `Request::forwarded_addr()`
to get a trusted address at exactly specified proxy depth.
Comment on lines +124 to +125
#[allow(dead_code)]
fn forwarded_addr(&self, trusted: usize) -> Result<Option<String>> {

Choose a reason for hiding this comment

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

This function would be helpful, if it was accessible.

Github stops me from suggesting, so I just write it out.
The #[allow(dead_code)] is not nessesary when the function gets a pub

Copy link
Author

Choose a reason for hiding this comment

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

This is modelled exactly along the existing functions and API. I'd rather have this merged with no fuss than propose an new API.

/// This method will attempt to retrieve a specific forwarded IP address of the requester
/// in the following manner:
///
/// If the `X-Forwarded-For` header exists, this method will return the `trusted` right most

Choose a reason for hiding this comment

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

Should not it say "left most address", or I am understanding it wrong ?

Copy link
Author

Choose a reason for hiding this comment

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

The n'th (with n=trusted) address from the right end. Usually an address farther to the left (front) can't be trusted -- it's an arbitrary value from a proxy /client farther away from the server. E.g. if I only trust my own proxy, I want the last (right most) entry.

If a request goes through multiple proxies, the IP addresses of each successive proxy is listed. This means, the right-most IP address is the IP address of the most recent proxy and the left-most IP address is the IP address of the originating client.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For#Directives

Choose a reason for hiding this comment

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

Huge thanks for the explanation. Also, I must be blind, cos I did not notice rsplit instead of split. So, apologies, my bad.

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.

3 participants