| 1 |
from aiohttp import hdrs, web |
| 2 |
|
| 3 |
|
| 4 |
_TRUE = "true" |
| 5 |
|
| 6 |
|
| 7 |
class _PreflightHandler: |
| 8 |
|
| 9 |
@staticmethod |
| 10 |
def _parse_request_method(request: web.Request): |
| 11 |
"""Parse Access-Control-Request-Method header of the preflight request""" |
| 12 |
method = request.headers.get(hdrs.ACCESS_CONTROL_REQUEST_METHOD) |
| 13 |
if method is None: |
| 14 |
raise web.HTTPForbidden( |
| 15 |
text="CORS preflight request failed: " |
| 16 |
"'Access-Control-Request-Method' header is not specified" |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
return method |
| 23 |
|
| 24 |
@staticmethod |
| 25 |
def _parse_request_headers(request: web.Request): |
| 26 |
"""Parse Access-Control-Request-Headers header or the preflight request |
| 27 |
|
| 28 |
Returns set of headers in upper case. |
| 29 |
""" |
| 30 |
headers = request.headers.get(hdrs.ACCESS_CONTROL_REQUEST_HEADERS) |
| 31 |
if headers is None: |
| 32 |
return frozenset() |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
headers = (h.strip(" \t").upper() for h in headers.split(",")) |
| 39 |
|
| 40 |
return frozenset(filter(None, headers)) |
| 41 |
|
| 42 |
async def _get_config(self, request, origin, request_method): |
| 43 |
raise NotImplementedError() |
| 44 |
|
| 45 |
async def _preflight_handler(self, request: web.Request): |
| 46 |
"""CORS preflight request handler""" |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
origin = request.headers.get(hdrs.ORIGIN) |
| 51 |
if origin is None: |
| 52 |
|
| 53 |
raise web.HTTPForbidden( |
| 54 |
text="CORS preflight request failed: " |
| 55 |
"origin header is not specified in the request" |
| 56 |
) |
| 57 |
|
| 58 |
|
| 59 |
request_method = self._parse_request_method(request) |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
try: |
| 64 |
config = await self._get_config(request, origin, request_method) |
| 65 |
except KeyError: |
| 66 |
raise web.HTTPForbidden( |
| 67 |
text="CORS preflight request failed: " |
| 68 |
"request method {!r} is not allowed " |
| 69 |
"for {!r} origin".format(request_method, origin) |
| 70 |
) |
| 71 |
|
| 72 |
if not config: |
| 73 |
|
| 74 |
|
| 75 |
raise web.HTTPForbidden( |
| 76 |
text="CORS preflight request failed: " "no origins are allowed" |
| 77 |
) |
| 78 |
|
| 79 |
options = config.get(origin, config.get("*")) |
| 80 |
if options is None: |
| 81 |
|
| 82 |
|
| 83 |
raise web.HTTPForbidden( |
| 84 |
text="CORS preflight request failed: " |
| 85 |
"origin '{}' is not allowed".format(origin) |
| 86 |
) |
| 87 |
|
| 88 |
|
| 89 |
request_headers = self._parse_request_headers(request) |
| 90 |
|
| 91 |
|
| 92 |
if options.allow_headers == "*": |
| 93 |
pass |
| 94 |
else: |
| 95 |
disallowed_headers = request_headers - options.allow_headers |
| 96 |
if disallowed_headers: |
| 97 |
raise web.HTTPForbidden( |
| 98 |
text="CORS preflight request failed: " |
| 99 |
"headers are not allowed: {}".format(", ".join(disallowed_headers)) |
| 100 |
) |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
response = web.Response() |
| 107 |
|
| 108 |
|
| 109 |
response.headers[hdrs.ACCESS_CONTROL_ALLOW_ORIGIN] = origin |
| 110 |
if options.allow_credentials: |
| 111 |
|
| 112 |
response.headers[hdrs.ACCESS_CONTROL_ALLOW_CREDENTIALS] = _TRUE |
| 113 |
|
| 114 |
|
| 115 |
if options.max_age is not None: |
| 116 |
response.headers[hdrs.ACCESS_CONTROL_MAX_AGE] = str(options.max_age) |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
response.headers[hdrs.ACCESS_CONTROL_ALLOW_METHODS] = request_method |
| 122 |
|
| 123 |
|
| 124 |
if request_headers: |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
response.headers[hdrs.ACCESS_CONTROL_ALLOW_HEADERS] = ",".join( |
| 129 |
request_headers |
| 130 |
) |
| 131 |
|
| 132 |
return response |