Enhanced Trailing Whitespace Handling in HTTP Headers#3429
Enhanced Trailing Whitespace Handling in HTTP Headers#3429Nirab123456 wants to merge 14 commits intotornadoweb:masterfrom
Conversation
…verRequest and RequestHandler. Enhanced support for JSON, form-encoded, and multipart data, including file uploads. Updated unit tests to cover all scenarios, ensuring robust handling of requests.
|
@bdarnell can you please review my tests ?? |
bdarnell
left a comment
There was a problem hiding this comment.
This is a different approach than I described in #3321 (comment). Why?
My plan for testing this was to start with HTTPHeadersTest.test_multi_line to add more continuation line cases and ensuring that the final \r\n\r\n is right. Unit tests for parse_line are good too, though.
@bdarnell In response to: Discussion & Pull Request ReviewI have updated the handling of the In this revised implementation:
This update resolves several shortcomings in Tornado’s prior implementation by correctly handling edge cases that were previously unaddressed. Specifically, the original code had the following issues in tests:
The updated code now adheres to RFC 7230 specifications, ensuring these cases are handled correctly and improving Tornado's robustness. Errors in the Original Code:
Updated Test Code: def test_multiple_content_length_headers(self):
headers = HTTPHeaders()
headers.parse_line("Content-Length: 123")
headers.parse_line("Content-Length: 123")
self.assertEqual(headers.get("content-length"), "123")
with self.assertRaises(HTTPInputError):
headers.parse_line("Content-Length: 456") # Should raise an error due to conflicting values
def test_invalid_content_length(self):
headers = HTTPHeaders()
with self.assertRaises(HTTPInputError):
headers.parse_line("Content-Length: abc") # Should raise an error due to non-integer value
def test_negative_content_length(self):
headers = HTTPHeaders()
with self.assertRaises(HTTPInputError):
headers.parse_line("Content-Length: -123") # Should raise an error due to negative value
def test_leading_trailing_whitespace(self):
headers = HTTPHeaders()
headers.parse_line("Content-Length: 123 ")
self.assertEqual(headers.get('content-length'), '123') # Should handle trailing whitespace correctly
def test_zero_content_length(self):
headers = HTTPHeaders()
headers.parse_line("Content-Length: 0")
self.assertEqual(headers.get('content-length'), '0') # Should correctly handle zeroContext for
|
I don't think In any case, I may be getting mixed up but I don't know if modifying
I reiterate my earlier comment, which you didn't answer. This change leaves the problematic handling of whitespace in continuation lines alone, which can be a problem for more headers than content-length. |
|
I've got my own version of a fix for this in #3477 |


Screenshot of Test Results
Updated CodeOriginal CodeTest:
test_parse_line_with_trailing_spacesDescription:
The modifications made to the
parse_linemethod in theHTTPHeadersclass effectively resolve the issue of improper handling of trailing and leading whitespace in HTTP headers, specifically addressing a critical edge case involving theContent-Lengthheader. This issue was highlighted in a GitHub discussion (#3321), where it was noted that trailing spaces in the header value could lead to errors during processing, especially whenContent-Lengthis the last header in a request.How the Code Solves the Issue:
Robust Whitespace Management:
parse_lineremoves leading and trailing whitespace from the header line usingline.strip(). This ensures that any extra spaces do not affect the stored value, thus preventing potential formatting issues when accessing the header later.strip()prevents cases likeContent-Length: 0from storing an unintended value with trailing spaces.Specific Handling of
Content-Length:Content-Lengthheader and ensures that the value after the colon is correctly extracted and stripped of whitespace. For example, parsingContent-Length : 123would result in the correct value of'123', completely ignoring any spaces.42from a continuation line) or appending unintended spaces due to continuation line logic.Continuation Line Handling:
" ", the new logic ensures that this line is effectively ignored, thus maintaining the integrity of the previously stored header value.Error Prevention:
ValueErroroccurrences that arise from malformed headers. This makes the header processing more resilient and less prone to user errors in HTTP requests.Conclusion:
The enhancements made in the
parse_linemethod significantly improve the handling of HTTP headers, specifically addressing the issues related to trailing and leading whitespace in header values. By implementing a robust approach to whitespace management and providing specific handling for critical headers likeContent-Length, the code mitigates potential parsing errors and ensures adherence to HTTP standards, thereby improving overall functionality and reliability. This change effectively resolves the edge cases discussed in the issue, enhancing the usability of theHTTPHeadersclass in real-world applications.