1use super::*;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum Kind {
9 GitHub,
10 GhEnterprise,
11 GitLab,
12 Bitbucket,
13}
14
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct Host {
17 pub origin: Origin,
18 pub kind: Kind,
19}
20
21pub fn kind_of(host: &str) -> Kind {
22 if host == "github.com" {
23 Kind::GitHub
24 } else if host.contains("gitlab") {
25 Kind::GitLab
26 } else if host.contains("bitbucket") {
27 Kind::Bitbucket
28 } else {
29 Kind::GhEnterprise
30 }
31}
32
33impl Host {
34 pub fn of(origin: Origin) -> Host {
35 let kind = kind_of(&origin.host);
36 Host { origin, kind }
37 }
38
39 pub fn archive_url(&self, path: &str, reference: &str) -> String {
42 let r = enc(reference);
43 match self.kind {
44 Kind::GitHub => format!("https://api.github.com/repos/{path}/tarball/{r}"),
45 Kind::GhEnterprise => format!("{}/api/v3/repos/{path}/tarball/{r}", self.origin),
46 Kind::GitLab => format!("{}/api/v4/projects/{}/repository/archive.tar.gz?sha={r}", self.origin, enc(path)),
47 Kind::Bitbucket => format!("{}/{path}/get/{r}.tar.gz", self.origin),
48 }
49 }
50
51 fn git_user(&self) -> &'static str {
53 match self.kind {
54 Kind::GitHub | Kind::GhEnterprise => "x-access-token",
55 Kind::GitLab => "oauth2",
56 Kind::Bitbucket => "x-token-auth",
57 }
58 }
59
60 fn origins(&self) -> Vec<Origin> {
63 let mut v = vec![self.origin.clone()];
64 if self.kind == Kind::GitHub {
65 for h in ["api.github.com", "codeload.github.com"] {
66 v.push(Origin {
67 scheme: "https".into(),
68 host: h.into(),
69 port: 443,
70 });
71 }
72 }
73 v
74 }
75}
76
77fn enc(s: &str) -> String {
78 let mut out = String::new();
79 for b in s.bytes() {
80 if b.is_ascii_alphanumeric() || b"-_.~".contains(&b) {
81 out.push(b as char);
82 } else {
83 out.push_str(&format!("%{b:02X}"));
84 }
85 }
86 out
87}
88
89pub fn git_header(host: &Host, auth: &Auth) -> String {
92 match auth {
93 Auth::Bearer(t) => Auth::Basic(host.git_user().into(), t.clone()).header(),
94 other => other.header(),
95 }
96}
97
98fn strip_scheme(h: &str) -> String {
99 h.trim()
100 .trim_start_matches("https://")
101 .trim_start_matches("http://")
102 .trim_end_matches('/')
103 .to_ascii_lowercase()
104}
105
106fn env_auth(host: &Host, env: &Env) -> Option<Auth> {
108 let h = &host.origin.host;
109 match host.kind {
110 Kind::GitHub => ["GH_TOKEN", "GITHUB_TOKEN"].iter().find_map(|k| env.get(k)).map(Auth::Bearer),
111 Kind::GhEnterprise => (env.get("GH_HOST").map(|g| strip_scheme(&g)).as_deref() == Some(h.as_str()))
112 .then(|| ["GH_ENTERPRISE_TOKEN", "GITHUB_ENTERPRISE_TOKEN"].iter().find_map(|k| env.get(k)))
113 .flatten()
114 .map(Auth::Bearer),
115 Kind::GitLab => (h == "gitlab.com" || env.get("GITLAB_HOST").map(|g| strip_scheme(&g)).as_deref() == Some(h.as_str()))
116 .then(|| env.get("GITLAB_TOKEN"))
117 .flatten()
118 .map(Auth::Bearer),
119 Kind::Bitbucket => env
120 .get("BITBUCKET_TOKEN")
121 .map(Auth::Bearer)
122 .or_else(|| Some(Auth::Basic(env.get("BITBUCKET_USERNAME")?, env.get("BITBUCKET_APP_PASSWORD")?))),
123 }
124}
125
126pub fn credential_fill(origin: &Origin, extra_env: &[(&str, &str)]) -> Option<(String, String)> {
129 use std::io::Write;
130 use std::process::{Command, Stdio};
131 let default = if origin.scheme == "https" { 443 } else { 80 };
132 let host = if origin.port == default {
133 origin.host.clone()
134 } else {
135 format!("{}:{}", origin.host, origin.port)
136 };
137 let mut cmd = Command::new("git");
138 cmd.args(["credential", "fill"])
139 .env("GIT_TERMINAL_PROMPT", "0")
140 .env("GCM_INTERACTIVE", "never")
141 .stdin(Stdio::piped())
142 .stdout(Stdio::piped())
143 .stderr(Stdio::null());
144 for (k, v) in extra_env {
145 cmd.env(k, v);
146 }
147 let mut child = cmd.spawn().ok()?;
148 child
149 .stdin
150 .take()?
151 .write_all(format!("protocol={}\nhost={host}\n\n", origin.scheme).as_bytes())
152 .ok()?;
153 let mut stdout = child.stdout.take()?;
154 let reader = std::thread::spawn(move || {
155 let mut s = String::new();
156 let _ = std::io::Read::read_to_string(&mut stdout, &mut s);
157 s
158 });
159 let deadline = std::time::Instant::now() + std::time::Duration::from_secs(15);
160 loop {
161 match child.try_wait() {
162 Ok(Some(st)) if st.success() => break,
163 Ok(Some(_)) | Err(_) => return None,
164 Ok(None) if std::time::Instant::now() >= deadline => {
165 let _ = child.kill();
166 let _ = child.wait();
167 return None;
168 }
169 Ok(None) => std::thread::sleep(std::time::Duration::from_millis(20)),
170 }
171 }
172 let out = reader.join().ok()?;
173 let get = |k: &str| out.lines().find_map(|l| l.strip_prefix(&format!("{k}=")).map(String::from));
174 Some((get("username")?, get("password")?))
175}
176
177pub fn credentials(host: &Host, env: &Env, fill: bool) -> Creds {
180 let auth = env_auth(host, env).or_else(|| fill.then(|| credential_fill(&host.origin, &[])).flatten().map(|(u, p)| Auth::Basic(u, p)));
181 let mut creds = Creds::default();
182 if let Some(a) = auth {
183 for o in host.origins() {
184 creds.push(Credential::new(o, a.clone()));
185 }
186 }
187 creds
188}
189
190pub fn parse_repo_url(url: &str) -> Option<(Origin, String)> {
194 let u = url.trim();
195 let u = u.strip_prefix("git+").unwrap_or(u);
196 let (host_port, path, explicit_git) = if let Some(rest) = u.strip_prefix("git@") {
197 let (h, p) = rest.split_once(':')?;
198 (h.to_string(), p.to_string(), true)
199 } else if let Some(rest) = u.strip_prefix("ssh://").or_else(|| u.strip_prefix("git://")) {
200 let rest = rest.strip_prefix("git@").unwrap_or(rest);
201 let (h, p) = rest.split_once('/')?;
202 (h.split(':').next()?.to_string(), p.to_string(), true)
203 } else {
204 let rest = u.strip_prefix("https://").or_else(|| u.strip_prefix("http://"))?;
205 let (h, p) = rest.split_once('/')?;
206 (h.to_string(), p.to_string(), false)
207 };
208 let host = host_port.rsplit('@').next()?.to_ascii_lowercase();
209 if host.is_empty() || host == "github.com" || host == "www.github.com" {
210 return None;
211 }
212 let path = path.split(['#', '?']).next()?.trim_matches('/');
213 let had_git = path.ends_with(".git");
214 let path = path.trim_end_matches(".git");
215 let kind = kind_of(&host);
216 let looks_like_repo = explicit_git || had_git || kind == Kind::GitLab || kind == Kind::Bitbucket || host.contains("github") || host.starts_with("git.");
217 if !looks_like_repo {
218 return None;
219 }
220 let segs: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
221 let cut = segs.iter().position(|s| *s == "-" || *s == "tree" || *s == "blob").unwrap_or(segs.len());
223 let segs = &segs[..cut];
224 if segs.len() < 2 || (kind != Kind::GitLab && segs.len() > 2) {
225 return None;
226 }
227 let safe = |s: &&str| !s.is_empty() && *s != "." && *s != ".." && s.bytes().all(|b| b.is_ascii_alphanumeric() || b"-_.".contains(&b));
228 if !segs.iter().all(safe) {
229 return None;
230 }
231 let scheme = if u.starts_with("http://") { "http" } else { "https" };
232 let url = Url::parse(&format!("{scheme}://{host}/"))?;
233 Some((url.origin(), segs.join("/")))
234}
235
236#[cfg(test)]
237mod tests {
238 use super::*;
239
240 fn host(u: &str) -> Host {
241 Host::of(Url::parse(u).unwrap().origin())
242 }
243
244 #[test]
245 fn repo_urls_on_other_hosts() {
246 let p = |u: &str| parse_repo_url(u).map(|(o, p)| (o.to_string(), p));
247 assert_eq!(
248 p("git+https://git.acme.example/team/lib.git"),
249 Some(("https://git.acme.example".into(), "team/lib".into()))
250 );
251 assert_eq!(
252 p("https://gitlab.acme.example/grp/sub/lib"),
253 Some(("https://gitlab.acme.example".into(), "grp/sub/lib".into()))
254 );
255 assert_eq!(p("git@bitbucket.org:ws/repo.git"), Some(("https://bitbucket.org".into(), "ws/repo".into())));
256 assert_eq!(p("https://github.com/o/r"), None);
257 assert_eq!(p("https://foo.readthedocs.io/en/latest"), None);
259 assert_eq!(p("https://example.com/o/r"), None);
260 }
261
262 #[test]
263 fn env_tokens_belong_to_their_own_host() {
264 let e = Env::fixed(
265 &[
266 ("GH_TOKEN", "SECRET-GH"),
267 ("GITLAB_TOKEN", "SECRET-GL"),
268 ("GITLAB_HOST", "https://gitlab.acme.example"),
269 ],
270 None,
271 );
272 let gh = credentials(&host("https://github.com/"), &e, false);
273 assert!(gh.for_url(&Url::parse("https://codeload.github.com/o/r/tar.gz/x").unwrap()).is_some());
274 assert!(gh.for_url(&Url::parse("https://git.acme.example/o/r").unwrap()).is_none());
275 assert!(credentials(&host("https://github.acme.example/"), &e, false).0.is_empty());
277 let gl = credentials(&host("https://gitlab.acme.example/"), &e, false);
278 assert_eq!(
279 gl.for_url(&Url::parse("https://gitlab.acme.example/api/v4/x").unwrap()).unwrap().header(),
280 "Bearer SECRET-GL"
281 );
282 assert!(credentials(&host("https://gitlab.other.example/"), &e, false).0.is_empty());
283 assert!(!format!("{gh:?}{gl:?}").contains("SECRET"));
284 }
285
286 #[test]
287 fn git_endpoint_uses_basic_with_the_host_user() {
288 let h = host("https://gitlab.acme.example/");
289 assert_eq!(git_header(&h, &Auth::Bearer("t".into())), Auth::Basic("oauth2".into(), "t".into()).header());
290 assert_eq!(
291 git_header(&h, &Auth::Basic("u".into(), "p".into())),
292 Auth::Basic("u".into(), "p".into()).header()
293 );
294 }
295
296 #[test]
297 fn archive_urls() {
298 assert_eq!(
299 host("https://github.com/").archive_url("o/r", "v1.0.0"),
300 "https://api.github.com/repos/o/r/tarball/v1.0.0"
301 );
302 assert_eq!(
303 host("https://gitlab.x.example/").archive_url("g/s/r", "v1"),
304 "https://gitlab.x.example/api/v4/projects/g%2Fs%2Fr/repository/archive.tar.gz?sha=v1"
305 );
306 assert_eq!(
307 host("https://bitbucket.org/").archive_url("w/r", "v1"),
308 "https://bitbucket.org/w/r/get/v1.tar.gz"
309 );
310 assert_eq!(
311 host("https://git.x.example/").archive_url("o/r", "v1"),
312 "https://git.x.example/api/v3/repos/o/r/tarball/v1"
313 );
314 }
315
316 #[test]
317 fn credential_fill_reads_the_helper_for_that_host() {
318 if std::process::Command::new("git").arg("--version").output().is_err() {
319 return;
320 }
321 let o = Url::parse("https://git.acme.example/").unwrap().origin();
322 let got = credential_fill(
323 &o,
324 &[
325 ("GIT_CONFIG_GLOBAL", "/dev/null"),
326 ("GIT_CONFIG_SYSTEM", "/dev/null"),
327 ("GIT_CONFIG_COUNT", "1"),
328 ("GIT_CONFIG_KEY_0", "credential.helper"),
329 ("GIT_CONFIG_VALUE_0", "!f() { echo username=me; echo password=SECRET-FILL; }; f"),
330 ],
331 );
332 assert_eq!(got, Some(("me".into(), "SECRET-FILL".into())));
333 }
334}