Repository navigation
feat: ssl.mode = argument to dbConnect() to require or relax TLS verification - #650
Conversation
…verification The new argument takes "disabled", "preferred", "required", "verify_ca", or "verify_identity", modeled after the `--ssl-mode` option of the `mysql` command-line client. The default `NULL` keeps the behavior of the client library. With MariaDB Connector/C, the mode sets MYSQL_OPT_SSL_ENFORCE and MYSQL_OPT_SSL_VERIFY_SERVER_CERT. Connector/C cannot verify the certificate chain without the host name, so "verify_ca" is an error there. Connector/C falls back to an unencrypted connection if the server offers no TLS and verification is off, so "required" checks the cipher after connecting. With the MySQL client library 8.0 and later, the mode sets MYSQL_OPT_SSL_MODE. Other client libraries reject any mode. `ssl.mode` cannot be combined with the CLIENT_SSL flags or, for "disabled", with the other `ssl.*` arguments. The hint for a certificate error now points to `ssl.ca` and `ssl.mode = "required"`. Closes #341, closes #631. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd43pnoHuAnGBgMih89
Resolved:
|
Resolved: Credentials before the TLS check with
|
Resolved: Option files overriding
|
Resolved: Should
|
Resolved: The hint on TLS error 2026The hint added on main for connect error 2026 (TLS) suggested setting
Generated by Claude Code |
With MariaDB Connector/C, `ssl.mode = "required"` detects a missing TLS layer only after authentication. The documentation and the error message now say so and point to `"verify_identity"` with `ssl.ca`. The documentation also says that Connector/C 3.4 accepts an untrusted certificate for 127.0.0.1 and ::1 without a CA, and that the MySQL client library 8.0 ignores `ssl-mode` in an option file but applies `ssl-ca` and `ssl-cipher`. The tests compare client versions with `package_version()`. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd43pnoHuAnGBgMih89
Resolved: Name and values of the new argument, compared with RPostgresRPostgres has no TLS code of its own: Aligning names with RPostgres would mean libpq spellings for MySQL concepts that do not map one to one (
Generated by Claude Code |
|
Responded. Anything else to do? Prepare very brief responses to the issues, each in a separate comment here. |
|
Draft reply for #341:
Generated by Claude Code |
|
Draft reply for #631:
Generated by Claude Code |
Problem
#341: the server runs with
require_secure_transport = ON. Themysqlcommand-line client connects, butdbConnect()fails with "Connections using insecure transport are prohibited".dbConnect()has no argument to require TLS (sslmode = "REQUIRED"lands in...and is ignored). The only ways areclient.flag = CLIENT_SSLor an option file, and the documentation does not explain either well.#631: after upgrading to a build with MariaDB Connector/C 3.4, connecting to a server with a self-signed certificate fails with "TLS/SSL error: self-signed certificate in certificate chain". Connector/C 3.4 turned on TLS with peer verification by default.
client.flag = CLIENT_SSLdoes not help, because it requests TLS but leaves the default verification on. The only working fix was an option file withssl-enforce=1andssl-verify-server-cert=0, as also found in #256.Design
A new argument
ssl.modeofdbConnect(), modeled after the--ssl-modeoption of themysqlcommand-line client and MariaDB's--sslplus--ssl-verify-server-cert. It takes"disabled","preferred","required","verify_ca", or"verify_identity". The defaultNULLsets no option, so nothing changes for anyone who does not pass the argument. The R side validates the value withrlang::arg_match0(), and rejects the combination with theCLIENT_SSLorCLIENT_SSL_VERIFY_SERVER_CERTflags (two ways to say the same thing that may disagree) and the combination of"disabled"with any otherssl.*argument (Connector/C turns TLS back on when one of them is set).Per client library, detected by header macros as in #628, not by
MYSQL_VERSION_ID:MARIADB_PACKAGE_VERSION_ID >= 30000, defined only by Connector/C, so the client libraries of MariaDB server 10.0 and 10.1 from Connection options assume a client library feature set by version number (SSL_ENFORCE, deprecated RECONNECT) #630 are excluded):MYSQL_OPT_SSL_ENFORCEis off for"disabled"and on otherwise,MYSQL_OPT_SSL_VERIFY_SERVER_CERTis on only for"verify_identity". Connector/C has a single verification switch that checks the chain and the host name together, so"verify_ca"is an error that points to"verify_identity". Connector/C silently falls back to an unencrypted connection when the server offers no TLS and verification is off (plugins/auth/my_auth.cin 3.3 and 3.4), so for"required"and"verify_identity"the connection is closed with an error ifmysql_get_ssl_cipher()returnsNULLafter connecting."preferred"is exactly that fallback behavior.RMARIADB_MYSQL_8_OR_LATERfrom fix: Detect MariaDB Connector/C by its macros, not by version number #628):MYSQL_OPT_SSL_MODEwith the matchingSSL_MODE_*value. All five modes work.What
CLIENT_SSLactually does today, verified with the code and the runs below: with the MySQL client library it setsSSL_MODE_REQUIRED; with Connector/C it setsMYSQL_OPT_SSL_ENFORCE, which encrypts but keeps the library's verification default (off before 3.4, on from 3.4) and falls back to plaintext if the server has no TLS. So the old advice "To enforce SSL, add CLIENT_SSL" was right for MySQL and wrong in two ways for Connector/C. TheClient-flagshelp page now points tossl.modeand describes the flags per library, and thedbConnect()example usesssl.mode = "verify_identity", ssl.ca = ...instead ofclient.flag = CLIENT_SSL(the example also had an uncommented prose line that was a syntax error).The hint on error 2026, which suggested
MARIADB_TLS_DISABLE_PEER_VERIFICATION=1, now suggestsssl.caorssl.mode = "required", and only appears if the message mentions a certificate (not for "SSL is required but the server doesn't support it").Evidence
A separate MariaDB 10.11 instance with a self-signed CA, a server certificate for
localhost/127.0.0.1, and--require-secure-transport=ON, listening on 127.0.0.1:3307. Each line isdbConnect(MariaDB(), host = "127.0.0.1", port = 3307, user = "tls", password = "tls", ...)followed bySHOW STATUS LIKE 'Ssl_cipher'. MariaDB 10.11 reports a plaintext login underrequire_secure_transportas "Access denied" (MySQL server reports the "insecure transport" message from #341); withSET GLOBAL require_secure_transport = OFFthe same plaintext login succeeds.RMariaDB built against the system libmariadb-dev (Connector/C 3.3.17):
RMariaDB built against libmysqlclient 8.0.46 (headers from the Ubuntu libmysqlclient-dev package extracted to a local prefix, the installed libmysqlclient21 at run time):
RMariaDB built against Connector/C 3.4.5 from source in a local prefix, which reproduces #631. Connector/C 3.4 skips verification for the host names
127.0.0.1and::1, so this run useshost = "vm", an/etc/hostsalias for 127.0.0.1:The last line is expected: the certificate is issued for
localhostand127.0.0.1, notvm.ssl.mode = "disabled"with Connector/C 3.4.5, whose default is TLS with verification: withrequire_secure_transport = OFFit connects without TLS (Ssl_cipher = '') for both127.0.0.1andvm, where the default connects with TLS on127.0.0.1and fails verification onvm. Withrequire_secure_transport = ONit is refused ("Access denied") for both hosts.Option files with libmysqlclient 8.0.46:
ssl-mode=VERIFY_IDENTITYorssl-mode=DISABLEDin[client]or[rs-dbi]has no effect, with or withoutssl.mode(the connection uses TLS without verification as before). The file is read, though:ssl-ca=/nonexistent/ca.pemor an invalidssl-cipherin[client]makes the connection fail, the cipher even withssl.mode = "disabled". So the client library ignoresssl-modein option files, andssl.modedecides the mode. With Connector/C 3.3.17,ssl-verify-server-cert=1in the option file wins overssl.mode = "required".The same instance restarted without TLS (
--skip-ssl) shows the fallback and the new check. Connector/C 3.3.17:NULL,"disabled","preferred", andclient.flag = CLIENT_SSLconnect unencrypted;"required"fails with "ssl.mode = "required"requires TLS, but the connection is not encrypted.";"verify_identity"fails with "SSL is required, but the server does not support it". libmysqlclient 8.0.46:"required","verify_identity", andCLIENT_SSLfail with "SSL is required but the server doesn't support it".Tests
tests/testthat/test-ssl-mode.Rhas three tests of argument validation that need no server, and six tests that skip unlessRMARIADB_TLS_TEST_HOSTnames a TLS server withrequire_secure_transport = ON(optionalRMARIADB_TLS_TEST_PORT,_USER,_PASSWORD;RMARIADB_TLS_TEST_CAfor the verification tests). Results for test-ssl-mode.R: Connector/C 3.3.17 12 passed; libmysqlclient 8.0.46 13 passed, 0 skipped; Connector/C 3.4.5 11 passed, 1 skipped (verification without CA on 127.0.0.1, see above); without the environment variable 6 passed, 6 skipped. The full suite on Connector/C 3.3 has 3 failures in test-timezone.R, which need named time zones that the local test server lacks; they are unrelated.Not tested: the MySQL 26.x client library and MySQL 5.7 or older clients (rejected with an error by design), Windows and macOS builds, and a real MySQL server. CI does not run the TLS tests, since it has no TLS server configured.
Open questions
"verify_ca"is an error on Connector/C. Mapping it to"verify_identity"(stricter) would be an alternative, as wouldMYSQL_OPT_SSL_ENFORCEwithssl.ca, which in Connector/C 3.3 verifies the chain only but in 3.4 also checks the host name."required"detects a missing TLS layer only after authentication, so the authentication exchange (and a cleartext password, if the server asks formysql_clear_password) has already happened unencrypted. libmysqlclient refuses before authenticating, as does Connector/C with"verify_identity". The check stays; thessl.modedocumentation and the error message now say this and point to"verify_identity"withssl.ca.group = "rs-dbi") are applied when connecting and win overssl.mode. libmysqlclient 8.0 ignoresssl-modein option files but appliesssl-caandssl-cipher. The documentation describes both.ssl.ca, Connector/C 3.4 and later accept an untrusted certificate with"verify_identity"for the hosts127.0.0.1and::1. The documentation mentions it, and the test that expects a failure skips in that case.CLIENT_SSLalone keep its current meaning, or be translated tossl.mode = "required"on Connector/C as well? This PR leaves it unchanged.Closes #341, closes #631.
https://claude.ai/code/session_01Hr1rd43pnoHuAnGBgMih89
Generated by Claude Code