firebase-functions icon indicating copy to clipboard operation
firebase-functions copied to clipboard

fix(https): defer CORS param resolution until runtime

Open CorieW opened this issue 6 months ago • 2 comments

What This PR Solves

Fixes https://github.com/firebase/firebase-functions/issues/1773.

CORS options can be backed by params such as defineList, but v2 HTTPS handlers were resolving those params while the Functions control API was analyzing source code. At that point runtime env values may not exist yet, which can trigger deploy-time warnings or failures like parsing undefined.

This PR keeps the param-backed CORS option unresolved during function definition and resolves it from the CORS middleware at request time instead. It preserves debug CORS behavior and the existing single-origin normalization.

Testing Details

  • Updated v2 HTTPS unit coverage for onRequest({ cors: defineList(...) }) so function analysis can run with FUNCTIONS_CONTROL_API=true before the param value exists, then runtime request handling resolves ORIGINS.
  • Updated v2 HTTPS unit coverage for onCall({ cors: defineList(...) }) with the same deploy-time/runtime split.
  • Manually tested with a local test-project/functions app exporting both corsParamOnRequest and corsParamOnCall, each using cors: defineList("ALLOWED_ORIGINS").
  • Manual test loaded the functions while FUNCTIONS_CONTROL_API=true and ALLOWED_ORIGINS was unset, confirming module analysis no longer resolves/parses the param during deploy-time loading.
  • Manual test then set ALLOWED_ORIGINS=["https://app.example.com","https://admin.example.com"] and verified CORS preflight behavior: allowed origins returned the matching Access-Control-Allow-Origin header, and https://evil.example.com did not.

CorieW avatar May 14 '26 15:05 CorieW

Thanks for doing this. I see the bug that needs to be corrected. But this only fixes the V2 API and not the V1 API. I'm going to try a different approach where we can accept a param in the common library and it resolves the param inside the callback for the express server (since that is only run during live execution it is safe there)

inlined avatar Jun 12 '26 20:06 inlined

https://github.com/firebase/firebase-functions/pull/1903 tries a different approach. By consolidating more logic into /common we can avoid global executions of the param access code as well as removing duplicate code

inlined avatar Jun 13 '26 01:06 inlined

Closing as #1903 has been merged.

IzaakGough avatar Jul 06 '26 10:07 IzaakGough