Skip to content
Merged
2 changes: 1 addition & 1 deletion src/firebase_functions/dataconnect_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class MutationEventData:
_C1 = _typing.Callable[[_E1], None]


def _dataconnect_endpoint_handler(
def _dataconnect_endpoint_handler( # noqa: PLR0917
func: _C1,
event_type: str,
service_pattern: _path_pattern.PathPattern | None,
Expand Down
21 changes: 17 additions & 4 deletions src/firebase_functions/https_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,23 @@ def on_request_inner_decorator(func: _C1):
func_with_init = _core._with_init(func)

if options.cors is not None:
wrapped_function = _cross_origin(
methods=options.cors.cors_methods,
origins=options.cors.cors_origins,
)(func_with_init)
# Omit unset fields rather than passing None, which overrides flask-cors defaults.
if options.cors.cors_methods is not None and options.cors.cors_origins is not None:
cors_decorator = _cross_origin(
methods=options.cors.cors_methods,
origins=options.cors.cors_origins,
)
elif options.cors.cors_methods is not None:
cors_decorator = _cross_origin(
methods=options.cors.cors_methods,
)
elif options.cors.cors_origins is not None:
cors_decorator = _cross_origin(
origins=options.cors.cors_origins,
)
else:
cors_decorator = _cross_origin()
wrapped_function = cors_decorator(func_with_init)
else:
wrapped_function = func_with_init

Expand Down
5 changes: 4 additions & 1 deletion src/firebase_functions/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,21 @@ class CorsOptions:
https://flask-cors.corydolphin.com/en/latest/configuration.html
"""

cors_origins: str | list[str] | _re.Pattern | None = None
cors_origins: str | list[str] | _re.Pattern[str] | None = None
"""
The origin(s) to allow requests from. An origin configured here that matches the value of
the ``Origin`` header in a preflight ``OPTIONS`` request is returned as the value of the
``Access-Control-Allow-Origin`` response header.
Defaults to ``"*"`` (all origins) when not specified.
"""

cors_methods: str | list[str] | None = None
"""
The method(s) which the allowed origins are allowed to access.
These are included in the ``Access-Control-Allow-Methods`` response headers
to the preflight ``OPTIONS`` requests.
Defaults to all standard HTTP methods when not specified (for ``on_request``;
``on_call`` handlers always restrict allowed methods to POST).
"""


Expand Down
70 changes: 70 additions & 0 deletions tests/test_https_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Tests for the https module.
"""

import re
import unittest
from unittest.mock import Mock

Expand Down Expand Up @@ -109,3 +110,72 @@ def test_https_error_passes_message_to_super(self):

self.assertEqual(str(err), "something went wrong")
self.assertEqual(err.args, ("something went wrong",))

def test_on_request_cors_options_branches(self):
Comment thread
shettyvarun268 marked this conversation as resolved.
Resolved
"""
CorsOptions with either or both fields unset should fall back to
flask-cors defaults on preflight requests instead of passing None.
"""
cases = [
(
CorsOptions(cors_origins="https://example.com", cors_methods=["GET", "POST"]),
"https://example.com",
"https://example.com",
{"GET", "POST"},
),
(
CorsOptions(cors_origins="https://example.com"),
"https://example.com",
"https://example.com",
{"DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"},
),
(
CorsOptions(cors_methods=["GET"]),
"https://example.com",
"https://example.com",
{"GET"},
),
(
CorsOptions(),
"https://example.com",
"https://example.com",
{"DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"},
),
(
CorsOptions(cors_origins="https://allowed.example.com"),
"https://example.com",
None,
set(),
),
(
CorsOptions(
cors_origins=re.compile(r"https://.*\.example\.com"),
cors_methods=["GET"],
),
"https://sub.example.com",
"https://sub.example.com",
{"GET"},
),
]

for cors_opts, req_origin, expected_origin, expected_methods in cases:
with self.subTest(cors=cors_opts, origin=req_origin):
app = Flask(__name__)

@https_fn.on_request(cors=cors_opts)
def handler(_req: Request):
return "OK"

headers = {
"Origin": req_origin,
"Access-Control-Request-Method": "GET",
}
with app.test_request_context("/", method="OPTIONS", headers=headers) as ctx:
resp = handler(ctx.request)

self.assertEqual(resp.headers.get("Access-Control-Allow-Origin"), expected_origin)
allow_methods = resp.headers.get("Access-Control-Allow-Methods", "")
self.assertEqual(
{m.strip() for m in allow_methods.split(",") if m.strip()},
expected_methods,
)
Loading