Repository navigation
fix(tls): trust the OS certificate store in addition to the bundled roots - #628
Open
hogan-yuan wants to merge 5 commits into
Open
hogan-yuan wants to merge 5 commits into
hogan-yuan wants to merge 5 commits into
Conversation
This was referenced Nov 18, 2026
hogan-yuan
added a commit
that referenced
this pull request
Nov 19, 2026
No workflow in this repo declares a `concurrency` group, so every push to a pull request queues another full CI matrix and **the run it replaced keeps its place in line**. ## What that looked like Observed while iterating on #627 and #628: ``` 全仓库最近 12 次运行:queued=6, completed=6 最近一次真正完成的运行:186 分钟前 最老的排队运行:187 分钟 ``` Six runs queued at once; four were for commits that were no longer any branch's HEAD. Nothing started for roughly three hours. Cancelling the four obsolete runs by hand is what got the queue moving again: ``` 44282677026 b18750b superseded → cancelled 44266307848 b846aea superseded → cancelled 44265659864 acc1c25 superseded → cancelled 44259650117 3e99304 superseded → cancelled ``` With nine jobs fanning out across a wide platform matrix, each obsolete run is a lot of runner time spent on a commit nobody is waiting for. ## The change ```yaml concurrency: group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} cancel-in-progress: true ``` `github.head_ref` is set **only** for `pull_request` events, so: - **a pull request** groups by its branch — a new commit cancels the run for the commit it replaced - **a push to main** falls back to `github.run_id`, which is unique per run, so those get a group of their own: never cancelled, and never made to queue behind each other either That second half matters. The naive `group: ${{ github.workflow }}-${{ github.ref }}` would put every main push in one group — either cancelling a main run that a later commit supersedes, or serialising them. ## Why only `ci.yml` The other five workflows should not cancel, so they are untouched: | workflow | trigger | why not | |---|---|---| | `release.yml` | push to `release` | publishes artifacts; aborting halfway is worse than running twice | | `docs.yml` | push to `release` | deploys docs; same | | `check_docs.yml` | push to `main` | main only, infrequent | | `close-slate-issues.yml` | weekly cron | nothing to supersede | | `issue-notify.yml` | issue opened | nothing to supersede | ## Related, deliberately not bundled - **longbridge-mcp** has no concurrency group in any of its four workflows. - **longbridge-terminal** has one in three of seven, but not in `test.yml`. Both are worth the same change, but neither has shown this backlog — their CI is small where this one fans out — so they belong in their own PRs rather than here. ## Honest note on how this came up A good part of the backlog was self-inflicted: I pushed small follow-up commits to #627 and #628 seven times while reviewing them, and in a repo with no concurrency group each of those reset a full matrix. The fix is worth having regardless, but the measurement above is as much a description of my own push cadence as of the CI configuration. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
…oots Behind a corporate TLS-inspecting proxy every HTTPS call failed with `invalid peer certificate: UnknownIssuer`, including `longbridge auth login` at `/oauth2/register`, while other clients on the same machine worked. The proxy's root CA is installed in the OS trust store, but the SDK asked reqwest and tokio-tungstenite for the bundled webpki roots only, so the OS store was never consulted and no environment variable could add to it. Enable the native-roots feature alongside the webpki one in the HTTP client, the geo lookup and the WebSocket client. Both libraries add the two sets to the same root store rather than choosing between them, and tokio-tungstenite explicitly treats an empty native store as non-fatal when webpki roots are also present, so this is a superset of what was trusted before: a machine with an empty or unreadable OS store behaves exactly as it did. No code change is needed — each root set defaults to enabled when its feature is on. Verified on macOS that a client built with the native roots alone reaches openapi.longbridge.com, as does one built with the webpki roots alone, so neither set is load-bearing by itself. The proxy case itself could not be reproduced here; it follows from the proxy CA being in the OS store, which the reporter confirmed by reaching the same endpoint with PowerShell. Cargo.lock is unchanged: rustls-native-certs was already in the graph. Refs longbridge/developers#1153
Trusting the OS certificate store made every WebSocket connection pay for reading it. `connect_async` takes no connector, so tokio-tungstenite builds a fresh `rustls::ClientConfig` per connection, and `rustls-native-certs` does not cache: a measured `load_native_certs()` on macOS costs ~90ms every call (135ms cold, 89-95ms warm, 163 certs). The SDK holds two of these connections and rebuilds them on every reconnect, so a flapping network would pay it over and over. Build the connector once in a `LazyLock` and hand it to `connect_async_tls_with_config`. The root set is assembled exactly as tokio-tungstenite 0.27 assembles its own: native certs added first, with loading errors logged and an empty result tolerated, then the bundled webpki roots unioned on top. So what is trusted does not change; only how often it is computed does. Verified by counting initialisations across three contexts: the native store is read once, not three times. rustls becomes a direct dependency, so name the crypto provider explicitly rather than relying on another crate in the graph to turn one on: `ring` is what the workspace already resolves to via reqwest, and aws-lc-rs is absent from the lockfile, so there is no ambiguity and the resolved feature set for the main build is unchanged. Without a provider feature `ClientConfig::builder()` panics at runtime, which would have made longbridge-wscli unusable on its own. Cargo.lock is unchanged.
Same defect as the WebSocket connector, in a path the first review missed. `create_http_client` is called from three places, and one of them is `refresh_token`, which runs for the lifetime of the process. Each call did a full `reqwest::Client::builder().build()`, and now that the TLS config trusts the OS certificate store that build reads the platform trust store synchronously. Measured on macOS: a fresh `build()` costs 90-145ms, so every token refresh paid it. Behind a `LazyLock` the first call takes 160ms and the next takes 333ns -- it is a clone of an internally reference-counted handle, which also means refreshes now share a connection pool instead of discarding one each time. A failed build is deliberately not cached as a failure: the next call rebuilds so the caller still receives the real `reqwest::Error` rather than a sticky one from startup. No test asserts the timing; a wall-clock assertion would be flaky in CI, and `LazyLock` already guarantees the build happens once.
Making wsclient a direct dependent of webpki-roots to build the shared connector added `webpki-roots = "0.26"`, following what tokio-tungstenite uses. That is not what the rest of the stack uses: hyper-rustls, and so reqwest, resolve webpki-roots 1.0. The result was two copies of the crate in every binary, with the WebSocket path trusting a different, older CA snapshot than the HTTP path. Move the direct dependency to `webpki-roots = "1"`, and drop `rustls-tls-webpki-roots` from tokio-tungstenite, which is what pulled the 0.26 copy. Both of its root features exist only to gate its own root loading, which this crate never reaches now that every connection passes an explicit connector; one of them still has to be on for `Connector::Rustls` and `connect_async_tls_with_config` to exist, so `rustls-tls-native-roots` stays. Its dependency, rustls-native-certs, is one we already take directly. Verified: the lockfile now resolves a single webpki-roots (1.0.6), the connector builds with 136 roots, and a real wss:// handshake against the test gateway still completes. Also correcting something stated in the first commit of this branch: "Cargo.lock is unchanged" says nothing in this repo, because `/Cargo.lock` is in .gitignore and is not tracked at all. It reads like a verified claim about dependency stability and is not one. (longbridge-terminal does track its lockfile, so the equivalent statement there is real.)
The entry described the change as strictly additive -- two root sets unioned, nothing previously working breaks -- which is true and only half the story. Trusting the platform store also means a CA installed on the machine can now intercept SDK traffic, where before it could not. That belongs in the changelog, not only in the pull request: the people who need to know are the ones reading release notes, not reviewing the diff.
hogan-yuan
force-pushed
the
fix/tls-trust-os-root-store
branch
from
November 19, 2026 02:05
6723a8a to
24c1a57
Compare
This branch has not been deployed
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.
Behind a corporate TLS-inspecting proxy (Zscaler and the like) every HTTPS call failed with
invalid peer certificate: UnknownIssuer, while other clients on the same machine reached the same endpoint fine.Refs longbridge/developers#1153. The CLI needs the matching change: longbridge/longbridge-terminal#351.
Cause
The proxy's root CA is installed in the OS trust store. The SDK asked
reqwestandtokio-tungstenitefor the bundled webpki roots only, so the OS store was never consulted and no CA-bundle environment variable could add to it:Fix
Enable the native-roots feature alongside the webpki one, in the HTTP client, the geo lookup and the WebSocket client. Each root set defaults to enabled when its feature is on, so the trust change itself needs no code.
This is a security-posture decision, not just a bug fix
Worth stating explicitly rather than leaving a reviewer to infer it. Before this change the SDK trusted only the roots compiled into the binary, which made it immune to a CA injected into the local trust store. After it, a root certificate installed on the machine can intercept trading traffic — which is exactly what the reporter wants, because that is how their employer's proxy works.
For a trading SDK that trade-off deserves a conscious yes. The argument for it: the machine's trust store is already the trust anchor for every other tool on that host, an attacker who can install a root CA has administrative control anyway, and refusing to honour it only pushes users toward disabling verification entirely. But it is a real widening, and it should be approved as one.
Why this is a superset, not a swap
I read both libraries at the versions the lockfile actually resolves —
tokio-tungstenite0.27, not the latest — rather than assuming.reqwest (
async_impl/client.rs) adds both sets to the sameroot_cert_store:tokio-tungstenite 0.27 (
src/tls.rs) does the same, and explicitly treats an empty native store as non-fatal when webpki roots are also present:So a machine with an empty or unreadable OS store behaves exactly as it did before.
Cargo.locksays nothing here:/Cargo.lockis in this repo's.gitignoreand is not tracked. An earlier version of this description claimed it was "unchanged", which reads like verified dependency stability and is not. (longbridge-terminal does track its lockfile, so the same statement on longbridge/longbridge-terminal#351 is real.)Second commit: don't pay for the OS store on every connection
Reviewing the first commit turned up a regression it introduced.
wsclientcallsconnect_asyncwith no connector, so tokio-tungstenite builds a freshrustls::ClientConfigper connection — andrustls-native-certsdoes not cache. Measured on macOS:The HTTP side is fine:
SHARED_HTTP_CLIENTis aLazyLock, so it reads the store once per process. The WebSocket side held two connections and rebuilt them on every reconnect, so a flapping network would have paid ~90ms each time.The second commit builds the connector once in a
LazyLockand passes it toconnect_async_tls_with_config, assembling the root set exactly as tokio-tungstenite 0.27 assembles its own. Verified by counting initialisations across three contexts: the native store is read once, not three times.That makes
rustlsa direct dependency, which surfaced a second latent problem: the workspace resolvesrustlswith theringprovider only via reqwest, andClientConfig::builder()panics at runtime without a provider — solongbridge-wscliwas not buildable as a standalone crate. The provider is now named explicitly.aws-lc-rsis absent from the lockfile, so there is no ambiguity, and the resolved feature set for the main build is unchanged.Verification
On macOS, three clients against
openapi.longbridge.com:(404 is the expected answer for
/.) Neither root set is load-bearing on its own, and the shared client works with both. The WebSocket path was exercised against the test gateway: the connection reaches the application layer, so the cached connector completes a real TLS handshake.What this does not prove: I could not reproduce the TLS-inspection case itself — there is no such proxy here. That the fix resolves it follows from the proxy CA being in the OS store, which the reporter confirmed by reaching the same endpoint with PowerShell on the affected machine. The part I verified is that the OS store is now loaded and sufficient.
cargo clippy --all --all-features: diffed againstmain, zero new warnings. Tests pass.🤖 Generated with Claude Code