-
Notifications
You must be signed in to change notification settings - Fork 228
fix(https): defer CORS param resolution until runtime #1887
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
Draft
CorieW
wants to merge
2
commits into
master
Choose a base branch
from
@invertase/fix-cors-param-deploy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -255,6 +255,42 @@ | |
| return !value || auth.token[claim] === value; | ||
| }; | ||
|
|
||
| type ResolvedCorsOrigin = boolean | string | RegExp | Array<boolean | string | RegExp>; | ||
|
|
||
| function normalizeCorsOrigin( | ||
| origin: ResolvedCorsOrigin | undefined | ||
| ): ResolvedCorsOrigin | undefined { | ||
| // 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) { | ||
| return origin[0]; | ||
| } | ||
|
|
||
| return origin; | ||
| } | ||
|
|
||
| function resolveCorsOrigin( | ||
| corsOption: HttpsOptions["cors"], | ||
| isCorsDebugEnabled: boolean | ||
| ): cors.CorsOptions["origin"] { | ||
| if (isCorsDebugEnabled) { | ||
| // Respect `cors: false` to turn off cors even if debug feature is enabled. | ||
| return corsOption === false ? false : true; | ||
| } | ||
|
|
||
| if (corsOption instanceof Expression) { | ||
| return ( | ||
| _requestOrigin: string | undefined, | ||
| callback: (err: Error | null, origin?: ResolvedCorsOrigin) => void | ||
| ): void => { | ||
| callback(null, normalizeCorsOrigin(corsOption.value())); | ||
| }; | ||
| } | ||
|
|
||
| return normalizeCorsOrigin(corsOption); | ||
| } | ||
|
|
||
| /** | ||
| * Handles HTTPS requests. | ||
| */ | ||
|
|
@@ -299,7 +335,7 @@ | |
| /** | ||
| * Handles HTTPS requests. | ||
| * @param handler - A function that takes a {@link https.Request} and response object, same signature as an Express app. | ||
| * @returns A function that you can export and deploy. | ||
Check warningCode scanning / CodeQL Permissive CORS configuration Medium
CORS Origin allows broad access due to
permissive or user controlled value Error loading related location Loading CORS Origin allows broad access due to permissive or user controlled value Error loading related location Loading CORS Origin allows broad access due to permissive or user controlled value Error loading related location Loading CORS Origin allows broad access due to permissive or user controlled value. CORS Origin allows broad access due to permissive or user controlled value. CORS Origin allows broad access due to permissive or user controlled value. CORS Origin allows broad access due to permissive or user controlled value. |
||
| */ | ||
| export function onRequest( | ||
| handler: (request: Request, response: express.Response) => void | Promise<void> | ||
|
|
@@ -323,18 +359,9 @@ | |
|
|
||
| handler = withErrorHandler(handler); | ||
|
|
||
| if (isDebugFeatureEnabled("enableCors") || "cors" in opts) { | ||
| 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; | ||
| } | ||
| // 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[0]; | ||
| } | ||
| const isCorsDebugEnabled = isDebugFeatureEnabled("enableCors"); | ||
| if (isCorsDebugEnabled || "cors" in opts) { | ||
| const origin = resolveCorsOrigin(opts.cors, isCorsDebugEnabled); | ||
| const middleware = cors({ origin }); | ||
|
|
||
| const userProvidedHandler = handler; | ||
|
|
@@ -434,24 +461,14 @@ | |
| opts = optsOrHandler as CallableOptions; | ||
| } | ||
|
|
||
| let cors: string | boolean | RegExp | Array<string | RegExp> | undefined; | ||
| let cors: HttpsOptions["cors"]; | ||
| if ("cors" in opts) { | ||
| if (opts.cors instanceof Expression) { | ||
| cors = opts.cors.value(); | ||
| } else { | ||
| cors = opts.cors; | ||
| } | ||
| 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. | ||
| if (Array.isArray(origin) && origin.length === 1) { | ||
| origin = origin[0]; | ||
| } | ||
| const origin = resolveCorsOrigin(cors, isDebugFeatureEnabled("enableCors")); | ||
Check warningCode scanning / CodeQL Permissive CORS configuration Medium
CORS Origin allows broad access due to
permissive or user controlled value Error loading related location Loading CORS Origin allows broad access due to permissive or user controlled value Error loading related location Loading CORS Origin allows broad access due to permissive or user controlled value Error loading related location Loading CORS Origin allows broad access due to permissive or user controlled value Error loading related location Loading CORS Origin allows broad access due to permissive or user controlled value Error loading related location Loading CORS Origin allows broad access due to permissive or user controlled value. CORS Origin allows broad access due to permissive or user controlled value. CORS Origin allows broad access due to permissive or user controlled value. CORS Origin allows broad access due to permissive or user controlled value. CORS Origin allows broad access due to permissive or user controlled value. |
||
|
|
||
| // fix the length of handler to make the call to handler consistent | ||
| const fixedLen = (req: CallableRequest<T>, resp?: CallableResponse<Stream>) => handler(req, resp); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
corsOptionis anExpression, its value is resolved at runtime within the CORS middleware callback. SincecorsOption.value()can throw an error (e.g., if a required parameter is missing or invalid at runtime), it is safer to wrap this call in a try-catch block and pass any error to thecallback. This ensures thecorsmiddleware can handle the error gracefully by propagating it to the Express error handler vianext(err).