Documentation
¶
Overview ¶
Package mitm terminates TLS for an allowlist of API hosts so that a closed client that honours HTTPS_PROXY and a CA bundle env var can be inspected by the proxy. Every other host is blind-tunnelled. See spec §19.3 and §19.7.
Index ¶
Constants ¶
const ( // DefaultCAValidity is the lifetime of a newly generated CA (~1 year). DefaultCAValidity = 365 * 24 * time.Hour // PrevCAGrace is how long a replaced CA certificate stays in the bundle // (as ca.prev.crt) after regeneration, so clients that loaded the old // bundle keep working while they restart. The old key is overwritten. PrevCAGrace = 7 * 24 * time.Hour )
Variables ¶
var CAEnvVars = []string{
"SSL_CERT_FILE",
"SSL_CERT_DIR",
"NODE_EXTRA_CA_CERTS",
"REQUESTS_CA_BUNDLE",
"CURL_CA_BUNDLE",
}
CAEnvVars lists the environment variables that tell common clients which CA bundle to trust.
var ErrNoSystemBundle = errors.New("mitm: no system CA bundle file found")
ErrNoSystemBundle is returned when no file in SystemBundlePaths exists.
var SystemBundlePaths = []string{
"/etc/ssl/certs/ca-certificates.crt",
"/etc/pki/tls/certs/ca-bundle.crt",
"/etc/ssl/ca-bundle.pem",
"/etc/ssl/cert.pem",
}
SystemBundlePaths lists system root bundles, tried in order when a bundle includes system roots. On platforms without such a file, Bundle returns ErrNoSystemBundle and the caller should export the CA alone.
Functions ¶
func ClassifyHandshakeError ¶
ClassifyHandshakeError reports whether err is a client-side TLS alert that means the client refused the minted certificate. It returns a short reason such as "unknown_ca" and true for those alerts. Other errors return false.
func ClientSupport ¶
ClientSupport returns the CA env vars from CAEnvVars that are set to a non-blank value in env, in CAEnvVars order. It never returns nil.
Types ¶
type CA ¶
type CA struct {
// contains filtered or unexported fields
}
CA is a per-install certificate authority that mints leaf certificates. Its private key never leaves this process and is never logged.
func LoadOrCreateCA ¶
LoadOrCreateCA loads the CA stored in dir, or generates a new one. A stored CA is replaced when its key is missing or inconsistent, when it is within validity/12 of expiry, or when its name constraints differ from opts.Domains. On replacement the previous certificate is kept as ca.prev.crt for PrevCAGrace and is included in Bundle. The directory is kept at 0700 and the key file at 0600. Nothing is installed in the system trust store.
The whole load-check-generate-persist sequence runs under an exclusive lock on dir/ca.lock, so concurrent processes never interleave key and certificate writes.
func (*CA) Bundle ¶
Bundle returns the CA certificate as PEM, followed by the previous CA certificate while it is inside its grace period, then optionally the system roots. Use it for SSL_CERT_FILE, NODE_EXTRA_CA_CERTS, REQUESTS_CA_BUNDLE and CURL_CA_BUNDLE, scoped to the client process. Including the system roots keeps the client trusting other hosts, because these variables replace its store.
func (*CA) Certificate ¶
func (ca *CA) Certificate() *x509.Certificate
Certificate returns the CA certificate. Callers must not modify it.
type CAOptions ¶
type CAOptions struct {
// Domains restricts the names the CA may issue for (X.509 DNS name
// constraints). "host" permits host and its subdomains; "*.domain" is
// accepted as shorthand for "domain". Empty means unconstrained.
Domains []string
// Validity of a newly generated CA. Zero means DefaultCAValidity.
Validity time.Duration
// LeafCacheSize bounds the per-host leaf cache. Zero means 256.
LeafCacheSize int
}
CAOptions configures LoadOrCreateCA.
type Options ¶
type Options struct {
// CA mints the leaf certificates. Required.
CA *CA
// Domains are the hosts whose TLS is terminated. "host" matches exactly;
// "*.domain" matches subdomains. Each must be permitted by CA's constraints.
Domains []string
// AllowPorts are the CONNECT target ports accepted. Default: 443.
AllowPorts []int
// Handler receives the decrypted requests for Domains. Required. Each
// request has URL.Scheme "https" and URL.Host set to the real target.
Handler http.Handler
// Token is the Proxy-Authorization secret. Empty means a random 256-bit
// token, to be placed in HTTPS_PROXY as http://acd-ctx:<token>@127.0.0.1:port.
Token string
// Dial reaches real hosts, for both blind tunnels and the upstream transport.
// Default: net.Dialer with a 10s timeout that refuses loopback, link-local
// (cloud metadata), unspecified and multicast addresses, checked on the
// resolved IP so DNS rebinding cannot bypass it.
Dial func(ctx context.Context, network, addr string) (net.Conn, error)
// AllowLocalTargets disables the default Dial guard. It has no effect when
// Dial is set, because a custom Dial owns its own policy.
AllowLocalTargets bool
// UpstreamTLS verifies real hosts. Nil means the system roots.
UpstreamTLS *tls.Config
// Logger receives diagnostics. It never receives tokens, headers or keys.
Logger *slog.Logger
}
Options configures a forward proxy Server.
type Rejection ¶
type Rejection struct {
Host string
Count int
Last time.Time
Reason string // e.g. "unknown_ca", "bad_certificate"
Err string // last handshake error text
}
Rejection is the per-host record of a client refusing the minted certificate.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a loopback-only forward proxy (http.Handler) for CONNECT requests. Domains in the allowlist are decrypted and handed to Options.Handler. All other targets are blind-tunnelled.
func New ¶
New builds a Server. It fails when a domain is not permitted by the CA's name constraints, so a configured domain can never get a certificate the CA cannot issue.
func (*Server) Rejections ¶
Rejections returns the per-host count of client-side certificate refusals, sorted by host.