-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Add core application configuration, CORS middleware, and origin restriction with corresponding tests. #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
5043b2c
feat: Add core application configuration, CORS middleware, and origin…
kemalcalak 4aab923
feat: Implement same-origin request handling and add corresponding tests
kemalcalak cb3c208
fix: Correct endpoint in cross-origin POST request test to ensure acc…
kemalcalak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,81 @@ | ||
| import pytest | ||
| from httpx import AsyncClient | ||
|
|
||
| from app.core.config import settings | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_allowed_origin(client: AsyncClient): | ||
| # Use an origin from settings.all_cors_origins | ||
| # settings.all_cors_origins includes FRONTEND_HOST | ||
| allowed_origin = settings.FRONTEND_HOST | ||
| response = await client.get( | ||
| "/users/me", # Correct path | ||
| headers={"Origin": allowed_origin}, | ||
| ) | ||
| # It should not be 404 due to origin (might be 401 if not logged in, but not 404) | ||
| assert response.status_code != 404 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_unauthorized_origin(client: AsyncClient): | ||
| response = await client.get( | ||
| "/auth/login", # Use an endpoint that exists | ||
| headers={"Origin": "http://malicious.com"}, | ||
| ) | ||
| assert response.status_code == 404 | ||
| assert response.json() == {"success": False, "error": "RESOURCE_NOT_FOUND"} | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_no_origin(client: AsyncClient): | ||
| response = await client.get("/auth/login") | ||
| # Should not be 404 due to missing origin | ||
| assert response.status_code != 404 | ||
| # If /health doesn't exist, this might fail, but let's check a known endpoint | ||
| pass | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_same_origin_post(client: AsyncClient): | ||
| """Test that same-origin POST requests are allowed (not blocked with 404).""" | ||
| # The client fixture uses http://test as the base origin (from conftest base_url) | ||
| same_origin = "http://test" | ||
|
|
||
| response = await client.post( | ||
| "/auth/logout", # Correct path (base_url already includes /api/v1) | ||
| headers={"Origin": same_origin}, | ||
| ) | ||
|
|
||
| # Should NOT be 404 due to origin check (may fail with 401 if not authenticated, but not 404) | ||
| assert response.status_code != 404, ( | ||
| "Same-origin POST request should not be blocked with 404" | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_same_origin_put(client: AsyncClient): | ||
| """Test that same-origin PUT requests are allowed (not blocked with 404).""" | ||
| same_origin = "http://test" | ||
|
|
||
| response = await client.put( | ||
| "/users/me", | ||
| headers={"Origin": same_origin}, | ||
| ) | ||
|
|
||
| # Should NOT be 404 due to origin check (may fail with 401/422, but not 404) | ||
| assert response.status_code != 404, ( | ||
| "Same-origin PUT request should not be blocked with 404" | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_cross_origin_post_blocked(client: AsyncClient): | ||
| """Test that cross-origin POST requests are still blocked (404).""" | ||
| response = await client.post( | ||
| "/auth/logout", | ||
| headers={"Origin": "http://malicious.com"}, | ||
| ) | ||
|
|
||
| assert response.status_code == 404 | ||
| assert response.json() == {"success": False, "error": "RESOURCE_NOT_FOUND"} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.