python / python/cpython

asyncio SSL loses received data when `get_buffer()` returns a `bytearray`

Đang mở
#156,275 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

stdlib topic-asyncio type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Bug report

Bug description:

Over TLS, a BufferedProtocol returning a bytearray from get_buffer() gets NULs instead of data - with the byte count still reported as correct.

Repro (run from a CPython directory root):

import asyncio 
import ssl

D = "Lib/test/certdata/"

class Buffered(asyncio.BufferedProtocol):
    buf = bytearray(100)
    def get_buffer(self, hint): return self.buf
    def buffer_updated(self, n): print("BufferedProtocol:", bytes(self.buf[:n]))

class Plain(asyncio.Protocol):
    def data_received(self, data): print("Protocol        :", data)

async def h(r, w):
    w.write(b"A"*30); await w.drain(); w.write(b"B"*30)

async def main():
    srv_ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER); srv_ctx.load_cert_chain(D + "keycert3.pem")
    cli_ctx = ssl.create_default_context(cafile=D + "pycacert.pem")
    srv = await asyncio.start_server(h, "localhost", 0, ssl=srv_ctx)
    port = srv.sockets[0].getsockname()[1]
    loop = asyncio.get_running_loop()
    for proto in (Buffered, Plain):
        await loop.create_connection(proto, "localhost", port, ssl=cli_ctx)
        await asyncio.sleep(0.3)

asyncio.run(main())

The output would be, Protocol is here to have an example of proper behavior

BufferedProtocol: b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Protocol        : b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBBBB'

Doc says (asyncio-protocol.rst - 620)

get_buffer() must return an object implementing the buffer protocol.

Proposed fix is to take memoryview(buf) once before the loop and read into view[offset:]

I have a fix ready for that

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs
  • gh-156276

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

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

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách xem lại tài liệu giao thức asyncio tại asyncio-protocol.rst, khoảng dòng 620, và tái hiện ví dụ TLS BufferedProtocol từ issue. Sau đó kiểm tra PR được liên kết gh-156276 và các bài kiểm thử vận chuyển SSL asyncio liên quan; hoàn tất khi các bộ đệm bytearray giữ nguyên dữ liệu TLS đã nhận và hồi quy được kiểm thử.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
networking
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
35/100

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.