Skip to content

Expose OpenSSL::SSL::SSLContext object - #303

Open
rhenium wants to merge 2 commits into
ruby:masterfrom
rhenium:ky/expose-ssl-context
Open

rhenium wants to merge 2 commits into
ruby:masterfrom
rhenium:ky/expose-ssl-context

Conversation

@rhenium

@rhenium rhenium commented Jun 21, 2026

Copy link
Copy Markdown
Member

Add Net::HTTP#ssl_context to return the SSLContext used for HTTPS connections, and reuse the same object for future connections made by the same Net::HTTP instance.

Also change existing SSL/TLS-related accessors to interact with the SSLContext directly, rather than temporarily storing values in instance variables and applying them later in Net::HTTP#start.

Note that the SSLContext becomes frozen after the first connection is made using the Net::HTTP instance. Although I do not expect this to affect most users, it is a compatibility concern.

[Feature #19641] https://bugs.ruby-lang.org/issues/19641


/cc @shouichi

An alternative to #147. Instead of accepting user-supplied SSLContext, this PR makes Net::HTTP prepare SSLContext earlier and expose it. This avoids ambiguity when both Net::HTTP accessors and SSLContext accessors are used.

This doesn't address my comment in the PR #147 (comment), but I think this incompatibility may be unavoidable. It should be documented in the release note.

This resolves #139 by allowing users to do the following:

http = Net::HTTP.new(host, 443)
http.use_ssl = true
http.ssl_context.options |= OpenSSL::SSL::OP_IGNORE_UNEXPECTED_EOF

@shouichi

Copy link
Copy Markdown

Thank you for the work. Feel free to close #147.

@forthrin

Copy link
Copy Markdown

Can this be merged into the gem?

Add a new test case to confirm the current behavior.

Commit fa68e64 fixed the handling of certificate identity check
when connecting to an IP address, but the happy path is currently not
covered by the test suite.
Add Net::HTTP#ssl_context to return the SSLContext used for HTTPS
connections to allow users to configure it directly.

Also change the existing SSL/TLS-related accessors to forward to the
SSLContext instance directly rather than storing values in Net::HTTP's
instance variables and applying them later in Net::HTTP#start. This
avoids inconsistencies between, for example, http.cert_store and
http.ssl_context.cert_store.

The SSLContext instance is now reused for all connections made by a
Net::HTTP instance. As a result, the SSLContext becomes frozen on the
first connection attempt, and SSL/TLS-related options can no longer be
changed afterward. Although I do not expect this to affect most users,
it is technically a breaking change.

[Feature #19641]
@rhenium
rhenium force-pushed the ky/expose-ssl-context branch from 40107ee to bf989e3 Compare November 27, 2026 10:04
@rhenium
rhenium requested a review from nurse November 27, 2026 10:14
@rhenium

rhenium commented Nov 27, 2026

Copy link
Copy Markdown
Member Author

@nurse What do you think about this?

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants