asyncio SSL loses received data when `get_buffer()` returns a `bytearray`
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia esaminando la documentazione dei protocolli asyncio in asyncio-protocol.rst intorno alla riga 620 e riproduci l’esempio TLS BufferedProtocol dell’issue. Poi esamina la PR collegata gh-156276 e i test pertinenti del trasporto SSL di asyncio; il lavoro è completato quando i buffer bytearray preservano i dati TLS ricevuti e la regressione è coperta.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- networking
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 35/100