Skip to content

Commit def3084

Browse files
committed
Preserve Git username for SSH dependencies
1 parent 3355259 commit def3084

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

crates/pypi-types/src/requirement.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,12 @@ impl Requirement {
8080
subdirectory,
8181
url,
8282
} => {
83-
// Redact the repository URL.
84-
let _ = repository.set_password(None);
85-
let _ = repository.set_username("");
83+
// Redact the repository URL, but allow `git@`.
84+
redact_git_credentials(&mut repository);
8685

8786
// Redact the PEP 508 URL.
8887
let mut url = url.to_url();
89-
let _ = url.set_password(None);
90-
let _ = url.set_username("");
88+
redact_git_credentials(&mut url);
9189
let url = VerbatimUrl::from_url(url);
9290

9391
Self {
@@ -598,8 +596,7 @@ impl From<RequirementSource> for RequirementSourceWire {
598596
let mut url = repository;
599597

600598
// Redact the credentials.
601-
let _ = url.set_username("");
602-
let _ = url.set_password(None);
599+
redact_git_credentials(&mut url);
603600

604601
// Clear out any existing state.
605602
url.set_fragment(None);
@@ -699,8 +696,7 @@ impl TryFrom<RequirementSourceWire> for RequirementSource {
699696
repository.set_query(None);
700697

701698
// Redact the credentials.
702-
let _ = repository.set_username("");
703-
let _ = repository.set_password(None);
699+
redact_git_credentials(&mut repository);
704700

705701
// Create a PEP 508-compatible URL.
706702
let mut url = Url::parse(&format!("git+{repository}"))?;
@@ -765,6 +761,18 @@ impl TryFrom<RequirementSourceWire> for RequirementSource {
765761
}
766762
}
767763

764+
/// Remove the credentials from a Git URL, allowing the generic `git` username (without a password)
765+
/// in SSH URLs, as in, `ssh://[email protected]/...`.
766+
pub fn redact_git_credentials(url: &mut Url) {
767+
// For URLs that use the `git` convention (i.e., `ssh://[email protected]/...`), avoid dropping the
768+
// username.
769+
if url.scheme() == "ssh" && url.username() == "git" && url.password().is_none() {
770+
return;
771+
}
772+
let _ = url.set_password(None);
773+
let _ = url.set_username("");
774+
}
775+
768776
#[cfg(test)]
769777
mod tests {
770778
use std::path::{Path, PathBuf};

crates/uv-resolver/src/lock.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ use distribution_types::{
2525
use pep440_rs::Version;
2626
use pep508_rs::{MarkerEnvironment, MarkerTree, VerbatimUrl, VerbatimUrlError};
2727
use platform_tags::{TagCompatibility, TagPriority, Tags};
28-
use pypi_types::{HashDigest, ParsedArchiveUrl, ParsedGitUrl, Requirement, RequirementSource};
28+
use pypi_types::{
29+
redact_git_credentials, HashDigest, ParsedArchiveUrl, ParsedGitUrl, Requirement,
30+
RequirementSource,
31+
};
2932
use uv_configuration::ExtrasSpecification;
3033
use uv_distribution::DistributionDatabase;
3134
use uv_fs::{relative_to, PortablePath, PortablePathBuf, Simplified};
@@ -2367,8 +2370,7 @@ fn locked_git_url(git_dist: &GitSourceDist) -> Url {
23672370
let mut url = git_dist.git.repository().clone();
23682371

23692372
// Redact the credentials.
2370-
let _ = url.set_username("");
2371-
let _ = url.set_password(None);
2373+
redact_git_credentials(&mut url);
23722374

23732375
// Clear out any existing state.
23742376
url.set_fragment(None);
@@ -2830,14 +2832,12 @@ fn normalize_requirement(
28302832
subdirectory,
28312833
url,
28322834
} => {
2833-
// Redact the repository URL.
2834-
let _ = repository.set_password(None);
2835-
let _ = repository.set_username("");
2835+
// Redact the credentials.
2836+
redact_git_credentials(&mut repository);
28362837

28372838
// Redact the PEP 508 URL.
28382839
let mut url = url.to_url();
2839-
let _ = url.set_password(None);
2840-
let _ = url.set_username("");
2840+
redact_git_credentials(&mut url);
28412841
let url = VerbatimUrl::from_url(url);
28422842

28432843
Ok(Requirement {

crates/uv/src/commands/project/add.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use anyhow::{bail, Context, Result};
66
use cache_key::RepositoryUrl;
77
use owo_colors::OwoColorize;
88
use pep508_rs::{ExtraName, Requirement, VersionOrUrl};
9+
use pypi_types::redact_git_credentials;
910
use rustc_hash::{FxBuildHasher, FxHashMap};
1011
use tracing::debug;
1112
use uv_auth::{store_credentials_from_url, Credentials};
@@ -360,8 +361,9 @@ pub(crate) async fn add(
360361
if let Some(credentials) = credentials {
361362
debug!("Caching credentials for: {git}");
362363
GIT_STORE.insert(RepositoryUrl::new(&git), credentials);
363-
let _ = git.set_username("");
364-
let _ = git.set_password(None);
364+
365+
// Redact the credentials.
366+
redact_git_credentials(&mut git);
365367
};
366368
Some(Source::Git {
367369
git,

0 commit comments

Comments
 (0)