Skip to content

Reorganize TLS options: implement PREFERRED/REQUIRED SSL mode behavior - #1234

Merged
methane merged 11 commits into
mainfrom
copilot/add-ssl-mode-required-option
May 8, 2026
Merged

Reorganize TLS options: implement PREFERRED/REQUIRED SSL mode behavior#1234
methane merged 11 commits into
mainfrom
copilot/add-ssl-mode-required-option

Conversation

CopilotAI commented May 7, 2026

Copy link
Copy Markdown
Contributor

PyMySQL had no way to emulate MySQL's ssl_mode=REQUIRED, and ssl_disabled was effectively a no-op. This reorganizes TLS behavior around a clear priority order without adding new options.

Behavior

Evaluated in order:

  1. ssl_disabled=True → prohibit SSL
  2. Any explicit SSL option set (ssl, ssl_ca, ssl_cert, ssl_key, ssl_key_password, ssl_verify_cert, ssl_verify_identity) → REQUIRED: raises OperationalError(CR_SSL_CONNECTION_ERROR) if the server doesn't advertise CLIENT.SSL
  3. No SSL options → PREFERRED: attempt SSL if the server supports it, fall back to plain connection otherwise
# Now raises OperationalError if server doesn't support SSLconn=pymysql.connect(host=..., ssl_ca="/path/to/ca.pem")
# Silently falls back to plaintext if server has no SSL — no config neededconn=pymysql.connect(host=...)
# Explicitly opt outconn=pymysql.connect(host=..., ssl_disabled=True)

Changes

  • connections.py

    • Added self._ssl_required to distinguish REQUIRED vs PREFERRED at connect time
    • PREFERRED mode: creates a default no-verify SSLContext via _create_ssl_ctx({}) when no SSL options are given and the ssl module is available
    • _request_authentication: refactored into a client_flags/_do_ssl pattern — CLIENT.SSL is added to the handshake flags only when the SSL upgrade actually happens (needed for PREFERRED, already set for REQUIRED); raises on REQUIRED + no server SSL; falls back silently for PREFERRED
  • tests/test_connection.py: new tests covering PREFERRED setup, REQUIRED error path (with CA context assertion), and PREFERRED fallback

  • CHANGELOG.md: documents the behavior change

CopilotAI linked an issue May 7, 2026 that may be closed by this pull request
Comment threadpymysql/connections.py Fixed
CopilotAI changed the title [WIP] Add option to emulate ssl_mode=REQUIRED in MySQLReorganize TLS options: implement PREFERRED/REQUIRED SSL mode behaviorMay 7, 2026
CopilotAI requested a review from methaneMay 7, 2026 10:08
Comment threadpymysql/connections.py Outdated
CopilotAI requested a review from methaneMay 8, 2026 05:43
Comment threadCHANGELOG.md
@methane
methane marked this pull request as ready for review May 8, 2026 06:33
CopilotAI requested a review from methaneMay 8, 2026 06:35
methane added 2 commits May 8, 2026 18:42
Updated changelog to reflect breaking changes in v1.2.0.
@methane
methane merged commit 8e3a79d into mainMay 8, 2026
23 of 25 checks passed
@methane
methane deleted the copilot/add-ssl-mode-required-option branch May 8, 2026 10:14
methane pushed a commit that referenced this pull request May 15, 2026
- update `CHANGELOG.md` for `v1.2.0` release preparation
- add `Release date: TBD`
- add changelog entries for user-visible changes since `v1.1.3` (#1234,
#1235, #1236, #1237)
Sign up for freeto 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.

Option to emulate ssl_mode=REQUIRED in mysql

3 participants

@methane@github-advanced-security