Visitar URL original
bpo-11671: add header validation from http.client to wsgiref.headers.Headers by epicfaace · Pull Request #15299 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20210308172043/https://github.com/python/cpython/pull/15299
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-11671: add header validation from http.client to wsgiref.headers.Headers #15299

Open
wants to merge 8 commits into
base: master
from

Conversation

@epicfaace
Copy link
Contributor

@epicfaace epicfaace commented Aug 15, 2019 •

Adds header validation to disallow invalid characters in header names and values. This also fixes https://bugs.python.org/issue28778

I based this off the initial patch in bpo-11671, with a few changes:

  • only check whether headers are valid upon class initialization or calling add_header
  • use regex's from http.client, so that it disallows spaces in header names too

I know that http.client's regex (added in bpo-22928, a112a8a) is not RFC compliant, but it seemed better to at least make both libraries have the same behavior with regards to header validation.

https://bugs.python.org/issue11671

if not _is_legal_header_name(header.encode('ascii')):
raise ValueError('Invalid header name %r' % (header,))
if _is_illegal_header_value(value.encode('ascii')):
raise ValueError('Invalid header value %r' % (value,))

This comment has been minimized.

@vstinner

vstinner Aug 19, 2019
Member

I would prefer to initialize self.headers to an empty list and then call "for header, value in headers: self.add_header(header, value)". Then remove self._convert_string_type(k) calls below.

This comment has been minimized.

@epicfaace

epicfaace Aug 19, 2019
Author Contributor

Added this. Why would we want to remove self._convert_string_type(k) calls though?

Lib/test/test_wsgiref.py Show resolved Hide resolved
@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Aug 19, 2019

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@epicfaace
Copy link
Contributor Author

@epicfaace epicfaace commented Aug 19, 2019

I have made the requested changes; please review again

@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Aug 19, 2019

Thanks for making the requested changes!

@vstinner: please review the changes made to this pull request.

blurb-it bot and others added 2 commits Aug 27, 2019
('ValidName', 'InvalidValue\r'),
('ValidName', 'InvalidValue\n'),
(b'InvalidName', 'ValidValue', AssertionError),
('ValidName', b'InvalidValue', AssertionError)

This comment has been minimized.

@vstinner

vstinner Aug 27, 2019
Member

I would prefer to get a TypeError rather than an AssertionError.

('ValidName', b'InvalidValue', AssertionError)
)

def test_add_header_validation(self):

This comment has been minimized.

@vstinner

vstinner Aug 27, 2019
Member

I suggest to merge the two tests a single test, to factorize code.

I suggest to remove the b'InvalidName' and b'InvalidValue' from validation_cases. Maybe create a second list, maybe put it directly in the test method to avoid with self.assertRaises(exception[0]) if len(exception) else self.assertRaisesRegex(ValueError, 'Invalid header'): complexity.

@@ -525,6 +525,40 @@ def testExtras(self):
'\r\n'
)

validation_cases = (

This comment has been minimized.

@vstinner

vstinner Aug 27, 2019
Member

Please add a comment mentioning bpo-11671.

Rather than an hardcoded list, why not building such list using a few invalid characters. For example for header names:

invalid_names = []
invalid_name_chars = ['\r', '\n', '\r\n', ':']
for invalid_chars in invalid_name_chars:
  invalid_names.append(f'{invalid_chars}InvalidName')
  invalid_names.append(f'Invalid{invalid_chars}Name')
  invalid_names.append(f'InvalidName{invalid_chars}')
invalid_names.append(' InvalidName')

Are whitespaces (" ") and colon (":") only invalid at the beginning of the name?

@@ -181,4 +183,10 @@ def add_header(self, _name, _value, **_params):
else:
v = self._convert_string_type(v)
parts.append(_formatparam(k.replace('_', '-'), v))
self._headers.append((self._convert_string_type(_name), "; ".join(parts)))
header = self._convert_string_type(_name)

This comment has been minimized.

@vstinner

vstinner Aug 27, 2019
Member

Can you rename the variable to header_name (or maybe name, I didn't check if it's already used)?

value = "; ".join(parts)
if not _is_legal_header_name(header.encode('ascii')):
raise ValueError('Invalid header name %r' % (header,))
if _is_illegal_header_value(value.encode('ascii')):

This comment has been minimized.

@vstinner

vstinner Aug 27, 2019
Member

http/client.py encodes the value to Latin1, not ASCII

('ValidName', 'InvalidValue\n'),
(b'InvalidName', 'ValidValue', AssertionError),
('ValidName', b'InvalidValue', AssertionError)
)

This comment has been minimized.

@vstinner

vstinner Aug 27, 2019
Member

Are there already tests for header names which cannot be encoded to ASCII? And tests for values not encodable to Latin1?

@bedevere-bot
Copy link

@bedevere-bot bedevere-bot commented Aug 27, 2019

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@csabella
Copy link
Contributor

@csabella csabella commented Feb 4, 2020

@epicfaace, please address the code review comments. Thank you!

@csabella
Copy link
Contributor

@csabella csabella commented May 23, 2020

@epicfaace ping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants