Skip to content

Default ssl_mode to SSL_PREFERRED_NOVERIFY for TCP connections - #307

Open
Yuhi-Sato wants to merge 2 commits into
trilogy-libraries:mainfrom
Yuhi-Sato:ruby-default-ssl-preferred
Open

Default ssl_mode to SSL_PREFERRED_NOVERIFY for TCP connections#307
Yuhi-Sato wants to merge 2 commits into
trilogy-libraries:mainfrom
Yuhi-Sato:ruby-default-ssl-preferred

Conversation

@Yuhi-Sato

Copy link
Copy Markdown
Contributor

Fixes #306

Summary

When the ssl_mode option is omitted, the Ruby binding now defaults TCP connections to Trilogy::SSL_PREFERRED_NOVERIFY: TLS is attempted, and the connection falls back to plaintext when the server doesn't support it. This matches the default behavior of MySQL client programs / libmysqlclient (SSL_MODE_PREFERRED) and avoids silently downgrading to plaintext when migrating from mysql2.

  • The server certificate is not verified in this mode; pass SSL_VERIFY_CA / SSL_VERIFY_IDENTITY explicitly when verification is needed, or SSL_REQUIRED_NOVERIFY to make TLS mandatory.
  • An explicit ssl_mode is honored as before, including SSL_DISABLED.
  • Unix socket connections still default to plaintext, matching libmysqlclient's PREFERRED behavior of not encrypting them.
  • Ruby binding only — no changes to the C API defaults, the trilogy_ssl_mode_t enum, or trilogy_sockopt_t zero-value semantics.

Tests

Adds DefaultSslModeTest, separate from SslTest because SslTest#setup skips everything on non-TLS servers while the fallback test here must run exactly then:

  • omitted ssl_mode connects with TLS on a TLS-capable server
  • explicit SSL_DISABLED stays plaintext (guards the zero value against being clobbered by the default, since SSL_DISABLED == 0)
  • omitted ssl_mode falls back to plaintext on a server without TLS — this runs on the MariaDB 10.6/10.11 CI jobs, which don't enable TLS
  • Unix socket connections stay plaintext by default

The tests call Trilogy.new directly because the new_tcp_client helper passes ssl_mode explicitly, and check SHOW SESSION STATUS LIKE 'Ssl_cipher' rather than performance_schema.session_status because the fallback test runs on servers where performance_schema is OFF (the MariaDB < 11.4 default) and that table is empty.

Docs

  • README: new SSL/TLS section describing the default, the fallback, the lack of certificate verification, and how to opt into stricter modes.
  • CHANGELOG entry under Unreleased.

🤖 Generated with Claude Code

When the Ruby binding's `ssl_mode` option is omitted, TCP connections
previously stayed plaintext even when the server supports TLS. Match the
MySQL client library's default instead: attempt TLS and fall back to
plaintext when the server doesn't support it. The server certificate is
not verified in this mode.

An explicit `ssl_mode` is honored as before, including `SSL_DISABLED`.
Unix socket connections still default to plaintext, matching
libmysqlclient's PREFERRED behavior of not encrypting them.

The new tests use SHOW SESSION STATUS rather than
performance_schema.session_status because the fallback test runs on
servers without TLS (CI's MariaDB 10.6/10.11 jobs), where
performance_schema is OFF and its session_status table is empty.

Fix: trilogy-libraries#306

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

@composerinteralia composerinteralia left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the default seems OK to me, but might need to wait for a major release since it's a breaking change.

@Yuhi-Sato

Copy link
Copy Markdown
Contributor Author

@composerinteralia
I understand, thanks!

@byroot

byroot commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

since it's a breaking change.

Is it though? I may be missing something, but since it automatically fall backs to unencrypted (the current default), I wouldn't expect breakages.

Unless somehow using unverified SSL instead of plain text cause some ripple effect.

@composerinteralia

Copy link
Copy Markdown
Collaborator

Ah yeah, good point. I guess it's not breaking then 👍🏻

@Yuhi-Sato

Yuhi-Sato commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

@byroot
I agree it probably won't cause breakages because of the fallback.

My only concern is that it changes the default behavior, and while the SSL overhead is probably negligible, it's not entirely free.

@byroot

byroot commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

We can bump the major, but the annoying thing is that active record won't boot https://github.com/rails/rails/blob/0f36bbf72cc8b814bf1ad05c896c9c427b18217f/activerecord/lib/active_record/connection_adapters/trilogy_adapter.rb#L5

@Yuhi-Sato

Copy link
Copy Markdown
Contributor Author

@byroot
Ah, I see. Since I also opened #309, I think a major version bump makes sense. I'd be happy to follow up with a Rails PR to bump the supported Trilogy version once Trilogy 3 is released.

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.

Prefer TLS by default when ssl_mode is omitted

3 participants