Skip to content

OpenSSL::SSL::SSLSocket writes plaintext to the wire unless #connect is called #9

Closed

Description

@tarcieri

Repro here:

https://gist.github.com/tarcieri/b20437a1b9364d82c365

Attempting to do I/O before the SSL handshake has completed should raise an exception instead of writing plaintext to the wire.

Activity

  1. rhenium commented on Jul 1, 2016

    @rhenium
    Member

    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

  2. tarcieri commented on Jul 2, 2016

    @tarcieri
    CollaboratorAuthor

    @rhenium that's unfortunate 😢

    I have run into similar problems with the test suite trying to make improvements like this.

  3. rhenium commented on Nov 21, 2016

    @rhenium
    Member

    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.

  4. ioquatix commented on Feb 19, 2018

    @ioquatix
    Member

    +1

  5. jawj commented on Jun 7, 2025

    @jawj

    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.

    Could you please bring it back?

  6. tarcieri commented on Jun 7, 2025

    @tarcieri
    CollaboratorAuthor

    @jawj can't you just perform I/O on the socket prior to handing it to OpenSSL::SSL::SSLSocket.new?

  7. jawj commented on Jun 7, 2025

    @jawj

    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 = false
        client_sock = ssl_server.accept  # opens socket to client (no SSL yet)
    
        pg_ssl_req = client_sock.read(8)  # the Postgres SSLRequest
        if pg_ssl_req.unpack1('Q') == 3393130284026167296
          puts 'got SSLRequest message'
        else
          puts 'no SSLRequest, quitting'
          exit 1
        end
    
        client_sock.write('S')  # yes, we speak TLS
        client_sock = client_sock.accept  # begin handshake
        puts '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.

  8. tarcieri commented on Jun 7, 2025

    @tarcieri
    CollaboratorAuthor

    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.

  9. jawj commented on Jun 9, 2025

    @jawj

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions