Annotation of pkgsrc/lang/python27/patches/patch-Lib_urlparse.py, revision 1.1.2.3
1.1.2.3 ! tm 1: $NetBSD$
1.1.2.2 tm 2:
3: Fix CVE-2021-23336: Add `separator` argument to parse_qs; warn with default
4: Via Fedora:
5: https://src.fedoraproject.org/rpms/python2.7/blob/rawhide/f/00359-CVE-2021-23336.patch
6:
7: --- Lib/urlparse.py.orig 2020-04-19 21:13:39.000000000 +0000
8: +++ Lib/urlparse.py
9: @@ -29,6 +29,7 @@ test_urlparse.py provides a good indicat
10: """
11:
12: import re
13: +import os
14:
15: __all__ = ["urlparse", "urlunparse", "urljoin", "urldefrag",
16: "urlsplit", "urlunsplit", "parse_qs", "parse_qsl"]
17: @@ -382,7 +383,8 @@ def unquote(s):
18: append(item)
19: return ''.join(res)
20:
21: -def parse_qs(qs, keep_blank_values=0, strict_parsing=0, max_num_fields=None):
22: +def parse_qs(qs, keep_blank_values=0, strict_parsing=0, max_num_fields=None,
23: + separator=None):
24: """Parse a query given as a string argument.
25:
26: Arguments:
27: @@ -405,14 +407,23 @@ def parse_qs(qs, keep_blank_values=0, st
28: """
29: dict = {}
30: for name, value in parse_qsl(qs, keep_blank_values, strict_parsing,
31: - max_num_fields):
32: + max_num_fields, separator):
33: if name in dict:
34: dict[name].append(value)
35: else:
36: dict[name] = [value]
37: return dict
38:
39: -def parse_qsl(qs, keep_blank_values=0, strict_parsing=0, max_num_fields=None):
40: +class _QueryStringSeparatorWarning(RuntimeWarning):
41: + """Warning for using default `separator` in parse_qs or parse_qsl"""
42: +
43: +# The default "separator" for parse_qsl can be specified in a config file.
44: +# It's cached after first read.
1.1.2.3 ! tm 45: +_QS_SEPARATOR_CONFIG_FILENAME = '@PKG_SYSCONFDIR@/python/urllib.cfg'
1.1.2.2 tm 46: +_default_qs_separator = None
47: +
48: +def parse_qsl(qs, keep_blank_values=0, strict_parsing=0, max_num_fields=None,
49: + separator=None):
50: """Parse a query given as a string argument.
51:
52: Arguments:
53: @@ -434,15 +445,72 @@ def parse_qsl(qs, keep_blank_values=0, s
54:
55: Returns a list, as G-d intended.
56: """
57: +
58: + if (not separator or (not isinstance(separator, (str, bytes)))) and separator is not None:
59: + raise ValueError("Separator must be of type string or bytes.")
60: +
61: + # Used when both "&" and ";" act as separators. (Need a non-string value.)
62: + _legacy = object()
63: +
64: + if separator is None:
65: + global _default_qs_separator
66: + separator = _default_qs_separator
67: + envvar_name = 'PYTHON_URLLIB_QS_SEPARATOR'
68: + if separator is None:
69: + # Set default separator from environment variable
70: + separator = os.environ.get(envvar_name)
71: + config_source = 'environment variable'
72: + if separator is None:
73: + # Set default separator from the configuration file
74: + try:
75: + file = open(_QS_SEPARATOR_CONFIG_FILENAME)
76: + except EnvironmentError:
77: + pass
78: + else:
79: + with file:
80: + import ConfigParser
81: + config = ConfigParser.ConfigParser()
82: + config.readfp(file)
83: + separator = config.get('parse_qs', envvar_name)
84: + _default_qs_separator = separator
85: + config_source = _QS_SEPARATOR_CONFIG_FILENAME
86: + if separator is None:
87: + # The default is '&', but warn if not specified explicitly
88: + if ';' in qs:
89: + from warnings import warn
90: + warn("The default separator of urlparse.parse_qsl and "
91: + + "parse_qs was changed to '&' to avoid a web cache "
92: + + "poisoning issue (CVE-2021-23336). "
93: + + "By default, semicolons no longer act as query field "
94: + + "separators. "
95: + + "See https://access.redhat.com/articles/5860431 for "
96: + + "more details.",
97: + _QueryStringSeparatorWarning, stacklevel=2)
98: + separator = '&'
99: + elif separator == 'legacy':
100: + separator = _legacy
101: + elif len(separator) != 1:
102: + raise ValueError(
103: + '{} (from {}) must contain '.format(envvar_name, config_source)
104: + + '1 character, or "legacy". See '
105: + + 'https://access.redhat.com/articles/5860431 for more details.'
106: + )
107: +
108: # If max_num_fields is defined then check that the number of fields
109: # is less than max_num_fields. This prevents a memory exhaustion DOS
110: # attack via post bodies with many fields.
111: if max_num_fields is not None:
112: - num_fields = 1 + qs.count('&') + qs.count(';')
113: + if separator is _legacy:
114: + num_fields = 1 + qs.count('&') + qs.count(';')
115: + else:
116: + num_fields = 1 + qs.count(separator)
117: if max_num_fields < num_fields:
118: raise ValueError('Max number of fields exceeded')
119:
120: - pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
121: + if separator is _legacy:
122: + pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
123: + else:
124: + pairs = [s1 for s1 in qs.split(separator)]
125: r = []
126: for name_value in pairs:
127: if not name_value and not strict_parsing:
CVSweb <webmaster@jp.NetBSD.org>