/// Redact credentials in a database target, hiding malformed URLs entirely. #[must_use] pub fn redact_connection_url(raw: &str) -> String { if numeric_port_authority_end(raw).is_some_and(|end| raw[end..].contains('@')) { return "[redacted]".to_string(); } let Ok(mut parsed) = url::Url::parse(raw) else { return "[redacted]".to_string(); }; redact_url_userinfo(&mut parsed); redact_url_query(&mut parsed); redact_url_fragment(&mut parsed); parsed.to_string() } /// Rewrite every URL in `message` so its `user:pass@` userinfo and any /// sensitive query parameter render as ``. Shared with the config /// layer, whose operator-supplied endpoints can carry either. #[must_use] pub fn redact_url_credentials(message: &str) -> String { let mut redacted = String::with_capacity(message.len()); let mut cursor = 0; while let Some(relative_scheme_end) = message[cursor..].find("://") { let scheme_end = cursor + relative_scheme_end; let scheme_start = find_scheme_start(message, scheme_end); if scheme_start == scheme_end || !is_valid_scheme(&message[scheme_start..scheme_end]) { redacted.push_str(&message[cursor..scheme_end + 3]); cursor = scheme_end + 3; continue; } let url_end = find_url_end(message, scheme_end + 3); let (candidate, suffix) = split_trailing_punctuation(&message[scheme_start..url_end]); let Some(safe_url) = redact_url_candidate(candidate) else { redacted.push_str(&message[cursor..url_end]); cursor = url_end; continue; }; redacted.push_str(&message[cursor..scheme_start]); redacted.push_str(&safe_url); redacted.push_str(suffix); cursor = url_end; } redacted.push_str(&message[cursor..]); redacted } pub(super) fn find_scheme_start(message: &str, scheme_end: usize) -> usize { let bytes = message.as_bytes(); let mut start = scheme_end; while start > 0 { let byte = bytes[start - 1]; if !byte.is_ascii_alphanumeric() && byte != b'+' && byte != b'.' && byte != b'-' { break; } start -= 1; } start } pub(super) fn is_valid_scheme(scheme: &str) -> bool { let mut chars = scheme.bytes(); let Some(first) = chars.next() else { return false; }; first.is_ascii_alphabetic() && chars.all(|byte| { byte.is_ascii_alphanumeric() || byte == b'+' || byte == b'.' || byte == b'-' }) } pub(super) fn find_url_end(message: &str, url_start: usize) -> usize { message[url_start..] .char_indices() .find_map(|(offset, ch)| is_url_delimiter(ch).then_some(url_start + offset)) .unwrap_or(message.len()) } pub(super) fn is_url_delimiter(ch: char) -> bool { ch.is_whitespace() || matches!(ch, '"' | '\'' | '`' | '<' | '>' | '(' | ')' | '{' | '}') } pub(super) fn split_trailing_punctuation(candidate: &str) -> (&str, &str) { let mut end = candidate.len(); while let Some(ch) = candidate[..end].chars().next_back() { if !matches!(ch, '.' | ',' | ';' | '!') { break; } end -= ch.len_utf8(); } (&candidate[..end], &candidate[end..]) } pub(super) fn redact_url_candidate(candidate: &str) -> Option { let Ok(url) = url::Url::parse(candidate) else { return redact_unparsable_url_candidate(candidate.to_owned()); }; let Some(userinfo_end) = find_password_parsed_as_port(candidate) else { return redact_parsed_url(url); }; let redacted = redact_userinfo_ending_at(candidate, userinfo_end)?; Some(redact_url_candidate(&redacted).unwrap_or(redacted)) } /// Returns the userinfo `@` of a URL such as `postgres://admin:123/secret@db/x`, /// which parses with port `123` and the rest of the password after it. /// An `@` that starts a path segment is taken for a scoped package name, so /// `http://localhost:4873/@scope/pkg` keeps its host. pub(super) fn find_password_parsed_as_port(candidate: &str) -> Option { let authority_end = numeric_port_authority_end(candidate)?; find_last_at_outside_segment_start(candidate, authority_end, candidate.len()) } fn numeric_port_authority_end(candidate: &str) -> Option { let authority_start = candidate.find("://")? + 3; let authority_end = authority_start + candidate[authority_start..].find(['/', '?', '#'])?; let authority = &candidate[authority_start..authority_end]; let (_, port) = authority.rsplit_once(':')?; if authority.contains('@') || !port.bytes().all(|byte| byte.is_ascii_digit()) { return None; } Some(authority_end) } pub(super) fn find_last_at_outside_segment_start( candidate: &str, start: usize, end: usize, ) -> Option { candidate[start..end] .rmatch_indices('@') .map(|(offset, _)| start + offset) .find(|&at| candidate.as_bytes()[at - 1] != b'/') } pub(super) fn redact_parsed_url(mut url: url::Url) -> Option { let mut changed = redact_url_userinfo(&mut url); changed |= redact_url_query(&mut url); changed |= redact_url_fragment(&mut url); changed.then(|| url.to_string()) } pub(super) fn redact_url_userinfo(url: &mut url::Url) -> bool { if url.username().is_empty() && url.password().is_none() { return false; } let username_redacted = url.set_username("redacted").is_ok(); let password_dropped = url.set_password(None).is_ok(); username_redacted || password_dropped } pub(super) fn redact_url_query(url: &mut url::Url) -> bool { if url.query().is_none() { return false; } let mut changed = false; let pairs = url .query_pairs() .map(|(key, value)| { if is_sensitive_query_key(&key) { changed = true; (key.into_owned(), "redacted".to_string()) } else { (key.into_owned(), value.into_owned()) } }) .collect::>(); if changed { url.query_pairs_mut() .clear() .extend_pairs( pairs .iter() .map(|(key, value)| (&**key, &**value)), ); } changed } pub(super) fn redact_url_fragment(url: &mut url::Url) -> bool { if url.fragment().is_none() { return false; } url.set_fragment(None); true } pub(super) fn redact_unparsable_url_candidate(candidate: String) -> Option { let mut redacted = candidate; let mut changed = false; if let Some(safe_url) = redact_unparsable_url_userinfo(&redacted) { redacted = safe_url; changed = true; } if let Some(safe_url) = redact_sensitive_query_values(&redacted) { redacted = safe_url; changed = true; } if let Some(safe_url) = redact_fragment(&redacted) { redacted = safe_url; changed = true; } changed.then_some(redacted) } pub(super) fn redact_unparsable_url_userinfo(candidate: &str) -> Option { let authority_start = candidate.find("://")? + 3; // Any `@` is accepted when none qualifies, so a password ending in `/` stays hidden. let userinfo_end = find_last_at_outside_segment_start(candidate, authority_start, candidate.len()) .or_else(|| { candidate[authority_start..] .rfind('@') .map(|offset| authority_start + offset) })?; redact_userinfo_ending_at(candidate, userinfo_end) } pub(super) fn redact_userinfo_ending_at(candidate: &str, userinfo_end: usize) -> Option { let authority_start = candidate.find("://")? + 3; // A `?` or `#` before the `@` belongs to either the password or a query or fragment // that holds a secret, so neither side of the `@` is safe to log. if candidate[authority_start..userinfo_end].contains(['?', '#']) { return Some(format!("{}redacted", &candidate[..authority_start])); } let mut redacted = String::with_capacity(candidate.len()); redacted.push_str(&candidate[..authority_start]); redacted.push_str("redacted@"); redacted.push_str(&candidate[userinfo_end + 1..]); Some(redacted) } pub(super) fn redact_sensitive_query_values(candidate: &str) -> Option { let query_start = candidate.find('?')?; let fragment_start = candidate[query_start + 1..] .find('#') .map(|offset| query_start + 1 + offset) .unwrap_or(candidate.len()); let query = &candidate[query_start + 1..fragment_start]; let mut redacted = String::with_capacity(candidate.len()); redacted.push_str(&candidate[..=query_start]); let mut changed = false; for segment in query.split_inclusive('&') { let (pair, separator) = segment .strip_suffix('&') .map(|pair| (pair, "&")) .unwrap_or((segment, "")); if let Some(value_start) = pair.find('=') { let key = &pair[..value_start]; if is_sensitive_query_key(key) { redacted.push_str(key); redacted.push_str("=redacted"); redacted.push_str(separator); changed = true; continue; } } redacted.push_str(segment); } redacted.push_str(&candidate[fragment_start..]); changed.then_some(redacted) } pub(super) fn redact_fragment(candidate: &str) -> Option { let fragment_start = candidate.find('#')?; Some(candidate[..fragment_start].to_string()) } pub(super) fn is_sensitive_query_key(key: &str) -> bool { let normalized = key .chars() .filter(|ch| *ch != '-' && *ch != '_') .map(|ch| ch.to_ascii_lowercase()) .collect::(); matches!( normalized.as_str(), "auth" | "user" | "username" | "sslpassword" | "authtoken" | "password" | "passwd" | "pwd" | "secret" | "token" | "accesstoken" | "apikey" | "signature" | "sig" | "credential" | "accesskeyid" | "awsaccesskeyid" | "xamzsignature" | "xamzcredential" | "xamzsecuritytoken" | "xgoogsignature" | "xgoogcredential", ) }