mirror of
https://github.com/astral-sh/uv.git
synced 2025-11-20 03:49:54 +00:00
Add DisplaySafeUrl newtype to prevent leaking of credentials by default (#13560)
Prior to this PR, there were numerous places where uv would leak credentials in logs. We had a way to mask credentials by calling methods or a recently-added `redact_url` function, but this was not secure by default. There were a number of other types (like `GitUrl`) that would leak credentials on display. This PR adds a `DisplaySafeUrl` newtype to prevent leaking credentials when logging by default. It takes a maximalist approach, replacing the use of `Url` almost everywhere. This includes when first parsing config files, when storing URLs in types like `GitUrl`, and also when storing URLs in types that in practice will never contain credentials (like `DirectorySourceUrl`). The idea is to make it easy for developers to do the right thing and for the compiler to support this (and to minimize ever having to manually convert back and forth). Displaying credentials now requires an active step. Note that despite this maximalist approach, the use of the newtype should be zero cost. One conspicuous place this PR does not use `DisplaySafeUrl` is in the `uv-auth` crate. That would require new clones since there are calls to `request.url()` that return a `&Url`. One option would have been to make `DisplaySafeUrl` wrap a `Cow`, but this would lead to lifetime annotations all over the codebase. I've created a separate PR based on this one (#13576) that updates `uv-auth` to use `DisplaySafeUrl` with one new clone. We can discuss the tradeoffs there. Most of this PR just replaces `Url` with `DisplaySafeUrl`. The core is `uv_redacted/lib.rs`, where the newtype is implemented. To make it easier to review the rest, here are some points of note: * `DisplaySafeUrl` has a `Display` implementation that masks credentials. Currently, it will still display the username when there is both a username and password. If we think is the wrong choice, it can now be changed in one place. * `DisplaySafeUrl` has a `remove_credentials()` method and also a `.to_string_with_credentials()` method. This allows us to use it in a variety of scenarios. * `IndexUrl::redacted()` was renamed to `IndexUrl::removed_credentials()` to make it clearer that we are not masking. * We convert from a `DisplaySafeUrl` to a `Url` when calling `reqwest` methods like `.get()` and `.head()`. * We convert from a `DisplaySafeUrl` to a `Url` when creating a `uv_auth::Index`. That is because, as mentioned above, I will be updating the `uv_auth` crate to use this newtype in a separate PR. * A number of tests (e.g., in `pip_install.rs`) that formerly used filters to mask tokens in the test output no longer need those filters since tokens in URLs are now masked automatically. * The one place we are still knowingly writing credentials to `pyproject.toml` is when a URL with credentials is passed to `uv add` with `--raw`. Since displaying credentials is no longer automatic, I have added a `to_string_with_credentials()` method to the `Pep508Url` trait. This is used when `--raw` is passed. Adding it to that trait is a bit weird, but it's the simplest way to achieve the goal. I'm open to suggestions on how to improve this, but note that because of the way we're using generic bounds, it's not as simple as just creating a separate trait for that method.
This commit is contained in:
parent
b80cafd5e8
commit
c19a294a48
100 changed files with 1266 additions and 2249 deletions
|
|
@ -30,7 +30,7 @@ use uv_distribution_types::{
|
|||
Dist, DistributionMetadata, FileLocation, GitSourceDist, IndexLocations, IndexMetadata,
|
||||
IndexUrl, Name, PathBuiltDist, PathSourceDist, RegistryBuiltDist, RegistryBuiltWheel,
|
||||
RegistrySourceDist, RemoteSource, Requirement, RequirementSource, ResolvedDist, StaticMetadata,
|
||||
ToUrlError, UrlString, redact_credentials,
|
||||
ToUrlError, UrlString,
|
||||
};
|
||||
use uv_fs::{PortablePath, PortablePathBuf, relative_to};
|
||||
use uv_git::{RepositoryReference, ResolvedRepositoryReference};
|
||||
|
|
@ -45,6 +45,7 @@ use uv_pypi_types::{
|
|||
ConflictPackage, Conflicts, HashAlgorithm, HashDigest, HashDigests, Hashes, ParsedArchiveUrl,
|
||||
ParsedGitUrl,
|
||||
};
|
||||
use uv_redacted::DisplaySafeUrl;
|
||||
use uv_small_str::SmallString;
|
||||
use uv_types::{BuildContext, HashStrategy};
|
||||
use uv_workspace::WorkspaceMember;
|
||||
|
|
@ -1404,7 +1405,7 @@ impl Lock {
|
|||
.into_iter()
|
||||
.filter_map(|index| match index.url() {
|
||||
IndexUrl::Pypi(_) | IndexUrl::Url(_) => {
|
||||
Some(UrlString::from(index.url().redacted().as_ref()))
|
||||
Some(UrlString::from(index.url().without_credentials().as_ref()))
|
||||
}
|
||||
IndexUrl::Path(_) => None,
|
||||
})
|
||||
|
|
@ -2238,7 +2239,7 @@ impl Package {
|
|||
Source::Direct(url, direct) => {
|
||||
let filename: WheelFilename =
|
||||
self.wheels[best_wheel_index].filename.clone();
|
||||
let url = Url::from(ParsedArchiveUrl {
|
||||
let url = DisplaySafeUrl::from(ParsedArchiveUrl {
|
||||
url: url.to_url().map_err(LockErrorKind::InvalidUrl)?,
|
||||
subdirectory: direct.subdirectory.clone(),
|
||||
ext: DistExtension::Wheel,
|
||||
|
|
@ -2400,7 +2401,7 @@ impl Package {
|
|||
GitUrl::from_commit(url, GitReference::from(git.kind.clone()), git.precise)?;
|
||||
|
||||
// Reconstruct the PEP 508-compatible URL from the `GitSource`.
|
||||
let url = Url::from(ParsedGitUrl {
|
||||
let url = DisplaySafeUrl::from(ParsedGitUrl {
|
||||
url: git_url.clone(),
|
||||
subdirectory: git.subdirectory.clone(),
|
||||
});
|
||||
|
|
@ -2419,7 +2420,7 @@ impl Package {
|
|||
return Ok(None);
|
||||
};
|
||||
let location = url.to_url().map_err(LockErrorKind::InvalidUrl)?;
|
||||
let url = Url::from(ParsedArchiveUrl {
|
||||
let url = DisplaySafeUrl::from(ParsedArchiveUrl {
|
||||
url: location.clone(),
|
||||
subdirectory: direct.subdirectory.clone(),
|
||||
ext: DistExtension::Source(ext),
|
||||
|
|
@ -2498,8 +2499,9 @@ impl Package {
|
|||
name: name.clone(),
|
||||
version: version.clone(),
|
||||
})?;
|
||||
let file_url = Url::from_file_path(workspace_root.join(path).join(file_path))
|
||||
.map_err(|()| LockErrorKind::PathToUrl)?;
|
||||
let file_url =
|
||||
DisplaySafeUrl::from_file_path(workspace_root.join(path).join(file_path))
|
||||
.map_err(|()| LockErrorKind::PathToUrl)?;
|
||||
let filename = sdist
|
||||
.filename()
|
||||
.ok_or_else(|| LockErrorKind::MissingFilename {
|
||||
|
|
@ -3192,7 +3194,7 @@ impl Source {
|
|||
match index_url {
|
||||
IndexUrl::Pypi(_) | IndexUrl::Url(_) => {
|
||||
// Remove any sensitive credentials from the index URL.
|
||||
let redacted = index_url.redacted();
|
||||
let redacted = index_url.without_credentials();
|
||||
let source = RegistrySource::Url(UrlString::from(redacted.as_ref()));
|
||||
Ok(Source::Registry(source))
|
||||
}
|
||||
|
|
@ -3405,7 +3407,7 @@ impl TryFrom<SourceWire> for Source {
|
|||
match wire {
|
||||
Registry { registry } => Ok(Source::Registry(registry.into())),
|
||||
Git { git } => {
|
||||
let url = Url::parse(&git)
|
||||
let url = DisplaySafeUrl::parse(&git)
|
||||
.map_err(|err| SourceParseError::InvalidUrl {
|
||||
given: git.to_string(),
|
||||
err,
|
||||
|
|
@ -3913,12 +3915,12 @@ impl From<GitSourceKind> for GitReference {
|
|||
}
|
||||
}
|
||||
|
||||
/// Construct the lockfile-compatible [`URL`] for a [`GitSourceDist`].
|
||||
fn locked_git_url(git_dist: &GitSourceDist) -> Url {
|
||||
/// Construct the lockfile-compatible [`DisplaySafeUrl`] for a [`GitSourceDist`].
|
||||
fn locked_git_url(git_dist: &GitSourceDist) -> DisplaySafeUrl {
|
||||
let mut url = git_dist.git.repository().clone();
|
||||
|
||||
// Redact the credentials.
|
||||
redact_credentials(&mut url);
|
||||
// Remove the credentials.
|
||||
url.remove_credentials();
|
||||
|
||||
// Clear out any existing state.
|
||||
url.set_fragment(None);
|
||||
|
|
@ -4183,8 +4185,9 @@ impl Wheel {
|
|||
.into());
|
||||
}
|
||||
};
|
||||
let file_url = Url::from_file_path(root.join(index_path).join(file_path))
|
||||
.map_err(|()| LockErrorKind::PathToUrl)?;
|
||||
let file_url =
|
||||
DisplaySafeUrl::from_file_path(root.join(index_path).join(file_path))
|
||||
.map_err(|()| LockErrorKind::PathToUrl)?;
|
||||
let file = Box::new(uv_distribution_types::File {
|
||||
dist_info_metadata: false,
|
||||
filename: SmallString::from(filename.to_string()),
|
||||
|
|
@ -4571,8 +4574,8 @@ fn normalize_file_location(location: &FileLocation) -> Result<UrlString, ToUrlEr
|
|||
}
|
||||
}
|
||||
|
||||
/// Convert a [`Url`] into a normalized [`UrlString`] by removing the fragment.
|
||||
fn normalize_url(mut url: Url) -> UrlString {
|
||||
/// Convert a [`DisplaySafeUrl`] into a normalized [`UrlString`] by removing the fragment.
|
||||
fn normalize_url(mut url: DisplaySafeUrl) -> UrlString {
|
||||
url.set_fragment(None);
|
||||
UrlString::from(url)
|
||||
}
|
||||
|
|
@ -4606,8 +4609,8 @@ fn normalize_requirement(
|
|||
let git = {
|
||||
let mut repository = git.repository().clone();
|
||||
|
||||
// Redact the credentials.
|
||||
redact_credentials(&mut repository);
|
||||
// Remove the credentials.
|
||||
repository.remove_credentials();
|
||||
|
||||
// Remove the fragment and query from the URL; they're already present in the source.
|
||||
repository.set_fragment(None);
|
||||
|
|
@ -4617,7 +4620,7 @@ fn normalize_requirement(
|
|||
};
|
||||
|
||||
// Reconstruct the PEP 508 URL from the underlying data.
|
||||
let url = Url::from(ParsedGitUrl {
|
||||
let url = DisplaySafeUrl::from(ParsedGitUrl {
|
||||
url: git.clone(),
|
||||
subdirectory: subdirectory.clone(),
|
||||
});
|
||||
|
|
@ -4692,7 +4695,7 @@ fn normalize_requirement(
|
|||
let index = index
|
||||
.map(|index| index.url.into_url())
|
||||
.map(|mut index| {
|
||||
redact_credentials(&mut index);
|
||||
index.remove_credentials();
|
||||
index
|
||||
})
|
||||
.map(|index| IndexMetadata::from(IndexUrl::from(VerbatimUrl::from_url(index))));
|
||||
|
|
@ -4715,14 +4718,14 @@ fn normalize_requirement(
|
|||
ext,
|
||||
url: _,
|
||||
} => {
|
||||
// Redact the credentials.
|
||||
redact_credentials(&mut location);
|
||||
// Remove the credentials.
|
||||
location.remove_credentials();
|
||||
|
||||
// Remove the fragment from the URL; it's already present in the source.
|
||||
location.set_fragment(None);
|
||||
|
||||
// Reconstruct the PEP 508 URL from the underlying data.
|
||||
let url = Url::from(ParsedArchiveUrl {
|
||||
let url = DisplaySafeUrl::from(ParsedArchiveUrl {
|
||||
url: location.clone(),
|
||||
subdirectory: subdirectory.clone(),
|
||||
ext,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue