Skip to content

Commit

Permalink
fix(cloud-function): strip X-Forwarded-Host + Forwarded headers (#8894)
Browse files Browse the repository at this point in the history
`http-proxy` with `xfwd: true` only sets x-forwarded-{for,port,proto},
so the `X-Forwarded-Host` header stays in place, causing side-effects.

To be on the safe side, we also remove the `Forwarded` header, because
it may contain `host` directive, even though we don't currently use it.

Co-authored-by: Leo McArdle <[email protected]>
  • Loading branch information
caugner and LeoMcA authored May 19, 2023
1 parent 2c81bf5 commit 74bab35
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cloud-function/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import { redirectLocale } from "./middlewares/redirect-locale.js";
import { redirectTrailingSlash } from "./middlewares/redirect-trailing-slash.js";
import { requireOrigin } from "./middlewares/require-origin.js";
import { notFound } from "./middlewares/not-found.js";
import { stripForwardedHostHeaders } from "./middlewares/stripForwardedHostHeaders.js";

const router = Router();
router.use(stripForwardedHostHeaders);
router.use(redirectLeadingSlash);
router.all(
"/api/v1/stripe/plans",
Expand Down
15 changes: 15 additions & 0 deletions cloud-function/src/middlewares/stripForwardedHostHeaders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { NextFunction, Request, Response } from "express";

// Don't strip other `X-Forwarded-*` headers.
const HEADER_REGEXP = /^(x-forwarded-host|forwarded)$/i;

export async function stripForwardedHostHeaders(
req: Request,
_res: Response,
next: NextFunction
) {
Object.keys(req.headers)
.filter((name) => HEADER_REGEXP.test(name))
.forEach((name) => delete req.headers[name]);
next();
}

0 comments on commit 74bab35

Please sign in to comment.