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

[breaking] rename xForwardedForIndex to XFF_DEPTH #4332

Merged
merged 10 commits into from
Mar 16, 2022

Conversation

benmccann
Copy link
Member

Having to specify a negative number is a bit of an unusual API, so flip it and make it positive

This is a breaking change for anyone already using xForwardedForIndex

@changeset-bot
Copy link

changeset-bot bot commented Mar 15, 2022

🦋 Changeset detected

Latest commit: fb3e63e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/adapter-node Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

packages/adapter-node/README.md Outdated Show resolved Hide resolved
packages/adapter-node/README.md Outdated Show resolved Hide resolved
```

For that reason you should always use a negative number (depending on the number of proxies) if you need to trust `event.clientAddress`. In the above example, `0` would yield the spoofed address while `-3` would continue to work.
If there may be a variable number of proxies, you would have to read the `X-Forwarded-For` header yourself and read the IP address from the left, but be very careful that you do not use the result for any applications with possible security implications since `X-Forwarded-For` is [trivial to spoof](https://adam-p.ca/blog/2022/03/x-forwarded-for/).
Copy link
Member

Choose a reason for hiding this comment

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

Is this a realistic scenario? Surely there's going to be a fixed number of proxies unless you changed your network architecture?

I think it might be worth preserving the previous illustration, because most people aren't going to know the implications of 'read from the left' or 'read from the right' (at least, I wouldn't have known before this week)

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking that there could be proxies that are not controlled by your own server infrastructure. E.g. some users may have the traffic flow through a corporate proxy at the user's workplace while others wouldn't, so there could always be variable number of proxies. This could probably be worded more clearly

Copy link
Member

Choose a reason for hiding this comment

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

Surely the relevant number isn't the proxies between the server and the client, but the (trusted) proxies between the server and the public internet?

Copy link
Member Author

@benmccann benmccann Mar 15, 2022

Choose a reason for hiding this comment

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

Why would that be? Any proxy could be added to the X-Forwarded-For

Sometimes you could have:

<client address>, <trusted proxy address>

And sometimes you could have:

<client address>, <untrusted proxy address>, <trusted proxy address>

In this case it doesn't work to go a set number from the right. And actually now I'm remembering @mrkishi suggesting that we would need to not just use an index, but check the addresses in the list and I'm wondering if we should just remove the ability to lookup by index as it actually does not seem very safe to me.

Copy link
Member

Choose a reason for hiding this comment

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

Using an index is fine, you just need to be careful do it from the right (as we're already doing). If you have an untrusted proxy after a trusted one, you can't trust the header at all, so in that case you shouldn't set the header in order to get remoteAddress instead.

A list of trusted proxy "addresses" is a valid option, but since it was considered too troublesome, we can just let it up to the user since they already have access to the header. Even with a list of trusted proxies, you should still read from the right: keep discarding trusted addresses from the right and use the first untrusted one as the client address.

@mrkishi
Copy link
Member

mrkishi commented Mar 15, 2022

I just realized this should also be an environment variable, since it's not app- but deployment-specific. So ADDRESS_HEADER and...

X_FORWARDED_FOR_DEPTH?
X_FORWARDED_FOR_TRUST?
X_FORWARDED_FOR_TRUSTED_PROXIES?

@benmccann
Copy link
Member Author

yeah, making it an environment variable probably makes sense

@benmccann benmccann changed the title [breaking] rename xForwardedForIndex to xForwardedForNumProxies [breaking] rename xForwardedForIndex to XFF_DEPTH Mar 16, 2022

```
<client address>, <proxy 1 address>, <proxy 2 address>
```

To get the client address we could use `xForwardedFor: 0` or `xForwardedFor: -3`, which counts back from the number of addresses.
To get the client address we could use `XFF_DEPTH=3`.
Copy link
Member

@Rich-Harris Rich-Harris Mar 16, 2022

Choose a reason for hiding this comment

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

I'm not sure how to turn this into a suggestion given that it contains a fenced code block:


Some guides will tell you to read the left-most address, but this leaves you vulnerable to spoofing:

<spoofed address>, <client address>, <proxy 1 address>, <proxy 2 address>

Instead, we read from the right, accounting for the number of trusted proxies. In this case, we would use XFF_DEPTH=3.

@Rich-Harris Rich-Harris mentioned this pull request Mar 16, 2022
* use XFF_DEPTH_ENV

* get rid of get_xff_depth

* typo
Co-authored-by: Maurício Kishi <mrkishi@users.noreply.github.com>
@Rich-Harris Rich-Harris merged commit 1bde07d into master Mar 16, 2022
@Rich-Harris Rich-Harris deleted the xForwardedForNumProxies branch March 16, 2022 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants