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

fix: Detect host and protocol for alternate links correctly when running behind a proxy #462

Merged
merged 8 commits into from
Aug 22, 2023

Conversation

HHongSeungWoo
Copy link
Contributor

We are currently utilizing a reverse proxy in front of Next.js.
request.url is returning an internal address instead of an external one.
Consequently, the rewrite functionality is not behaving as expected, and the alternatelink header is inadvertently exposing the internal address.

@vercel
Copy link

vercel bot commented Aug 19, 2023

@HHongSeungWoo is attempting to deploy a commit to the next-intl Team on Vercel.

A member of the Team first needs to authorize it.

@vercel
Copy link

vercel bot commented Aug 19, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
example-next-13-next-auth ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 22, 2023 9:56am
next-intl-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 22, 2023 9:56am
next-intl-example-next-13 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 22, 2023 9:56am

@HHongSeungWoo
Copy link
Contributor Author

I'm concerned about trusting the host of the header, but if there are multiple external addresses on the same server, I can't think of any other solution :(

Copy link
Owner

@amannn amannn left a comment

Choose a reason for hiding this comment

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

Thank you so much for proposing this fix @HHongSeungWoo!

It's definitely a good idea to use the external host for the alternate links, please see my inline comment for details.

Can you please also include tests that would have failed previously and will now pass after the fix?

Thank you so much! 🙌

packages/next-intl/src/middleware/middleware.tsx Outdated Show resolved Hide resolved
packages/next-intl/src/middleware/middleware.tsx Outdated Show resolved Hide resolved
@HHongSeungWoo
Copy link
Contributor Author

스크린샷 2023-08-21 18-23-30
스크린샷 2023-08-21 18-22-31

before / after

@dzenand
Copy link

dzenand commented Aug 21, 2023

Is this somehow related to this issue?
vercel/next.js#54277

@amannn
Copy link
Owner

amannn commented Aug 21, 2023

This looks great! The only thing missing would be a regression test that helps to ensure that we keep this functionality in the future.

Thanks also for linking to the Next.js issue, I'll keep an eye on that, but seems like it should be addressed on the Next.js side.

@HHongSeungWoo
Copy link
Contributor Author

HHongSeungWoo commented Aug 21, 2023

This looks great! The only thing missing would be a regression test that helps to ensure that we keep this functionality in the future.

Thanks also for linking to the Next.js issue, I'll keep an eye on that, but seems like it should be addressed on the Next.js side.

I'm unsure about the necessary test cases. The request.url within the middleware is a string generated by Next.js. If a hostname is configured, it will be used; otherwise, localhost will be used.

Upon reviewing the code, it appears that the option trustHostHeader, which is not currently utilized, generates the URL with the request's host when set to true. However, since this isn't documented, it seems to be an unused option.

These changes are independent of external configurations or code modifications, so I can't think of any regression cases unless there are changes to the HTTP protocol itself.

…est` with `NextRequest` constructor to avoid unnecessary indirection
@amannn
Copy link
Owner

amannn commented Aug 22, 2023

@HHongSeungWoo I've added a commit where the now fixed behavior is handled within a single test case. I also noticed that we should consider the protocol as well and fixed that accordingly.

Thanks for noticing that we can use the NextRequest constructor instead of constructing a mock object, I've adapted the tests to use that consistently to avoid unnecessary redirection.

Would you like to have a last look? From my opinion this is good to go! 👍

@HHongSeungWoo
Copy link
Contributor Author

@HHongSeungWoo I've added a commit where the now fixed behavior is handled within a single test case. I also noticed that we should consider the protocol as well and fixed that accordingly.

Thanks for noticing that we can use the NextRequest constructor instead of constructing a mock object, I've adapted the tests to use that consistently to avoid unnecessary redirection.

Would you like to have a last look? From my opinion this is good to go! 👍

Thank you for making the necessary corrections. It looks better to me as well. :)

@amannn amannn merged commit 747cf8e into amannn:main Aug 22, 2023
7 checks passed
@CarbonC
Copy link

CarbonC commented Aug 23, 2023

Hello @amannn ! This looks great, but please would it be possible to push this fix to the beta branch as well? Currently im stuck with the bug on the beta-10 :-/
Thanks for the great work

@amannn
Copy link
Owner

amannn commented Aug 23, 2023

@CarbonC Yep, definitely! I've got another release on the main branch coming, then I'll rebase the RSC branch and publish a new beta!

@amannn
Copy link
Owner

amannn commented Aug 24, 2023

@CarbonC next-intl@3.0.0-beta.11 is out now with all recent improvements from the main branch!

@HHongSeungWoo HHongSeungWoo deleted the fix/host branch August 25, 2023 15:06
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