Skip to content

Commit

Permalink
fix(remix-express): respect expressjs hostname property when behind r…
Browse files Browse the repository at this point in the history
…everse proxies (#7323)

Co-authored-by: Matt Brophy <[email protected]>
  • Loading branch information
rossipedia and brophdawg11 authored Oct 23, 2023
1 parent 882f370 commit ef67e12
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/fix-express-proxy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@remix-run/express": patch
---

Allow the `@remix-run/express` adapter to work behind a proxy when using `app.enable('trust proxy')`
- Previously, this used `req.get('host')` to construct the Remix `Request`, but that does not respect `X-Forwarded-Host`
- This now uses `req.hostname` which will respect `X-Forwarded-Host`
9 changes: 8 additions & 1 deletion packages/remix-express/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,14 @@ export function createRemixRequest(
req: express.Request,
res: express.Response
): Request {
let url = new URL(`${req.protocol}://${req.get("host")}${req.url}`);
// req.hostname doesn't include port information so grab that from
// `X-Forwarded-Host` or `Host`
let [,hostnamePort] = req.get('X-Forwarded-Host')?.split(':') ?? [];
let [,hostPort] = req.get('host')?.split(':') ?? [];
let port = hostnamePort || hostPort;
// Use req.hostname here as it respects the "trust proxy" setting
let resolvedHost = `${req.hostname}${port ? `:${port}` : ""}`;
let url = new URL(`${req.protocol}://${resolvedHost}${req.url}`);

// Abort action/loaders once we can no longer write a response
let controller = new AbortController();
Expand Down

0 comments on commit ef67e12

Please sign in to comment.