Skip to content

Commit 7ae310c

Browse files
authored
Backwards Compatible Fix for CVE-2024-6221 (#363)
1 parent f25c6b2 commit 7ae310c

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

docs/configuration.rst

+14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ CORS_ALLOW_HEADERS (:py:class:`~typing.List` or :py:class:`str`)
2323
Headers to accept from the client.
2424
Headers in the :http:header:`Access-Control-Request-Headers` request header (usually part of the preflight OPTIONS request) matching headers in this list will be included in the :http:header:`Access-Control-Allow-Headers` response header.
2525

26+
CORS_ALLOW_PRIVATE_NETWORK (:py:class:`bool`)
27+
If True, the response header :http:header:`Access-Control-Allow-Private-Network`
28+
will be set with the value 'true' whenever the request header
29+
:http:header:`Access-Control-Request-Private-Network` has a value 'true'.
30+
31+
If False, the reponse header :http:header:`Access-Control-Allow-Private-Network`
32+
will be set with the value 'false' whenever the request header
33+
:http:header:`Access-Control-Request-Private-Network` has a value of 'true'.
34+
35+
If the request header :http:header:`Access-Control-Request-Private-Network` is
36+
not present or has a value other than 'true', the response header
37+
:http:header:`Access-Control-Allow-Private-Network` will not be set.
38+
2639
CORS_ALWAYS_SEND (:py:class:`bool`)
2740
Usually, if a request doesn't include an :http:header:`Origin` header, the client did not request CORS.
2841
This means we can ignore this request.
@@ -83,6 +96,7 @@ Default values
8396
~~~~~~~~~~~~~~
8497

8598
* CORS_ALLOW_HEADERS: "*"
99+
* CORS_ALLOW_PRIVATE_NETWORK: True
86100
* CORS_ALWAYS_SEND: True
87101
* CORS_AUTOMATIC_OPTIONS: True
88102
* CORS_EXPOSE_HEADERS: None

flask_cors/core.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
'CORS_MAX_AGE', 'CORS_SEND_WILDCARD',
3737
'CORS_AUTOMATIC_OPTIONS', 'CORS_VARY_HEADER',
3838
'CORS_RESOURCES', 'CORS_INTERCEPT_EXCEPTIONS',
39-
'CORS_ALWAYS_SEND']
39+
'CORS_ALWAYS_SEND', 'CORS_ALLOW_PRIVATE_NETWORK']
4040
# Attribute added to request object by decorator to indicate that CORS
4141
# was evaluated, in case the decorator and extension are both applied
4242
# to a view.
@@ -56,7 +56,8 @@
5656
vary_header=True,
5757
resources=r'/*',
5858
intercept_exceptions=True,
59-
always_send=True)
59+
always_send=True,
60+
allow_private_network=True)
6061

6162

6263
def parse_resources(resources):
@@ -186,7 +187,8 @@ def get_cors_headers(options, request_headers, request_method):
186187

187188
if ACL_REQUEST_HEADER_PRIVATE_NETWORK in request_headers \
188189
and request_headers.get(ACL_REQUEST_HEADER_PRIVATE_NETWORK) == 'true':
189-
headers[ACL_RESPONSE_PRIVATE_NETWORK] = 'true'
190+
allow_private_network = 'true' if options.get('allow_private_network') else 'false'
191+
headers[ACL_RESPONSE_PRIVATE_NETWORK] = allow_private_network
190192

191193
# This is a preflight request
192194
# http://www.w3.org/TR/cors/#resource-preflight-requests

flask_cors/extension.py

+16
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,22 @@ class CORS(object):
138138
139139
Default : True
140140
:type vary_header: bool
141+
142+
:param allow_private_network:
143+
If True, the response header `Access-Control-Allow-Private-Network`
144+
will be set with the value 'true' whenever the request header
145+
`Access-Control-Request-Private-Network` has a value 'true'.
146+
147+
If False, the reponse header `Access-Control-Allow-Private-Network`
148+
will be set with the value 'false' whenever the request header
149+
`Access-Control-Request-Private-Network` has a value of 'true'.
150+
151+
If the request header `Access-Control-Request-Private-Network` is
152+
not present or has a value other than 'true', the response header
153+
`Access-Control-Allow-Private-Network` will not be set.
154+
155+
Default : True
156+
:type allow_private_network: bool
141157
"""
142158

143159
def __init__(self, app=None, **kwargs):

flask_cors/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '4.0.1'
1+
__version__ = '4.0.2'

0 commit comments

Comments
 (0)