Skip to content

Commit 8154556

Browse files
committed
ssl: preserve cleartext after MemoryBIO shutdown
Read at most one TLS record while waiting for close_notify in BIO mode, leaving any post-TLS cleartext queued for the BIO owner. Co-authored-by: Ivan Mironov <mironov.ivan@gmail.com> Assisted-by: OpenAI Codex: GPT-5
1 parent 46084f2 commit 8154556

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

‎crates/stdlib/src/ssl.rs‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,7 +1899,10 @@ mod _ssl {
18991899
sock_send_method: vm.ctx.none(),
19001900
sock_recv_method: vm.ctx.none(),
19011901

1902-
tls_record_header_buf: vm.ctx.none(),
1902+
tls_record_header_buf: vm
1903+
.ctx
1904+
.new_bytearray(Vec::with_capacity(TLS_RECORD_HEADER_SIZE))
1905+
.into(),
19031906
context: PyRwLock::new(zelf),
19041907
server_side,
19051908
server_hostname: PyRwLock::new(hostname),
@@ -4382,11 +4385,11 @@ mod _ssl {
43824385
return Ok(self.try_read_close_notify_socket(conn, vm));
43834386
}
43844387

4385-
// BIO mode: read from incoming BIO
4386-
match self.sock_recv(SSL3_RT_MAX_PACKET_SIZE, vm) {
4387-
Ok(bytes_obj) => {
4388-
let bytes = ArgBytesLike::try_from_object(vm, bytes_obj)?;
4389-
let data = bytes.borrow_buf();
4388+
// BIO mode: stop at the TLS record boundary so cleartext queued
4389+
// after close_notify remains available to the BIO's owner.
4390+
match self.sock_recv_at_most_one_tls_record(vm) {
4391+
Ok(bytes) => {
4392+
let data = bytes.as_bytes();
43904393

43914394
if data.is_empty() {
43924395
if let Some(ref bio) = self.incoming_bio {
@@ -4401,7 +4404,7 @@ mod _ssl {
44014404
return Ok(true);
44024405
}
44034406

4404-
let data_slice: &[u8] = data.as_ref();
4407+
let data_slice: &[u8] = data;
44054408
let mut cursor = std::io::Cursor::new(data_slice);
44064409
let _ = conn.read_tls(&mut cursor);
44074410
let _ = conn.process_new_packets();

0 commit comments

Comments
 (0)