-
Notifications
You must be signed in to change notification settings - Fork 3
fix(logging): redact sensitive credentials from log output #522
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ use libp2p::Multiaddr; | |
| use pluto_eth2util::enr::Record; | ||
| use tokio_util::sync::CancellationToken; | ||
| use tracing::{info, warn}; | ||
| use url::Url; | ||
|
|
||
| use crate::peer::{ | ||
| AddrInfo, MutablePeer, Peer, PeerError, addr_infos_from_p2p_addrs, peer_id_from_key, | ||
|
|
@@ -147,6 +148,21 @@ pub async fn new_relays( | |
| Ok(resp) | ||
| } | ||
|
|
||
| /// Strips embedded userinfo (basic-auth credentials) from a URL string before | ||
| /// it is passed to a log field so tokens and passwords cannot appear in logs. | ||
| fn redact_url(raw: &str) -> String { | ||
| let Ok(mut url) = Url::parse(raw) else { | ||
| return raw.to_owned(); | ||
| }; | ||
| if url.username().is_empty() && url.password().is_none() { | ||
| return raw.to_owned(); | ||
| } | ||
| if url.set_username("").is_err() || url.set_password(None).is_err() { | ||
| return "<redacted>".to_owned(); | ||
| } | ||
| url.to_string() | ||
| } | ||
|
Comment on lines
+153
to
+164
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Relays do not carry credentials and we do not intend to support such feature. For reference check Charon v1.7.1: https://github.com/ObolNetwork/charon/blob/749d2d7ab0b8ace34f2e686a5af5910c8adb4dc0/p2p/bootnode.go#L155. |
||
|
|
||
| /// Continuously resolves relay multiaddrs from an HTTP URL and updates the | ||
| /// MutablePeer. | ||
| /// | ||
|
|
@@ -169,7 +185,7 @@ async fn resolve_relay( | |
| { | ||
| Ok(addrs) => addrs, | ||
| Err(e) => { | ||
| tracing::error!(err = %e, url = %raw_url, "Failed resolving relay addresses from URL"); | ||
| tracing::error!(err = %e, url = %redact_url(&raw_url), "Failed resolving relay addresses from URL"); | ||
| return; | ||
| } | ||
| }; | ||
|
|
@@ -193,7 +209,7 @@ async fn resolve_relay( | |
| let peer = Peer::new_relay_peer(&infos[0]); | ||
| info!( | ||
| peer = %peer.name, | ||
| url = %raw_url, | ||
| url = %redact_url(&raw_url), | ||
| addrs = ?peer.addresses, | ||
| "Resolved new relay" | ||
| ); | ||
|
|
@@ -348,3 +364,29 @@ fn addr_info_from_p2p_addr(addr: &Multiaddr) -> std::result::Result<AddrInfo, Pe | |
|
|
||
| infos.pop().ok_or(PeerError::MissingPeerIdInMultiaddr) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::redact_url; | ||
|
|
||
| #[test] | ||
| fn redact_url_strips_basic_auth_credentials() { | ||
| let raw = "https://user:s3cr3t@relay.example.com/path"; | ||
| let result = redact_url(raw); | ||
| assert!(!result.contains("user"), "username leaked: {result}"); | ||
| assert!(!result.contains("s3cr3t"), "password leaked: {result}"); | ||
| assert!(result.contains("relay.example.com"), "host should remain: {result}"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn redact_url_is_noop_when_no_credentials() { | ||
| let raw = "https://relay.example.com/path"; | ||
| assert_eq!(redact_url(raw), raw); | ||
| } | ||
|
|
||
| #[test] | ||
| fn redact_url_returns_original_for_unparseable_input() { | ||
| let raw = "not a url %%"; | ||
| assert_eq!(redact_url(raw), raw); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please bump to the latest version