Skip to content

Commit 27ce234

Browse files
stfsytaeoldcolerogers
authored
fix: prioritize boolean cors value over debug flags (#1189)
* fix: prioritize boolean cors value over debug flags As discussed in firebase/firebase-tools#4862 it's not possible to disable cors in v2 functions, because the emulator enables the debug feature for cors overriding `opts.cors`. If we could prioritize the boolean flag over the debug feature, we could have both: The ability to disable cors and also prioritize the debug flag over user provided configuration. * chore: use less conditions to check desired cors state Co-authored-by: Daniel Lee <[email protected]> * style: format * chore: run prettier to follow formatting rules * style: format https spec file Co-authored-by: Daniel Lee <[email protected]> Co-authored-by: Cole Rogers <[email protected]>
1 parent 837ec38 commit 27ce234

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

spec/v2/providers/https.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,33 @@ describe("onRequest", () => {
215215

216216
sinon.restore();
217217
});
218+
219+
it("should NOT add CORS headers if debug feature is enabled and cors has value false", async () => {
220+
sinon.stub(debug, "isDebugFeatureEnabled").withArgs("enableCors").returns(true);
221+
222+
const func = https.onRequest({ cors: false }, (req, res) => {
223+
res.status(200).send("Good");
224+
});
225+
226+
const req = new MockRequest(
227+
{
228+
data: {},
229+
},
230+
{
231+
"Access-Control-Request-Method": "POST",
232+
"Access-Control-Request-Headers": "origin",
233+
origin: "example.com",
234+
}
235+
);
236+
req.method = "OPTIONS";
237+
238+
const resp = await runHandler(func, req as any);
239+
expect(resp.status).to.equal(200);
240+
expect(resp.body).to.be.equal("Good");
241+
expect(resp.headers).to.deep.equal({});
242+
243+
sinon.restore();
244+
});
218245
});
219246

220247
describe("onCall", () => {

src/v2/providers/https.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,12 @@ export function onRequest(
230230
}
231231

232232
if (isDebugFeatureEnabled("enableCors") || "cors" in opts) {
233-
const origin = isDebugFeatureEnabled("enableCors") ? true : opts.cors;
233+
let origin = opts.cors;
234+
if (isDebugFeatureEnabled("enableCors")) {
235+
// Respect `cors: false` to turn off cors even if debug feature is enabled.
236+
origin = opts.cors === false ? false : true;
237+
}
238+
234239
const userProvidedHandler = handler;
235240
handler = (req: Request, res: express.Response): void | Promise<void> => {
236241
return new Promise((resolve) => {

0 commit comments

Comments
 (0)