aio-libs / aio-libs/aiohttp

Add support of PROXY protocol for TLS (and without as well) web server

Đang mở
#11,252 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
enhancement
Ngôn ngữ chính
Python
Star
16.5k
Fork
2.4k
Merge trung bình
17 giờ 22 phút
Pull request đã merge (30 ngày)
212

Mô tả

Sometimes it's required to pass TLS connections through a balancer without terminating TLS there.

https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt

Support of such protocol is easy. Although some security measures should be applied. For example, users of AioHTTP should explicitly mark somehow that PROXY protocol expected. Without this (i.e. autodetection) malicious user may send PROXY protocol to alter source ip information.

```python
class _SSLProtocolProxyMonkeyPatch(asyncio.sslproto.SSLProtocol):
def __init__(self, *args, **kwargs) -> None:
self._probe_buffer = bytearray()
self._ssl_buffer: bytearray
self._ssl_buffer_view: memoryview
super().__init__(*args, **kwargs)
self._original_buffer_updated = super().buffer_updated

@override
def buffer_updated(self, nbytes: int) -> None: # pylint: disable=method-hidden
self._probe_buffer.extend(self._ssl_buffer_view[:nbytes])
if len(self._probe_buffer) > 5 and b'PROXY' not in self._probe_buffer:
self.__restore_original(self._probe_buffer)
return

# Handles textual protocol .There is another version of PROXY protocol - binary. it has fixed record length
if b'\r\n' not in self._probe_buffer:
if len(self._probe_buffer) <= 108: # 108 is maximal length for IPv4, make it better
return
raise RuntimeError(f'PROXY header is too long: {len(self._probe_buffer)} bytes, expected at most 108 bytes.')

(header, data) = self._probe_buffer.split(b'\r\n', maxsplit=1)
unused_proxy, protocol, source_ip, unused_destination_ip, source_port, unused_destination_port = header.decode('ascii').strip().split()
# TODO: check protocol value, i.e. TCP4 for example
self._extra['peername'] = (source_ip, int(source_port))
self.__restore_original(data)

def __restore_original(self, data: bytearray) -> None:
self.buffer_updated = self._original_buffer_updated # type: ignore[method-assign]
delattr(self, '_original_buffer_updated')
delattr(self, '_probe_buffer')
if data:
self._ssl_buffer[: len(data)] = data
self.buffer_updated(len(data))

asyncio.sslproto.SSLProtocol = _SSLProtocolProxyMonkeyPatch # type: ignore[misc]
```

This is how we monkey-patched. Normal fix is expected.

### Related component

Server

### Code of Conduct

- [x] I agree to follow the aio-libs Code of Conduct

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.