Skip to content

feat: accept RegExp values in Expression/thenElse for CORS options - #1944

Open
IzaakGough wants to merge 6 commits into
masterfrom
@invertase/feat-accept-RegExp-values-in-Expression-for-cors-options
Open

feat: accept RegExp values in Expression/thenElse for CORS options#1944
IzaakGough wants to merge 6 commits into
masterfrom
@invertase/feat-accept-RegExp-values-in-Expression-for-cors-options

Conversation

@IzaakGough

@IzaakGough IzaakGough commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Fixes #1943

Expression<T> was constrained to string | number | boolean | string[], so a param ternary could not select between RegExp values even though cors accepts them. Users hit a type error when writing something like params.defineBoolean("X").thenElse(/a\.com$/, /b\.com$/) for a v2 HTTPS function's cors option.

This widens the Expression type parameter to a new exported ExpressionValue union that also covers RegExp and Array<string | RegExp>, adds those expression forms to CorsOption, and fixes CEL serialization so a RegExp is emitted as its string form rather than being dropped by JSON.stringify (which turns /foo/ into {}).

relnote: none

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request extends the Expression type and related helper functions to support RegExp and Array<string | RegExp> types, enabling dynamic selection of CORS origins via ternary expressions. It also updates the cors option in HTTPS options to use the shared CorsOption type and adds comprehensive unit tests. However, a high-severity issue was identified in src/params/types.ts where a single RegExp (not wrapped in an array) falls through to the default else block in refOf, resulting in an unquoted string representation that is invalid in CEL. A suggestion has been provided to explicitly handle RegExp and wrap its string representation in JSON.stringify.

Comment thread src/params/types.ts
A single RegExp (not wrapped in an array) passed to thenElse fell
through to arg.toString(), producing an unquoted /pattern/ in the
generated CEL string, which is invalid and fails at deploy time.
@IzaakGough
IzaakGough marked this pull request as ready for review August 6, 2026 12:25
Replace the eight inline copies of the Expression type bound with a
single exported ExpressionValue alias, so future additions to the set of
values an Expression can resolve to only need editing in one place.

Also close two gaps in the new tests:
- the nested thenElse case resolved the outer true branch, so the nested
  expression was never evaluated. Drive it from the false branch instead
  and assert both inner branches.
- the onRequest CORS case only asserted the true branch, so an
  implementation that always returned ifTrue would have passed. Add the
  false-branch case, asserting the non-matching origin is not allowed.

@cabljac cabljac left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice work @IzaakGough, solid PR. Checked out the branch, ran the specs and compiled the #1943 repro against it, all good. The refOf fix and the two-branch E2E preflight tests are exactly the right shape. This all makes sense to me, but since it widens the public API (Expression, CorsOption, new ExpressionValue export) we'll run it by the Firebase team before it lands.

Small asks inline. One more on the issue itself: this fully fixes #1943 as filed, but the author also mentions wanting regex CORS patterns read directly from .env files (via #1903), which this doesn't cover (an env var string still won't become a RegExp). Since merge auto-closes the issue, please reply on the thread first clarifying that. Whenever a fix partially addresses what a reporter wants, say so before the close, otherwise they reopen confused.

Comment thread src/v2/providers/https.ts
| boolean
| RegExp
| Array<string | RegExp>;
cors?: CorsOption;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

HttpsOptions.cors now references CorsOption in the public API, but it isn't in this module's export list (line 54). I ran api-extractor against the branch and it flags (ae-forgotten-export) The symbol "CorsOption" needs to be exported by the entry point, so the docs render the type unlinked. It's a warning rather than a failure, and v1 has the same gap on master, but since we're touching this surface please add CorsOption to the export type {...} here and in v1's provider.

Comment thread src/params/types.ts
* `Array<string | RegExp>` are additionally allowed so that expressions can
* select between literals for options that accept them, such as `cors`.
*/
export type ExpressionValue =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tiny one: string[] is already covered by Array<string | RegExp>, so the union member is redundant for the type checker. Your comment explains it documents the param-holdable types, which is a fair reason to keep it, so your call. If you keep it, no change needed.

Comment thread src/params/types.ts
* - Numbers and booleans are not quoted explicitly
*/
function refOf<T extends string | number | boolean | string[]>(arg: T | Expression<T>): string {
function refOf<T extends ExpressionValue>(arg: T | Expression<T>): string {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The doc comment above refOf still only describes strings, arrays, numbers and booleans. Please add a line for the new RegExp behavior (emitted as its quoted string form) so it matches what the function does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Params CompareExpression .thenElse does not support RegExp as value, therefore disallows the use of RegExp values for CORS options

2 participants