diff --git a/python_multipart/multipart.py b/python_multipart/multipart.py index ace4a8f..1382f5c 100644 --- a/python_multipart/multipart.py +++ b/python_multipart/multipart.py @@ -1102,10 +1102,17 @@ def data_callback(name: CallbackName, end_i: int, remaining: bool = False) -> No c = data[i] if state == MultipartState.START: + # Stop parsing if there is no boundary within the first chunk + if i == 16: + msg = "Too much junk in front of first boundary (%d)" % (i,) + self.logger.warning(msg) + e = MultipartParseError(msg) + e.offset = i + raise e + # Skip leading newlines if c == CR or c == LF: i += 1 - self.logger.debug("Skipping leading CR/LF at %d", i) continue # index is used as in index into our boundary. Set to 0. @@ -1398,9 +1405,9 @@ def data_callback(name: CallbackName, end_i: int, remaining: bool = False) -> No i -= 1 elif state == MultipartState.END: - # Do nothing and just consume a byte in the end state. - if c not in (CR, LF): - self.logger.warning("Consuming a byte '0x%x' in the end state", c) # pragma: no cover + # Skip junk after the last boundary + i = length + break else: # pragma: no cover (error case) # We got into a strange state somehow! Just stop processing.