python / python/cpython

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

Abierto
#156,275 3 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

stdlib topic-asyncio type-bug
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza revisando la documentación de protocolos de asyncio en asyncio-protocol.rst alrededor de la línea 620 y reproduce el ejemplo de TLS BufferedProtocol del issue. Después inspecciona el PR enlazado gh-156276 y las pruebas relevantes del transporte SSL de asyncio; se considera terminado cuando los búferes bytearray conservan los datos TLS recibidos y la regresión está cubierta.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
networking
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.