Skip to content

Net::HTTP doesn't allow to set SSL options #139

Description

@casperisfine

Context

Since OpenSSL 3.x, when a server close the TCP connection without first calling SSL_shutdown, the SSL client now error with SSL_read: unexpected eof while reading.

In OpenSSL 1.x, the client wouldn't mind and would behave like if the connection was cleanly closed.

To restore the 1.x behavior, you can set a specific options:

ssl_context = OpenSSL::SSL::SSLContext.new
ssl_context.options |= OpenSSL::SSL::OP_IGNORE_UNEXPECTED_EOF

Problem

The issue is that Net::HTTP only allow to set specific fields on the SSLContext object, and options is not one of them.

Workaround

The issue can be worked around by changing the default options globally:

if OpenSSL::SSL.const_defined?(:OP_IGNORE_UNEXPECTED_EOF)
  OpenSSL::SSL::SSLContext::DEFAULT_PARAMS[:options] |= OpenSSL::SSL::OP_IGNORE_UNEXPECTED_EOF
end

However this impact all SSL connections, not just the ones that need it.

Solution

Not sure what the best API would be. But it would be great if we could directly pass a SSLContext instance to Net::HTTP, so that we're not limited on the SSL configuration.

Activity

  1. adeherdt-r7 commented on Jul 24, 2024

    @adeherdt-r7

    Running into this same issue as well with the upgrade to OpenSSL 3 for our Ruby installation.
    As our client gems rely on net-http, this issue causes a lot of SSL_READ errors to appear in our client side, despite the server logs on the calling side show that the response has been sent to the client.

    I can confirm that the workaround as a monkey patch definitely works and restores functionality, but setting this as a global option isn't ideal either. With the upgrades everywhere coming for OpenSSL 3, it would be nice to see net-http accomodate either the configuration options for this, or by default start handling this behavior.

  2. timcraft commented on Jun 9, 2025

    @timcraft

    As a simpler alternative to passing a SSLContext instance I've opened #218 which implements a #ssl_options attribute, which would make it fairly straightforward to specify options per connection instead of globally:

    http.ssl_options = OpenSSL::SSL::OP_IGNORE_UNEXPECTED_EOF
  3. forthrin commented on May 15, 2026

    @forthrin

    Is this happening? Or is there another way to do it?

  4. forthrin commented on Jun 21, 2026

    @forthrin

    Well, so we're back here again. This has been stale for a year. Not sure why @timcraft withdrew his PR? Is there a workaround or patch that can be used pending an official, well-pondered fix?

  5. rhenium commented on Jun 21, 2026

    @rhenium
    Member

    SSLContext#options often has a non-zero value by default and it's usually desirable to keep existing options. This makes it unsuitable for simply adding to SSL_ATTRIBUTES. (And we probably don't want a method named Net::HTTP#options.)

    I think exposing the SSLContext object to users is a better approach: #303

  6. forthrin commented on Jun 21, 2026

    @forthrin
    require 'uri'
    require 'net/http'
    
    uri = URI('https://www.moviesubtitles.org/search.php?q=paris+new+york')
    http = Net::HTTP.new(uri.host, 443)
    http.use_ssl = true
    http.ssl_context.options |= OpenSSL::SSL::OP_IGNORE_UNEXPECTED_EOF
    res = http.send_request('GET', uri.request_uri)
    puts res.body.gsub(/(?<=>)/, "\n").split("\n").last(10)

    This does indeed work! Assuming this require forms like above or below.

    res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
      http.ssl_context.options |= OpenSSL::SSL::OP_IGNORE_UNEXPECTED_EOF
      http.get(uri.request_uri)
    end

    Would an alternative solution be to throw the exception as before, but still return the body, so that the developer may rescue and get the body? This would make for more concise code and access to convenience functions.

    PS! Had a couple of /protocol.rb:241:in 'Net::BufferedIO#rbuf_fill': end of file reached (EOFError) happening (on a different site), but these might be a different issue.

  7. forthrin commented on Jun 22, 2026

    @forthrin

    As a (ostensibly related) side note, shouldn't http.use_ssl = true (and even 443) be implied by now?

    • Assume https/SSL for everything
    • Require dev to explicitly downgrade to http
    • Automatically downgrade for

    More than 99 percent of the top one hundred thousand websites support HTTPS. Non SSL websites are disproportionately concentrated among low traffic, small scale, or legacy sites.

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