celld icon indicating copy to clipboard operation
celld copied to clipboard

Let TLS sockets trust additional roots, as `fetch()` does

Open connorff opened this issue 2 months ago • 1 comments

In v0.5.1, fetch() verifies servers against:

  • the Mozilla roots
  • the OS trust store
  • SSL_CERT_FILE

TCP sockets verify against the Mozilla roots only (webpki_roots::TLS_SERVER_ROOTS), which cloudflare-compat.md documents so that celld works on machines without /etc/ssl/certs. So a Worker can fetch() a server whose certificate comes from a private CA, but it can't open a TLS socket to it. That blocks database drivers such as pg, which upgrade with startTls().

Repro: serve HTTPS on localhost:8443 with a certificate from a private CA in ca.pem, then run this Worker with SSL_CERT_FILE=ca.pem celld dev:

import { connect } from "cloudflare:sockets";

export default {
  async fetch() {
    const viaFetch = await fetch("https://localhost:8443/").then((r) => r.status, (e) => e.message);
    const socket = connect({ hostname: "localhost", port: 8443 }, { secureTransport: "starttls" }).startTls();
    const viaSocket = await socket.opened.then(() => "ok", (e) => e.message);

    return Response.json({ viaFetch, viaSocket });
  },
};
  • Expected: { "viaFetch": 200, "viaSocket": "ok" }
  • Actual: { "viaFetch": 200, "viaSocket": "TLS handshake failed: invalid peer certificate: UnknownIssuer" }

Suggested fix: build the socket connector from the same roots as reqwest (native certs plus webpki), so SSL_CERT_FILE applies to every outbound TLS client. ws_client.rs builds its roots the same way and likely needs the same change.

connorff avatar Sep 24 '26 01:09 connorff

I hit this with a PostgreSQL driver (SSLRequest, then startTls()) and tried the suggested fix on v0.6.3. It works:

  • A new outbound_tls::root_store() builds Mozilla's roots plus rustls_native_certs::load_native_certs() once per process. The native set is SSL_CERT_FILE/SSL_CERT_DIR when set, otherwise the OS store, as fetch() gets through reqwest.
  • js/tcp.rs and ws_client.rs both use it. The celld_internal_tests root seam still adds its extra root on top.
  • Load errors and unparsable certificates are logged at error and skipped. The store then fails closed for servers that needed them.
  • cloudflare-compat.md now states the trust sources, that a change takes a restart, and that startTls() has no per-socket CA option.

Verified through startTls() against PostgreSQL with SSL_CERT_FILE=ca.pem celld dev, rotating the server certificate:

server certificate result
private CA, matching host ok
private CA, wrong SAN invalid peer certificate
private CA, expired invalid peer certificate
other CA invalid peer certificate: UnknownIssuer
SSL_CERT_FILE missing fails closed; ERROR celld::outbound_tls: could not load native TLS roots … No such file or directory

The same testing turned up a related socket-lifetime bug. Socket.close() with a pending read() and the writable locked skips the graceful writer close and calls __tcp_close. free() then removes the registry entry, but the in-flight op_tcp_read still holds its read half, so the TCP connection stays open until the event retires. A driver that reads continuously and closes on a silent peer is in exactly that state. In a probe, the peer stayed connected about 640 ms after close() in 3 of 6 runs. Giving each TcpSocket a CancellationToken that free() cancels, and running read/write/shutdown through asyncrt::select_biased! against it, brought that to 0 of 6.

I have both as a branch. I'm happy to open a PR here, or two PRs if you'd rather track the close fix separately.

chr33s avatar Oct 26 '26 10:10 chr33s