diff --git a/proxy/cf_h2.py b/proxy/cf_h2.py index 42c379d..e22614b 100644 --- a/proxy/cf_h2.py +++ b/proxy/cf_h2.py @@ -21,6 +21,7 @@ from .utils import ( log = logging.getLogger('tg-mtproto-proxy') MAX_PACKET = 4 * 1024 * 1024 +MAX_PLAIN_PADDING = 256 MAX_CHANNEL_REQUESTS = 8 MAX_CHANNEL_BYTES = 8 * 1024 * 1024 MAX_LANE_REQUESTS = 64 @@ -116,16 +117,20 @@ async def _read_packet(reader, decryptor, tag: bytes) -> Tuple[bytes, bool]: else: value, = struct.unpack(' 15: - raise ValueError('invalid padded native packet') + max_padding = 15 + padding = length - packet_length + if not 24 <= packet_length <= length or padding > max_padding: + raise ValueError('invalid padded native packet: frame=%d packet=%d padding=%d limit=%d' % ( + length, packet_length, padding, max_padding)) body = body[:packet_length] if len(body) % 4 or len(body) > MAX_PACKET: raise ValueError('invalid native packet alignment or size') diff --git a/tests/test_cf_h2.py b/tests/test_cf_h2.py index 4b7c0d1..6e48894 100644 --- a/tests/test_cf_h2.py +++ b/tests/test_cf_h2.py @@ -56,6 +56,47 @@ class NativeFramingTest(unittest.IsolatedAsyncioTestCase): self.assertEqual(decoded, body, padding) self.assertFalse(quick) + async def test_plaintext_accepts_extended_padding_used_by_apple_and_android(self): + body = b'\x00' * 8 + b'm' * 8 + struct.pack('