python / python/cpython

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

Offen
#156,275 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

stdlib topic-asyncio type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, die asyncio-Protokolldokumentation in asyncio-protocol.rst um Zeile 620 zu prüfen und das TLS BufferedProtocol-Beispiel aus dem Issue nachzustellen. Untersuche anschließend den verlinkten PR gh-156276 und die relevanten asyncio-SSL-Transporttests; abgeschlossen ist die Aufgabe, wenn bytearray-Puffer empfangene TLS-Daten bewahren und die Regression abgedeckt ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
networking
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.