Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c7e0151

Browse files
ngnpopefelixxm
authored andcommitted
[3.2.x] Fixed CVE-2023-23969 -- Prevented DoS with pathological values for Accept-Language.
The parsed values of Accept-Language headers are cached in order to avoid repetitive parsing. This leads to a potential denial-of-service vector via excessive memory usage if the raw value of Accept-Language headers is very large. Accept-Language headers are now limited to a maximum length in order to avoid this issue.
1 parent 9da4634 commit c7e0151

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

django/utils/translation/trans_real.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
# magic gettext number to separate context from message
3131
CONTEXT_SEPARATOR = "\x04"
3232

33+
# Maximum number of characters that will be parsed from the Accept-Language
34+
# header to prevent possible denial of service or memory exhaustion attacks.
35+
# About 10x longer than the longest value shown on MDN’s Accept-Language page.
36+
ACCEPT_LANGUAGE_HEADER_MAX_LENGTH = 500
37+
3338
# Format of Accept-Language header values. From RFC 2616, section 14.4 and 3.9
3439
# and RFC 3066, section 2.1
3540
accept_language_re = _lazy_re_compile(r'''
@@ -556,7 +561,7 @@ def get_language_from_request(request, check_path=False):
556561

557562

558563
@functools.lru_cache(maxsize=1000)
559-
def parse_accept_lang_header(lang_string):
564+
def _parse_accept_lang_header(lang_string):
560565
"""
561566
Parse the lang_string, which is the body of an HTTP Accept-Language
562567
header, and return a tuple of (lang, q-value), ordered by 'q' values.
@@ -578,3 +583,28 @@ def parse_accept_lang_header(lang_string):
578583
result.append((lang, priority))
579584
result.sort(key=lambda k: k[1], reverse=True)
580585
return tuple(result)
586+
587+
588+
def parse_accept_lang_header(lang_string):
589+
"""
590+
Parse the value of the Accept-Language header up to a maximum length.
591+
592+
The value of the header is truncated to a maximum length to avoid potential
593+
denial of service and memory exhaustion attacks. Excessive memory could be
594+
used if the raw value is very large as it would be cached due to the use of
595+
functools.lru_cache() to avoid repetitive parsing of common header values.
596+
"""
597+
# If the header value doesn't exceed the maximum allowed length, parse it.
598+
if len(lang_string) <= ACCEPT_LANGUAGE_HEADER_MAX_LENGTH:
599+
return _parse_accept_lang_header(lang_string)
600+
601+
# If there is at least one comma in the value, parse up to the last comma
602+
# before the max length, skipping any truncated parts at the end of the
603+
# header value.
604+
index = lang_string.rfind(",", 0, ACCEPT_LANGUAGE_HEADER_MAX_LENGTH)
605+
if index > 0:
606+
return _parse_accept_lang_header(lang_string[:index])
607+
608+
# Don't attempt to parse if there is only one language-range value which is
609+
# longer than the maximum allowed length and so truncated.
610+
return ()

docs/releases/3.2.17.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,12 @@ Django 3.2.17 release notes
66

77
Django 3.2.17 fixes a security issue with severity "moderate" in 3.2.16.
88

9-
...
9+
CVE-2023-23969: Potential denial-of-service via ``Accept-Language`` headers
10+
===========================================================================
11+
12+
The parsed values of ``Accept-Language`` headers are cached in order to avoid
13+
repetitive parsing. This leads to a potential denial-of-service vector via
14+
excessive memory usage if large header values are sent.
15+
16+
In order to avoid this vulnerability, the ``Accept-Language`` header is now
17+
parsed up to a maximum length.

tests/i18n/tests.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,14 @@ def test_parse_spec_http_header(self):
13521352
('de;q=0.', [('de', 0.0)]),
13531353
('en; q=1,', [('en', 1.0)]),
13541354
('en; q=1.0, * ; q=0.5', [('en', 1.0), ('*', 0.5)]),
1355+
(
1356+
'en' + '-x' * 20,
1357+
[('en-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x', 1.0)],
1358+
),
1359+
(
1360+
', '.join(['en; q=1.0'] * 20),
1361+
[('en', 1.0)] * 20,
1362+
),
13551363
# Bad headers
13561364
('en-gb;q=1.0000', []),
13571365
('en;q=0.1234', []),
@@ -1367,6 +1375,10 @@ def test_parse_spec_http_header(self):
13671375
('12-345', []),
13681376
('', []),
13691377
('en;q=1e0', []),
1378+
# Invalid as language-range value too long.
1379+
('xxxxxxxx' + '-xxxxxxxx' * 500, []),
1380+
# Header value too long, only parse up to limit.
1381+
(', '.join(['en; q=1.0'] * 500), [('en', 1.0)] * 45),
13701382
]
13711383
for value, expected in tests:
13721384
with self.subTest(value=value):

0 commit comments

Comments
 (0)