Skip to content

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

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

IzaakGough wants to merge 17 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>, and adds those expression forms to CorsOption.

refOf also gains a RegExp case. That one is not a deploy fix: cors is resolved per request by resolveCorsOrigin and has no entry in optionsToEndpoint, optionsToTrigger or optionsToTriggerAnnotations, so a cors expression never reaches the wire manifest. What the case fixes is the diagnostic string, reached via .toCEL(), .value() and toJSON(), where a RegExp previously stringified to {}. A test in both v1 and v2 pins that cors stays out of the manifest, since that is the assumption the change rests on.

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
Comment thread src/params/types.ts Outdated
Comment thread src/params/types.ts
CorsOption is referenced by HttpsOptions.cors in both providers but was
not in either module's export list, so api-extractor reported
ae-forgotten-export and the docs rendered the type unlinked.
@IzaakGough
IzaakGough requested a review from cabljac August 25, 2026 10:16

@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.

This looks like the right general design, requesting changes for some things below.

Widening at the Expression level looks forced by the hierarchy (thenElse returns a TernaryExpression, which extends Expression<T>), and I checked the widening can't leak a RegExp into anything serialized: tsc rejects Expression<RegExp> on region, serviceAccount, memory, omit and networkInterface.tags.

Comment thread src/params/types.ts
Comment thread src/common/providers/https.ts
Comment thread src/params/types.ts Outdated
Comment thread src/common/providers/https.ts
ExpressionValue named the wider set an expression can resolve to, but the
narrower set a param can hold was still spelled out literally in
CompareExpression, ParamSpec, WireParamSpec, ParamOptions and Param. Extract it
so both constraints have names and the distinction between them is visible.
CorsOption is public on the v1.https and v2.https namespaces, so it needs its
own JSDoc block. Also mention the param ternary form on the cors option itself
in both v1 and v2, since that is where a user looks for it.
RegExp has no CEL literal form, so a cors expression holding one is only safe
because cors is resolved per request and never serialized. Pin that assumption
in v1 and v2 rather than leaving it implicit.
@IzaakGough
IzaakGough requested a review from cabljac September 10, 2026 11:30
Covariance makes it assignable to Expression<Array<string | RegExp>>, so it
reads as removable without the note.

@ajperel ajperel 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.

I need to spend some more time wrapping my head around all this. It's a part of functions I haven't looked at too much. In the meantime I've asked @Berlioz to review as well as he knows more of it. But I want to understand and be confident in this PR.

| Expression<string[]>
| Expression<RegExp>
| Expression<Array<string | RegExp>>
| boolean

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.

Should we also support Expression?

@ajperel
ajperel requested a review from Berlioz September 19, 2026 00:11
@Berlioz

Berlioz commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

Sorry to jump and and say things right before running off for vacation, but my first instinct here is wondering whether doing this kind of invasive surgery on the params type system is worth the risk when there's a perfectly reasonable hack available (we control how options are set by the SDK, so we can just special-case the cors option for anything beginning and ending with \, right?). Yes that's a hack, but I'm not sure it's any more of a hack than creating a new Expression type that can be reasonably used in only a single place.

If we do decide that type surgery is worth doing in this case, this PR pretty much LGTM though.

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

4 participants