NATS TLS certificate common name validation bypass
The NATS official Rust clients are vulnerable to MitM when using TLS.A fix for the `nats` crate hasn't been released yet. Since the `nats` crate is going to be deprecated anyway, consider switching to `async-nats` `>= 0.29` which already fixed this vulnerability.The common name of the server's TLS certificate is validated against the `host`name provided by the server's plaintext `INFO` message during the initial connection setup phase. A MitM proxy can tamper with the `host` field's value by substituting it with the common name of a valid certificate it controls, fooling the client into accepting it.Reproduction stepsThe NATS Rust client tries to establish a new connectionThe connection is intercepted by a MitM proxyThe proxy makes a separate connection to the NATS serverThe NATS server replies with an `INFO` messageThe proxy reads the `INFO`, alters the `host` JSON field and passes the tampered `INFO` back to the clientThe proxy upgrades the client connection to TLS, presenting a certificate issued by a certificate authority present in the client's keychain. In the previous step the `host` was set to the common name of said certificate`rustls` accepts the certificate, having verified that the common name matches the attacker-controlled value it was givenThe client has been fooled by the MitM proxy into accepting the attacker-controlled certificate