Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent passwords from being logged #2839

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions sqlx-core/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,44 @@ impl LogSettings {
}
}

/// `Secret` is a wrapper around a String which ensures it is not going
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// `Secret` is a wrapper around a String which ensures it is not going
/// `Secret` is a wrapper around a String which ensures it is going

/// to be masked when printed in a regular fashion
#[derive(Clone,PartialEq,Eq)]
pub struct Secret(String);

impl std::fmt::Debug for Secret {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("********")
}
}
impl std::fmt::Display for Secret {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(self, f)
}
}
impl From<String> for Secret {
fn from(value: String) -> Self {
Self(value)
}
}
impl From<&str> for Secret {
fn from(value: &str) -> Self {
Self(value.to_owned())
}
}
impl From<Secret> for String {
fn from(value: Secret) -> Self {
value.0
}
}
impl std::ops::Deref for Secret {
type Target = str;

fn deref(&self) -> &Self::Target {
self.0.deref()
}
}

pub trait ConnectOptions: 'static + Send + Sync + FromStr<Err = Error> + Debug + Clone {
type Connection: Connection + ?Sized;

Expand Down
5 changes: 3 additions & 2 deletions sqlx-mysql/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod parse;
mod ssl_mode;

use crate::{connection::LogSettings, net::tls::CertificateInput};
use sqlx_core::connection::Secret;
pub use ssl_mode::MySqlSslMode;

/// Options and flags which can be used to configure a MySQL connection.
Expand Down Expand Up @@ -57,7 +58,7 @@ pub struct MySqlConnectOptions {
pub(crate) port: u16,
pub(crate) socket: Option<PathBuf>,
pub(crate) username: String,
pub(crate) password: Option<String>,
pub(crate) password: Option<Secret>,
pub(crate) database: Option<String>,
pub(crate) ssl_mode: MySqlSslMode,
pub(crate) ssl_ca: Option<CertificateInput>,
Expand Down Expand Up @@ -132,7 +133,7 @@ impl MySqlConnectOptions {

/// Sets the password to connect with.
pub fn password(mut self, password: &str) -> Self {
self.password = Some(password.to_owned());
self.password = Some(password.into());
self
}

Expand Down
3 changes: 2 additions & 1 deletion sqlx-postgres/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::fmt::{Display, Write};
use std::path::{Path, PathBuf};

use sqlx_core::connection::Secret;
pub use ssl_mode::PgSslMode;

use crate::{connection::LogSettings, net::tls::CertificateInput};
Expand Down Expand Up @@ -83,7 +84,7 @@
pub(crate) port: u16,
pub(crate) socket: Option<PathBuf>,
pub(crate) username: String,
pub(crate) password: Option<String>,
pub(crate) password: Option<Secret>,
pub(crate) database: Option<String>,
pub(crate) ssl_mode: PgSslMode,
pub(crate) ssl_root_cert: Option<CertificateInput>,
Expand Down Expand Up @@ -154,7 +155,7 @@
ssl_mode: var("PGSSLMODE")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or_default(),

Check failure on line 158 in sqlx-postgres/src/options/mod.rs

View workflow job for this annotation

GitHub Actions / Unit Test (tokio, native-tls)

mismatched types

Check failure on line 158 in sqlx-postgres/src/options/mod.rs

View workflow job for this annotation

GitHub Actions / CLI Unit Test

mismatched types

Check failure on line 158 in sqlx-postgres/src/options/mod.rs

View workflow job for this annotation

GitHub Actions / Check (tokio, none)

mismatched types

Check failure on line 158 in sqlx-postgres/src/options/mod.rs

View workflow job for this annotation

GitHub Actions / Check (async-std, none)

mismatched types

Check failure on line 158 in sqlx-postgres/src/options/mod.rs

View workflow job for this annotation

GitHub Actions / Build SQLx CLI

mismatched types

Check failure on line 158 in sqlx-postgres/src/options/mod.rs

View workflow job for this annotation

GitHub Actions / CLI Binaries (ubuntu-latest)

mismatched types
statement_cache_capacity: 100,
application_name: var("PGAPPNAME").ok(),
extra_float_digits: Some("3".into()),
Expand All @@ -173,7 +174,7 @@
);
}

self

Check failure on line 177 in sqlx-postgres/src/options/mod.rs

View workflow job for this annotation

GitHub Actions / Unit Test (tokio, native-tls)

mismatched types

Check failure on line 177 in sqlx-postgres/src/options/mod.rs

View workflow job for this annotation

GitHub Actions / CLI Unit Test

mismatched types

Check failure on line 177 in sqlx-postgres/src/options/mod.rs

View workflow job for this annotation

GitHub Actions / Check (tokio, none)

mismatched types

Check failure on line 177 in sqlx-postgres/src/options/mod.rs

View workflow job for this annotation

GitHub Actions / Check (async-std, none)

mismatched types

Check failure on line 177 in sqlx-postgres/src/options/mod.rs

View workflow job for this annotation

GitHub Actions / Build SQLx CLI

mismatched types

Check failure on line 177 in sqlx-postgres/src/options/mod.rs

View workflow job for this annotation

GitHub Actions / CLI Binaries (ubuntu-latest)

mismatched types
}

/// Sets the name of the host to connect to.
Expand Down Expand Up @@ -272,7 +273,7 @@
///
/// # Example
///
/// ```rust

Check failure on line 276 in sqlx-postgres/src/options/mod.rs

View workflow job for this annotation

GitHub Actions / Unit Test (tokio, native-tls)

mismatched types

Check failure on line 276 in sqlx-postgres/src/options/mod.rs

View workflow job for this annotation

GitHub Actions / CLI Unit Test

mismatched types

Check failure on line 276 in sqlx-postgres/src/options/mod.rs

View workflow job for this annotation

GitHub Actions / Check (tokio, none)

mismatched types

Check failure on line 276 in sqlx-postgres/src/options/mod.rs

View workflow job for this annotation

GitHub Actions / Check (async-std, none)

mismatched types

Check failure on line 276 in sqlx-postgres/src/options/mod.rs

View workflow job for this annotation

GitHub Actions / Build SQLx CLI

mismatched types

Check failure on line 276 in sqlx-postgres/src/options/mod.rs

View workflow job for this annotation

GitHub Actions / CLI Binaries (ubuntu-latest)

mismatched types
/// # use sqlx_core::postgres::PgConnectOptions;
/// let options = PgConnectOptions::new()
/// .database("postgres");
Expand Down
2 changes: 2 additions & 0 deletions sqlx-postgres/src/options/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ fn it_parses_password_with_non_ascii_chars_correctly() {
let opts = PgConnectOptions::from_str(url).unwrap();

assert_eq!(Some("p@ssw0rd".into()), opts.password);
assert_eq!("********", format!("{0}", opts.password.unwrap()));
assert_eq!("********", format!("{0:?}", opts.password.unwrap()));
}

#[test]
Expand Down
Loading