Skip to main content

lockdocs_core/private/
go.rs

1//! Go private modules: `GOPROXY`, `GOPRIVATE`, `GONOPROXY`, `GONOSUMDB` (from
2//! the environment or the `go env` file) and `~/.netrc`. A module a private
3//! proxy serves is read from that proxy; a private module without one is read
4//! straight from its git host at the version's tag, like `go` does.
5
6use super::*;
7
8const PUBLIC_PROXIES: &[&str] = &["proxy.golang.org", "goproxy.io", "goproxy.cn"];
9
10/// A Go setting: the environment, then the `go env -w` file.
11fn var(env: &Env, name: &str) -> Option<String> {
12    if let Some(v) = env.get(name) {
13        return Some(v);
14    }
15    let file = env.get("GOENV").map(PathBuf::from).or_else(|| env.home_file(".config/go/env"))?;
16    read(&file)?
17        .lines()
18        .find_map(|l| l.strip_prefix(&format!("{name}=")).map(|v| v.trim().to_string()).filter(|v| !v.is_empty()))
19}
20
21fn glob(pat: &str, s: &str) -> bool {
22    let (p, t): (Vec<char>, Vec<char>) = (pat.chars().collect(), s.chars().collect());
23    fn m(p: &[char], t: &[char]) -> bool {
24        match p.first() {
25            None => t.is_empty(),
26            Some('*') => (0..=t.len()).any(|i| m(&p[1..], &t[i..])),
27            Some('?') => !t.is_empty() && m(&p[1..], &t[1..]),
28            Some(c) => t.first() == Some(c) && m(&p[1..], &t[1..]),
29        }
30    }
31    m(&p, &t)
32}
33
34/// Go's pattern rule: each comma-separated glob matches a prefix of the module
35/// path made of as many elements as the glob has.
36pub fn matches_patterns(patterns: &str, module: &str) -> bool {
37    patterns.split(',').map(str::trim).filter(|p| !p.is_empty()).any(|p| {
38        let n = p.split('/').count();
39        let prefix: Vec<&str> = module.split('/').take(n).collect();
40        prefix.len() == n && glob(p, &prefix.join("/"))
41    })
42}
43
44fn private_module(env: &Env, name: &str) -> bool {
45    ["GOPRIVATE", "GONOPROXY", "GONOSUMDB"]
46        .iter()
47        .any(|v| var(env, v).is_some_and(|p| matches_patterns(&p, name)))
48}
49
50/// Is this module private by the user's Go settings (process environment)?
51pub fn is_private_module(name: &str) -> bool {
52    direct(&Env::process(), name)
53}
54
55/// Does Go skip proxies for this module (so it is read from its git host)?
56fn direct(env: &Env, name: &str) -> bool {
57    ["GOPRIVATE", "GONOPROXY"]
58        .iter()
59        .any(|v| var(env, v).is_some_and(|p| matches_patterns(&p, name)))
60}
61
62pub fn plan(dep: &Dep, root: &Path, env: &Env) -> Plan {
63    let _ = root;
64    let private = private_module(env, &dep.name);
65    let mut creds = Creds::default();
66    let mut candidates = Vec::new();
67    let proxies = var(env, "GOPROXY").unwrap_or_default();
68    for p in proxies.split([',', '|']).map(str::trim) {
69        let Some(u) = Url::parse(p) else { continue };
70        if PUBLIC_PROXIES.contains(&u.host.as_str()) {
71            continue;
72        }
73        let cred = match &u.userinfo {
74            Some((user, Some(pw))) => Some(Credential::new(u.origin(), Auth::Basic(user.clone(), pw.clone()))),
75            _ => netrc_for(env, &u),
76        };
77        if let Some(c) = cred {
78            creds.push(c);
79        }
80        candidates.push(Candidate {
81            base: u.clean().trim_end_matches('/').to_string(),
82            private: true,
83            definitive: private,
84            pinned: None,
85        });
86    }
87    if private && direct(env, &dep.name) {
88        let host = dep.name.split('/').next().unwrap_or_default();
89        if let Some(u) = Url::parse(&format!("https://{host}/")) {
90            candidates.push(Candidate {
91                base: u.origin().to_string(),
92                private: true,
93                definitive: true,
94                pinned: Some("direct".into()),
95            });
96        }
97    }
98    if candidates.first().is_none_or(|c| !c.definitive) {
99        candidates.push(Candidate {
100            base: "https://proxy.golang.org".into(),
101            private: false,
102            definitive: false,
103            pinned: None,
104        });
105    }
106    Plan { candidates, creds }
107}
108
109/// `v1.2.3-0.20240101000000-abcdef123456` -> the commit prefix.
110fn pseudo_commit(version: &str) -> Option<&str> {
111    let (rest, hash) = version.rsplit_once('-')?;
112    let (_, stamp) = rest.rsplit_once('-')?;
113    (hash.len() == 12 && hash.bytes().all(|b| b.is_ascii_hexdigit()) && stamp.len() == 14 && stamp.bytes().all(|b| b.is_ascii_digit())).then_some(hash)
114}
115
116/// `(repository path, module subdirectory)` of a module path on a git host.
117pub fn split_module(module: &str) -> Option<(String, Option<String>)> {
118    let mut segs: Vec<&str> = module.split('/').skip(1).collect();
119    if let Some(i) = segs.iter().position(|s| s.ends_with(".git")) {
120        let repo = segs[..=i].join("/").trim_end_matches(".git").to_string();
121        segs.drain(..=i);
122        return Some((repo, major_trim(segs)));
123    }
124    if segs.len() < 2 {
125        return None;
126    }
127    let repo = segs[..2].join("/");
128    Some((repo, major_trim(segs[2..].to_vec())))
129}
130
131fn major_trim(mut segs: Vec<&str>) -> Option<String> {
132    if segs
133        .last()
134        .is_some_and(|s| s.len() > 1 && s.starts_with('v') && s[1..].bytes().all(|b| b.is_ascii_digit()))
135    {
136        segs.pop();
137    }
138    (!segs.is_empty()).then(|| segs.join("/"))
139}
140
141/// Download from a private proxy or straight from the git host. Ok(false) when absent.
142pub fn download(http: &Http, env: &Env, dep: &Dep, c: &Candidate, dir: &Path) -> Result<bool> {
143    if c.pinned.as_deref() == Some("direct") {
144        return download_direct(env, dep, c, dir);
145    }
146    let m = crate::locate::go_escape(&dep.name);
147    let v = crate::locate::go_escape(&dep.version);
148    let Some(bytes) = http.get_ok(&format!("{}/{m}/@v/{v}.zip", c.base.trim_end_matches('/')), None)? else {
149        return Ok(false);
150    };
151    crate::fetch::unzip(&bytes, dir, false, Some(&format!("{}@{}/", dep.name, dep.version)))?;
152    Ok(true)
153}
154
155fn download_direct(env: &Env, dep: &Dep, c: &Candidate, dir: &Path) -> Result<bool> {
156    let Some(origin) = Url::parse(&c.base).map(|u| u.origin()) else {
157        return Ok(false);
158    };
159    let Some((repo, sub)) = split_module(&dep.name) else {
160        bail!("{}: cannot tell which repository this module path names", dep.name);
161    };
162    let host = git::Host::of(origin);
163    let version = dep.version.trim_end_matches("+incompatible");
164    let reference = match (pseudo_commit(version), &sub) {
165        (Some(h), _) => h.to_string(),
166        (None, Some(s)) => format!("{s}/{version}"),
167        (None, None) => version.to_string(),
168    };
169    let http = Http::new(git::credentials(&host, env, true));
170    let Some(bytes) = http.get_ok(&host.archive_url(&repo, &reference), None)? else {
171        return Ok(false);
172    };
173    crate::fetch::untar_gz_sub(&bytes, dir, sub.as_deref())?;
174    Ok(true)
175}
176
177#[cfg(test)]
178mod tests {
179    use super::super::testutil::*;
180    use super::*;
181
182    fn dep(name: &str, v: &str) -> Dep {
183        Dep {
184            eco: Eco::Go,
185            name: name.into(),
186            version: v.into(),
187            direct: true,
188            from: "go.mod".into(),
189        }
190    }
191
192    #[test]
193    fn patterns_follow_go_semantics() {
194        assert!(matches_patterns("corp.example/*,github.com/acme", "corp.example/x/y"));
195        assert!(matches_patterns("corp.example", "corp.example/x/y"));
196        assert!(matches_patterns("github.com/acme", "github.com/acme/lib/v2"));
197        assert!(!matches_patterns("github.com/acme", "github.com/acmeco/lib"));
198        assert!(!matches_patterns("", "a/b"));
199        assert!(!matches_patterns("corp.example/x/y/z", "corp.example/x"));
200    }
201
202    #[test]
203    fn private_proxy_creds_and_direct_modules() {
204        let home = tempfile::tempdir().unwrap();
205        write(&home.path().join(".netrc"), "machine goproxy.corp.example login ci password SECRET-NETRC\n");
206        let env = Env::fixed(
207            &[
208                ("GOPROXY", "https://goproxy.corp.example/go,https://proxy.golang.org,direct"),
209                ("GOPRIVATE", "git.corp.example/*"),
210            ],
211            Some(home.path()),
212        );
213        let t = tempfile::tempdir().unwrap();
214        let p = plan(&dep("git.corp.example/team/lib", "v1.2.3"), t.path(), &env);
215        assert!(p.definitely_private());
216        assert_eq!(p.candidates[0].base, "https://goproxy.corp.example/go");
217        assert_eq!(p.candidates[1].pinned.as_deref(), Some("direct"));
218        assert!(p.creds.for_url(&Url::parse("https://goproxy.corp.example/go/x").unwrap()).is_some());
219        assert!(p.creds.for_url(&Url::parse("https://proxy.golang.org/x").unwrap()).is_none());
220        // A public module: the private proxy is tried first (non-definitive), public follows.
221        let q = plan(&dep("github.com/gin-gonic/gin", "v1.10.0"), t.path(), &env);
222        assert!(q.any_private() && !q.definitely_private());
223        assert!(!q.candidates.last().unwrap().private);
224        // With nothing configured, everything is public.
225        assert!(!plan(&dep("github.com/x/y", "v1.0.0"), t.path(), &Env::fixed(&[], None)).any_private());
226    }
227
228    #[test]
229    fn go_env_file_is_read() {
230        let home = tempfile::tempdir().unwrap();
231        write(&home.path().join(".config/go/env"), "GOPRIVATE=git.corp.example\nGOFLAGS=-mod=mod\n");
232        let env = Env::fixed(&[], Some(home.path()));
233        assert!(plan(&dep("git.corp.example/a/b", "v1.0.0"), home.path(), &env).definitely_private());
234    }
235
236    #[test]
237    fn module_paths_and_versions() {
238        assert_eq!(split_module("git.corp.example/team/lib"), Some(("team/lib".into(), None)));
239        assert_eq!(split_module("git.corp.example/team/lib/v3"), Some(("team/lib".into(), None)));
240        assert_eq!(
241            split_module("git.corp.example/team/lib/sub/pkg/v2"),
242            Some(("team/lib".into(), Some("sub/pkg".into())))
243        );
244        assert_eq!(split_module("git.corp.example/g/s/lib.git/mod"), Some(("g/s/lib".into(), Some("mod".into()))));
245        assert_eq!(pseudo_commit("v0.0.0-20240101123456-abcdef123456"), Some("abcdef123456"));
246        assert_eq!(pseudo_commit("v1.2.3"), None);
247    }
248
249    #[test]
250    fn downloads_from_a_private_proxy() {
251        let zip = {
252            use std::io::Write;
253            let mut w = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
254            let o = zip::write::SimpleFileOptions::default();
255            w.start_file("corp.example/lib@v1.0.0/README.md", o).unwrap();
256            w.write_all(b"# lib").unwrap();
257            w.finish().unwrap().into_inner()
258        };
259        let m = serve(move |p, auth| match (p, auth) {
260            ("/go/corp.example/lib/@v/v1.0.0.zip", Some("Basic Y2k6U0VDUkVULVBX")) => (200, "application/zip".into(), zip.clone(), vec![]),
261            _ => (401, "text/plain".into(), vec![], vec![]),
262        });
263        let home = tempfile::tempdir().unwrap();
264        write(&home.path().join(".netrc"), "machine 127.0.0.1 login ci password SECRET-PW\n");
265        let env = Env::fixed(&[("GOPROXY", &format!("{}/go", m.base)), ("GOPRIVATE", "corp.example")], Some(home.path()));
266        let t = tempfile::tempdir().unwrap();
267        let d = dep("corp.example/lib", "v1.0.0");
268        let p = plan(&d, t.path(), &env);
269        let out = tempfile::tempdir().unwrap();
270        assert!(download(&Http::new(p.creds), &env, &d, &p.candidates[0], out.path()).unwrap());
271        assert_eq!(std::fs::read_to_string(out.path().join("README.md")).unwrap(), "# lib");
272    }
273}