|
| 1 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +# License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 3 | +# You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | +import fnmatch |
| 5 | +import functools |
| 6 | + |
| 7 | +from pyramid.settings import asbool |
| 8 | + |
| 9 | + |
| 10 | +CORS_PARAMETERS = ( |
| 11 | + "cors_headers", |
| 12 | + "cors_enabled", |
| 13 | + "cors_origins", |
| 14 | + "cors_credentials", |
| 15 | + "cors_max_age", |
| 16 | + "cors_expose_all_headers", |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +def get_cors_preflight_view(service): |
| 21 | + """Return a view for the OPTION method. |
| 22 | +
|
| 23 | + Checks that the User-Agent is authorized to do a request to the server, and |
| 24 | + to this particular service, and add the various checks that are specified |
| 25 | + in http://www.w3.org/TR/cors/#resource-processing-model. |
| 26 | + """ |
| 27 | + |
| 28 | + def _preflight_view(request): |
| 29 | + response = request.response |
| 30 | + origin = request.headers.get("Origin") |
| 31 | + supported_headers = service.cors_supported_headers_for() |
| 32 | + |
| 33 | + if not origin: |
| 34 | + request.errors.add("header", "Origin", "this header is mandatory") |
| 35 | + |
| 36 | + requested_method = request.headers.get("Access-Control-Request-Method") |
| 37 | + if not requested_method: |
| 38 | + request.errors.add( |
| 39 | + "header", "Access-Control-Request-Method", "this header is mandatory" |
| 40 | + ) |
| 41 | + |
| 42 | + if not (requested_method and origin): |
| 43 | + return |
| 44 | + |
| 45 | + requested_headers = request.headers.get("Access-Control-Request-Headers", ()) |
| 46 | + |
| 47 | + if requested_headers: |
| 48 | + requested_headers = map(str.strip, requested_headers.split(",")) |
| 49 | + |
| 50 | + if requested_method not in service.cors_supported_methods: |
| 51 | + request.errors.add("header", "Access-Control-Request-Method", "Method not allowed") |
| 52 | + |
| 53 | + if not service.cors_expose_all_headers: |
| 54 | + for h in requested_headers: |
| 55 | + if h.lower() not in [s.lower() for s in supported_headers]: |
| 56 | + request.errors.add( |
| 57 | + "header", "Access-Control-Request-Headers", 'Header "%s" not allowed' % h |
| 58 | + ) |
| 59 | + |
| 60 | + supported_headers = set(supported_headers) | set(requested_headers) |
| 61 | + |
| 62 | + response.headers["Access-Control-Allow-Headers"] = ",".join(supported_headers) |
| 63 | + |
| 64 | + response.headers["Access-Control-Allow-Methods"] = ",".join(service.cors_supported_methods) |
| 65 | + |
| 66 | + max_age = service.cors_max_age_for(requested_method) |
| 67 | + if max_age is not None: |
| 68 | + response.headers["Access-Control-Max-Age"] = str(max_age) |
| 69 | + |
| 70 | + return None |
| 71 | + |
| 72 | + return _preflight_view |
| 73 | + |
| 74 | + |
| 75 | +def _get_method(request): |
| 76 | + """Return what's supposed to be the method for CORS operations. |
| 77 | + (e.g if the verb is options, look at the A-C-Request-Method header, |
| 78 | + otherwise return the HTTP verb). |
| 79 | + """ |
| 80 | + if request.method == "OPTIONS": |
| 81 | + method = request.headers.get("Access-Control-Request-Method", request.method) |
| 82 | + else: |
| 83 | + method = request.method |
| 84 | + return method |
| 85 | + |
| 86 | + |
| 87 | +def ensure_origin(service, request, response=None, **kwargs): |
| 88 | + """Ensure that the origin header is set and allowed.""" |
| 89 | + response = response or request.response |
| 90 | + |
| 91 | + # Don't check this twice. |
| 92 | + if not request.info.get("cors_checked", False): |
| 93 | + method = _get_method(request) |
| 94 | + |
| 95 | + origin = request.headers.get("Origin") |
| 96 | + |
| 97 | + if not origin: |
| 98 | + always_cors = asbool(request.registry.settings.get("cornice.always_cors")) |
| 99 | + # With this setting, if the service origins has "*", then |
| 100 | + # always return CORS headers. |
| 101 | + origins = getattr(service, "cors_origins", []) |
| 102 | + if always_cors and "*" in origins: |
| 103 | + origin = "*" |
| 104 | + |
| 105 | + if origin: |
| 106 | + if not any([fnmatch.fnmatchcase(origin, o) for o in service.cors_origins_for(method)]): |
| 107 | + request.errors.add("header", "Origin", "%s not allowed" % origin) |
| 108 | + elif service.cors_support_credentials_for(method): |
| 109 | + response.headers["Access-Control-Allow-Origin"] = origin |
| 110 | + else: |
| 111 | + if any([o == "*" for o in service.cors_origins_for(method)]): |
| 112 | + response.headers["Access-Control-Allow-Origin"] = "*" |
| 113 | + else: |
| 114 | + response.headers["Access-Control-Allow-Origin"] = origin |
| 115 | + request.info["cors_checked"] = True |
| 116 | + return response |
| 117 | + |
| 118 | + |
| 119 | +def get_cors_validator(service): |
| 120 | + return functools.partial(ensure_origin, service) |
| 121 | + |
| 122 | + |
| 123 | +def apply_cors_post_request(service, request, response): |
| 124 | + """Handles CORS-related post-request things. |
| 125 | +
|
| 126 | + Add some response headers, such as the Expose-Headers and the |
| 127 | + Allow-Credentials ones. |
| 128 | + """ |
| 129 | + response = ensure_origin(service, request, response) |
| 130 | + method = _get_method(request) |
| 131 | + |
| 132 | + if ( |
| 133 | + service.cors_support_credentials_for(method) |
| 134 | + and "Access-Control-Allow-Credentials" not in response.headers |
| 135 | + ): |
| 136 | + response.headers["Access-Control-Allow-Credentials"] = "true" |
| 137 | + |
| 138 | + if request.method != "OPTIONS": |
| 139 | + # Which headers are exposed? |
| 140 | + supported_headers = service.cors_supported_headers_for(request.method) |
| 141 | + if supported_headers: |
| 142 | + response.headers["Access-Control-Expose-Headers"] = ", ".join(supported_headers) |
| 143 | + |
| 144 | + return response |
0 commit comments