|
@@ -32,9 +32,10 @@ _default = None
|
|
|
CONTEXT_SEPARATOR = "\x04"
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-ACCEPT_LANGUAGE_HEADER_MAX_LENGTH = 500
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+LANGUAGE_CODE_MAX_LENGTH = 500
|
|
|
|
|
|
|
|
|
|
|
@@ -498,11 +499,25 @@ def get_supported_language_variant(lang_code, strict=False):
|
|
|
If `strict` is False (the default), look for a country-specific variant
|
|
|
when neither the language code nor its generic variant is found.
|
|
|
|
|
|
+ The language code is truncated to a maximum length to avoid potential
|
|
|
+ denial of service attacks.
|
|
|
+
|
|
|
lru_cache should have a maxsize to prevent from memory exhaustion attacks,
|
|
|
as the provided language codes are taken from the HTTP request. See also
|
|
|
<https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.
|
|
|
"""
|
|
|
if lang_code:
|
|
|
+
|
|
|
+
|
|
|
+ if len(lang_code) > LANGUAGE_CODE_MAX_LENGTH:
|
|
|
+ if (
|
|
|
+ not strict
|
|
|
+ and (index := lang_code.rfind("-", 0, LANGUAGE_CODE_MAX_LENGTH)) > 0
|
|
|
+ ):
|
|
|
+
|
|
|
+ lang_code = lang_code[:index]
|
|
|
+ else:
|
|
|
+ raise ValueError("'lang_code' exceeds the maximum accepted length")
|
|
|
|
|
|
|
|
|
possible_lang_codes = [lang_code]
|
|
@@ -626,13 +641,13 @@ def parse_accept_lang_header(lang_string):
|
|
|
functools.lru_cache() to avoid repetitive parsing of common header values.
|
|
|
"""
|
|
|
|
|
|
- if len(lang_string) <= ACCEPT_LANGUAGE_HEADER_MAX_LENGTH:
|
|
|
+ if len(lang_string) <= LANGUAGE_CODE_MAX_LENGTH:
|
|
|
return _parse_accept_lang_header(lang_string)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- if (index := lang_string.rfind(",", 0, ACCEPT_LANGUAGE_HEADER_MAX_LENGTH)) > 0:
|
|
|
+ if (index := lang_string.rfind(",", 0, LANGUAGE_CODE_MAX_LENGTH)) > 0:
|
|
|
return _parse_accept_lang_header(lang_string[:index])
|
|
|
|
|
|
|