Repository navigation
Fail fast on invalid certificates at TLS config load - #2999
Conversation
Codecov Reportβ
All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## unstable #2999 +/- ##
============================================
- Coverage 74.38% 74.24% -0.14%
============================================
Files 129 129
Lines 71041 71041
============================================
- Hits 52844 52745 -99
- Misses 18197 18296 +99
π New features to boost your workflow:
|
570801b to
51925b4
Compare
|
Hello @madolson @zuiderkwast @PingXie, I briefly mentioned this PR in last week's weekly meeting. |
|
Thanks for the heads up. A small behavior change doesn't necessarily need to be a major decision, if it's backward compatible and doesn't introduce any new public API (configs, commands, etc.) and doesn't break any user scenario. So what exactly is the behavior change? Before:
After:
CONFIG SET can now return an error where it previously returned OK, but that OK was silencing a severe admin error resulting in clients unable to connect. To me, it doesn't seem like a breaking change, so I don't think it needs to be a major decision, but @valkey-io/core-team please speak up if you think otherwise. I (or someone else) will review this PR soon. |
zuiderkwast
left a comment
There was a problem hiding this comment.
Looks good to me in general. I have only minor comments.
We use camelCase for function names and snake_case for variables, so please follow this style in new code. (Sorry, the style is not written anywhere. We're working on it.)
zuiderkwast
left a comment
There was a problem hiding this comment.
Looks good, thank you!
@madolson do you think the behavior change poses any risk?
|
The new behavior is better so good from my end. Btw, @zuiderkwast once we merge #3076 copilot should be able to take care of the coding style review (and we can keep iterating on the instructions). |
Gotcha. I approved it now. :) |
There was a problem hiding this comment.
Pull request overview
This PR adds fail-fast validation for TLS certificates at configuration load time, rejecting certificates that are expired or not yet valid. This applies to server certificates, client certificates, and CA certificates (both file and directory-based), and works during both server startup and runtime CONFIG SET operations.
Changes:
- Added certificate validity checking functions (
isCertValid,loadCaCertDir,areAllCaCertsValid) to validate X509 certificates - Enhanced TLS configuration to eagerly load and validate all CA certificates from directories
- Generated test certificates (expired and not-yet-valid) for comprehensive testing
- Added integration tests to verify rejection of invalid certificates at startup
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/tls.c | Implements certificate validation logic with three new functions to check certificate validity, eagerly load CA certificates from directories, and validate all loaded CA certificates |
| utils/gen-test-certs.sh | Extends certificate generation script to create expired and not-yet-valid certificates for testing (server, client, and CA certificates) |
| tests/unit/tls.tcl | Adds integration tests to verify that invalid certificates are rejected at server startup for all certificate types |
| tests/README.md | Updates documentation to mention generation of invalid test certificates |
### Overview This PR adds support for automatic background TLS reloading, closes #2649 TLS validity checks and fail-fast behavior on invalid certificates are handled separately in #2999. - New configuration - `tls-auto-reload-interval <seconds>` - `0` disabled (default, backward compatible) - `>0` check interval in seconds - TLS materials change detection in background - SHA-256 fingerprint checking for certificate files - `inode + mtime` checking for CA certificate directories and key files - Skips reload if materials haven't changed `tlsCheckMaterialsAndUpdateCache` - TLS contexts reload - CPU-intensive certificate parsing happens in dedicated BIO worker thread `BIO_TLS_RELOAD` - Main thread never blocks, atomically swaps SSL contexts - Two-phase reload: background preparation `tlsConfigureAsync` + main thread application `tlsApplyPendingReload` **Note**: Original TLS load and reload still remain in main thread using `tlsConfigureSync`, including: - Initial TLS load (server startup) - Runtime reload via CONFIG SET --------- Signed-off-by: Yang Zhao <zymy701@gmail.com>
This comment was marked as off-topic.
This comment was marked as off-topic.
### Overview This PR adds support for automatic background TLS reloading, closes valkey-io#2649 TLS validity checks and fail-fast behavior on invalid certificates are handled separately in valkey-io#2999. - New configuration - `tls-auto-reload-interval <seconds>` - `0` disabled (default, backward compatible) - `>0` check interval in seconds - TLS materials change detection in background - SHA-256 fingerprint checking for certificate files - `inode + mtime` checking for CA certificate directories and key files - Skips reload if materials haven't changed `tlsCheckMaterialsAndUpdateCache` - TLS contexts reload - CPU-intensive certificate parsing happens in dedicated BIO worker thread `BIO_TLS_RELOAD` - Main thread never blocks, atomically swaps SSL contexts - Two-phase reload: background preparation `tlsConfigureAsync` + main thread application `tlsApplyPendingReload` **Note**: Original TLS load and reload still remain in main thread using `tlsConfigureSync`, including: - Initial TLS load (server startup) - Runtime reload via CONFIG SET --------- Signed-off-by: Yang Zhao <zymy701@gmail.com> Signed-off-by: arshidkv12 <arshidkv12@gmail.com>
Signed-off-by: Yang Zhao <zymy701@gmail.com>
Signed-off-by: Yang Zhao <zymy701@gmail.com>
Signed-off-by: Yang Zhao <zymy701@gmail.com>
Signed-off-by: Yang Zhao <zymy701@gmail.com>
Signed-off-by: Yang Zhao <zymy701@gmail.com>
Changes include: - Unify TLS topic naming: previously some references used βencryptionβ while others used βtlsβ - Remove source code repo specific information: instructions on building or running unit tests - Add information on new TLS feature and behavior: - valkey-io/valkey#2999 - valkey-io/valkey#3020 --------- Signed-off-by: Yang Zhao <zymy701@gmail.com>
### Overview This PR adds support for automatic background TLS reloading, closes valkey-io#2649 TLS validity checks and fail-fast behavior on invalid certificates are handled separately in valkey-io#2999. - New configuration - `tls-auto-reload-interval <seconds>` - `0` disabled (default, backward compatible) - `>0` check interval in seconds - TLS materials change detection in background - SHA-256 fingerprint checking for certificate files - `inode + mtime` checking for CA certificate directories and key files - Skips reload if materials haven't changed `tlsCheckMaterialsAndUpdateCache` - TLS contexts reload - CPU-intensive certificate parsing happens in dedicated BIO worker thread `BIO_TLS_RELOAD` - Main thread never blocks, atomically swaps SSL contexts - Two-phase reload: background preparation `tlsConfigureAsync` + main thread application `tlsApplyPendingReload` **Note**: Original TLS load and reload still remain in main thread using `tlsConfigureSync`, including: - Initial TLS load (server startup) - Runtime reload via CONFIG SET --------- Signed-off-by: Yang Zhao <zymy701@gmail.com>
This PR adds the certificates validation at TLS load, rejects invalid (expired/not-yet-valid) certificates: Apply to all TLS config paths: - Server certificates `tls-cert-file` - Server-side client certificates `tls-client-cert-file` - CA certificate file `tls-ca-cert-file` - CA certificate directory `tls-ca-cert-dir` (now eagerly loaded to be consistent with file-based CAs) Apply to both scenarios: - Server startup (initial TLS load) - Runtime reload vis `CONFIG SET` ### Implementation - Added `isCertValid` function to check if an X509 certificate is within its validity period (not expired, not future-dated) - Added `areAllCaCertsValid` function to iterate through all loaded CA certificates and validate them - Added `loadCaCertDir` function to eagerly load all certificates from a directory into the X509_STORE - Modified `createSSLContext` to validate: - Server/client certificates immediately after loading - All CA certificates after loading from file/directory ### Test results #### 1. Server startup (initial TLS load) ``` tls-cert-file ./tests/tls/server-expired.crt 41522:M 31 Dec 2025 16:13:18.851 # Server TLS certificate is invalid. Aborting TLS configuration. 41522:M 31 Dec 2025 16:13:18.851 # Failed to configure TLS. Check logs for more info. tls-client-cert-file ./tests/tls/client-expired.crt 41557:M 31 Dec 2025 16:14:43.296 # Client TLS certificate is invalid. Aborting TLS configuration. 41557:M 31 Dec 2025 16:14:43.296 # Failed to configure TLS. Check logs for more info. tls-ca-cert-file ./tests/tls/ca-expired.crt tls-ca-cert-dir ./tests/tls/ca-expired 41567:M 31 Dec 2025 16:15:15.635 # One or more loaded CA certificates are invalid. Aborting TLS configuration. 41567:M 31 Dec 2025 16:15:15.635 # Failed to configure TLS. Check logs for more info. ``` #### 2. Runtime reload via CONFIG SET ``` 127.0.0.1:6379> config set tls-cert-file ./tests/tls/server-expired.crt (error) ERR CONFIG SET failed (possibly related to argument 'tls-cert-file') - Unable to update TLS configuration. Check server logs. 62975:M 02 Jan 2026 20:10:43.588 # Server TLS certificate is invalid. Aborting TLS configuration. 62975:M 02 Jan 2026 20:10:43.588 # Failed applying new configuration. Possibly related to new tls-cert-file setting. Restoring previous settings. 127.0.0.1:6379> config set tls-client-cert-file ./tests/tls/client-expired.crt (error) ERR CONFIG SET failed (possibly related to argument 'tls-client-cert-file') - Unable to update TLS configuration. Check server logs. 62975:M 02 Jan 2026 20:10:57.972 # Client TLS certificate is invalid. Aborting TLS configuration. 62975:M 02 Jan 2026 20:10:57.972 # Failed applying new configuration. Possibly related to new tls-client-cert-file setting. Restoring previous settings. 127.0.0.1:6379> config set tls-ca-cert-file ./tests/tls/ca-expired.crt 127.0.0.1:6379> config set tls-ca-cert-dir ./tests/tls/ca-expired (error) ERR CONFIG SET failed (possibly related to argument 'tls-ca-cert-file') - Unable to update TLS configuration. Check server logs. 62975:M 02 Jan 2026 20:10:50.175 # One or more loaded CA certificates are invalid. Aborting TLS configuration. 62975:M 02 Jan 2026 20:10:50.175 # Failed applying new configuration. Possibly related to new tls-ca-cert-file setting. Restoring previous settings. ``` --------- Signed-off-by: Yang Zhao <zymy701@gmail.com>
### Overview This PR adds support for automatic background TLS reloading, closes valkey-io#2649 TLS validity checks and fail-fast behavior on invalid certificates are handled separately in valkey-io#2999. - New configuration - `tls-auto-reload-interval <seconds>` - `0` disabled (default, backward compatible) - `>0` check interval in seconds - TLS materials change detection in background - SHA-256 fingerprint checking for certificate files - `inode + mtime` checking for CA certificate directories and key files - Skips reload if materials haven't changed `tlsCheckMaterialsAndUpdateCache` - TLS contexts reload - CPU-intensive certificate parsing happens in dedicated BIO worker thread `BIO_TLS_RELOAD` - Main thread never blocks, atomically swaps SSL contexts - Two-phase reload: background preparation `tlsConfigureAsync` + main thread application `tlsApplyPendingReload` **Note**: Original TLS load and reload still remain in main thread using `tlsConfigureSync`, including: - Initial TLS load (server startup) - Runtime reload via CONFIG SET --------- Signed-off-by: Yang Zhao <zymy701@gmail.com> Signed-off-by: Harkrishn Patro <bunty.hari@gmail.com>
This PR adds the certificates validation at TLS load, rejects invalid (expired/not-yet-valid) certificates: Apply to all TLS config paths: - Server certificates `tls-cert-file` - Server-side client certificates `tls-client-cert-file` - CA certificate file `tls-ca-cert-file` - CA certificate directory `tls-ca-cert-dir` (now eagerly loaded to be consistent with file-based CAs) Apply to both scenarios: - Server startup (initial TLS load) - Runtime reload vis `CONFIG SET` ### Implementation - Added `isCertValid` function to check if an X509 certificate is within its validity period (not expired, not future-dated) - Added `areAllCaCertsValid` function to iterate through all loaded CA certificates and validate them - Added `loadCaCertDir` function to eagerly load all certificates from a directory into the X509_STORE - Modified `createSSLContext` to validate: - Server/client certificates immediately after loading - All CA certificates after loading from file/directory ### Test results #### 1. Server startup (initial TLS load) ``` tls-cert-file ./tests/tls/server-expired.crt 41522:M 31 Dec 2025 16:13:18.851 # Server TLS certificate is invalid. Aborting TLS configuration. 41522:M 31 Dec 2025 16:13:18.851 # Failed to configure TLS. Check logs for more info. tls-client-cert-file ./tests/tls/client-expired.crt 41557:M 31 Dec 2025 16:14:43.296 # Client TLS certificate is invalid. Aborting TLS configuration. 41557:M 31 Dec 2025 16:14:43.296 # Failed to configure TLS. Check logs for more info. tls-ca-cert-file ./tests/tls/ca-expired.crt tls-ca-cert-dir ./tests/tls/ca-expired 41567:M 31 Dec 2025 16:15:15.635 # One or more loaded CA certificates are invalid. Aborting TLS configuration. 41567:M 31 Dec 2025 16:15:15.635 # Failed to configure TLS. Check logs for more info. ``` #### 2. Runtime reload via CONFIG SET ``` 127.0.0.1:6379> config set tls-cert-file ./tests/tls/server-expired.crt (error) ERR CONFIG SET failed (possibly related to argument 'tls-cert-file') - Unable to update TLS configuration. Check server logs. 62975:M 02 Jan 2026 20:10:43.588 # Server TLS certificate is invalid. Aborting TLS configuration. 62975:M 02 Jan 2026 20:10:43.588 # Failed applying new configuration. Possibly related to new tls-cert-file setting. Restoring previous settings. 127.0.0.1:6379> config set tls-client-cert-file ./tests/tls/client-expired.crt (error) ERR CONFIG SET failed (possibly related to argument 'tls-client-cert-file') - Unable to update TLS configuration. Check server logs. 62975:M 02 Jan 2026 20:10:57.972 # Client TLS certificate is invalid. Aborting TLS configuration. 62975:M 02 Jan 2026 20:10:57.972 # Failed applying new configuration. Possibly related to new tls-client-cert-file setting. Restoring previous settings. 127.0.0.1:6379> config set tls-ca-cert-file ./tests/tls/ca-expired.crt 127.0.0.1:6379> config set tls-ca-cert-dir ./tests/tls/ca-expired (error) ERR CONFIG SET failed (possibly related to argument 'tls-ca-cert-file') - Unable to update TLS configuration. Check server logs. 62975:M 02 Jan 2026 20:10:50.175 # One or more loaded CA certificates are invalid. Aborting TLS configuration. 62975:M 02 Jan 2026 20:10:50.175 # Failed applying new configuration. Possibly related to new tls-ca-cert-file setting. Restoring previous settings. ``` --------- Signed-off-by: Yang Zhao <zymy701@gmail.com> Signed-off-by: Harkrishn Patro <bunty.hari@gmail.com>
|
@yang-z-o @zuiderkwast We talked about this PR while reviewing #2999, and it came up that this might also be considered a breaking change. We were aligned that it's the right long term behavior that we should reject, but we decided to revert this for 9.1 and move it to 10.0. We'll do this after the RC though. |
β¦#3572) This reverts commit eb3f465. As discussed in the TSC Core Meeting, this PR reverts #2999 from Valkey 9.1. We plan to do in next major version as it's a breaking change. **Note**: Merge conflicts in `src/tls.c` includes were resolved due to overlapping changes from PR #2913, which added `tlsUpdateCertInfoFromDir()` with shared header dependencies. Signed-off-by: Sarthak Aggarwal <sarthagg@amazon.com>
β¦o#3572) Revert of valkey-io#2999 (Prevent invalid TLS certificates from being loaded) was decided by TSC as it is a breaking change, deferred to next major. Signed-off-by: Sarthak Aggarwal <sarthagg@amazon.com>
### Problem When `tls-ca-cert-dir` is configured to a directory that exists but contains no certificates, the server starts successfully and logs no warning. This is because `SSL_CTX_load_verify_locations` with a directory argument is lazy, OpenSSL registers the path without scanning it, returning success even for an empty directory. The failure is only discovered at handshake time, when every connecting client sees `Server closed the connection` and the server logs `Error accepting a client connection: error:0A000086:SSL routines::certificate verify failed`. This is a silent misconfiguration that gives no actionable signal at startup or `CONFIG SET` time. ### Fix `loadCaCertDir` (introduced in #2999 for validity checking) already eagerly scans the directory to load certs. This PR extends it to count the certs loaded and fail immediately if zero certs were found. ### Behavior change Scenario 1: Startup with empty `tls-ca-cert-dir`. Before: Server starts, clients fail at handshake. After: Server refuses to start with a clear log message. Scenario 2: `CONFIG SET tls-ca-cert-dir <empty-dir>` Before: Returns OK, clients fail at handshake. After: Returns error, previous config preserved. Signed-off-by: Yang Zhao <zymy701@gmail.com>
|
@zuiderkwast @yang-z-o Just to ping folks. We decided to revert this from 9.1 since this felt like a breaking change, we then merged this into unstable thinking the next release was 10.0, but we decided to 9.2. Should we revert this just in 9.2 release branch again? |
Oh, I missed that and forgot we decided that. I saw it when going over the 9.2 board and saw it was ready. :/ Sure, we can revert it again then. (I don't think it's a problem to release it though. IMO, users relying on misconfiguration, such as empty files/directories, isn't something we need to worry about regarding semver. It's like if anyone is relying on a bug - that doesn't prevent us from fixing bugs.) |
### Problem When `tls-ca-cert-dir` is configured to a directory that exists but contains no certificates, the server starts successfully and logs no warning. This is because `SSL_CTX_load_verify_locations` with a directory argument is lazy, OpenSSL registers the path without scanning it, returning success even for an empty directory. The failure is only discovered at handshake time, when every connecting client sees `Server closed the connection` and the server logs `Error accepting a client connection: error:0A000086:SSL routines::certificate verify failed`. This is a silent misconfiguration that gives no actionable signal at startup or `CONFIG SET` time. ### Fix `loadCaCertDir` (introduced in valkey-io#2999 for validity checking) already eagerly scans the directory to load certs. This PR extends it to count the certs loaded and fail immediately if zero certs were found. ### Behavior change Scenario 1: Startup with empty `tls-ca-cert-dir`. Before: Server starts, clients fail at handshake. After: Server refuses to start with a clear log message. Scenario 2: `CONFIG SET tls-ca-cert-dir <empty-dir>` Before: Returns OK, clients fail at handshake. After: Returns error, previous config preserved. Signed-off-by: Yang Zhao <zymy701@gmail.com>
Fixes #5506 Since 9.2.0, valkey-server refuses to configure TLS when any certificate it loads is outside its validity period, so an expired old root left in a `tls-ca-cert-file` bundle after a CA rotation stops the server from starting, even though OpenSSL would never build a chain through that cert. 9.1.3 starts fine on the same files. The checks came from #2999 and #3522, which the TSC decided were a breaking change: #3572 reverted them from 9.1 and the plan was to land them in the next major, but the next release turned out to be 9.2 rather than 10.0 and the revert was never carried over. These two commits revert them from the 9.2 branch so 9.x behaves like 9.1 again; `unstable` keeps them for 10.0. This was generated by AI but verified, with love, by a human. --------- Signed-off-by: Madelyn Olson <madelyneolson@gmail.com>
> [!CAUTION] > **Opened ready with 1 unresolved security item** (`force_ready` was set): release impact may require higher urgency or security treatment. Security classification is human-owned: settle the urgency and the Security Fixes entries (re-run the cut with them, or edit through review comments) before merging. All 4 flagged items: notes flagged low-confidence; AI triaged PRs without release-notes (confirm include/exclude); release impact may require higher urgency or security treatment; notes credited to a backport. Cuts **9.2.1** onto release line `9.2`. - Promotes the release notes into a dated section, bumps `src/version.h`, and refreshes the running contributor list. - Release notes computed over the range below (`9.2.0..9dc1926`): ``` mode: ga source_ref: 9.2 target_branch: 9.2 base: 9.2.0 @ d23684e head: 9dc1926 @ 9dc1926 ``` ###β οΈ Notes to double-check The generator flagged these notes low-confidence; confirm category and wording before merging: | PR | Category | Why flagged | |----|----------|-------------| | #5366 | Bug Fixes | explicit impact scope omitted: Linux | ###β οΈ Release impact and urgency need review Code detected release-impact terms (availability, memory safety, data integrity, access control, protocol, compatibility) in these PRs; this forces review, it is not a severity classification. The requested urgency is **MODERATE**. Confirm whether these changes require `HIGH`, `CRITICAL`, or `SECURITY`, and whether any need a hand-authored **Security Fixes** entry: <details> <summary>7 PR(s) with impact signals</summary> | PR | Title | Signal | |----|-------|--------| | [#5366](#5366) | Fix double mutex unlock when shrinking IO threads | undefined behavior or unsafe arithmetic | | [#5468](#5468) | Don't resend data to a close_asap client after an IO-thread write | server crash, assertion, or availability failure | | [#5112](#5112) | Pause rehashing of path hash payloads during background iteration | server crash, assertion, or availability failure | | [#4712](#4712) | Fix execGetKeys() dropping the first 256 keys past the static buffer | server crash, assertion, or availability failure | | [#4675](#4675) | Fix cluster bus self-forget use-after-free | memory-safety risk | | [#5452](#5452) | Ignore replica claims that would close a replicaof cycle | server crash, assertion, or availability failure | | [#3245](#3245) | Fix assertion crash in processIOThreadsReadDone when DONT_PARSE client has stale argc | server crash, assertion, or availability failure | </details> ### Triage summary - 8 PR(s) triaged into the notes - 3 PR(s) triaged out Per-PR verdicts (with reasons) are in the run's `triage-verdicts` artifact. To include a wrongly-dropped PR, label it `release-notes` and re-cut. ###β οΈ Notes credited to a backport (original PR not recovered) These notes credit the backport PR, not the change's author; confirm the original PR and correct the credit before merging: | Backport PR | Title | |-------------|-------| | [#5550](#5550) | [9.2] Revert fail-fast TLS certificate checks (#2999, #3522) | ### Wording carried from other release lines A backported change is cut once per release line, and each cut words its notes independently. To keep one commit from being described differently per branch, a note whose source PR another line already published takes that published wording verbatim (joined on the trailing `(#N)`, which always names the original source PR). Only the text is reused; the category, the `by @handle` credit, and the `(#N)` are this cut's own. - Release lines consulted: `9.1`, `9.0`, `8.1`, `8.0`, `7.2`, `7.0`, `6.2`, `6.0`, `5.0`, `4.0`, `3.2`, `3.0`, `2.8`, `2.6`, `2.4`, `2.2` - No note needed its wording replaced. *Generated by valkey-ci-agent. Review before merging into the release line.* Signed-off-by: valkeyrie-bot[bot] <3692572+valkeyrie-bot[bot]@users.noreply.github.com> Co-authored-by: valkeyrie-bot[bot] <3692572+valkeyrie-bot[bot]@users.noreply.github.com>
Closes #2997
Overview
This PR adds the certificates validation at TLS load, rejects invalid (expired/not-yet-valid) certificates:
Apply to all TLS config paths:
tls-cert-filetls-client-cert-filetls-ca-cert-filetls-ca-cert-dir(now eagerly loaded to be consistent with file-based CAs)Apply to both scenarios:
CONFIG SETImplementation
isCertValidfunction to check if an X509 certificate is within its validity period (not expired, not future-dated)areAllCaCertsValidfunction to iterate through all loaded CA certificates and validate themloadCaCertDirfunction to eagerly load all certificates from a directory into the X509_STOREcreateSSLContextto validate:Test results
1. Server startup (initial TLS load)
2. Runtime reload via CONFIG SET