-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from bettersg/fix/108-fix-cors-issues
Fix/108 fix cors issues
- Loading branch information
Showing
6 changed files
with
119 additions
and
98 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from typing import Dict, Union | ||
|
||
from firebase_functions import https_fn | ||
|
||
|
||
# Define allowed origins | ||
ALLOWED_ORIGINS = [ | ||
"http://localhost:3000", # Local development | ||
"https://schemessg-v3-dev.web.app", # Staging frontend | ||
] | ||
|
||
|
||
def get_cors_headers(request: https_fn.Request) -> Dict[str, str]: | ||
""" | ||
Return CORS headers based on the request origin. | ||
Only allows specific origins. | ||
Args: | ||
request: The incoming request containing origin information | ||
""" | ||
origin = request.headers.get("Origin", "") | ||
|
||
# Only allow specified origins | ||
if origin in ALLOWED_ORIGINS: | ||
return { | ||
"Access-Control-Allow-Origin": origin, | ||
"Access-Control-Allow-Methods": "GET, POST, OPTIONS", | ||
"Access-Control-Allow-Headers": "Content-Type", | ||
"Access-Control-Max-Age": "3600", | ||
} | ||
|
||
# If origin not allowed, return headers without Access-Control-Allow-Origin | ||
return { | ||
"Access-Control-Allow-Methods": "GET, POST, OPTIONS", | ||
"Access-Control-Allow-Headers": "Content-Type", | ||
"Access-Control-Max-Age": "3600", | ||
} | ||
|
||
|
||
def handle_cors_preflight( | ||
request: https_fn.Request, allowed_methods: str = "GET, POST, OPTIONS" | ||
) -> Union[https_fn.Response, tuple]: | ||
""" | ||
Handle CORS preflight requests with origin validation | ||
Args: | ||
request: The incoming request | ||
allowed_methods: Comma-separated string of allowed HTTP methods | ||
""" | ||
headers = get_cors_headers(request) | ||
headers["Access-Control-Allow-Methods"] = allowed_methods | ||
|
||
# If origin wasn't in allowed list, get_cors_headers won't include Allow-Origin | ||
# In that case, return 403 Forbidden | ||
if "Access-Control-Allow-Origin" not in headers: | ||
return https_fn.Response(response="Origin not allowed", status=403, headers=headers) | ||
|
||
return https_fn.Response(response="", status=204, headers=headers) |