Repository navigation
Verify TLS for MySQL and the remote SQL API, and keep passwords out of logs - #44
Merged
Merged
Conversation
Whenever TLS was on for MySQL, which is always for IBM Cloud Databases hosts, the toolkit set verify_mode to CERT_NONE and check_hostname to False. Anyone between it and the server could present any certificate and read the credentials. TLS options now move from the connection string into an ssl.SSLContext that always verifies the certificate chain and the host name. sslMode=REQUIRED and VERIFY_CA are treated as VERIFY_IDENTITY, a private CA is named with ssl_ca, and unknown ssl* options are refused instead of ignored. A failed certificate check now says to add ssl_ca. sslMode was also stripped before it was read, so VERIFY_IDENTITY on any other host connected without TLS. It now turns TLS on. The per-record database switch keeps the TLS context instead of rebuilding it from a URL that no longer has the options. The Beaver curation runner and the schema extractor had their own copies of the old code, and both now use the library's. Signed-off-by: Oktie Hassanzadeh <oktie@oktie.com>
MySQL execution logged its connection string at DEBUG level. That is the toolkit's default level, and a source checkout also writes the log to data/results/bak/log.txt, so every MySQL run put the database password on the console and on disk. A new helper, redact_url_password, shows it as *** in those messages and in the Beaver curation runner. The BIRD Postgres schema converter logged its connection string at info level. It now names only the environment variable it read. It also failed both with and without --pg_conn_str, because an else was indented under the wrong if. Signed-off-by: Oktie Hassanzadeh <oktie@oktie.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Human security review is needed to assess the retained aiomysql handshake downgrade to plaintext and residual credential-exposure risk, with real IBM Cloud validation unavailable.
No open issues 🎉
What changed in this PR
This PR hardens database connections and logging in the toolkit’s execution library and curation scripts by verifying MySQL certificates when TLS is used and removing passwords from connection-string logs.
Changes:
- Centralizes MySQL TLS configuration, verifies certificate chains and hostnames, and preserves TLS settings when switching databases.
- Redacts logged MySQL passwords and fixes PostgreSQL connection-string selection in the BIRD converter.
- Adds regression tests and documents TLS configuration and private-CA requirements.
| File | Description |
|---|---|
tests/test_mysql_tls.py |
Tests TLS options, certificate verification, and context preservation. |
tests/test_connection_string_logging.py |
Tests password redaction and MySQL logging. |
src/text2sql_eval_toolkit/execution/execution_tools.py |
Centralizes verified TLS handling, preserves contexts, and redacts passwords. |
scripts/curation/sqlalchemy_schema_extractor.py |
Uses shared TLS handling and removes insecure fallback and raw URL output. |
scripts/curation/bird_schema_converter_postgres.py |
Fixes connection-string selection and removes password-bearing logging. |
scripts/curation/beaver_sql_runner.py |
Reuses shared TLS normalization and password redaction. |
env.example |
Adds MySQL TLS and private-CA configuration guidance. |
docs/guide/configuration.md |
Documents MySQL TLS options and certificate requirements. |
CHANGELOG.md |
Records TLS verification, logging, and converter fixes. |
Review effort: Balanced
aiomysql and PyMySQL treat an SSL context as a preference: when the server's handshake does not advertise CLIENT_SSL, they carry on unencrypted and send the login anyway. Anyone who could rewrite that handshake could strip the offer and collect the credentials, even with sslMode=VERIFY_IDENTITY. With TLS on, normalize_mysql_connection_string now also returns an async_creator_fn that builds an aiomysql connection which raises CR_SSL_CONNECTION_ERROR (2026) before authenticating if TLS was not offered. The schema extractor does the same for PyMySQL through a do_connect handler. The new tests run the real SQLAlchemy and aiomysql path against a fake server on localhost. Without the guard, the server receives the 89-byte login packet, and with it, nothing. Signed-off-by: Oktie Hassanzadeh <oktie@oktie.com>
Both of its requests passed verify=False while sending SQL_API_KEY as a bearer token, so anyone between the toolkit and the API could present any certificate and read the key. They now use requests' default verification, and a private CA is trusted through REQUESTS_CA_BUNDLE. Signed-off-by: Oktie Hassanzadeh <oktie@oktie.com>
The guard against stripped TLS overrides the private Connection._request_authentication in aiomysql and PyMySQL. If a release renamed it, the override would silently stop running and the drivers would go back to logging in without TLS. Both guards now refuse to connect when the method is missing. Signed-off-by: Oktie Hassanzadeh <oktie@oktie.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes an improper certificate validation report: MySQL connections over TLS had certificate verification turned off, so the toolkit sent MySQL credentials over a connection it never checked. The later commits close the other ways credentials could cross an unchecked connection: a MySQL server could have TLS stripped from its handshake, and
RemoteDatabaseConnectorsent its API key withverify=False. They also stop the logs from printing database passwords.TLS verification
Whenever TLS was on, which is always for IBM Cloud Databases hosts,
normalize_mysql_connection_stringbuilt its context withverify_mode = CERT_NONEandcheck_hostname = False. Anyone between the toolkit and the server could present any certificate and read the login.TLS options now move from the connection string into an
ssl.SSLContextthat always verifies the certificate chain and the host name. There is no option to turn the check off.sslMode=VERIFY_IDENTITY, any other hostsslModewas stripped before it was read)sslMode=REQUIREDorVERIFY_CAVERIFY_IDENTITY)ssl=truessl_ca=/path/to/ca.pemSSLContext)ssl_check_hostname=falseor another unknownssl*optionValueErrorsslMode=DISABLED,ssl=falseAlso in this commit:
sslModeno longer breaks the rest of the query string. The old regex left&charset=...without its?.scripts/curation/beaver_sql_runner.pyandsqlalchemy_schema_extractor.pyhad their own copies of the old code, and both now use the library's. The extractor used PyMySQL's dict form, which skips the certificate check whenever no CA is named. It also printed the raw connection string.docs/guide/configuration.md, and a note inenv.example.Upgrading. A server whose certificate comes from a private CA now fails to connect until that CA is named with
ssl_ca=/path/to/ca.pem. IBM Cloud Databases includes its CA with the service credentials. Connect by the host name on the certificate, not by IP address. When the check fails, the connection error says so.TLS cannot be stripped
aiomysql and PyMySQL treat an SSL context as a preference. When the server's handshake doesn't advertise
CLIENT_SSL, they carry on unencrypted and send the login anyway, so an attacker who can rewrite the handshake gets the credentials, even withsslMode=VERIFY_IDENTITY. Copilot flagged this, and the first version of this PR listed it as not covered.With TLS on,
normalize_mysql_connection_stringnow also returns anasync_creator_fnfor SQLAlchemy. It builds an aiomysql connection that raisesCR_SSL_CONNECTION_ERROR(2026) before authenticating if TLS wasn't offered. The schema extractor does the same for PyMySQL through ado_connecthandler.RemoteDatabaseConnectorBoth of its requests sent
SQL_API_KEYas a bearer token withverify=False. They now use the default verification fromrequests, and a private CA is trusted throughREQUESTS_CA_BUNDLE. Noverify=False,CERT_NONEor disabled host-name check is left insrcorscripts.Passwords in logs
MySQL execution logged its connection string at DEBUG level. That is the toolkit's default level, and a source checkout also writes the log to
data/results/bak/log.txt, so every MySQL run put the password on the console and on disk. A new helper,redact_url_password, shows it as***, and the Beaver curation runner uses it too.The BIRD Postgres schema converter logged its connection string at info level. It now names only the environment variable it read. It also failed both with and without
--pg_conn_str, because anelsewas indented under the wrongif.Testing
tests/test_mysql_tls.py,tests/test_connection_string_logging.pyandtests/test_remote_connector_tls.py. The full hermetic suite passes (1280 tests), Ruff and Black are clean, and each commit's own tests pass at that commit.mysql_run_execution_asyncand aiomysql:sslMode=VERIFY_IDENTITY, CA not namedsslMode=REQUIRED, CA not namedssl_canamed, host name matchesssl_canamed, connecting by IPssl_canamedThe reported scenario, before and after
An IBM Cloud Databases host (
*.databases.appdomain.cloud, resolved to a local fake server) behind an on-path proxy with a self-signed certificate the host doesn't trust, reached throughmysql_run_execution_async:mainsslMode=VERIFY_IDENTITYNot covered
Connection._request_authenticationin aiomysql and PyMySQL. If a release renames it, both guards refuse to connect rather than fall back, and the library's downgrade tests run the real driver. The extractor has no automated test, only the manual check above.