Skip to content

Commit 8460ef5

Browse files
astral-automations-bot[bot]zaniebotzanieb
authored
Share URL userinfo redaction (#21912)
`DisplaySafeUrl` implements the same credential-masking policy separately for `Display` and `Debug`. Share the private userinfo decision so both formatters handle passwords, standalone username tokens, and the generic Git username consistently. URL parsing, credential removal, serialization, and query-parameter redaction are unchanged. --------- Co-authored-by: zaniebot <[email protected]> Co-authored-by: Zanie Blue <[email protected]>
1 parent 094c399 commit 8460ef5

1 file changed

Lines changed: 17 additions & 22 deletions

File tree

‎crates/uv-redacted/src/lib.rs‎

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -263,19 +263,7 @@ impl Display for DisplaySafeUrl {
263263
impl Debug for DisplaySafeUrl {
264264
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
265265
let url = &self.0;
266-
// For URLs that use the `git` convention (i.e., `ssh://[email protected]/...`), avoid masking the
267-
// username.
268-
let (username, password) = if is_ssh_git_username(url) {
269-
(url.username(), None)
270-
} else if url.username() != "" && url.password().is_some() {
271-
(url.username(), Some("****"))
272-
} else if url.username() != "" {
273-
("****", None)
274-
} else if url.password().is_some() {
275-
("", Some("****"))
276-
} else {
277-
("", None)
278-
};
266+
let (username, password) = redacted_credentials(url);
279267

280268
f.debug_struct("DisplaySafeUrl")
281269
.field("scheme", &url.scheme())
@@ -321,6 +309,18 @@ fn is_ssh_git_username(url: &Url) -> bool {
321309
&& url.password().is_none()
322310
}
323311

312+
/// Returns the URL's username and password with sensitive values redacted for display.
313+
fn redacted_credentials(url: &Url) -> (&str, Option<&str>) {
314+
match (url.username(), url.password()) {
315+
(username, Some(_)) => (username, Some("****")),
316+
("", None) => ("", None),
317+
// The generic Git username is not sensitive.
318+
(username, None) if is_ssh_git_username(url) => (username, None),
319+
// Other standalone usernames may be tokens.
320+
(_, None) => ("****", None),
321+
}
322+
}
323+
324324
fn is_sensitive_query_parameter(key: &str) -> bool {
325325
SENSITIVE_QUERY_PARAMETERS
326326
.iter()
@@ -358,15 +358,10 @@ fn display_with_redacted_credentials(
358358
if url.has_authority() {
359359
write!(f, "//")?;
360360

361-
if url.username() != "" && url.password().is_some() {
362-
write!(f, "{}", url.username())?;
363-
write!(f, ":****@")?;
364-
} else if url.username() != "" && is_ssh_git_username(url) {
365-
write!(f, "{}@", url.username())?;
366-
} else if url.username() != "" {
367-
write!(f, "****@")?;
368-
} else if url.password().is_some() {
369-
write!(f, ":****@")?;
361+
match redacted_credentials(url) {
362+
("", None) => {}
363+
(username, Some(password)) => write!(f, "{username}:{password}@")?,
364+
(username, None) => write!(f, "{username}@")?,
370365
}
371366

372367
write!(f, "{}", url.host_str().unwrap_or(""))?;

0 commit comments

Comments
 (0)