-
Notifications
You must be signed in to change notification settings - Fork 149
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
Add Cross-Origin Resource Sharing (CORS) support. #98
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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,128 @@ | ||
import fnmatch | ||
|
||
|
||
CORS_PARAMETERS = ('cors_headers', 'cors_enabled', 'cors_origins', | ||
'cors_credentials', 'cors_max_age', | ||
'cors_expose_all_headers') | ||
|
||
|
||
def get_cors_preflight_view(service): | ||
"""Return a view for the OPTION method. | ||
|
||
Checks that the User-Agent is authorized to do a request to the server, and | ||
to this particular service, and add the various checks that are specified | ||
in http://www.w3.org/TR/cors/#resource-processing-model. | ||
""" | ||
|
||
def _preflight_view(request): | ||
response = request.response | ||
origin = request.headers.get('Origin') | ||
supported_headers = service.cors_supported_headers | ||
|
||
if not origin: | ||
request.errors.add('header', 'Origin', | ||
'this header is mandatory') | ||
|
||
requested_method = request.headers.get('Access-Control-Request-Method') | ||
if not requested_method: | ||
request.errors.add('header', 'Access-Control-Request-Method', | ||
'this header is mandatory') | ||
|
||
if not (requested_method and origin): | ||
return | ||
|
||
requested_headers = ( | ||
request.headers.get('Access-Control-Request-Headers', ())) | ||
|
||
if requested_headers: | ||
requested_headers = requested_headers.split(',') | ||
|
||
if requested_method not in service.cors_supported_methods: | ||
request.errors.add('header', 'Access-Control-Request-Method', | ||
'Method not allowed') | ||
|
||
if not service.cors_expose_all_headers: | ||
for h in requested_headers: | ||
if not h.lower() in [s.lower() for s in supported_headers]: | ||
request.errors.add( | ||
'header', | ||
'Access-Control-Request-Headers', | ||
'Header "%s" not allowed' % h) | ||
|
||
supported_headers = set(supported_headers) | set(requested_headers) | ||
|
||
response.headers['Access-Control-Allow-Headers'] = ( | ||
','.join(supported_headers)) | ||
|
||
response.headers['Access-Control-Allow-Methods'] = ( | ||
','.join(service.cors_supported_methods)) | ||
|
||
max_age = service.cors_max_age_for(requested_method) | ||
if max_age is not None: | ||
response.headers['Access-Control-Max-Age'] = str(max_age) | ||
|
||
return 'ok' | ||
return _preflight_view | ||
|
||
|
||
def _get_method(request): | ||
"""Return what's supposed to be the method for CORS operations. | ||
(e.g if the verb is options, look at the A-C-Request-Method header, | ||
otherwise return the HTTP verb). | ||
""" | ||
if request.method == 'OPTIONS': | ||
method = request.headers.get('Access-Control-Request-Method', | ||
request.method) | ||
else: | ||
method = request.method | ||
return method | ||
|
||
|
||
def get_cors_validator(service): | ||
"""Create a cornice validator to handle CORS-related verifications. | ||
|
||
Checks, if an "Origin" header is present, that the origin is authorized | ||
(and issue an error if not) | ||
""" | ||
|
||
def _cors_validator(request): | ||
response = request.response | ||
method = _get_method(request) | ||
|
||
# If we have an "Origin" header, check it's authorized and add the | ||
# response headers accordingly. | ||
origin = request.headers.get('Origin') | ||
if origin: | ||
if not any([fnmatch.fnmatchcase(origin, o) | ||
for o in service.cors_origins_for(method)]): | ||
request.errors.add('header', 'Origin', | ||
'%s not allowed' % origin) | ||
else: | ||
response.headers['Access-Control-Allow-Origin'] = origin | ||
return _cors_validator | ||
|
||
|
||
def get_cors_filter(service): | ||
"""Create a cornice filter to handle CORS-related post-request | ||
things. | ||
|
||
Add some response headers, such as the Expose-Headers and the | ||
Allow-Credentials ones. | ||
""" | ||
|
||
def _cors_filter(response, request): | ||
method = _get_method(request) | ||
|
||
if (service.cors_support_credentials(method) and | ||
not 'Access-Control-Allow-Credentials' in response.headers): | ||
response.headers['Access-Control-Allow-Credentials'] = 'true' | ||
|
||
if request.method is not 'OPTIONS': | ||
# Which headers are exposed? | ||
supported_headers = service.cors_supported_headers | ||
if supported_headers: | ||
response.headers['Access-Control-Expose-Headers'] = ( | ||
', '.join(supported_headers)) | ||
|
||
return response | ||
return _cors_filter |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a larger architectural thought, I wonder if we should pass the service object into each validator by default. IOW, make the signature of a validator function "validate(service, request)" or similar. How common is it to make a validator that closes over the service object like this?