python / python/cpython

[ssl] Reading from non-blocking socket wrapped by SSLContext returns an empty bytes object in case if the client stays connected

未关闭
#125,637 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib topic-SSL type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

I run my client-server application using SSLContext. And during communication I faced with situation when I receive an empty bytes object from the socket on server side. But in Wireshark dump I can see that client is present and sends data. If I add some delay after I receive empty bytes and try read from socket again I get some data. My code properly works on python 3.8 but it fails on 3.12. Also I tested without ssl on 3.12 and it works fine.

# how the socket initiated:
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain(*ssl_context)
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind((host, port))
self.socket.listen(1)
self.socket = context.wrap_socket(self.socket, server_side=True)


# how I accept client:
while not True:
    self.socket.settimeout(1)
    try:
        self.connection, _ = self.socket.accept()
        self.connection.settimeout(0.01)
        break
    except (socket.timeout, ssl.SSLError):
        continue


# how I read the data:
def read_data(self):
    data = b''
    if self.connection is not None:
        try:
            print(f"start recv at: {datetime.now().isoformat()}")
            if data := self.connection.recv(256):
                print(f"finish recv at: {datetime.now().isoformat()}")
                return data
            else:
                print(f"no data at: {datetime.now().isoformat()}")
                self.connection = None
                return b''
        except (socket.timeout, OSError):
            return data
    return data
CPython versions tested on:

3.12

Operating systems tested on:

Linux

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

该 payload 未指出任何仓库文件或测试。首先在 Linux 上使用 Python 3.8 和 3.12 重现所提供的 SSLContext 和 socket 序列,然后跟踪 SSL 包装 socket 的读取路径。完成的标准是:在受影响的场景中,已连接客户端的待处理数据不会被报告为空读取。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
networking, security
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。