feat(gateway): mTLS client-certificate listener (Phase 1) - #2
Open
marcorivm wants to merge 1 commit into
Open
Conversation
Add a second listener that terminates mutual TLS, requiring a client certificate signed by a dedicated client CA (distinct from the MITM CA). After the handshake the stream is handed to the existing connection handler unchanged; the peer certificate's identity (URI-SAN preferred, else CN) is extracted and threaded into ProxyContext. Phase 1 logs the identity only — no enforcement against the agent token yet. - New src/client_ca.rs: ClientIdentity, identity extraction with injection-safe sanitization, path-or-inline PEM loading, and MtlsConfig with fail-closed from_parts/from_env. - Fail-closed startup: mis-configured mTLS aborts main before DB/crypto init; never falls back to plaintext. - SECURITY: client-cert verifier uses the default deny anon policy (no allow_unauthenticated); GATEWAY_CLIENT_CA is rejected if its SPKI matches the host-resident MITM CA's public key. - GATEWAY_PLAIN_BIND fails closed on an unparseable value; accept loop retries transient errors so one listener can't cancel the other. - mTLS is off when GATEWAY_MTLS_PORT is unset (backward compatible). - 40 client_ca tests + mTLS e2e through handle_connection; OSS build, clippy --all-targets -D warnings, and fmt all clean. Claude-Session: https://claude.ai/code/session_01BgJuqEJqf7ZiUdqHWxi6bt
This was referenced Jul 30, 2026
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.
Phase 1 of remote-gateway hardening — mTLS listener
Enables hosting the gateway on a different machine than the clients (Dokploy / Railway, where Tailscale can't run). First of a stacked series; later PRs add client-cert issuance (Phase 2), an
onecli-gateway relayclient (Phase 3), nanoclaw integration (Phase 4), and cert↔token binding enforcement (Phase 5).What this PR does
ca.rs. After the handshake the stream is handed to the existing connection handler unchanged, so all CONNECT / MITM / Axum logic is reused.ProxyContext. Phase 1 logs it only — no enforcement against the agent token yet (Phase 5).GATEWAY_MTLS_PORTis unset — fully backward compatible; the plaintext listener behaves exactly as before.Security properties
mainbefore DB/crypto init — never falls back to plaintext.allow_unauthenticated); rustls validates chain + expiry during the handshake.GATEWAY_CLIENT_CAis rejected if its SPKI matches the host-resident MITM CA's public key — that key signs leaf certs on demand, so trusting it as a client anchor would let anyone mint their own client cert.GATEWAY_PLAIN_BINDfails closed on an unparseable value (no silent0.0.0.0); the accept loop retries transient errors so one listener can't cancel the other.Env contract (all off unless
GATEWAY_MTLS_PORTis set)GATEWAY_MTLS_PORT,GATEWAY_TLS_CERT,GATEWAY_TLS_KEY,GATEWAY_CLIENT_CA(each path-or-inline PEM),GATEWAY_PLAIN_BIND(default0.0.0.0).Testing
client_caunit tests + an mTLS e2e throughhandle_connection);cargo clippy --all-targets -- -D warningsandcargo fmt --checkclean./healthz200 (proves no Phase-1 enforcement); hostile CN/SAN dropped; same-public-key-different-cert client CA → rejected; allfrom_partsfail-closed cases.Notes for reviewers
cargo build --features cloudcurrently fails with 27 errors — pre-existing in theee/*.rsstubs on this branch, reproduced on the base commit; none originate in this PR's code. Validated on OSS build only.https://claude.ai/code/session_01BgJuqEJqf7ZiUdqHWxi6bt