Skip to content

Fix NoHostnameTlsVerifier for rustls 0.23.24 and above - #3861

Merged
abonander merged 1 commit into
transact-rs:mainfrom
elichai:rustls-0.23.24
Jul 2, 2025
Merged

abonander merged 1 commit into
transact-rs:mainfrom
elichai:rustls-0.23.24

Conversation

@elichai

@elichai elichai commented May 15, 2025

Copy link
Copy Markdown
Contributor

From Rustls 0.23.24 release:

New feature: More detailed and helpful error reporting for common certificate errors, such as name mismatches and certificate expiry. Users who std::fmt::Display the rustls Error type will take advantage of this automatically. Users handling CertificateError variants individually should note the new variants, such as CertificateError::NotValidForNameContext (compare CertificateError::NotValidForName).

This fixes the NoHostnameTlsVerifier by also matching on this new variant.

Fixes #3793

@abonander

Copy link
Copy Markdown
Collaborator

@elichai can you rebase, please? I think I fixed the error by generating new certificates in #3914.

@elichai

elichai commented Jul 1, 2025

Copy link
Copy Markdown
Contributor Author

@elichai can you rebase, please? I think I fixed the error by generating new certificates in #3914.

Done :)

@abonander
abonander merged commit 2970559 into transact-rs:main Jul 2, 2025
@elichai
elichai deleted the rustls-0.23.24 branch October 6, 2025 09:46
@elichai

elichai commented Oct 6, 2025

Copy link
Copy Markdown
Contributor Author

Any chance we can get this into a release? @abonander
Thanks!!

kiwidream added a commit to Qbit-Org/qbit-mining-bootstrap that referenced this pull request Oct 28, 2026
…er name, backporting transact-rs/sqlx#3861 (#713) (#807)

Cause. sqlx-postgres runs sslmode=verify-ca through sqlx-core's
NoHostnameTlsVerifier. It runs rustls's WebPKI checks, which verify the
chain before the name, and accepts the certificate when the name is the
only failure. sqlx-core 0.8.6 matched only CertificateError::
NotValidForName, but rustls 0.23.24 and later (the lock has 0.23.46)
report a name mismatch as NotValidForNameContext. So verify-ca refused a
CA-signed certificate for another name, as verify-full does. The load
harness's delay proxy is dialled as 127.0.0.1 with sslmode kept, so a
verify-ca run through it failed with "certificate not valid for name
127.0.0.1".

Change.
- vendor/sqlx-core carries transact-rs/sqlx#3861 (merge 2970559e, first
  released in 0.9.0) as upstream wrote it, without its Cargo.lock hunk:
  the verifier matches NotValidForName | NotValidForNameContext { .. },
  and sqlx-core's rustls requirement rises from 0.23.15 to 0.23.24, the
  first release with that variant (Cargo.toml.orig and the same
  requirement in Cargo.toml). Both lock files already build rustls
  0.23.46, so neither changes. Every other error and every other sslmode
  are unchanged, and verify-full still checks the name.
- vendor/sqlx-core/README.md records it as the second local change next
  to #4251, with its regression test and the 0.9.0 removal condition.
  The root Cargo.toml, the check script's docstring and the server's
  dev-dependency comment name both fixes.
- crates/qbit-prism-load/README.md's --database-url row says sslmode is
  kept, that verify-full refuses the certificate through the proxy at
  127.0.0.1, and that verify-ca passes. #713's interim advice to use
  sslmode=require is not needed with the fix.

The test. tests/sqlx_tls_verify_ca.rs runs sqlx-core's rustls client
against a rustls server over an in-memory socket, polling by hand, with
a CA, a server certificate it signs for db.internal only, and the same
certificate from an untrusted issuer, all Ed25519 from fixed seeds.
verify-ca at 127.0.0.1 connects; verify-full there is refused with
NotValidForNameContext for 127.0.0.1; both connect for db.internal; and
verify-ca still refuses the untrusted issuer as UnknownIssuer. Against
the unpatched verifier only the first fails, with the #713 error.

Co-authored-by: Claude Opus 5.5 <[email protected]>
ShukantPal added a commit to impalasys/talon that referenced this pull request Nov 9, 2026
A gateway configured with PostgreSQL `sslmode=verify-ca` still exits
before opening its listener after the Rustls provider initialization
fix. Cloud SQL logs show `SSLV3_ALERT_BAD_CERTIFICATE` before a database
username is received. The deployed CA matches the instance's current CA.

