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

Skip to content

Commit 9d7bd5a

Browse files
ngnpopefelixxm
authored andcommitted
[4.1.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 d3edac6 commit 9d7bd5a

File tree

5 files changed

+72
-5
lines changed

5 files changed

+72
-5
lines changed

django/utils/translation/trans_real.py

Lines changed: 30 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(
@@ -586,7 +591,7 @@ def get_language_from_request(request, check_path=False):
586591

587592

588593
@functools.lru_cache(maxsize=1000)
589-
def parse_accept_lang_header(lang_string):
594+
def _parse_accept_lang_header(lang_string):
590595
"""
591596
Parse the lang_string, which is the body of an HTTP Accept-Language
592597
header, and return a tuple of (lang, q-value), ordered by 'q' values.
@@ -608,3 +613,27 @@ def parse_accept_lang_header(lang_string):
608613
result.append((lang, priority))
609614
result.sort(key=lambda k: k[1], reverse=True)
610615
return tuple(result)
616+
617+
618+
def parse_accept_lang_header(lang_string):
619+
"""
620+
Parse the value of the Accept-Language header up to a maximum length.
621+
622+
The value of the header is truncated to a maximum length to avoid potential
623+
denial of service and memory exhaustion attacks. Excessive memory could be
624+
used if the raw value is very large as it would be cached due to the use of
625+
functools.lru_cache() to avoid repetitive parsing of common header values.
626+
"""
627+
# If the header value doesn't exceed the maximum allowed length, parse it.
628+
if len(lang_string) <= ACCEPT_LANGUAGE_HEADER_MAX_LENGTH:
629+
return _parse_accept_lang_header(lang_string)
630+
631+
# If there is at least one comma in the value, parse up to the last comma
632+
# before the max length, skipping any truncated parts at the end of the
633+
# header value.
634+
if (index := lang_string.rfind(",", 0, ACCEPT_LANGUAGE_HEADER_MAX_LENGTH)) > 0:
635+
return _parse_accept_lang_header(lang_string[:index])
636+
637+
# Don't attempt to parse if there is only one language-range value which is
638+
# longer than the maximum allowed length and so truncated.
639+
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.

docs/releases/4.0.9.txt

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

77
Django 4.0.9 fixes a security issue with severity "moderate" in 4.0.8.
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.

docs/releases/4.1.6.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,18 @@ Django 4.1.6 release notes
44

55
*February 1, 2023*
66

7-
Django 4.1.6 fixes a security issue with severity "moderate" and several bugs
8-
in 4.1.5.
7+
Django 4.1.6 fixes a security issue with severity "moderate" and a bug in
8+
4.1.5.
9+
10+
CVE-2023-23969: Potential denial-of-service via ``Accept-Language`` headers
11+
===========================================================================
12+
13+
The parsed values of ``Accept-Language`` headers are cached in order to avoid
14+
repetitive parsing. This leads to a potential denial-of-service vector via
15+
excessive memory usage if large header values are sent.
16+
17+
In order to avoid this vulnerability, the ``Accept-Language`` header is now
18+
parsed up to a maximum length.
919

1020
Bugfixes
1121
========

tests/i18n/tests.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,14 @@ def test_parse_spec_http_header(self):
17301730
("de;q=0.", [("de", 0.0)]),
17311731
("en; q=1,", [("en", 1.0)]),
17321732
("en; q=1.0, * ; q=0.5", [("en", 1.0), ("*", 0.5)]),
1733+
(
1734+
"en" + "-x" * 20,
1735+
[("en-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x", 1.0)],
1736+
),
1737+
(
1738+
", ".join(["en; q=1.0"] * 20),
1739+
[("en", 1.0)] * 20,
1740+
),
17331741
# Bad headers
17341742
("en-gb;q=1.0000", []),
17351743
("en;q=0.1234", []),
@@ -1746,6 +1754,10 @@ def test_parse_spec_http_header(self):
17461754
("", []),
17471755
("en;q=1e0", []),
17481756
("en-au;q=1.0", []),
1757+
# Invalid as language-range value too long.
1758+
("xxxxxxxx" + "-xxxxxxxx" * 500, []),
1759+
# Header value too long, only parse up to limit.
1760+
(", ".join(["en; q=1.0"] * 500), [("en", 1.0)] * 45),
17491761
]
17501762
for value, expected in tests:
17511763
with self.subTest(value=value):

0 commit comments

Comments
 (0)