You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
OpenSSL::SSL::SSLSocket writes plaintext to the wire unless #connect is called #9
Tthe code is already in the oldest version of 'OpenSSL for Ruby' I could find, but the history before 'SSLSocket' is merged into it is missing. So I still don't know why it is made so.
I'm kind of amazed that this functionality was removed.
The documentation still says you can read and write unencrypted data prior to making or accepting an SSL connection. If you can't, then what's the point of the start_immediately attribute on SSLServer, for example?
This functionality is necessary for various protocols, such as Postgres, where an 'SSLRequest' message is sent in plaintext to determine whether the server supports SSL before the TLS handshake begins.
I'm not using SSLSocket directly, so I'm not sure. This is the code that's now broken:
server=TCPServer.new('0.0.0.0',port)ssl_server=OpenSSL::SSL::SSLServer.new(server,ssl_ctx)ssl_server.start_immediately=falseclient_sock=ssl_server.accept# opens socket to client (no SSL yet)pg_ssl_req=client_sock.read(8)# the Postgres SSLRequestifpg_ssl_req.unpack1('Q') == 3393130284026167296puts'got SSLRequest message'elseputs'no SSLRequest, quitting'exit1endclient_sock.write('S')# yes, we speak TLSclient_sock=client_sock.accept# begin handshakeputs'TLS connection established with client'
Do you know how to do raw I/O here prior to starting the TLS handshake?
For context: I had a script that was working nicely with the Mac system Ruby (version 2.6), then upgraded to 3.4 because I needed newer features in net/imap, and found that my script was broken.
Accept the connection as a TCPServer, then do client_sock.read(8) directly on that socket, prior to involving OpenSSL at all.
If you determine you need to upgrade to an SSL socket, do ssl = OpenSSL::SSL::SSLSocket.new(client_sock) and then call ssl.accept to begin the handshake.
Activity
rhenium commentedon Jul 1, 2016
Note:
As an experiment, I disabled it but several tests outside test/openssl failed.
rhenium/ruby@195c9ad?w=1
https://gist.github.com/rhenium/8209b06c77a877beed98895c0c803df4
Tthe code is already in the oldest version of 'OpenSSL for Ruby' I could find, but the history before 'SSLSocket' is merged into it is missing. So I still don't know why it is made so.
http://cvs.savannah.gnu.org/viewvc/ossl/ossl_ssl.c?revision=1.1&root=rubypki&sortdir=down&view=markup
tarcieri commentedon Jul 2, 2016
@rhenium that's unfortunate 😢
I have run into similar problems with the test suite trying to make improvements like this.
rhenium commentedon Nov 21, 2016
Actually net/http was the only using the behavior. It has been replaced now: ruby/ruby@4081b34
Since the gem also targets 2.3 for now, we can't remove it immediately, though.
ioquatix commentedon Feb 19, 2018
+1
ssl: disallow reading/writing to unstarted SSL socket
[ruby/openssl] ssl: disallow reading/writing to unstarted SSL socket
jawj commentedon Jun 7, 2025
I'm kind of amazed that this functionality was removed.
The documentation still says you can read and write unencrypted data prior to making or accepting an SSL connection. If you can't, then what's the point of the
start_immediatelyattribute onSSLServer, for example?This functionality is necessary for various protocols, such as Postgres, where an 'SSLRequest' message is sent in plaintext to determine whether the server supports SSL before the TLS handshake begins.
Could you please bring it back?
tarcieri commentedon Jun 7, 2025
@jawj can't you just perform I/O on the socket prior to handing it to
OpenSSL::SSL::SSLSocket.new?jawj commentedon Jun 7, 2025
I'm not using
SSLSocketdirectly, so I'm not sure. This is the code that's now broken:Do you know how to do raw I/O here prior to starting the TLS handshake?
For context: I had a script that was working nicely with the Mac system Ruby (version 2.6), then upgraded to 3.4 because I needed newer features in
net/imap, and found that my script was broken.tarcieri commentedon Jun 7, 2025
Accept the connection as a
TCPServer, then doclient_sock.read(8)directly on that socket, prior to involvingOpenSSLat all.If you determine you need to upgrade to an SSL socket, do
ssl = OpenSSL::SSL::SSLSocket.new(client_sock)and then callssl.acceptto begin the handshake.jawj commentedon Jun 9, 2025
Many thanks for the quick replies. This works.
But I do think you might consider either adding back this feature, or otherwise documenting that it has been removed and what to do instead.
ssl: disallow reading/writing to unstarted SSL socket