SQLx 0.8.6's Rustls `NoHostnameTlsVerifier` only suppresses
`CertificateError::NotValidForName`. Rustls 0.23.37 returns
`NotValidForNameContext`, so SQLx accidentally enforces hostname
matching under `verify-ca`. This is the upstream bug fixed in
[transact-rs/sqlx#3861](transact-rs/sqlx#3861),
whose fix is absent from the pinned 0.8.6 release. A local synthetic
connection reproduced the exact raw error:
```text
InvalidCertificate(NotValidForNameContext {
    expected: IpAddress(127.0.0.1),
    presented: ["DnsName(\"database.example.test\")"]
})
```

Select SQLx's supported `tls-native-tls` backend so the connection URL's
TLS policy works: `verify-ca` validates the certificate chain without
requiring a hostname match, and `verify-full` also validates the
hostname. In the Linux runtime this uses OpenSSL. The runtime Dockerfile
already installs `libssl-dev` in the builder and `libssl3` in the final
images. Linux binary-artifact consumers must likewise provide the system
OpenSSL libraries. IAM token acquisition and URL selection continue
through the existing shared connection path.

Strengthen the three real-binary startup regressions to require a
completed TLS session and a PostgreSQL startup packet. The earlier tests
stopped at ClientHello, which missed this certificate-validation
failure. Add negative cases for an untrusted CA and a hostname mismatch
under `verify-full`. Generate short-lived test certificates and
disposable keys in memory; rcgen, time, and tokio-rustls are dev
dependencies only. The fake server ends before database authentication,
so these tests do not claim to validate Cloud SQL IAM login.

Validation:
- With the existing Rustls backend and the strengthened tests: all three
trusted-CA startup cases failed; both rejection cases passed.
- With native TLS: all five complete-handshake cases passed, covering
gateway, worker, and node.
- Full Rust suite: 1,061 tests passed.
- Normal pre-push hook passed: locked Cargo metadata, formatting, binary
builds, and the full locked test suite.
- `git diff --check` passed.
- No production configuration, database ownership, IAM permissions, or
revision assignments were changed.

Requires security review before merge because this changes the
PostgreSQL TLS backend. After merge and successful main runtime-image
CI, publish the corrected image and update the deployment's Terraform
image pin. The current healthy revisions should keep serving until that
rollout; worker revision switching remains manual via gcloud.

Co-authored-by: Impala Systems <[email protected]>
kiwidream added a commit to Qbit-Org/qbit-mining-bootstrap that referenced this pull request Nov 12, 2026
…and CI checks vendored sqlx-core against upstream's .crate plus recorded patches (#825)

## Problem

#825 items 1 and 2, from #818's and #794's reviews:

1. **verify-ca without a root certificate passes startup.** Since #818, sqlx-postgres refuses every `sslmode=verify-ca` connect that has no `sslrootcert` (or `PGSSLROOTCERT`). But nothing at startup checked for it, so a `PRISM_DATABASE_URL` or peer URL like that passed `check-config` and startup, then failed every connect.
2. **Nothing ties `vendor/sqlx-core` to upstream.** `check_vendored_sqlx_core.py` checked that both lock files build the vendored copy and that its advisories are reviewed. It never checked the files, so an unrecorded edit to the vendored TLS code would pass CI.

## Change

### Startup refuses verify-ca without a root certificate (item 1)

- **Which settings.** `PRISM_DATABASE_URL` (frontend, `check-config`, the database commands and the public reader) and `PRISM_PEER_DATABASE_URL` / `PRISM_PEER_DATABASE_URL_FALLBACK` (the dual writer). Each is refused by name, with the connect's own message:
  > sslmode=verify-ca requires sslrootcert: verify-ca does not check the server's host name, so it trusts only the root certificates in sslrootcert, never the public roots; set sslrootcert, or use sslmode=verify-full
- **Resolved as the connect resolves them.** The mode and certificate come from SQLx's `PgConnectOptions::from_url`, the parser the connect itself uses, so there is no second parser: the URL's `sslmode` and `sslrootcert` win over `PGSSLMODE` and `PGSSLROOTCERT`. `ssl_root_cert` has no getter, so it is read back through `to_url_lossy()`, as the existing password check already does. An empty `sslrootcert` names no file, so it counts as none. The certificate file is not read here; a missing or unusable file is still the connect's error, as before.
- **Other modes are unchanged:** `verify-full`, `require`, `prefer`, `allow` and `disable`.
- **One more refusal.** `PRISM_DATABASE_URL` is now parsed with the client's parser, as the peer URLs already were. A URL the client can't use (for example `sslmode=sometimes`) is refused at startup, by name, instead of at the first connect.
- **`make doctor` shows it.** `scripts/check-env.sh` relays only known, value-free public-reader errors, so the new refusal showed as a generic image error. It now relays `public PRISM_DATABASE_URL: sslmode=verify-ca requires sslrootcert`.

### CI checks vendored sqlx-core against upstream's `.crate` plus recorded patches (item 2)

- **The recorded changes are patches.** `vendor/sqlx-core.patches/` holds one `diff -u` patch per change, each with a description above it:
  - `0001-sqlx-4251-poll-ready-waits-on-either-direction.patch` (transact-rs/sqlx#4251, #771);
  - `0002-sqlx-3861-verify-ca-accepts-not-valid-for-name-context.patch` (transact-rs/sqlx#3861, #713);
  - `0003-verify-ca-trusts-only-sslrootcert.patch` (the local change, #818).
- **CI rebuilds the tree from upstream and compares.** The step that checks the lock files now downloads `sqlx-core-0.8.6.crate` from static.crates.io, as the alert step downloads promtool. The checker:
  - refuses the `.crate` unless its SHA-256 is the `ee6798b1…68dc6` that `vendor/sqlx-core/README.md` records, and reads it in memory, never extracting it;
  - applies the patches in name order, each hunk only at the line it names and on exact context, with no offset and no fuzz;
  - accepts `\ No newline at end of file` only on a file's last line, so a marker can't join a line onto the next one that review reads as separate;
  - allows free text only before a patch's first file, and refuses anything else outside the hunks;
  - fails unless the result is `vendor/sqlx-core` byte for byte: no file differs, none is missing, none is a symbolic link, and none is added but `README.md` and `reviewed-advisories.toml`. The build reads neither of those: `Cargo.toml` names no `readme` or build script, and no source includes them.
- **A change to a patched file has to change its patch.** The review's edit, `tls_rustls.rs`'s verify-ca store to `let mut cert_store = public_roots();`, now fails. It passes only once a patch records it.
- **Recording a change.** `--unrecorded-diff` prints what the tree changes beyond the recorded patches, as the next patch. The README's "Changing this copy" gives the steps.
- **Two README rules.** The README must record the `.crate`'s SHA-256 exactly once, and it must name every patch file. The second keeps its change list in step with the patches; it checks nothing the README says.
- **Locally, no flag needed.** Without `--upstream-crate`, the script uses Cargo's cached `.crate` if there is one. Without one, it skips the source check and says so.
- **The Python tests use the real crate too.** The `python-tests` job downloads the same `.crate` and names it in `SQLX_CORE_UPSTREAM_CRATE`, so the real-tree tests run in CI. If it is named but missing, they fail rather than skip.
- **The manifest is gone.** I kept the earlier offline premise until review showed it was false: `ci.yml` already downloads promtool. The patch check covers everything `vendor/sqlx-core.sha256` did. The manifest's only extra was working without the `.crate`, which CI now always has. And its upstream/patched/added labels were the part offline CI could not check. Removing it also removes `--write-manifest` and its README gate, which a whitespace edit or a deleted manifest got past.

**What this proves, and what it doesn't.**
- **It proves** that `vendor/sqlx-core` is upstream 0.8.6 plus the recorded patches, byte for byte.
- **It doesn't judge the patches.** A patch that trusted the public roots again would pass, and so would a commit that changed both the code and its patch.
- **The control is review.** Review every change under `vendor/sqlx-core.patches/`, where each change to the vendored code is a diff against upstream. `sqlx_tls_verify_ca.rs` and `sqlx_tls_readiness.rs` check what the three changes do.
- **Two files are unchecked:** edits to the README and to `reviewed-advisories.toml`.

The README says the same.

## Tests run

All local. The Python checks and guards ran again on this head, after merging 3.x.x:

- **Vendored source:** `python3 -m unittest -v tests.test_check_vendored_sqlx_core`: 19 passed. I ran it with `SQLX_CORE_UPSTREAM_CRATE` set to a `.crate` freshly downloaded from static.crates.io, as CI sets it, and again with Cargo's cached copy. With the variable naming a missing file, the two real-tree tests fail. The tests cover:
  - the real tree is 0.8.6 plus the 3 patches: 4 of 100 files patched, README.md and reviewed-advisories.toml added;
  - the review's `public_roots()` edit to the real `tls_rustls.rs` fails, and passes only once its `--unrecorded-diff` is saved as a patch;
  - each of these fails: an edited upstream file, a patched file changed beyond its patch, a removed patch, an added source file, an added `build.rs`, `.cargo-ok`, a missing upstream file, a symbolic link, and a `.crate` other than the README's;
  - each of these patches is refused:
    - a hunk at another line, on other context, or with a `+` start that disagrees;
    - an insertion past a file's end, and a no-newline marker that joins two lines, even when the tree matches what a lenient applier makes of them;
    - a patch that creates a file, or touches a file upstream lacks or one outside the crate;
    - timestamps, two paths, short, long or miscounted hunks, text between hunks, a malformed header, or a hunk before any file;
    - no hunks, a file named twice, no final newline, or no file at all;
    - a stray file, an upper-case name, a symlink or a directory in `vendor/sqlx-core.patches/`;
  - a last line without a newline, on either side, round-trips through `--unrecorded-diff`;
  - edits to the README and to `reviewed-advisories.toml` pass. A README that records two `.crate` SHA-256s fails, and so does one that doesn't name a patch (without the hint to record a patch);
  - a `.crate` whose version is not the vendored `Cargo.toml`'s fails, and the error names the version;
  - without `--upstream-crate`, the script checks against Cargo's cached `.crate`, and skips the source check when there is none;
  - `ci.yml` downloads the `.crate` before the check and the Python shard, and passes it to both.
- **CI's step, run locally as written** (with `RUNNER_TEMP` set): it downloaded the `.crate` (SHA-256 `ee6798b1…68dc6`) and the check passed.
- **check-env:** `python3 -m unittest -v tests.test_check_env_public_credentials`: 13 passed. The new relay test fails without the `check-env.sh` line.
- **Startup, through the real binary** (unchanged by the follow-up commit; run on `d3cb5a92`):
  - `cargo test --locked -p qbit-prism-server --test config_cli --no-fail-fast`: 32 passed.
    - `verify_ca_without_a_root_certificate_fails_before_startup`: 10 cases over the three URL settings, covering `sslrootcert` absent or empty, `PGSSLROOTCERT`, `PGSSLMODE`, verify-full and require.
    - `startup_refuses_verify_ca_with_the_message_the_connect_gives`: startup's error equals `PgConnection::connect`'s, against a fake server that answers the SSLRequest with `S` and then sees no further byte.
  - `cargo test --locked -p qbit-prism-server --test public_database_config --no-fail-fast`: 6 passed, through `check-public-database-config` and `public-api`.
  - `cargo test --locked -p qbit-prism-server --lib config::`: 13 passed.
  - With the check disabled, the new verify-ca tests fail.
  - `cargo fmt --all -- --check` passes. `cargo clippy --locked -p qbit-prism-server --all-targets -- -D warnings` passes, apart from the three known `mismatched_lifetime_syntaxes` warnings in vendored sqlx-core.
- **Guards:** `check_vendored_sqlx_core.py --upstream-crate <.crate>`, `check_prism_pool_acquires.py`, `check_gate_env_reads.py`, `check_prism_settings.py`, `check_deleted_test_map.py` and `check_toolchain_pin.py` all pass. `bash -n scripts/check-env.sh` passes, and the workflow parses as YAML.

No new PostgreSQL-gated tests, settings or deleted tests.

## CI

- **Correction to this PR's first body.** CI does run `tests/test_check_vendored_sqlx_core.py`: `run_python_test_shard.py` discovers `tests/test_*.py`, and run 41738227640's shard 2 ran all 18 of the earlier tests. Its upstream test was skipped there because no `.crate` was cached. The `python-tests` job now downloads one.
- **What CI runs.** The lint job's vendored-sqlx-core step runs the source check against the downloaded `.crate`. CI's full suites cover the rest of `qbit-prism-server`.

## Known, and out of scope

- **A whitespace-only `sslrootcert`** passes the startup check. The connect still refuses it.
- **Some `tools.rs` commands skip the startup check.** They read the database URLs directly:
  - `fatal-state show` (`tools.rs:902`)
  - `submission-hold show` (`:1142`)
  - `candidates list` (`:1242`)
  - recover planning (`:1410`)
  - node-identity repersonalise's peer URLs (`:971-976`)

  The connect still refuses each with the same message.
- **`policy-transition`** checks the target configuration with the process's own `PG*` environment. It applies only the `PRISM_` and `QBIT_` settings from its `--to` file, and it can't change `PRISM_DATABASE_URL` anyway.

Item 3 (the release notes rewrite) is #832. Items 4 and 5 are not in this PR.

Refs #825

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Squashed commits:
d3cb5a9 3.x.x: startup refuses sslmode=verify-ca without a root certificate, and CI checks every vendored sqlx-core file against a manifest (#825)
3a7cfc8 3.x.x: CI checks vendored sqlx-core against upstream's .crate plus recorded patches, and doctor relays the verify-ca refusal (#825)
a078cba Merge remote-tracking branch 'origin/3.x.x' into orca/825-release-and-tls-guards
a18fba8 3.x.x: a vendored sqlx-core patch applies only as it reads: no line joined by a no-newline marker, no insertion past the end (#825)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
kiwidream added a commit to Qbit-Org/qbit-mining-bootstrap that referenced this pull request Nov 13, 2026
…and CI checks vendored sqlx-core against upstream's .crate plus recorded patches (#825)

## Problem

#825 items 1 and 2, from #818's and #794's reviews:

1. **verify-ca without a root certificate passes startup.** Since #818, sqlx-postgres refuses every `sslmode=verify-ca` connect that has no `sslrootcert` (or `PGSSLROOTCERT`). But nothing at startup checked for it, so a `PRISM_DATABASE_URL` or peer URL like that passed `check-config` and startup, then failed every connect.
2. **Nothing ties `vendor/sqlx-core` to upstream.** `check_vendored_sqlx_core.py` checked that both lock files build the vendored copy and that its advisories are reviewed. It never checked the files, so an unrecorded edit to the vendored TLS code would pass CI.

## Change

### Startup refuses verify-ca without a root certificate (item 1)

- **Which settings.** `PRISM_DATABASE_URL` (frontend, `check-config`, the database commands and the public reader) and `PRISM_PEER_DATABASE_URL` / `PRISM_PEER_DATABASE_URL_FALLBACK` (the dual writer). Each is refused by name, with the connect's own message:
  > sslmode=verify-ca requires sslrootcert: verify-ca does not check the server's host name, so it trusts only the root certificates in sslrootcert, never the public roots; set sslrootcert, or use sslmode=verify-full
- **Resolved as the connect resolves them.** The mode and certificate come from SQLx's `PgConnectOptions::from_url`, the parser the connect itself uses, so there is no second parser: the URL's `sslmode` and `sslrootcert` win over `PGSSLMODE` and `PGSSLROOTCERT`. `ssl_root_cert` has no getter, so it is read back through `to_url_lossy()`, as the existing password check already does. An empty `sslrootcert` names no file, so it counts as none. The certificate file is not read here; a missing or unusable file is still the connect's error, as before.
- **Other modes are unchanged:** `verify-full`, `require`, `prefer`, `allow` and `disable`.
- **One more refusal.** `PRISM_DATABASE_URL` is now parsed with the client's parser, as the peer URLs already were. A URL the client can't use (for example `sslmode=sometimes`) is refused at startup, by name, instead of at the first connect.
- **`make doctor` shows it.** `scripts/check-env.sh` relays only known, value-free public-reader errors, so the new refusal showed as a generic image error. It now relays `public PRISM_DATABASE_URL: sslmode=verify-ca requires sslrootcert`.

### CI checks vendored sqlx-core against upstream's `.crate` plus recorded patches (item 2)

- **The recorded changes are patches.** `vendor/sqlx-core.patches/` holds one `diff -u` patch per change, each with a description above it:
  - `0001-sqlx-4251-poll-ready-waits-on-either-direction.patch` (transact-rs/sqlx#4251, #771);
  - `0002-sqlx-3861-verify-ca-accepts-not-valid-for-name-context.patch` (transact-rs/sqlx#3861, #713);
  - `0003-verify-ca-trusts-only-sslrootcert.patch` (the local change, #818).
- **CI rebuilds the tree from upstream and compares.** The step that checks the lock files now downloads `sqlx-core-0.8.6.crate` from static.crates.io, as the alert step downloads promtool. The checker:
  - refuses the `.crate` unless its SHA-256 is the `ee6798b1…68dc6` that `vendor/sqlx-core/README.md` records, and reads it in memory, never extracting it;
  - applies the patches in name order, each hunk only at the line it names and on exact context, with no offset and no fuzz;
  - accepts `\ No newline at end of file` only on a file's last line, so a marker can't join a line onto the next one that review reads as separate;
  - allows free text only before a patch's first file, and refuses anything else outside the hunks;
  - fails unless the result is `vendor/sqlx-core` byte for byte: no file differs, none is missing, none is a symbolic link, and none is added but `README.md` and `reviewed-advisories.toml`. The build reads neither of those: `Cargo.toml` names no `readme` or build script, and no source includes them.
- **A change to a patched file has to change its patch.** The review's edit, `tls_rustls.rs`'s verify-ca store to `let mut cert_store = public_roots();`, now fails. It passes only once a patch records it.
- **Recording a change.** `--unrecorded-diff` prints what the tree changes beyond the recorded patches, as the next patch. The README's "Changing this copy" gives the steps.
- **Two README rules.** The README must record the `.crate`'s SHA-256 exactly once, and it must name every patch file. The second keeps its change list in step with the patches; it checks nothing the README says.
- **Locally, no flag needed.** Without `--upstream-crate`, the script uses Cargo's cached `.crate` if there is one. Without one, it skips the source check and says so.
- **The Python tests use the real crate too.** The `python-tests` job downloads the same `.crate` and names it in `SQLX_CORE_UPSTREAM_CRATE`, so the real-tree tests run in CI. If it is named but missing, they fail rather than skip. The download carries the same-tree skip's condition (#839), like the job's other steps, so a run that skips the shard skips it too.
- **The manifest is gone.** I kept the earlier offline premise until review showed it was false: `ci.yml` already downloads promtool. The patch check covers everything `vendor/sqlx-core.sha256` did. The manifest's only extra was working without the `.crate`, which CI now always has. And its upstream/patched/added labels were the part offline CI could not check. Removing it also removes `--write-manifest` and its README gate, which a whitespace edit or a deleted manifest got past.

**What this proves, and what it doesn't.**
- **It proves** that `vendor/sqlx-core` is upstream 0.8.6 plus the recorded patches, byte for byte.
- **It doesn't judge the patches.** A patch that trusted the public roots again would pass, and so would a commit that changed both the code and its patch.
- **The control is review.** Review every change under `vendor/sqlx-core.patches/`, where each change to the vendored code is a diff against upstream. `sqlx_tls_verify_ca.rs` and `sqlx_tls_readiness.rs` check what the three changes do.
- **Two files are unchecked:** edits to the README and to `reviewed-advisories.toml`.

The README says the same.

## Tests run

All local. The Python checks and guards ran again on this head, after merging 3.x.x:

- **Vendored source:** `python3 -m unittest -v tests.test_check_vendored_sqlx_core`: 19 passed. I ran it with `SQLX_CORE_UPSTREAM_CRATE` set to a `.crate` freshly downloaded from static.crates.io, as CI sets it, and again with Cargo's cached copy. With the variable naming a missing file, the two real-tree tests fail. The tests cover:
  - the real tree is 0.8.6 plus the 3 patches: 4 of 100 files patched, README.md and reviewed-advisories.toml added;
  - the review's `public_roots()` edit to the real `tls_rustls.rs` fails, and passes only once its `--unrecorded-diff` is saved as a patch;
  - each of these fails: an edited upstream file, a patched file changed beyond its patch, a removed patch, an added source file, an added `build.rs`, `.cargo-ok`, a missing upstream file, a symbolic link, and a `.crate` other than the README's;
  - each of these patches is refused:
    - a hunk at another line, on other context, or with a `+` start that disagrees;
    - an insertion past a file's end, and a no-newline marker that joins two lines, even when the tree matches what a lenient applier makes of them;
    - a patch that creates a file, or touches a file upstream lacks or one outside the crate;
    - timestamps, two paths, short, long or miscounted hunks, text between hunks, a malformed header, or a hunk before any file;
    - no hunks, a file named twice, no final newline, or no file at all;
    - a stray file, an upper-case name, a symlink or a directory in `vendor/sqlx-core.patches/`;
  - a last line without a newline, on either side, round-trips through `--unrecorded-diff`;
  - edits to the README and to `reviewed-advisories.toml` pass. A README that records two `.crate` SHA-256s fails, and so does one that doesn't name a patch (without the hint to record a patch);
  - a `.crate` whose version is not the vendored `Cargo.toml`'s fails, and the error names the version;
  - without `--upstream-crate`, the script checks against Cargo's cached `.crate`, and skips the source check when there is none;
  - `ci.yml` downloads the `.crate` before the check and the Python shard, and passes it to both.
- **CI's step, run locally as written** (with `RUNNER_TEMP` set): it downloaded the `.crate` (SHA-256 `ee6798b1…68dc6`) and the check passed.
- **check-env:** `python3 -m unittest -v tests.test_check_env_public_credentials`: 13 passed. The new relay test fails without the `check-env.sh` line.
- **Startup, through the real binary** (unchanged by the follow-up commit; run on `d3cb5a92`):
  - `cargo test --locked -p qbit-prism-server --test config_cli --no-fail-fast`: 32 passed.
    - `verify_ca_without_a_root_certificate_fails_before_startup`: 10 cases over the three URL settings, covering `sslrootcert` absent or empty, `PGSSLROOTCERT`, `PGSSLMODE`, verify-full and require.
    - `startup_refuses_verify_ca_with_the_message_the_connect_gives`: startup's error equals `PgConnection::connect`'s, against a fake server that answers the SSLRequest with `S` and then sees no further byte.
  - `cargo test --locked -p qbit-prism-server --test public_database_config --no-fail-fast`: 6 passed, through `check-public-database-config` and `public-api`.
  - `cargo test --locked -p qbit-prism-server --lib config::`: 13 passed.
  - With the check disabled, the new verify-ca tests fail.
  - `cargo fmt --all -- --check` passes. `cargo clippy --locked -p qbit-prism-server --all-targets -- -D warnings` passes, apart from the three known `mismatched_lifetime_syntaxes` warnings in vendored sqlx-core.
- **Guards:** `check_vendored_sqlx_core.py --upstream-crate <.crate>`, `check_prism_pool_acquires.py`, `check_gate_env_reads.py`, `check_prism_settings.py`, `check_deleted_test_map.py` and `check_toolchain_pin.py` all pass. `bash -n scripts/check-env.sh` passes, and the workflow parses as YAML.

No new PostgreSQL-gated tests, settings or deleted tests.

## CI

- **Correction to this PR's first body.** CI does run `tests/test_check_vendored_sqlx_core.py`: `run_python_test_shard.py` discovers `tests/test_*.py`, and run 41738227640's shard 2 ran all 18 of the earlier tests. Its upstream test was skipped there because no `.crate` was cached. The `python-tests` job now downloads one.
- **What CI runs.** The lint job's vendored-sqlx-core step runs the source check against the downloaded `.crate`. CI's full suites cover the rest of `qbit-prism-server`.

## Known, and out of scope

- **A whitespace-only `sslrootcert`** passes the startup check. The connect still refuses it.
- **Some `tools.rs` commands skip the startup check.** They read the database URLs directly:
  - `fatal-state show` (`tools.rs:902`)
  - `submission-hold show` (`:1142`)
  - `candidates list` (`:1242`)
  - recover planning (`:1410`)
  - node-identity repersonalise's peer URLs (`:971-976`)

  The connect still refuses each with the same message.
- **`policy-transition`** checks the target configuration with the process's own `PG*` environment. It applies only the `PRISM_` and `QBIT_` settings from its `--to` file, and it can't change `PRISM_DATABASE_URL` anyway.

Item 3 (the release notes rewrite) is #832. Items 4 and 5 are not in this PR.

Refs #825

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Squashed commits:
af6780d 3.x.x: startup refuses sslmode=verify-ca without a root certificate, and CI checks vendored sqlx-core against upstream's .crate plus recorded patches (#825)
64ee188 Merge remote-tracking branch 'origin/3.x.x' into orca/825-release-and-tls-guards
12b8887 3.x.x: python-tests downloads upstream sqlx-core only in a run that runs the shard (#825)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
cjorge-thegraph added a commit to graphprotocol/dipper that referenced this pull request Nov 21, 2026
)

**`sslmode=verify-ca` now skips the hostname check, as libpq does**, so
dipper can verify a Cloud SQL private-IP instance's certificate.

## Problem
- A Cloud SQL private-IP (PSA) instance's server certificate carries
**one name only: its DNS name with a trailing dot**, e.g.
`DNS:d0b150ab5022.76ws9odrdkfn.us-central1.sql-psa.goog.`. rustls
(webpki) never accepts a presented name with a trailing dot, so
`verify-full` can't connect, whatever the URL says.
- `verify-ca` should be the answer (check the chain against
`sslrootcert`, skip the name), but with sqlx 0.8 it behaves as
`verify-full`. sqlx's `NoHostnameTlsVerifier` swallows only
`CertificateError::NotValidForName`, while rustls ≥ 0.23.24 returns
`CertificateError::NotValidForNameContext`. dipper's own test
`verify_ca_still_checks_the_hostname` recorded exactly this and said to
update the docs once a later sqlx fixed it.
- Seen on testnet's dipper on Cloud SQL (2026-11-21): `invalid peer
certificate: certificate not valid for name "<host>"; certificate is
only valid for DnsName("<host>.")`. It runs with `sslmode=require`
(encrypted, unverified) until this lands.

## Fix
- **sqlx 0.8.2 → 0.9.1.** sqlx fixed the verifier upstream
(transact-rs/sqlx#3861, *Fix NoHostnameTlsVerifier for rustls 0.23.24
and above*); it shipped in 0.9.0 and no 0.8.x has it. sqlx 0.8 has no
hook to plug in a verifier of dipper's own, so the bump is the fix.
- **Rust 1.91.0 → 1.94.0** (`rust-toolchain.toml`, both Dockerfiles;
`rust:1.94.0-slim-bookworm` exists), which sqlx 0.9 requires.
- **sqlx 0.9 API changes:**
  - `ArgumentBuffer` has no lifetime.
- `[u8; N]` now also implements `Type`/`Encode` for `Any`, so the bytea
new-types name `Postgres`.
- The one dynamically built query is wrapped in `AssertSqlSafe`, because
only numbered placeholders are interpolated and the values are bound.
- **Docs:** `db.url` describes `verify-ca` as it now behaves, with Cloud
SQL as the example.

## Tests
- `verify_ca_still_checks_the_hostname` →
**`verify_ca_skips_the_hostname_check`**.
- New: **`verify_ca_accepts_a_cloud_sql_certificate`** (a CA-signed
certificate whose only name has a trailing dot),
**`verify_full_refuses_a_cloud_sql_certificate_even_by_its_name`** (why
`verify-ca` is the mode to use there) and
**`verify_ca_still_refuses_a_certificate_from_an_unknown_ca`** (the
chain is still checked).
- Local, Rust 1.94.0:
- `cargo clippy --all-targets -- -D warnings --force-warn deprecated -D
dead-code`: clean.
  - `cargo +nightly fmt --check`: clean.
  - Unit tests: **1085 passed, 0 failed** (`db::tests`: 12/12).
- Integration tests (`tests::it_`): **12 passed, 0 failed** (pgtemp
Postgres); the public-API integration tests (`--test '*'`) run in CI.

## Not in this PR
- No release. After it's released, the testnet deployment switches
`db.url` from `sslmode=require` to `sslmode=verify-ca`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

NoHostnameTlsVerifier does not work with rustls v0.23.24 or later

2 participants