Defining header value type requirements and tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nate Prewitt · committed by Nate Prewitt · parent 7700eca · Browse files · 2 files·+30 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+30 added−7 removed
- tests:+21 added−3 removed
- other:+9 added−4 removed
Provenance
- Commit
be31a90- Tree
542025f- Author
Nate Prewitt <Nate.Prewitt@gmail.com>- Committer
Nate Prewitt <Nate.Prewitt@gmail.com>- Parent
7700eca
2 files changed+30 added−7 removed
modified:requests/utils.py+9 added-4 removed
737 unmodified lines738 _CLEAN_HEADER_REGEX_BYTE = re.compile(b'^\\S[^\\r\\n]*$|^$')739 _CLEAN_HEADER_REGEX_STR = re.compile(r'^\S[^\r\n]*$|^$')740 741 def check_header_validity(header):742- """Verifies that header value doesn't contain leading whitespace or743- return characters. This prevents unintended header injection.742+ """Verifies that header value is a string which doesn't contain 743+ leading whitespace or return characters. This prevents unintended744+ header injection.745 746 :param header: tuple, in the format (name, value).747 """748 name, value = header1 unmodified line750 if isinstance(value, bytes):751 pat = _CLEAN_HEADER_REGEX_BYTE752 else:753 pat = _CLEAN_HEADER_REGEX_STR754+ try:753- if not pat.match(value):754- raise InvalidHeader("Invalid return character or leading space in header: %s" % name)755+ if not pat.match(value):756+ raise InvalidHeader("Invalid return character or leading space in header: %s" % name)757+ except TypeError:758+ raise InvalidHeader("Header value %s must be of type str or bytes, " 759+ "not %s" % (value, type(value)))760 761 def urldefragauth(url):762 """763 Given a url remove the fragment and the authentication part10 unmodified linesmodified:tests/test_requests.py+21 added-3 removed
1140 unmodified lines1141 # we go.1142 assert 'unicode' in p.headers.keys()1143 assert 'byte' in p.headers.keys()1144 1145- def test_header_validation(self,httpbin):1145+ def test_header_validation(self, httpbin):1146 """Ensure prepare_headers regex isn't flagging valid header contents."""1147 headers_ok = {'foo': 'bar baz qux',1148- 'bar': '1',1148+ 'bar': u'fbbq'.encode('utf8'),1149 'baz': '',1150- 'qux': str.encode(u'fbbq')}1150+ 'qux': '1'}1151 r = requests.get(httpbin('get'), headers=headers_ok)1152 assert r.request.headers['foo'] == headers_ok['foo']1153 1154+ def test_header_value_not_str(self, httpbin):1155+ """Ensure the header value is of type string or bytes as1156+ per discussion in GH issue #33861157+ """1158+ headers_int = {'foo': 3}1159+ headers_dict = {'bar': {'foo':'bar'}}1160+ headers_list = {'baz': ['foo', 'bar']}1161+1162+ # Test for int1163+ with pytest.raises(InvalidHeader):1164+ r = requests.get(httpbin('get'), headers=headers_int)1165+ # Test for dict1166+ with pytest.raises(InvalidHeader):1167+ r = requests.get(httpbin('get'), headers=headers_dict)1168+ # Test for list1169+ with pytest.raises(InvalidHeader):1170+ r = requests.get(httpbin('get'), headers=headers_list)1171+1172 def test_header_no_return_chars(self, httpbin):1173 """Ensure that a header containing return character sequences raise an1174 exception. Otherwise, multiple headers are created from single string.1175 """616 unmodified lines