Skip to main content

sanitize_connection_string

Function sanitize_connection_string 

Source
pub fn sanitize_connection_string(conn_str: &str) -> String
Expand description

Removes sensitive information (passwords) from a connection string before it is logged.

store_addrs entries can be a PostgreSQL DSN (a postgres(ql):// URL or a libpq keyword string), a MySQL/etcd URL, or an etcd host list.

The function fails closed: it never returns a URI-shaped input verbatim. In order:

  1. PostgreSQL DSNs are parsed with tokio_postgres::Config — the backend’s own parser — and logged via its Debug, which redacts the password. This matches the grammar exactly (multi-host URIs, \-escapes, any Unicode whitespace, percent-encoded query keys, &/;/:// inside values).
  2. Any other URL is redacted with the url crate.
  3. A URI-shaped input that neither parser accepted (e.g. a malformed multi-host Postgres URI, with or without leading whitespace) is redacted in full — credentials in an unparsable authority or query cannot be split out reliably, so the whole string is replaced rather than reparsed.
  4. Anything else is treated as a keyword string and best-effort redacted.