Skip to content
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

CORS origins of an array of one are made a scalar. #1536

Merged
merged 2 commits into from
Mar 19, 2024
Merged
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
17 changes: 15 additions & 2 deletions src/v2/providers/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,19 @@ export function onRequest(
// Respect `cors: false` to turn off cors even if debug feature is enabled.
origin = opts.cors === false ? false : true;
}
// 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.
if (Array.isArray(origin) && origin.length === 1) {
origin = origin[1];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a bug... shouldn't this either be origin = origin[0] or origin = undefined?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤬 Will fix this week

}
const middleware = cors({ origin });

const userProvidedHandler = handler;
handler = (req: Request, res: express.Response): void | Promise<void> => {
return new Promise((resolve) => {
res.on("finish", resolve);
cors({ origin })(req, res, () => {
middleware(req, res, () => {
resolve(userProvidedHandler(req, res));
});
});
Expand Down Expand Up @@ -363,7 +370,13 @@ export function onCall<T = any, Return = any | Promise<any>>(
opts = optsOrHandler as CallableOptions;
}

const origin = isDebugFeatureEnabled("enableCors") ? true : "cors" in opts ? opts.cors : true;
let origin = isDebugFeatureEnabled("enableCors") ? true : "cors" in opts ? opts.cors : true;
// 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.
if (Array.isArray(origin) && origin.length === 1) {
origin = origin[1];
}

// onCallHandler sniffs the function length to determine which API to present.
// fix the length to prevent api versions from being mismatched.
Expand Down
Loading