diff --git a/CHANGELOG.md b/CHANGELOG.md index e69de29bb..8be4ad4e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -0,0 +1 @@ +- fix: Remove false warning when using Expression in cors option #1802 diff --git a/spec/v2/providers/https.spec.ts b/spec/v2/providers/https.spec.ts index a62dfea5a..7a8bef55a 100644 --- a/spec/v2/providers/https.spec.ts +++ b/spec/v2/providers/https.spec.ts @@ -32,7 +32,14 @@ import { FULL_ENDPOINT, MINIMAL_V2_ENDPOINT, FULL_OPTIONS, FULL_TRIGGER } from " import { onInit } from "../../../src/v2/core"; import { Handler } from "express"; import { genkit } from "genkit"; -import { clearParams, defineBoolean, defineList, Expression } from "../../../src/params"; +import { + clearParams, + defineBoolean, + defineList, + defineString, + Expression, +} from "../../../src/params"; +import { logger } from "../../../src/logger"; function request(args: { data?: any; @@ -59,6 +66,41 @@ function request(args: { return ret; } +async function testWarningForCorsExpression( + createFunc: (options: { cors: Expression }) => https.HttpsFunction, + origin: string +) { + const loggerSpy = sinon.spy(logger, "warn"); + const projectId = defineString("PROJECT_ID"); + + try { + process.env.PROJECT_ID = "test-project"; + process.env.FUNCTIONS_CONTROL_API = "true"; + + const corsExpression = projectId.equals("test-project").thenElse([origin], []); + const func = createFunc({ cors: corsExpression }); + + const req = request({ + headers: { + referrer: origin, + "content-type": "application/json", + origin: origin, + }, + method: "OPTIONS", + }); + + const response = await runHandler(func, req); + + expect(response.status).to.equal(204); + expect(loggerSpy.called).to.be.false; + } finally { + delete process.env.PROJECT_ID; + delete process.env.FUNCTIONS_CONTROL_API; + clearParams(); + loggerSpy.restore(); + } +} + describe("onRequest", () => { beforeEach(() => { options.setGlobalOptions({}); @@ -336,6 +378,16 @@ describe("onRequest", () => { await runHandler(func, req); expect(hello).to.equal("world"); }); + + it("should not warn when using Expression-based cors config during deployment", async () => { + await testWarningForCorsExpression( + (opts) => + https.onRequest(opts, (req, res) => { + res.send("42"); + }), + "http://localhost:8000" + ); + }); }); describe("onCall", () => { @@ -605,6 +657,13 @@ describe("onCall", () => { expect(hello).to.equal("world"); }); + it("should not warn when using Expression-based cors config during deployment", async () => { + await testWarningForCorsExpression( + (opts) => https.onCall(opts, () => 42), + "http://localhost:5173" + ); + }); + describe("authPolicy", () => { before(() => { sinon.stub(debug, "isDebugFeatureEnabled").withArgs("skipTokenVerification").returns(true); diff --git a/src/v2/providers/https.ts b/src/v2/providers/https.ts index cfb3cfee3..9772e210c 100644 --- a/src/v2/providers/https.ts +++ b/src/v2/providers/https.ts @@ -324,7 +324,7 @@ export function onRequest( handler = withErrorHandler(handler); if (isDebugFeatureEnabled("enableCors") || "cors" in opts) { - let origin = opts.cors instanceof Expression ? opts.cors.value() : opts.cors; + let origin = opts.cors instanceof Expression ? opts.cors.runtimeValue() : opts.cors; if (isDebugFeatureEnabled("enableCors")) { // Respect `cors: false` to turn off cors even if debug feature is enabled. origin = opts.cors === false ? false : true; @@ -437,7 +437,7 @@ export function onCall, Stream = unknown>( let cors: string | boolean | RegExp | Array | undefined; if ("cors" in opts) { if (opts.cors instanceof Expression) { - cors = opts.cors.value(); + cors = opts.cors.runtimeValue(); } else { cors = opts.cors; }