Skip to content

Allow CORS to be a parameter #1688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Add @deprecated annotation to callable functions's auth policy.
- Add @deprecated annotation to callable functions's auth policy (#1675)
- Allows CORS to be a parameter. (#1688)
66 changes: 66 additions & 0 deletions spec/v2/providers/https.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ 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, defineList, Expression } from "../../../src/params";

function request(args: {
data?: any;
Expand Down Expand Up @@ -227,6 +228,43 @@ describe("onRequest", () => {
});
});

it("should allow cors params", async () => {
const origins = defineList("ORIGINS");

try {
process.env.ORIGINS = '["example.com","example2.com"]';
const func = https.onRequest(
{
cors: origins,
},
(req, res) => {
res.send("42");
}
);
const req = request({
headers: {
referrer: "example.com",
"content-type": "application/json",
origin: "example.com",
},
method: "OPTIONS",
});

const response = await runHandler(func, req);

expect(response.status).to.equal(204);
expect(response.headers).to.deep.equal({
"Access-Control-Allow-Origin": "example.com",
"Access-Control-Allow-Methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
"Content-Length": "0",
Vary: "Origin, Access-Control-Request-Headers",
});
} finally {
delete process.env.ORIGINS;
clearParams();
}
});

it("should add CORS headers if debug feature is enabled", async () => {
sinon.stub(debug, "isDebugFeatureEnabled").withArgs("enableCors").returns(true);

Expand Down Expand Up @@ -301,13 +339,19 @@ describe("onRequest", () => {
});

describe("onCall", () => {
let origins: Expression<string[]>;
beforeEach(() => {
origins = defineList("ORIGINS");
process.env.ORIGINS = '["example.com","example2.com"]';

options.setGlobalOptions({});
process.env.GCLOUD_PROJECT = "aProject";
});

afterEach(() => {
delete process.env.GCLOUD_PROJECT;
delete process.env.ORIGINS;
clearParams();
});

it("should return a minimal trigger/endpoint with appropriate values", () => {
Expand Down Expand Up @@ -441,6 +485,28 @@ describe("onCall", () => {
});
});

it("should allow cors params", async () => {
const func = https.onCall({ cors: origins }, () => 42);
const req = request({
headers: {
referrer: "example.com",
"content-type": "application/json",
origin: "example.com",
},
method: "OPTIONS",
});

const response = await runHandler(func, req);

expect(response.status).to.equal(204);
expect(response.headers).to.deep.equal({
"Access-Control-Allow-Origin": "example.com",
"Access-Control-Allow-Methods": "POST",
"Content-Length": "0",
Vary: "Origin, Access-Control-Request-Headers",
});
});

it("overrides CORS headers if debug feature is enabled", async () => {
sinon.stub(debug, "isDebugFeatureEnabled").withArgs("enableCors").returns(true);

Expand Down
23 changes: 20 additions & 3 deletions src/v2/providers/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ export interface HttpsOptions extends Omit<GlobalOptions, "region" | "enforceApp
* If this is an `Array`, allows requests from domains matching at least one entry of the array.
* Defaults to true for {@link https.CallableFunction} and false otherwise.
*/
cors?: string | boolean | RegExp | Array<string | RegExp>;
cors?:
| string
| Expression<string>
| Expression<string[]>
| boolean
| RegExp
| Array<string | RegExp>;

/**
* Amount of memory to allocate to a function.
Expand Down Expand Up @@ -314,7 +320,7 @@ export function onRequest(
}

if (isDebugFeatureEnabled("enableCors") || "cors" in opts) {
let origin = opts.cors;
let origin = opts.cors instanceof Expression ? opts.cors.value() : opts.cors;
if (isDebugFeatureEnabled("enableCors")) {
// Respect `cors: false` to turn off cors even if debug feature is enabled.
origin = opts.cors === false ? false : true;
Expand Down Expand Up @@ -424,7 +430,18 @@ export function onCall<T = any, Return = any | Promise<any>, Stream = unknown>(
opts = optsOrHandler as CallableOptions;
}

let origin = isDebugFeatureEnabled("enableCors") ? true : "cors" in opts ? opts.cors : true;
let cors: string | boolean | RegExp | Array<string | RegExp> | undefined;
if ("cors" in opts) {
if (opts.cors instanceof Expression) {
cors = opts.cors.value();
} else {
cors = opts.cors;
}
} else {
cors = true;
}

let origin = isDebugFeatureEnabled("enableCors") ? true : cors;
// Arrays cause the access-control-allow-origin header to be dynamic based
// on the origin header of the request. If there is only one element in the
// array, this is unnecessary.
Expand Down
Loading