-
Notifications
You must be signed in to change notification settings - Fork 57
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
What is the best way to grant CORS for all subdomains dynamically? #245
Comments
As i see, for preflight CORS requests it is enough to return non-200 response to deny user request. It is possible to extend views from both class BaseHandler(aiohttp.web.View, aiohttp_cors.CorsViewMixin):
async def _get_config(self, request, origin, request_method):
# Handle pre-flight requests
if not origin_is_allowed(origin):
raise KeyError # would cause 403 Forbidden response
return super()._get_config(request, origin, request_method) As for non-preflight requests, to deny user request we should not add CORS headers and i don't see easy implementation. There is But it is internal object - it is hard-coded in It is possible to extend both CorsConfig and _CorsConfigImpl, add unified (both for preflight and for non-preflight requests) hook to _CorsConfigImpl._on_response_prepare. What do you think? What is the best way to implement additional origin checks? |
I think yes, overriding |
@asvetlov, thank you. And what is the correct way for preflight requests? Is the only way to implement checks - to extend |
Hmm, hacking |
Thank you for fast response. My first idea was to do something like that:
But later i found that it is much easier to extend
also, it is possible to configure aiohttp_cors with custom ResourcesUrlDispatcherRouterAdapter class easily:
That seem be not so ugly and does not use internal api. |
Extending adaptor looks better. If somebody wants to maintain the project -- you are welcome! |
I need to provide CORS permissions for many subdomains (i do not know all), that are located on known list of domains. E.g. *.example.com, *.corp-example.com, etc.
To allow CORS for any origin, i can do the following:
It works, but is not secure. What is simplest way to check if client origin meets some requirements (e.g. that origin matches
^[a-z0-9_-]+.example.com$
) and if does not - to deny request?I supposed that it is possible to extend some basic method to add such checks, or to provide lambda to
aiohttp_cors.setup
defaults, that would accept origin as input parameter and return appropriate config.The text was updated successfully, but these errors were encountered: