Skip to content
Myr

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

2 files+30 added−7 removed

modified:requests/utils.py+9 added-4 removed
737 unmodified lines
738_CLEAN_HEADER_REGEX_BYTE = re.compile(b'^\\S[^\\r\\n]*$|^$')739_CLEAN_HEADER_REGEX_STR = re.compile(r'^\S[^\r\n]*$|^$')740741def 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.745746 :param header: tuple, in the format (name, value).747 """748 name, value = header
1 unmodified line
750 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)))760761def urldefragauth(url):762 """763 Given a url remove the fragment and the authentication part
10 unmodified lines
modified:tests/test_requests.py+21 added-3 removed
1140 unmodified lines
1141 # we go.1142 assert 'unicode' in p.headers.keys()1143 assert 'byte' in p.headers.keys()11441145 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']11531154 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']}11611162 # 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)11711172 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