Loopback proxy that adds a TLS client certificate for tools that cannot send one
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
renovate[bot] 2ddf1240ae
feat(deps): update module golang.org/x/vuln/cmd/govulncheck ( v1.9.0 → v1.10.0 ) (#19)
| datasource | package                           | from   | to      |
| ---------- | --------------------------------- | ------ | ------- |
| go         | golang.org/x/vuln/cmd/govulncheck | v1.9.0 | v1.10.0 |
2026-11-27 01:08:54 +00:00
.github/workflows feat(deps): update module golang.org/x/vuln/cmd/govulncheck ( v1.9.0 → v1.10.0 ) (#19) 2026-11-27 01:08:54 +00:00
docs Correct usage docs for routes and timeouts, test certificate paths (#17) 2026-11-22 19:17:59 +03:00
testdata/fuzz Stop reading oversized bodies in the fuzz wire reader 2026-11-08 12:08:02 +03:00
.gitignore feat: add loopback mTLS proxy 2026-11-01 19:26:04 +03:00
.golangci.yaml Adopt shared golangci-lint baseline 2026-11-09 07:52:39 +03:00
certificate.go Protect client PEM files and redact proxy errors (#10) 2026-11-19 20:50:29 +03:00
certificate_test.go Correct usage docs for routes and timeouts, test certificate paths (#17) 2026-11-22 19:17:59 +03:00
cli_test.go Validate upstream trust and fix CLI help output (#13) 2026-11-19 23:45:19 +03:00
fuzz_config_test.go Require listen ports and drain HTTP requests on shutdown (#12) 2026-11-19 23:32:53 +03:00
fuzz_harness_test.go Adopt shared golangci-lint baseline 2026-11-09 07:52:39 +03:00
fuzz_pki_test.go Add native fuzz targets 2026-11-08 11:08:58 +03:00
fuzz_proxy_test.go Seed the proxy fuzzer with Unicode case-fold hosts 2026-11-08 13:49:12 +03:00
fuzz_wire_test.go Stop reading oversized bodies in the fuzz wire reader 2026-11-08 12:08:02 +03:00
go.mod fix(deps): update go module directive ( 1.27.3 → 1.27.4 ) (#15) 2026-11-19 21:18:25 +00:00
go.sum feat: add loopback mTLS proxy 2026-11-01 19:26:04 +03:00
keychain_darwin.go Validate client certificate lifetime 2026-11-06 00:03:44 +03:00
keychain_other.go feat: add loopback mTLS proxy 2026-11-01 19:26:04 +03:00
keychain_other_test.go Correct usage docs for routes and timeouts, test certificate paths (#17) 2026-11-22 19:17:59 +03:00
LICENSE feat: add loopback mTLS proxy 2026-11-01 19:26:04 +03:00
main.go Correct usage docs for routes and timeouts, test certificate paths (#17) 2026-11-22 19:17:59 +03:00
main_test.go Correct usage docs for routes and timeouts, test certificate paths (#17) 2026-11-22 19:17:59 +03:00
proxy_shutdown_test.go Require listen ports and drain HTTP requests on shutdown (#12) 2026-11-19 23:32:53 +03:00
proxy_streaming_test.go Preserve encoded responses and stream during uploads (#7) 2026-11-19 17:36:44 +03:00
proxy_transport_test.go Protect client PEM files and redact proxy errors (#10) 2026-11-19 20:50:29 +03:00
README.md Correct usage docs for routes and timeouts, test certificate paths (#17) 2026-11-22 19:17:59 +03:00
readme_test.go Prevent bypass in the forward proxy example (#14) 2026-11-20 00:09:34 +03:00
renovate.json feat: add loopback mTLS proxy 2026-11-01 19:26:04 +03:00

mtls-proxy

A small loopback proxy for tools that cannot present a TLS client certificate. It listens on a loopback address, adds your client certificate, and forwards each listener to one upstream host. Terraform providers are the usual case: many have no client certificate setting and ignore HTTPS_PROXY.

The client certificate can come from the macOS Keychain, so the private key never has to be exported, from PEM files, or from environment variables.

Install

go install github.com/zekihan/mtls-proxy@latest

With -identity, macOS grants Keychain access per binary, so keep the binary at a fixed path. The Keychain needs cgo, which is the default on macOS. For a static Linux binary, build with CGO_ENABLED=0.

Usage

# macOS Keychain identity, selected by certificate common name
mtls-proxy -identity clients-laptop -ca-out ~/.local/state/mtls-proxy/ca.pem \
  -route 127.0.0.1:18443=app.example.com

# PEM files
mtls-proxy -cert client.pem -key client.key -ca-out ca.pem \
  -route 127.0.0.1:18443=app.example.com

# PEM values in environment variables, cleared once read
CERT_PEM="$cert" KEY_PEM="$key" mtls-proxy -cert-env CERT_PEM -key-env KEY_PEM \
  -ca-out ca.pem -route 127.0.0.1:18443=app.example.com

Set the variables only on the proxy's command, so other processes do not inherit the key. Do not do this in a bash -x script, because tracing prints the values.

Clearing them does not erase the environment the proxy started with. Processes running as your user can still read the key there (/proc/<pid>/environ on Linux, ps -E on macOS) until the proxy exits. To keep it out of the environment, pass it through process substitution instead:

mtls-proxy -cert <(your-secret-command cert) -key <(your-secret-command key) \
  -ca-out ca.pem -route 127.0.0.1:18443=app.example.com

-route and -forward can be repeated and mixed. -ca-out is required for any -route. A forward-only proxy does not write a certificate.

The proxy only listens on loopback IPs and refuses anything else. It refuses to start when the client certificate is expired or not yet valid, and it logs a warning when that certificate expires within 30 days.

A client has ten seconds to send request headers. The upstream TLS handshake has the same limit. After sending the full request, the proxy waits up to one minute for upstream response headers.

On interrupt or SIGTERM, the proxy stops accepting connections and gives active HTTP requests up to five seconds to finish.

-route: HTTPS reverse proxy

Point the client at https://127.0.0.1:18443 (or wss:// for WebSockets). The proxy serves HTTPS using a self-signed certificate generated at startup. The key stays in memory, and the certificate is written to -ca-out for the client to trust. It changes on every start. The proxy rewrites Host and SNI to the upstream.

-forward: HTTP_PROXY route for signed requests

Clients that sign the hostname, such as S3 SigV4, cannot use -route because the Host rewrite invalidates the signature. -forward keeps the hostname.

Start the proxy in one terminal.

mtls-proxy -cert client.pem -key client.key -forward 127.0.0.1:18445=s3.example.com

Run the client in another terminal. Set both spellings of the HTTP proxy variable for client compatibility and clear any inherited proxy bypass for this command.

HTTP_PROXY=http://127.0.0.1:18445 http_proxy=http://127.0.0.1:18445 \
  NO_PROXY= no_proxy= your-client --endpoint http://s3.example.com

It accepts only that upstream and refuses CONNECT, so it cannot be used as an open proxy. This hop is plain HTTP on loopback, and TLS starts at the proxy. A direct request to the http:// endpoint sends data in plaintext.

Security

  • Upstream certificates are verified against the system trust store.
  • Any local process can use your client identity through the proxy while it runs. Stop it when you finish.
  • Requests a browser makes on behalf of another site (Sec-Fetch-Site: cross-site or same-site) are refused, so a web page cannot use your client identity.