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

feat(node): add PORT_HEADER env var for reverse proxies with non-standard ports #11249

Merged

Conversation

aloisklink
Copy link
Contributor

Add support for the PORT_HEADER environment variable to @sveltejs/adapter-node that can be used to read the original port of a request from a reverse proxy, which is needed if the reverse proxy is hosted on a non-standard port (e.g. not port 80 for http nor port 443 for https).

This port is normally added as part of a x-forwarded-port header, which is used by many reverse proxies, although it's not as popular as the more common x-forwarded-proto and x-forwarded-host headers.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check
    • pnpm test is failing for me on the master branch, but I don't think this is necessary, since adapter-node doesn't currently have any tests. I have linted and checked my changes.

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

… ports

Add support for the `PORT_HEADER` environment variable, that can be used
to read the original port of a request from a reverse proxy, which is
needed if the reverse proxy is hosted on a non-standard port
(e.g. not port 80 for http nor port 443 for https).

This port is normally added as part of a [`x-forwarded-port`][1], which
is used by many reverse proxies, although it's not as popular as the
more common [`x-forwarded-proto`][2] and [`x-forwarded-host`][3]
headers.

[1]: https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/x-forwarded-headers.html#x-forwarded-port
[2]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Proto
[3]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Host
Copy link

changeset-bot bot commented Dec 9, 2023

🦋 Changeset detected

Latest commit: 3a64fab

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 Minor

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

Copy link
Member

@Conduitry Conduitry left a comment

Choose a reason for hiding this comment

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

This seems like a reasonable addition. For the documentation, though, is there a more agnostic source we can link to about the X-Forwarded-Port header? If not, I'd probably rather just not link to anything than link to an AWS documentation page.

@aloisklink
Copy link
Contributor Author

For the documentation, though, is there a more agnostic source we can link to about the X-Forwarded-Port header? If not, I'd probably rather just not link to anything than link to an AWS documentation page.

I've removed this link. Unfortunately, the only agnostic source I could find was https://dzone.com/articles/what-are-x-forwarded-headers-and-why-is-it-used, but it looks like that article might have been plagiarized from https://requestly.io/blog/what-are-x-forwarded-headers-and-why-it-is-used/, which is another product specific link.

Searching for "X-Forwarded-Port" on GitHub gives 44k matches, so it's clearly popular, but not popular enough to be on MDN!

I did find this blog post briefly mentioning it, but only in relation to the RFC 7239 Forwarded HTTP header, which makes it outdated (and I see you've already got #11148 open for adding support for it to SvelteKit, although I don't know how popular it is with reverse proxies).

@Rich-Harris Rich-Harris merged commit e2504f1 into sveltejs:main Jan 9, 2024
13 checks passed
@Rich-Harris
Copy link
Member

thank you!

@github-actions github-actions bot mentioned this pull request Jan 9, 2024
@aloisklink aloisklink deleted the feat-node/support-custom-port-on-proxy branch January 9, 2024 16:26
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.

4 participants