Skip to content

Commit b26aa46

Browse files
authored
unset host, if set, before sending proxy cloud run/functions request (#3025)
* unset host, if set, before sending proxy request to api client * prepare for more headers that I may not want to copy over * update changelog * update logic * fix formatting
1 parent 87434cc commit b26aa46

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
- Catches errors while updating authorized domains when deleting channels, printing a warning instead of failing.
2+
- Fixes issue where `host` header was being incorrectly set when proxying to Cloud Run or Cloud Functions for Firebase from the Hosting emulator. (#3012)

src/hosting/proxy.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ export function proxyRequestHandler(url: string, rewriteIdentifier: string): Req
6767
// forward the parsed __session cookie if any
6868
Cookie: sessionCookie || "",
6969
});
70+
// Skip particular header keys:
71+
// - using x-forwarded-host, don't need to keep `host` in the headers.
72+
const headersToSkip = new Set(["host"]);
7073
for (const key of Object.keys(req.headers)) {
74+
if (headersToSkip.has(key)) {
75+
continue;
76+
}
7177
const value = req.headers[key];
7278
if (value == undefined) {
7379
headers.delete(key);

src/test/hosting/cloudRunProxy.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,32 @@ describe("cloudRunProxy", () => {
107107
});
108108
});
109109

110+
it("should not send the `host` header if it's provided", async () => {
111+
nock(cloudRunApiOrigin)
112+
.get("/v1/projects/project-foo/locations/us-central1/services/helloworld")
113+
.reply(200, { status: { url: cloudRunServiceOrigin } });
114+
nock(cloudRunServiceOrigin, {
115+
reqheaders: {
116+
host: "helloworld-hash-uc.a.run.app:443",
117+
"x-forwarded-host": "localhost:3333",
118+
},
119+
})
120+
.get("/")
121+
.reply(200, "live version");
122+
123+
const mwGenerator = cloudRunProxy(fakeOptions);
124+
const mw = await mwGenerator(fakeRewrite);
125+
const spyMw = sinon.spy(mw);
126+
127+
return supertest(spyMw)
128+
.get("/")
129+
.set("host", "localhost:3333")
130+
.expect(200, "live version")
131+
.then(() => {
132+
expect(spyMw.calledOnce).to.be.true;
133+
});
134+
});
135+
110136
it("should resolve to a live version in another region", async () => {
111137
const cloudRunServiceOriginAsia = "https://helloworld-hash-as.a.run.app";
112138
nock(cloudRunApiOrigin)

0 commit comments

Comments
 (0)