fix: restrict CORS configuration to trusted origins#103
fix: restrict CORS configuration to trusted origins#103shogun444 wants to merge 5 commits intoLDFLK:mainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the application's security posture by transitioning from an overly permissive Cross-Origin Resource Sharing (CORS) configuration to a more secure, restricted approach. By allowing only explicitly defined trusted origins, the change mitigates potential cross-site scripting (XSS) and other web-based vulnerabilities, ensuring that only authorized front-end applications can interact with the API. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly restricts the CORS configuration by replacing the wildcard * for allow_origins with a configurable list of trusted origins from an environment variable. This is a great security improvement. I've added a couple of suggestions to make the implementation more robust and to further harden the security posture of the CORS policy.
|
|
There was a problem hiding this comment.
move load_dotenv() from inside load_config() and put it right after the imports. right now, the CORSMiddleware setup happens as soon as the file is loaded, but load_config() only runs much later when the app starts up. Because of that, by the time the middleware looks for ALLOWED_ORIGINS, it hasn't seen the .env
file yet and just defaults to localhost:3000.
|
|
Okay. |
|
@yasandu0505 |
@shogun444 I have added some comments brother , can you resolve them? , Thank You! |
main.py
Outdated
| @@ -1,10 +1,15 @@ | |||
| from fastapi import FastAPI | |||
| from src.routers import organisation_router, data_router, search_router, person_router | |||
| from dotenv import load_dotenv | |||
There was a problem hiding this comment.
In here you can't access the env like this. We have a separate config module. you can check it on @/src/core/config.py. Under that you can see how we have captured env variables. capture the env variables to that module and expose them through settings.
Then you can import it here on main.py
from src.core.config import settings
allowed_origins = settings.ALLOWED_ORIGINS.split(",")
if not allowed_origins:
allowed_origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=allowed_origins,
allow_credentials=False,
allow_methods=["*"],
allow_headers=["*"],
)Thank You! , Happy coding 💪😁
.env.example
Outdated
| @@ -1,4 +1,5 @@ | |||
| BASE_URL_QUERY=base-url-query | |||
| ALLOWED_ORIGINS=http://localhost:3000 | |||
There was a problem hiding this comment.
Can you please change this to something like your-allowed-origins , Thank You!
|
Yes I'll do it. |
Thank You Brother 💪 |
|
@yasandu0505 is this okay? |
Description
Replaces permissive CORS configuration (
allow_origins=["*"]) with a restricted list of trusted origins using theALLOWED_ORIGINSenvironment variable.Defaults to
http://localhost:3000for development.Related Issue
Fixes #102