Skip to content

Commit 74bab35

Browse files
caugnerLeoMcA
andauthored
fix(cloud-function): strip X-Forwarded-Host + Forwarded headers (#8894)
`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]>
1 parent 2c81bf5 commit 74bab35

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Diff for: cloud-function/src/app.ts

+2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import { redirectLocale } from "./middlewares/redirect-locale.js";
1717
import { redirectTrailingSlash } from "./middlewares/redirect-trailing-slash.js";
1818
import { requireOrigin } from "./middlewares/require-origin.js";
1919
import { notFound } from "./middlewares/not-found.js";
20+
import { stripForwardedHostHeaders } from "./middlewares/stripForwardedHostHeaders.js";
2021

2122
const router = Router();
23+
router.use(stripForwardedHostHeaders);
2224
router.use(redirectLeadingSlash);
2325
router.all(
2426
"/api/v1/stripe/plans",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { NextFunction, Request, Response } from "express";
2+
3+
// Don't strip other `X-Forwarded-*` headers.
4+
const HEADER_REGEXP = /^(x-forwarded-host|forwarded)$/i;
5+
6+
export async function stripForwardedHostHeaders(
7+
req: Request,
8+
_res: Response,
9+
next: NextFunction
10+
) {
11+
Object.keys(req.headers)
12+
.filter((name) => HEADER_REGEXP.test(name))
13+
.forEach((name) => delete req.headers[name]);
14+
next();
15+
}

0 commit comments

Comments
 (0)