-
-
Notifications
You must be signed in to change notification settings - Fork 778
Fix CORS headers not set on exceptions #1821
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
22 commits
Select commit
Hold shift + click to select a range
a0f2647
Fix CORS headers not set on exceptions
nielsbox 1965f81
Added CORS test
nielsbox 3bac45d
Cleanup
nielsbox 1a43872
Add Tests
nielsbox b51fce6
Do negative test
nielsbox 9cc22cf
Formatting
nielsbox c6cd126
Fix negative test
nielsbox a8d13ec
Formatting
nielsbox bc3a4a6
Check of origin is returned correctly
nielsbox 39f5ad3
Formatting
nielsbox 9260db4
Formatting
nielsbox 46e7191
Fix ServerError Middleware
nielsbox 99bc046
Address Comments
nielsbox 1783021
Address comments
nielsbox f1f5c40
Linters
nielsbox 020bb8c
Linters
nielsbox 60791c2
Address comment
nielsbox 2702fd9
Address comment
nielsbox 0cb8648
Linters
nielsbox b10d4bc
Linters
nielsbox 4deb6cc
Small docs improvements
RobbeSneyders 918d58c
Small typing fixes
RobbeSneyders 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import logging | ||
import typing as t | ||
|
||
from starlette.middleware.errors import ( | ||
ServerErrorMiddleware as StarletteServerErrorMiddleware, | ||
) | ||
from starlette.types import ASGIApp | ||
|
||
from connexion.exceptions import InternalServerError | ||
from connexion.lifecycle import ConnexionRequest, ConnexionResponse | ||
from connexion.middleware.exceptions import connexion_wrapper | ||
from connexion.types import MaybeAwaitable | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class ServerErrorMiddleware(StarletteServerErrorMiddleware): | ||
Ruwann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Subclass of starlette ServerErrorMiddleware to change handling of Unhandled Server | ||
exceptions to existing connexion behavior.""" | ||
|
||
def __init__( | ||
self, | ||
next_app: ASGIApp, | ||
handler: t.Optional[ | ||
t.Callable[[ConnexionRequest, Exception], MaybeAwaitable[ConnexionResponse]] | ||
] = None, | ||
): | ||
handler = connexion_wrapper(handler) if handler else None | ||
super().__init__(next_app, handler=handler) | ||
|
||
@staticmethod | ||
@connexion_wrapper | ||
def error_response(_request: ConnexionRequest, exc: Exception) -> ConnexionResponse: | ||
"""Default handler for any unhandled Exception""" | ||
logger.error("%r", exc, exc_info=exc) | ||
return InternalServerError().to_problem() |
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
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,44 @@ | ||
import json | ||
|
||
|
||
def test_cors_valid(cors_openapi_app): | ||
app_client = cors_openapi_app.test_client() | ||
origin = "http://localhost" | ||
response = app_client.post("/v1.0/goodday/dan", data={}, headers={"Origin": origin}) | ||
assert response.status_code == 201 | ||
assert "Access-Control-Allow-Origin" in response.headers | ||
assert origin == response.headers["Access-Control-Allow-Origin"] | ||
|
||
|
||
def test_cors_invalid(cors_openapi_app): | ||
app_client = cors_openapi_app.test_client() | ||
response = app_client.options( | ||
"/v1.0/goodday/dan", | ||
headers={"Origin": "http://0.0.0.0", "Access-Control-Request-Method": "POST"}, | ||
) | ||
assert response.status_code == 400 | ||
assert "Access-Control-Allow-Origin" not in response.headers | ||
|
||
|
||
def test_cors_validation_error(cors_openapi_app): | ||
app_client = cors_openapi_app.test_client() | ||
origin = "http://localhost" | ||
response = app_client.post( | ||
"/v1.0/body-not-allowed-additional-properties", | ||
data={}, | ||
headers={"Origin": origin}, | ||
) | ||
assert response.status_code == 400 | ||
assert "Access-Control-Allow-Origin" in response.headers | ||
assert origin == response.headers["Access-Control-Allow-Origin"] | ||
|
||
|
||
def test_cors_server_error(cors_openapi_app): | ||
app_client = cors_openapi_app.test_client() | ||
origin = "http://localhost" | ||
response = app_client.post( | ||
"/v1.0/goodday/noheader", data={}, headers={"Origin": origin} | ||
) | ||
assert response.status_code == 500 | ||
assert "Access-Control-Allow-Origin" in response.headers | ||
assert origin == response.headers["Access-Control-Allow-Origin"] |
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.