]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Add safelisted request headers to CORSMiddleware 880/head
authorVasilis Gerakaris <vasilis.gerakaris@navarino.gr>
Fri, 27 Mar 2020 15:02:28 +0000 (17:02 +0200)
committerVasilis Gerakaris <vasilis.gerakaris@navarino.gr>
Fri, 27 Mar 2020 15:25:27 +0000 (17:25 +0200)
See https://fetch.spec.whatwg.org/#cors-safelisted-request-header

starlette/middleware/cors.py

index 90ba18010032e8e00139bdca9f5dd96ce826261e..76c5814f35898e29616ae008216e1ebe99af2f8d 100644 (file)
@@ -7,6 +7,7 @@ from starlette.responses import PlainTextResponse, Response
 from starlette.types import ASGIApp, Message, Receive, Scope, Send
 
 ALL_METHODS = ("DELETE", "GET", "OPTIONS", "PATCH", "POST", "PUT")
+SAFELISTED_HEADERS = {"accept", "accept-language", "content-language", "content-type"}
 
 
 class CORSMiddleware:
@@ -48,7 +49,8 @@ class CORSMiddleware:
                 "Access-Control-Max-Age": str(max_age),
             }
         )
-        if allow_headers and "*" not in allow_headers:
+        allow_headers = SAFELISTED_HEADERS | set([h.lower for h in allow_headers])
+        if allow_headers and "*" not in allowed_headers:
             preflight_headers["Access-Control-Allow-Headers"] = ", ".join(allow_headers)
         if allow_credentials:
             preflight_headers["Access-Control-Allow-Credentials"] = "true"
@@ -56,7 +58,7 @@ class CORSMiddleware:
         self.app = app
         self.allow_origins = allow_origins
         self.allow_methods = allow_methods
-        self.allow_headers = [h.lower() for h in allow_headers]
+        self.allow_headers = allow_headers
         self.allow_all_origins = "*" in allow_origins
         self.allow_all_headers = "*" in allow_headers
         self.allow_origin_regex = compiled_allow_origin_regex