Annotation of pkgsrc/lang/python27/patches/patch-Lib_urlparse.py, revision 1.3
1.3 ! gutterid 1: $NetBSD: patch-Lib_urlparse.py,v 1.2 2021/10/27 23:58:55 gutteridge Exp $
1.1 gutterid 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:
1.3 ! gutterid 7: Fix CVE-2022-0391: urlparse does not sanitize URLs containing ASCII newline and tabs
! 8: Via Fedora:
! 9: https://src.fedoraproject.org/rpms/python2.7/raw/40dd05e5d77dbfa81777c9f84b704bc2239bf710/f/00377-CVE-2022-0391.patch
! 10:
1.1 gutterid 11: --- Lib/urlparse.py.orig 2020-04-19 21:13:39.000000000 +0000
12: +++ Lib/urlparse.py
13: @@ -29,6 +29,7 @@ test_urlparse.py provides a good indicat
14: """
15:
16: import re
17: +import os
18:
19: __all__ = ["urlparse", "urlunparse", "urljoin", "urldefrag",
20: "urlsplit", "urlunsplit", "parse_qs", "parse_qsl"]
1.3 ! gutterid 21: @@ -62,6 +63,9 @@ scheme_chars = ('abcdefghijklmnopqrstuvw
! 22: '0123456789'
! 23: '+-.')
! 24:
! 25: +# Unsafe bytes to be removed per WHATWG spec
! 26: +_UNSAFE_URL_BYTES_TO_REMOVE = ['\t', '\r', '\n']
! 27: +
! 28: MAX_CACHE_SIZE = 20
! 29: _parse_cache = {}
! 30:
! 31: @@ -184,12 +188,19 @@ def _checknetloc(netloc):
! 32: "under NFKC normalization"
! 33: % netloc)
! 34:
! 35: +def _remove_unsafe_bytes_from_url(url):
! 36: + for b in _UNSAFE_URL_BYTES_TO_REMOVE:
! 37: + url = url.replace(b, "")
! 38: + return url
! 39: +
! 40: def urlsplit(url, scheme='', allow_fragments=True):
! 41: """Parse a URL into 5 components:
! 42: <scheme>://<netloc>/<path>?<query>#<fragment>
! 43: Return a 5-tuple: (scheme, netloc, path, query, fragment).
! 44: Note that we don't break the components up in smaller bits
! 45: (e.g. netloc is a single string) and we don't expand % escapes."""
! 46: + url = _remove_unsafe_bytes_from_url(url)
! 47: + scheme = _remove_unsafe_bytes_from_url(scheme)
! 48: allow_fragments = bool(allow_fragments)
! 49: key = url, scheme, allow_fragments, type(url), type(scheme)
! 50: cached = _parse_cache.get(key, None)
! 51: @@ -382,7 +393,8 @@ def unquote(s):
1.1 gutterid 52: append(item)
53: return ''.join(res)
54:
55: -def parse_qs(qs, keep_blank_values=0, strict_parsing=0, max_num_fields=None):
56: +def parse_qs(qs, keep_blank_values=0, strict_parsing=0, max_num_fields=None,
57: + separator=None):
58: """Parse a query given as a string argument.
59:
60: Arguments:
1.3 ! gutterid 61: @@ -405,14 +417,23 @@ def parse_qs(qs, keep_blank_values=0, st
1.1 gutterid 62: """
63: dict = {}
64: for name, value in parse_qsl(qs, keep_blank_values, strict_parsing,
65: - max_num_fields):
66: + max_num_fields, separator):
67: if name in dict:
68: dict[name].append(value)
69: else:
70: dict[name] = [value]
71: return dict
72:
73: -def parse_qsl(qs, keep_blank_values=0, strict_parsing=0, max_num_fields=None):
74: +class _QueryStringSeparatorWarning(RuntimeWarning):
75: + """Warning for using default `separator` in parse_qs or parse_qsl"""
76: +
77: +# The default "separator" for parse_qsl can be specified in a config file.
78: +# It's cached after first read.
1.2 gutterid 79: +_QS_SEPARATOR_CONFIG_FILENAME = '@PKG_SYSCONFDIR@/python/urllib.cfg'
1.1 gutterid 80: +_default_qs_separator = None
81: +
82: +def parse_qsl(qs, keep_blank_values=0, strict_parsing=0, max_num_fields=None,
83: + separator=None):
84: """Parse a query given as a string argument.
85:
86: Arguments:
1.3 ! gutterid 87: @@ -434,15 +455,72 @@ def parse_qsl(qs, keep_blank_values=0, s
1.1 gutterid 88:
89: Returns a list, as G-d intended.
90: """
91: +
92: + if (not separator or (not isinstance(separator, (str, bytes)))) and separator is not None:
93: + raise ValueError("Separator must be of type string or bytes.")
94: +
95: + # Used when both "&" and ";" act as separators. (Need a non-string value.)
96: + _legacy = object()
97: +
98: + if separator is None:
99: + global _default_qs_separator
100: + separator = _default_qs_separator
101: + envvar_name = 'PYTHON_URLLIB_QS_SEPARATOR'
102: + if separator is None:
103: + # Set default separator from environment variable
104: + separator = os.environ.get(envvar_name)
105: + config_source = 'environment variable'
106: + if separator is None:
107: + # Set default separator from the configuration file
108: + try:
109: + file = open(_QS_SEPARATOR_CONFIG_FILENAME)
110: + except EnvironmentError:
111: + pass
112: + else:
113: + with file:
114: + import ConfigParser
115: + config = ConfigParser.ConfigParser()
116: + config.readfp(file)
117: + separator = config.get('parse_qs', envvar_name)
118: + _default_qs_separator = separator
119: + config_source = _QS_SEPARATOR_CONFIG_FILENAME
120: + if separator is None:
121: + # The default is '&', but warn if not specified explicitly
122: + if ';' in qs:
123: + from warnings import warn
124: + warn("The default separator of urlparse.parse_qsl and "
125: + + "parse_qs was changed to '&' to avoid a web cache "
126: + + "poisoning issue (CVE-2021-23336). "
127: + + "By default, semicolons no longer act as query field "
128: + + "separators. "
129: + + "See https://access.redhat.com/articles/5860431 for "
130: + + "more details.",
131: + _QueryStringSeparatorWarning, stacklevel=2)
132: + separator = '&'
133: + elif separator == 'legacy':
134: + separator = _legacy
135: + elif len(separator) != 1:
136: + raise ValueError(
137: + '{} (from {}) must contain '.format(envvar_name, config_source)
138: + + '1 character, or "legacy". See '
139: + + 'https://access.redhat.com/articles/5860431 for more details.'
140: + )
141: +
142: # If max_num_fields is defined then check that the number of fields
143: # is less than max_num_fields. This prevents a memory exhaustion DOS
144: # attack via post bodies with many fields.
145: if max_num_fields is not None:
146: - num_fields = 1 + qs.count('&') + qs.count(';')
147: + if separator is _legacy:
148: + num_fields = 1 + qs.count('&') + qs.count(';')
149: + else:
150: + num_fields = 1 + qs.count(separator)
151: if max_num_fields < num_fields:
152: raise ValueError('Max number of fields exceeded')
153:
154: - pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
155: + if separator is _legacy:
156: + pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
157: + else:
158: + pairs = [s1 for s1 in qs.split(separator)]
159: r = []
160: for name_value in pairs:
161: if not name_value and not strict_parsing:
CVSweb <webmaster@jp.NetBSD.org>