Skip to content

Commit

Permalink
Remove extraneous calls to .strip() in Chunked Encoding
Browse files Browse the repository at this point in the history
To be valid chunked encoding we should not be removing any whitespace as
the standard does not allow for optional whitespace.

If whitespace is encountered in the wrong place, it should lead to a 400
Bad Request instead.
  • Loading branch information
digitalresistor committed Mar 13, 2022
1 parent d9bdfa0 commit bd22869
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
6 changes: 1 addition & 5 deletions src/waitress/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ def received(self, s):
line = s[:pos]
s = s[pos + 2 :]
self.control_line = b""
line = line.strip()

if line:
# Begin a new chunk.
Expand All @@ -153,9 +152,6 @@ def received(self, s):

line = line[:semi]

# Remove any whitespace
line = line.strip()

if not ONLY_HEXDIG_RE.match(line):
self.error = BadRequest("Invalid chunk size")
self.all_chunks_received = True
Expand All @@ -164,7 +160,7 @@ def received(self, s):

# Can not fail due to matching against the regular
# expression above
sz = int(line.strip(), 16) # hexadecimal
sz = int(line, 16) # hexadecimal

if sz > 0:
# Start a new chunk.
Expand Down
4 changes: 3 additions & 1 deletion tests/test_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,9 @@ def test_received_valid_extensions(self, valid_extension):
assert result == len(data)
assert inst.error == None

@pytest.mark.parametrize("invalid_size", [b"0x04", b"+0x04", b"x04", b"+04"])
@pytest.mark.parametrize(
"invalid_size", [b"0x04", b"+0x04", b"x04", b"+04", b" 04", b" 0x04"]
)
def test_received_invalid_size(self, invalid_size):
from waitress.utilities import BadRequest

Expand Down

0 comments on commit bd22869

Please sign in to comment.