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

Handle reverse proxy setting a port in Forwarded-For #43352

Closed
wants to merge 4 commits into from

Conversation

Mika56
Copy link
Contributor

@Mika56 Mika56 commented Feb 5, 2024

As per RFC 7239, a reverse proxy may add a port to the client's IP address:

If the server receiving proxied requests requires some
address-based functionality, this parameter MAY instead contain an IP
address (and, potentially, a port number)
https://datatracker.ietf.org/doc/html/rfc7239.html#section-5.2:~:text=and%2C%20potentially%2C%20a%20port%20number

This MR removes that port from the IP address, allowing the Forwarde-For header to be properly handled.

@szaimen szaimen added the 2. developing Work in progress label Feb 5, 2024
@szaimen szaimen added this to the Nextcloud 29 milestone Feb 5, 2024
@szaimen szaimen added 3. to review Waiting for reviews and removed 2. developing Work in progress labels Feb 5, 2024
@@ -612,6 +612,11 @@ public function getRemoteAddress(): string {
$IP = substr($IP, 1, -1);
}

// remove client port when set
if(str_contains($IP, ':')) {
Copy link
Member

Choose a reason for hiding this comment

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

this completely breaks IPv6

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch. I've updated the PR with new code that should work better

Signed-off-by: Mikael Peigney <Mika56@users.noreply.github.com>
@kesselb
Copy link
Contributor

kesselb commented Feb 5, 2024

As reference, how Symfony solved the same problem: https://github.com/symfony/symfony/blob/d9d6024a1a663caa7f13360a4a1d9de443e9c954/src/Symfony/Component/HttpFoundation/Request.php#L1986-L1997

I recommend adding some automated tests to make sure the parsing does not break in the future.

@Mika56
Copy link
Contributor Author

Mika56 commented Feb 6, 2024

And the corresponding tests: https://github.com/symfony/symfony/blob/d9d6024a1a663caa7f13360a4a1d9de443e9c954/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php#L1034
Should we use their test suite? I don't see support for [IPv6]:port
Would that be adding a file to tests/lib/AppFramework/Http ?

@nickvergessen
Copy link
Member

You can add your tests in

public function testGetRemoteAddressWithoutTrustedRemote() {

@nickvergessen
Copy link
Member

Do you need more help? Or shall we take over to finish it?

@Mika56
Copy link
Contributor Author

Mika56 commented Feb 13, 2024

I lack time, if you're willing to take it over, please go for it! :)

Signed-off-by: Joas Schilling <coding@schilljs.com>
Signed-off-by: Joas Schilling <coding@schilljs.com>
@nickvergessen
Copy link
Member

Due to changes in CI a rebase was required, but I was unable to push that. So I made a new PR at #43552

@Mika56
Copy link
Contributor Author

Mika56 commented Feb 13, 2024

No problem, thanks for taking care of it!

@skjnldsv skjnldsv removed this from the Nextcloud 29 milestone Feb 23, 2024
@Mika56 Mika56 deleted the forwarded-for-port branch February 24, 2024 09:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3. to review Waiting for reviews
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants