"""Regression: malformed Content-Length must be rejected before any body read.""" import sys from pathlib import Path sys.path.insert(0,str(Path(__file__).resolve().parents[1]/"src")) from email.message import Message from types import SimpleNamespace from unittest.mock import Mock from messenger.tls_proxy import HttpForwarder for lengths in (['-1'],['abc'],['5','6']): handler=object.__new__(HttpForwarder) handler.headers=Message() for value in lengths:handler.headers.add_header('Content-Length',value) handler.server=SimpleNamespace(identity='collector',rule={'target':1,'http':{'collector':[['POST','/otlp/']]}}) handler.command='POST';handler.path='/otlp/v1/logs' handler.rfile=SimpleNamespace(read=Mock(return_value=b'')) handler.send_error=Mock() handler.forward() assert handler.send_error.called,'malformed length was accepted' handler.rfile.read.assert_not_called() print('PASS: negative, nonnumeric and duplicate Content-Length rejected before reading')