|
4 | 4 | from mitmproxy.net.http.http1.read import ( |
5 | 5 | read_request_head, |
6 | 6 | read_response_head, connection_close, expected_http_body_size, |
7 | | - _read_request_line, _read_response_line, _read_headers, get_header_tokens |
| 7 | + _read_request_line, _read_response_line, _read_headers, get_header_tokens, validate_headers |
8 | 8 | ) |
9 | 9 | from mitmproxy.test.tutils import treq, tresp |
10 | 10 |
|
@@ -59,6 +59,19 @@ def test_read_response_head(): |
59 | 59 | assert r.content is None |
60 | 60 |
|
61 | 61 |
|
| 62 | +def test_validate_headers(): |
| 63 | + # both content-length and chunked (possible request smuggling) |
| 64 | + with pytest.raises(ValueError, match="Received both a Transfer-Encoding and a Content-Length header"): |
| 65 | + validate_headers( |
| 66 | + Headers(transfer_encoding="chunked", content_length="42"), |
| 67 | + ) |
| 68 | + |
| 69 | + with pytest.raises(ValueError, match="Received an invalid header name"): |
| 70 | + validate_headers( |
| 71 | + Headers([(b"content-length ", b"42")]), |
| 72 | + ) |
| 73 | + |
| 74 | + |
62 | 75 | def test_expected_http_body_size(): |
63 | 76 | # Expect: 100-continue |
64 | 77 | assert expected_http_body_size( |
@@ -91,11 +104,6 @@ def test_expected_http_body_size(): |
91 | 104 | assert expected_http_body_size( |
92 | 105 | treq(headers=Headers(transfer_encoding="gzip,\tchunked")), |
93 | 106 | ) is None |
94 | | - # both content-length and chunked (possible request smuggling) |
95 | | - with pytest.raises(ValueError, match="Received both a Transfer-Encoding and a Content-Length header"): |
96 | | - expected_http_body_size( |
97 | | - treq(headers=Headers(transfer_encoding="chunked", content_length="42")), |
98 | | - ) |
99 | 107 | with pytest.raises(ValueError, match="Invalid transfer encoding"): |
100 | 108 | expected_http_body_size( |
101 | 109 | treq(headers=Headers(transfer_encoding="chun\u212Aed")), # "chunKed".lower() == "chunked" |
|
0 commit comments