Skip to content
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
4 changes: 2 additions & 2 deletions crates/uv-auth/src/keyring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl KeyringProvider {
}
target
} else {
url.to_string()
url.as_str().to_owned()
};

match &self.backend {
Expand Down Expand Up @@ -153,7 +153,7 @@ impl KeyringProvider {
}
target
} else {
url.to_string()
url.as_str().to_owned()
};

match &self.backend {
Expand Down
2 changes: 1 addition & 1 deletion crates/uv-git/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ fn submodule_auth_config(original_remote_url: &SafeUrl) -> Vec<String> {
let mut config = Vec::new();

if remote_url.as_str() != original_remote_url.as_str() {
let safe_root = remote_url_root(SafeUrl::from_url(remote_url.into_owned()));
let safe_root = remote_url_root(remote_url.into_owned());
let credentialed_root = remote_url_root(original_remote_url.clone());

if safe_root.as_str() != credentialed_root.as_str() {
Expand Down
13 changes: 12 additions & 1 deletion crates/uv-pypi-types/src/parsed_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ impl From<&ParsedArchiveUrl> for DirectUrl {
fn from(value: &ParsedArchiveUrl) -> Self {
Self::ArchiveUrl {
// Query parameters identify the source, so only strip username and password credentials.
url: value.url.without_credentials().to_string(),
url: value.url.without_credentials().as_str().to_owned(),
archive_info: ArchiveInfo {
hash: None,
hashes: None,
Expand Down Expand Up @@ -744,6 +744,17 @@ mod tests {
Ok(())
}

#[test]
fn direct_url_preserves_archive_query() -> Result<()> {
let url = SafeUrl::parse(
"https://user:password@example.com/project.tar.gz?sig=abc%2Bdef%3D&other=a+b#subdirectory=pkg",
)?;
let parsed = ParsedUrl::try_from(url)?;
let direct_url = DirectUrl::from(&parsed);
insta::assert_snapshot!(serde_json::to_string(&direct_url)?, @r#"{"url":"https://example.com/project.tar.gz?sig=abc%2Bdef%3D&other=a+b","archive_info":{},"subdirectory":"pkg"}"#);
Ok(())
}

#[test]
fn direct_url_preserves_git_repository_url() -> Result<()> {
for expected in [
Expand Down
24 changes: 17 additions & 7 deletions crates/uv-redacted/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,21 +277,31 @@ impl<P: SerializationPolicy> SafeUrl<P> {
let _ = self.0.set_password(None);
}

/// Returns the URL with any credentials removed.
pub fn without_credentials(&self) -> Cow<'_, Url> {
/// Return the URL with sensitive userinfo removed, retaining its serialization policy.
///
/// The complete query is retained; display and debug formatting redact recognized credentials.
/// Removing userinfo does not make an [`Unserializable`] URL serializable:
///
/// ```compile_fail
/// use uv_redacted::SafeUrl;
///
/// let url = SafeUrl::parse("https://user:password@example.com/file.whl")?;
/// serde_json::to_string(&url.without_credentials())?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn without_credentials(&self) -> Cow<'_, Self> {
if self.0.password().is_none() && self.0.username() == "" {
return Cow::Borrowed(&self.0);
return Cow::Borrowed(self);
}

// For URLs that use the `git` convention (i.e., `ssh://git@github.com/...`), avoid dropping the
// username.
if is_ssh_git_username(&self.0) {
return Cow::Borrowed(&self.0);
return Cow::Borrowed(self);
}

let mut url = self.0.clone();
let _ = url.set_username("");
let _ = url.set_password(None);
let mut url = self.clone();
url.remove_credentials();
Cow::Owned(url)
}

Expand Down
35 changes: 32 additions & 3 deletions crates/uv-redacted/src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Serialize for SafeUrl<Stripped> {
where
S: serde::Serializer,
{
self.without_credentials().serialize(serializer)
serializer.serialize_str(self.without_credentials().as_str())
}
}

Expand All @@ -77,6 +77,7 @@ impl Serialize for SafeUrl<Raw> {

#[cfg(test)]
mod tests {
use std::borrow::Cow;
use std::error::Error;

use insta::assert_snapshot;
Expand Down Expand Up @@ -108,6 +109,33 @@ mod tests {
Ok(())
}

#[test]
fn credential_removal_retains_policy_and_query() -> Result<(), Box<dyn Error>> {
let input = "https://user:password@example.com/file.whl?sig=abc%2Bdef%3D&other=a+b";
let expected = "https://example.com/file.whl?sig=abc%2Bdef%3D&other=a+b";
let url = SafeUrl::parse(input)?;
let removed: Cow<'_, SafeUrl> = url.without_credentials();
assert_eq!(removed.as_str(), expected);
assert_snapshot!(removed.to_string(), @"https://example.com/file.whl?sig=****&other=a+b");
assert_snapshot!(format!("{removed:?}"), @r#"SafeUrl { scheme: "https", cannot_be_a_base: false, username: "", password: None, host: Some(Domain("example.com")), port: None, path: "/file.whl", query: Some("sig=****&other=a+b"), fragment: None }"#);
assert_eq!(url.as_str(), input);

let stripped = url.clone().with_policy(Stripped);
let removed: Cow<'_, SafeUrl<Stripped>> = stripped.without_credentials();
assert_eq!(
serde_json::to_string(&removed)?,
serde_json::to_string(expected)?
);

let raw = url.with_policy(Raw);
let removed: Cow<'_, SafeUrl<Raw>> = raw.without_credentials();
assert_eq!(
serde_json::to_string(&removed)?,
serde_json::to_string(expected)?
);
Ok(())
}

#[test]
fn method_serialization_respects_policy() -> Result<(), Box<dyn Error>> {
let input = "https://user:password@example.com/file.whl?sig=abc%2Bdef%3D";
Expand All @@ -129,8 +157,9 @@ mod tests {

#[test]
fn borrowed_policy_retains_git_username() -> Result<(), Box<dyn Error>> {
let url = SafeUrl::parse("ssh://git@github.com/astral-sh/uv")?;
assert_snapshot!(serde_json::to_string(url.with_policy_ref(Stripped))?, @r#""ssh://git@github.com/astral-sh/uv""#);
let url = SafeUrl::parse("ssh://git@github.com/astral-sh/uv?sig=abc%2Bdef%3D")?;
assert_snapshot!(serde_json::to_string(url.with_policy_ref(Stripped))?, @r#""ssh://git@github.com/astral-sh/uv?sig=abc%2Bdef%3D""#);
assert_snapshot!(url.without_credentials().to_string(), @"ssh://git@github.com/astral-sh/uv?sig=****");
Ok(())
}

Expand Down
Loading