Skip to content

Commit 64920c8

Browse files
authored
Fix handling of URLs with port in credential.helper (#682)
Credentials stored by Git for the URL `https://example.com:3000/foo/bar` use `host=example.com:3000`. A CredentialHelper object for the same URL currently queries the credential helper with `host=example.com` and thus can't access the stored credentials.
1 parent 57b2a87 commit 64920c8

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

Diff for: src/cred.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub struct CredentialHelper {
2222
pub username: Option<String>,
2323
protocol: Option<String>,
2424
host: Option<String>,
25+
port: Option<u16>,
2526
path: Option<String>,
2627
url: String,
2728
commands: Vec<String>,
@@ -199,6 +200,7 @@ impl CredentialHelper {
199200
let mut ret = CredentialHelper {
200201
protocol: None,
201202
host: None,
203+
port: None,
202204
path: None,
203205
username: None,
204206
url: url.to_string(),
@@ -210,6 +212,7 @@ impl CredentialHelper {
210212
if let Some(url::Host::Domain(s)) = url.host() {
211213
ret.host = Some(s.to_string());
212214
}
215+
ret.port = url.port();
213216
ret.protocol = Some(url.scheme().to_string());
214217
}
215218
ret
@@ -408,7 +411,11 @@ impl CredentialHelper {
408411
let _ = writeln!(stdin, "protocol={}", p);
409412
}
410413
if let Some(ref p) = self.host {
411-
let _ = writeln!(stdin, "host={}", p);
414+
if let Some(ref p2) = self.port {
415+
let _ = writeln!(stdin, "host={}:{}", p, p2);
416+
} else {
417+
let _ = writeln!(stdin, "host={}", p);
418+
}
412419
}
413420
if let Some(ref p) = self.path {
414421
let _ = writeln!(stdin, "path={}", p);
@@ -639,6 +646,19 @@ echo password=$2
639646
assert_eq!(helper.path.as_deref(), Some("foo/bar"));
640647
}
641648

649+
#[test]
650+
fn credential_helper9() {
651+
let cfg = test_cfg! {
652+
"credential.helper" => "!f() { while read line; do eval $line; done; if [ \"$host\" = example.com:3000 ]; then echo username=a; echo password=b; fi; }; f"
653+
};
654+
let (u, p) = CredentialHelper::new("https://example.com:3000/foo/bar")
655+
.config(&cfg)
656+
.execute()
657+
.unwrap();
658+
assert_eq!(u, "a");
659+
assert_eq!(p, "b");
660+
}
661+
642662
#[test]
643663
#[cfg(feature = "ssh")]
644664
fn ssh_key_from_memory() {

0 commit comments

Comments
 (0)