Skip to main content

lockdocs_core/private/
mod.rs

1//! lockdocs Pro "private sources": read packages and docs from the private
2//! registries and git hosts the user has already configured, with the user's
3//! own credentials, each sent only to the host it belongs to.
4//!
5//! Nothing here decides whether the caller may use Pro: the binary checks the
6//! licence and sets `Options::private`. This module only knows two things: how
7//! to find the user's existing configuration (`.npmrc`, uv/pip config,
8//! `.cargo/config.toml`, `GOPROXY`, `~/.netrc`, token variables,
9//! `git credential fill`), and how to keep a credential on its own host:
10//!
11//! * a `Credential` carries the exact origin (scheme, host, port) it was
12//!   configured for, and `Creds::for_url` returns it only for that origin;
13//! * `Http` follows redirects itself, one hop at a time, and asks `Creds` again
14//!   for every hop, so a redirect to another host loses the header;
15//! * `Auth` and `Credential` print as `***`, and every error that reaches the
16//!   user passes through `Creds::redact`.
17
18pub mod cargo;
19pub mod git;
20pub mod go;
21pub mod npm;
22pub mod pypi;
23
24use crate::{Dep, Eco};
25use anyhow::{bail, Result};
26use std::collections::HashMap;
27use std::io::Read;
28use std::path::{Path, PathBuf};
29
30/// The feature name a Pro-less call reports in the `pro_required` answer.
31pub const FEATURE: &str = "Private sources";
32
33/// A request needed a private source and the licence does not cover it.
34#[derive(Debug, Clone)]
35pub struct PrivateRequired {
36    pub package: String,
37    /// The host (never a credential) the package lives on.
38    pub host: String,
39}
40
41impl std::fmt::Display for PrivateRequired {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        write!(
44            f,
45            "{} is read from {}, a source outside the public defaults; reading private sources is part of lockdocs Pro",
46            self.package, self.host
47        )
48    }
49}
50
51impl std::error::Error for PrivateRequired {}
52
53/// The process environment, or a fixed one for tests.
54#[derive(Clone)]
55pub struct Env {
56    vars: Option<HashMap<String, String>>,
57    pub home: Option<PathBuf>,
58}
59
60impl Env {
61    pub fn process() -> Env {
62        Env {
63            vars: None,
64            home: dirs::home_dir(),
65        }
66    }
67    pub fn fixed(vars: &[(&str, &str)], home: Option<&Path>) -> Env {
68        Env {
69            vars: Some(vars.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect()),
70            home: home.map(Path::to_path_buf),
71        }
72    }
73    /// A variable, `None` when unset or empty.
74    pub fn get(&self, k: &str) -> Option<String> {
75        let v = match &self.vars {
76            Some(m) => m.get(k).cloned(),
77            None => std::env::var(k).ok(),
78        };
79        v.filter(|v| !v.is_empty())
80    }
81    /// Every variable whose name starts with `prefix` (case-insensitive).
82    pub fn with_prefix(&self, prefix: &str) -> Vec<(String, String)> {
83        let p = prefix.to_ascii_uppercase();
84        let all: Vec<(String, String)> = match &self.vars {
85            Some(m) => m.iter().map(|(k, v)| (k.clone(), v.clone())).collect(),
86            None => std::env::vars().collect(),
87        };
88        let mut out: Vec<_> = all
89            .into_iter()
90            .filter(|(k, _)| k.to_ascii_uppercase().starts_with(&p) && !k.is_empty())
91            .collect();
92        out.sort();
93        out
94    }
95    pub fn home_file(&self, rel: &str) -> Option<PathBuf> {
96        self.home.as_ref().map(|h| h.join(rel))
97    }
98}
99
100pub(crate) fn read(p: &Path) -> Option<String> {
101    std::fs::read_to_string(p).ok()
102}
103
104// ---- URLs and origins ------------------------------------------------------
105
106/// A parsed http(s) URL. Only what credential binding needs.
107#[derive(Debug, Clone, PartialEq, Eq)]
108pub struct Url {
109    pub scheme: String,
110    pub host: String,
111    pub port: u16,
112    pub userinfo: Option<(String, Option<String>)>,
113    /// Path and query, always starting with `/`.
114    pub path: String,
115}
116
117impl Url {
118    pub fn parse(s: &str) -> Option<Url> {
119        let s = s.trim();
120        let (scheme, rest) = s.split_once("://")?;
121        let scheme = scheme.to_ascii_lowercase();
122        if scheme != "http" && scheme != "https" {
123            return None;
124        }
125        let (auth, path) = match rest.find(['/', '?', '#']) {
126            Some(i) => (&rest[..i], &rest[i..]),
127            None => (rest, ""),
128        };
129        let path = path.split('#').next().unwrap_or("");
130        let path = if path.starts_with('/') { path.to_string() } else { format!("/{path}") };
131        let (userinfo, hostport) = match auth.rfind('@') {
132            Some(i) => {
133                let ui = &auth[..i];
134                let (u, p) = match ui.split_once(':') {
135                    Some((u, p)) => (u, Some(p)),
136                    None => (ui, None),
137                };
138                (Some((pct_decode(u), p.map(pct_decode))), &auth[i + 1..])
139            }
140            None => (None, auth),
141        };
142        let (host, port) = if let Some(r) = hostport.strip_prefix('[') {
143            let (h, after) = r.split_once(']')?;
144            (format!("[{}]", h.to_ascii_lowercase()), after.strip_prefix(':').and_then(|p| p.parse().ok()))
145        } else {
146            match hostport.rsplit_once(':') {
147                Some((h, p)) if p.chars().all(|c| c.is_ascii_digit()) && !p.is_empty() => (h.to_ascii_lowercase(), p.parse().ok()),
148                _ => (hostport.to_ascii_lowercase(), None),
149            }
150        };
151        if host.is_empty() {
152            return None;
153        }
154        let port = port.unwrap_or(if scheme == "https" { 443 } else { 80 });
155        Some(Url {
156            scheme,
157            host,
158            port,
159            userinfo,
160            path,
161        })
162    }
163
164    pub fn origin(&self) -> Origin {
165        Origin {
166            scheme: self.scheme.clone(),
167            host: self.host.clone(),
168            port: self.port,
169        }
170    }
171
172    /// `scheme://host[:port]` plus the path, without credentials.
173    pub fn clean(&self) -> String {
174        format!("{}{}", self.origin(), self.path)
175    }
176
177    /// Resolve a `Location` header against this URL.
178    pub fn join(&self, loc: &str) -> Option<Url> {
179        if loc.contains("://") {
180            return Url::parse(loc);
181        }
182        if let Some(rest) = loc.strip_prefix("//") {
183            return Url::parse(&format!("{}://{rest}", self.scheme));
184        }
185        let path = if loc.starts_with('/') {
186            loc.to_string()
187        } else {
188            let base = self.path.split('?').next().unwrap_or("/");
189            let dir = &base[..base.rfind('/').map_or(0, |i| i + 1)];
190            format!("{dir}{loc}")
191        };
192        Some(Url {
193            path: normalize_path(&path),
194            userinfo: None,
195            ..self.clone()
196        })
197    }
198}
199
200/// Resolve `.` and `..` segments of a path (query kept as-is).
201fn normalize_path(p: &str) -> String {
202    let (path, query) = match p.split_once('?') {
203        Some((a, b)) => (a, Some(b)),
204        None => (p, None),
205    };
206    let mut out: Vec<&str> = Vec::new();
207    for seg in path.split('/').skip(1) {
208        match seg {
209            "." => {}
210            ".." => {
211                out.pop();
212            }
213            s => out.push(s),
214        }
215    }
216    let mut r = format!("/{}", out.join("/"));
217    if path.ends_with("/.") || path.ends_with("/..") {
218        r.push('/');
219    }
220    match query {
221        Some(q) => format!("{r}?{q}"),
222        None => r,
223    }
224}
225
226/// Scheme, host and port: the unit a credential is bound to.
227#[derive(Debug, Clone, PartialEq, Eq, Hash)]
228pub struct Origin {
229    pub scheme: String,
230    pub host: String,
231    pub port: u16,
232}
233
234impl std::fmt::Display for Origin {
235    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
236        let default = if self.scheme == "https" { 443 } else { 80 };
237        if self.port == default {
238            write!(f, "{}://{}", self.scheme, self.host)
239        } else {
240            write!(f, "{}://{}:{}", self.scheme, self.host, self.port)
241        }
242    }
243}
244
245fn pct_decode(s: &str) -> String {
246    let b = s.as_bytes();
247    let mut out = Vec::with_capacity(b.len());
248    let mut i = 0;
249    while i < b.len() {
250        if b[i] == b'%' && i + 2 < b.len() {
251            if let Ok(v) = u8::from_str_radix(&s[i + 1..i + 3], 16) {
252                out.push(v);
253                i += 3;
254                continue;
255            }
256        }
257        out.push(b[i]);
258        i += 1;
259    }
260    String::from_utf8_lossy(&out).into_owned()
261}
262
263// ---- base64 ----------------------------------------------------------------
264
265const B64: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
266
267pub(crate) fn b64_encode(input: &[u8]) -> String {
268    let mut out = String::new();
269    for c in input.chunks(3) {
270        let n = (u32::from(c[0]) << 16) | (u32::from(*c.get(1).unwrap_or(&0)) << 8) | u32::from(*c.get(2).unwrap_or(&0));
271        for i in 0..4 {
272            if i <= c.len() {
273                out.push(B64[((n >> (18 - 6 * i)) & 63) as usize] as char);
274            } else {
275                out.push('=');
276            }
277        }
278    }
279    out
280}
281
282pub(crate) fn b64_decode(s: &str) -> Option<Vec<u8>> {
283    let mut out = Vec::new();
284    let mut acc = 0u32;
285    let mut bits = 0;
286    for c in s.trim().trim_end_matches('=').bytes() {
287        let v = match c {
288            b'A'..=b'Z' => c - b'A',
289            b'a'..=b'z' => c - b'a' + 26,
290            b'0'..=b'9' => c - b'0' + 52,
291            b'+' | b'-' => 62,
292            b'/' | b'_' => 63,
293            _ => return None,
294        };
295        acc = (acc << 6) | u32::from(v);
296        bits += 6;
297        if bits >= 8 {
298            bits -= 8;
299            out.push((acc >> bits) as u8);
300            acc &= (1 << bits) - 1;
301        }
302    }
303    Some(out)
304}
305
306// ---- credentials -----------------------------------------------------------
307
308/// How a secret is presented. Prints as `***`.
309#[derive(Clone, PartialEq, Eq)]
310pub enum Auth {
311    Bearer(String),
312    Basic(String, String),
313    /// The `Authorization` value as-is (Cargo registry tokens).
314    Raw(String),
315}
316
317impl std::fmt::Debug for Auth {
318    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
319        f.write_str("Auth(***)")
320    }
321}
322
323impl Auth {
324    /// The `Authorization` header value.
325    pub fn header(&self) -> String {
326        match self {
327            Auth::Bearer(t) => format!("Bearer {t}"),
328            Auth::Basic(u, p) => format!("Basic {}", b64_encode(format!("{u}:{p}").as_bytes())),
329            Auth::Raw(v) => v.clone(),
330        }
331    }
332    /// Every substring that must never be printed.
333    fn secrets(&self) -> Vec<String> {
334        match self {
335            Auth::Bearer(t) | Auth::Raw(t) => vec![t.clone()],
336            Auth::Basic(u, p) => vec![p.clone(), b64_encode(format!("{u}:{p}").as_bytes())],
337        }
338    }
339}
340
341/// A secret bound to one origin (and optionally a path prefix on it).
342#[derive(Clone, PartialEq, Eq)]
343pub struct Credential {
344    pub origin: Origin,
345    pub path_prefix: Option<String>,
346    pub auth: Auth,
347}
348
349impl std::fmt::Debug for Credential {
350    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
351        write!(f, "Credential({}, ***)", self.origin)
352    }
353}
354
355impl Credential {
356    pub fn new(origin: Origin, auth: Auth) -> Credential {
357        Credential {
358            origin,
359            path_prefix: None,
360            auth,
361        }
362    }
363    fn matches(&self, url: &Url) -> bool {
364        self.origin == url.origin() && self.path_prefix.as_ref().is_none_or(|p| url.path.starts_with(p.as_str()))
365    }
366}
367
368/// The credentials in play for one operation.
369#[derive(Clone, Default, Debug)]
370pub struct Creds(pub Vec<Credential>);
371
372impl Creds {
373    pub fn push(&mut self, c: Credential) {
374        if !self.0.contains(&c) {
375            self.0.push(c);
376        }
377    }
378    /// The authorization for exactly this URL's origin, and nothing else.
379    /// The most specific path prefix wins.
380    pub fn for_url(&self, url: &Url) -> Option<&Auth> {
381        self.0
382            .iter()
383            .filter(|c| c.matches(url))
384            .max_by_key(|c| c.path_prefix.as_ref().map_or(0, String::len))
385            .map(|c| &c.auth)
386    }
387    /// `text` with every secret and every URL userinfo replaced by `***`.
388    pub fn redact(&self, text: &str) -> String {
389        let mut out = text.to_string();
390        for c in &self.0 {
391            for s in c.auth.secrets() {
392                if s.len() >= 3 {
393                    out = out.replace(&s, "***");
394                }
395            }
396        }
397        strip_userinfo(&out)
398    }
399}
400
401/// `https://user:pw@host/x` becomes `https://***@host/x`.
402pub fn strip_userinfo(text: &str) -> String {
403    let mut out = String::with_capacity(text.len());
404    let mut rest = text;
405    while let Some(i) = rest.find("://") {
406        let (head, tail) = rest.split_at(i + 3);
407        out.push_str(head);
408        let end = tail
409            .find(|c: char| c.is_whitespace() || c == '/' || c == '"' || c == '\'')
410            .unwrap_or(tail.len());
411        match tail[..end].rfind('@') {
412            Some(at) => {
413                out.push_str("***");
414                out.push_str(&tail[at..]);
415                rest = "";
416            }
417            None => {
418                out.push_str(&tail[..end]);
419                rest = &tail[end..];
420            }
421        }
422        if rest.is_empty() {
423            break;
424        }
425    }
426    out.push_str(rest);
427    out
428}
429
430// ---- netrc -----------------------------------------------------------------
431
432/// `machine HOST login L password P` entries (and `default`) of a netrc file.
433pub fn parse_netrc(text: &str) -> Vec<(Option<String>, String, String)> {
434    let mut out = Vec::new();
435    let mut toks = text.split_whitespace();
436    let (mut machine, mut login, mut password): (Option<String>, Option<String>, Option<String>) = (None, None, None);
437    let mut open = false;
438    let flush = |m: &mut Option<String>, l: &mut Option<String>, p: &mut Option<String>, open: &mut bool, out: &mut Vec<_>| {
439        if *open {
440            if let (Some(l), Some(p)) = (l.take(), p.take()) {
441                out.push((m.take(), l, p));
442            }
443        }
444        *m = None;
445        *l = None;
446        *p = None;
447        *open = false;
448    };
449    while let Some(t) = toks.next() {
450        match t {
451            "machine" => {
452                flush(&mut machine, &mut login, &mut password, &mut open, &mut out);
453                machine = toks.next().map(|h| h.to_ascii_lowercase());
454                open = true;
455            }
456            "default" => {
457                flush(&mut machine, &mut login, &mut password, &mut open, &mut out);
458                open = true;
459            }
460            "login" => login = toks.next().map(String::from),
461            "password" => password = toks.next().map(String::from),
462            "account" => {
463                toks.next();
464            }
465            "macdef" => {
466                // A macro runs to the next blank line; this tokenizer cannot see
467                // lines, so stop reading rather than misparse it.
468                break;
469            }
470            _ => {}
471        }
472    }
473    flush(&mut machine, &mut login, &mut password, &mut open, &mut out);
474    out
475}
476
477/// The netrc credential for `url`'s host, bound to that URL's origin.
478pub fn netrc_for(env: &Env, url: &Url) -> Option<Credential> {
479    let path = env
480        .get("NETRC")
481        .map(PathBuf::from)
482        .or_else(|| env.home_file(".netrc"))
483        .or_else(|| env.home_file("_netrc"))?;
484    let entries = parse_netrc(&read(&path)?);
485    let hit = entries
486        .iter()
487        .find(|(m, _, _)| m.as_deref() == Some(url.host.trim_matches(['[', ']'])))
488        .or_else(|| entries.iter().find(|(m, _, _)| m.is_none()))?;
489    Some(Credential::new(url.origin(), Auth::Basic(hit.1.clone(), hit.2.clone())))
490}
491
492// ---- HTTP with host-bound credentials ---------------------------------------
493
494const MAX_BODY: u64 = 64 * 1024 * 1024;
495const MAX_HOPS: usize = 5;
496
497pub struct Http {
498    agent: ureq::Agent,
499    pub creds: Creds,
500}
501
502pub struct Reply {
503    pub status: u16,
504    pub body: Vec<u8>,
505    /// The URL that finally answered (credentials stripped).
506    pub url: String,
507}
508
509impl Http {
510    pub fn new(creds: Creds) -> Http {
511        let agent = ureq::Agent::config_builder()
512            .timeout_global(Some(std::time::Duration::from_secs(60)))
513            .max_redirects(0)
514            .http_status_as_error(false)
515            .user_agent(concat!("lockdocs/", env!("CARGO_PKG_VERSION"), " (+https://github.com/SylphxAI/lockdocs)"))
516            .build()
517            .into();
518        Http { agent, creds }
519    }
520
521    /// GET with credentials bound to their origin. Redirects are followed here,
522    /// one hop at a time, and the credential is looked up again for each hop.
523    pub fn get(&self, url: &str, accept: Option<&str>) -> Result<Reply> {
524        let Some(mut cur) = Url::parse(url) else {
525            bail!("not an http(s) URL: {}", strip_userinfo(url));
526        };
527        for _ in 0..=MAX_HOPS {
528            let mut req = self.agent.get(&cur.clean());
529            if let Some(a) = accept {
530                req = req.header("Accept", a);
531            }
532            if let Some(auth) = self.creds.for_url(&cur) {
533                req = req.header("Authorization", &auth.header());
534            }
535            let mut res = match req.call() {
536                Ok(r) => r,
537                Err(e) => bail!("GET {}: {}", cur.clean(), self.creds.redact(&e.to_string())),
538            };
539            let status = res.status().as_u16();
540            if matches!(status, 301 | 302 | 303 | 307 | 308) {
541                let loc = res.headers().get("location").and_then(|v| v.to_str().ok()).map(String::from);
542                let Some(next) = loc.and_then(|l| cur.join(&l)) else {
543                    bail!("GET {}: redirect without a usable Location", cur.clean());
544                };
545                cur = next;
546                continue;
547            }
548            let mut body = Vec::new();
549            res.body_mut().as_reader().take(MAX_BODY + 1).read_to_end(&mut body)?;
550            if body.len() as u64 > MAX_BODY {
551                bail!("{} is larger than {} MB", cur.clean(), MAX_BODY / 1024 / 1024);
552            }
553            return Ok(Reply {
554                status,
555                body,
556                url: cur.clean(),
557            });
558        }
559        bail!("GET {}: too many redirects", strip_userinfo(url))
560    }
561
562    /// A 200 body, `None` on 404; 401/403 and other statuses are errors that
563    /// say which origin refused, never what was sent.
564    pub fn get_ok(&self, url: &str, accept: Option<&str>) -> Result<Option<Vec<u8>>> {
565        let r = self.get(url, accept)?;
566        match r.status {
567            200 => Ok(Some(r.body)),
568            404 | 410 => Ok(None),
569            401 | 403 => {
570                let host = Url::parse(&r.url).map_or_else(|| "the registry".to_string(), |u| u.origin().to_string());
571                let sent = Url::parse(&r.url).is_some_and(|u| self.creds.for_url(&u).is_some());
572                bail!(
573                    "{host} refused the request (HTTP {}){}",
574                    r.status,
575                    if sent {
576                        "; the configured credential for that host was rejected, check that it is current"
577                    } else {
578                        "; no credential is configured for that host"
579                    }
580                )
581            }
582            s => bail!("{}: HTTP {s}", r.url),
583        }
584    }
585}
586
587// ---- plans ------------------------------------------------------------------
588
589/// One place a package may be read from.
590#[derive(Debug, Clone)]
591pub struct Candidate {
592    /// Registry, index or proxy base URL (no credentials in it).
593    pub base: String,
594    /// Not one of the public defaults.
595    pub private: bool,
596    /// The user's config or the lockfile says this package belongs here, so a
597    /// public lookup would be wrong (or would leak the name).
598    pub definitive: bool,
599    /// The exact artifact URL the lockfile recorded, when it did.
600    pub pinned: Option<String>,
601}
602
603#[derive(Debug, Clone, Default)]
604pub struct Plan {
605    pub candidates: Vec<Candidate>,
606    pub creds: Creds,
607}
608
609impl Plan {
610    pub fn private_host(&self) -> Option<String> {
611        self.candidates
612            .iter()
613            .find(|c| c.private)
614            .and_then(|c| Url::parse(&c.base).map(|u| u.origin().host))
615    }
616    /// Needs a private source for sure: the first choice is private and definitive.
617    pub fn definitely_private(&self) -> bool {
618        self.candidates.first().is_some_and(|c| c.private && c.definitive)
619    }
620    pub fn any_private(&self) -> bool {
621        self.candidates.iter().any(|c| c.private)
622    }
623}
624
625/// Where `dep` would be read from, per the user's own configuration and lockfiles.
626pub fn plan(dep: &Dep, root: &Path, env: &Env) -> Plan {
627    match dep.eco {
628        Eco::Npm => npm::plan(dep, root, env),
629        Eco::PyPI => pypi::plan(dep, root, env),
630        Eco::Cargo => cargo::plan(dep, root, env),
631        Eco::Go => go::plan(dep, root, env),
632    }
633}
634
635/// Does reading `dep` need a private source for sure (no network)?
636pub fn needs_private(dep: &Dep, root: &Path) -> Option<PrivateRequired> {
637    let p = plan(dep, root, &Env::process());
638    p.definitely_private().then(|| PrivateRequired {
639        package: dep.id(),
640        host: p.private_host().unwrap_or_default(),
641    })
642}
643
644/// How a private download ended.
645pub enum Outcome {
646    /// Unpacked into the target directory; `host` is where it came from.
647    Done { host: String },
648    /// No private candidate applied; the caller runs the public path. Carries
649    /// the host of a private candidate that was skipped without Pro, so a
650    /// public failure can be reported as "needs Pro".
651    UsePublic { skipped: Option<PrivateRequired> },
652}
653
654/// Run the plan: private candidates first (when `allowed`), then hand over to
655/// the public path. Without `allowed`, a definitively private package fails at
656/// once with `PrivateRequired`, without any network.
657pub fn fetch_into(dep: &Dep, root: &Path, allowed: bool, dir: &Path) -> Result<Outcome> {
658    let env = Env::process();
659    let plan = plan(dep, root, &env);
660    if !plan.any_private() {
661        return Ok(Outcome::UsePublic { skipped: None });
662    }
663    let required = PrivateRequired {
664        package: dep.id(),
665        host: plan.private_host().unwrap_or_default(),
666    };
667    if !allowed {
668        if plan.definitely_private() {
669            return Err(required.into());
670        }
671        return Ok(Outcome::UsePublic { skipped: Some(required) });
672    }
673    let http = Http::new(plan.creds.clone());
674    let mut errors = Vec::new();
675    for c in &plan.candidates {
676        if !c.private {
677            return Ok(Outcome::UsePublic { skipped: None });
678        }
679        let r = match dep.eco {
680            Eco::Npm => npm::download(&http, dep, c, dir),
681            Eco::PyPI => pypi::download(&http, dep, c, dir),
682            Eco::Cargo => cargo::download(&http, dep, c, dir),
683            Eco::Go => go::download(&http, &env, dep, c, dir),
684        };
685        match r {
686            Ok(true) => {
687                return Ok(Outcome::Done {
688                    host: Url::parse(&c.base).map_or_else(|| "a private source".into(), |u| u.origin().to_string()),
689                })
690            }
691            Ok(false) => errors.push(format!(
692                "{}: not found",
693                Url::parse(&c.base).map_or_else(|| "private source".into(), |u| u.origin().to_string())
694            )),
695            Err(e) => errors.push(plan.creds.redact(&format!("{e:#}"))),
696        }
697    }
698    bail!("{}: not available from the configured private sources ({})", dep.id(), errors.join("; "))
699}
700
701#[cfg(test)]
702pub(crate) mod testutil {
703    use std::io::{Read, Write};
704    use std::sync::{Arc, Mutex};
705
706    /// A request the mock saw: path and the Authorization header, if any.
707    pub type Seen = Arc<Mutex<Vec<(String, Option<String>)>>>;
708
709    pub struct Mock {
710        pub base: String,
711        pub host: String,
712        pub seen: Seen,
713    }
714
715    /// An in-process HTTP server. `route(path, authorization)` returns
716    /// (status, content type, body, extra headers).
717    pub fn serve<F>(route: F) -> Mock
718    where
719        F: Fn(&str, Option<&str>) -> (u16, String, Vec<u8>, Vec<(String, String)>) + Send + 'static,
720    {
721        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
722        let addr = listener.local_addr().unwrap();
723        let seen: Seen = Default::default();
724        let log = seen.clone();
725        std::thread::spawn(move || {
726            for stream in listener.incoming() {
727                let Ok(mut c) = stream else { return };
728                let mut buf = Vec::new();
729                let mut chunk = [0u8; 4096];
730                while !buf.windows(4).any(|w| w == b"\r\n\r\n") {
731                    let n = c.read(&mut chunk).unwrap_or(0);
732                    if n == 0 {
733                        break;
734                    }
735                    buf.extend_from_slice(&chunk[..n]);
736                }
737                let head = String::from_utf8_lossy(&buf).to_string();
738                let path = head.lines().next().unwrap_or("").split(' ').nth(1).unwrap_or("").to_string();
739                let auth = head.lines().find_map(|l| {
740                    l.split_once(':')
741                        .filter(|(k, _)| k.eq_ignore_ascii_case("authorization"))
742                        .map(|(_, v)| v.trim().to_string())
743                });
744                log.lock().unwrap().push((path.clone(), auth.clone()));
745                let (status, ctype, body, extra) = route(&path, auth.as_deref());
746                let mut resp = format!(
747                    "HTTP/1.1 {status} X\r\nContent-Type: {ctype}\r\nContent-Length: {}\r\nConnection: close\r\n",
748                    body.len()
749                );
750                for (k, v) in extra {
751                    resp.push_str(&format!("{k}: {v}\r\n"));
752                }
753                resp.push_str("\r\n");
754                let _ = c.write_all(resp.as_bytes());
755                let _ = c.write_all(&body);
756                let _ = c.flush();
757                let _ = c.shutdown(std::net::Shutdown::Write);
758            }
759        });
760        Mock {
761            base: format!("http://{addr}"),
762            host: addr.to_string(),
763            seen,
764        }
765    }
766
767    pub fn tgz(files: &[(&str, &[u8])]) -> Vec<u8> {
768        let mut tar = tar::Builder::new(flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default()));
769        for (n, data) in files {
770            let mut h = tar::Header::new_gnu();
771            h.set_size(data.len() as u64);
772            h.set_mode(0o644);
773            h.set_cksum();
774            tar.append_data(&mut h, n, *data).unwrap();
775        }
776        tar.into_inner().unwrap().finish().unwrap()
777    }
778
779    /// A zip of `files`, for a wheel or sdist artifact in a test.
780    pub fn zip(files: &[(&str, &[u8])]) -> Vec<u8> {
781        use std::io::Write;
782        let mut w = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
783        let opts = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated);
784        for (name, data) in files {
785            w.start_file(*name, opts).unwrap();
786            w.write_all(data).unwrap();
787        }
788        w.finish().unwrap().into_inner()
789    }
790
791    pub fn write(p: &std::path::Path, text: &str) {
792        std::fs::create_dir_all(p.parent().unwrap()).unwrap();
793        std::fs::write(p, text).unwrap();
794    }
795}
796
797#[cfg(test)]
798mod tests {
799    use super::testutil::*;
800    use super::*;
801
802    #[test]
803    fn url_origin_and_userinfo() {
804        let u = Url::parse("https://User:p%40ss@Reg.Example.com:8443/a/b?x=1#f").unwrap();
805        assert_eq!(u.origin().to_string(), "https://reg.example.com:8443");
806        assert_eq!(u.userinfo, Some(("User".into(), Some("p@ss".into()))));
807        assert_eq!(u.clean(), "https://reg.example.com:8443/a/b?x=1");
808        assert_eq!(Url::parse("https://x.com").unwrap().origin().to_string(), "https://x.com");
809        assert_eq!(Url::parse("http://x.com:80/").unwrap().origin(), Url::parse("http://x.com/").unwrap().origin());
810        assert_ne!(Url::parse("http://x.com/").unwrap().origin(), Url::parse("https://x.com/").unwrap().origin());
811        assert_ne!(
812            Url::parse("https://x.com:444/").unwrap().origin(),
813            Url::parse("https://x.com/").unwrap().origin()
814        );
815        assert!(Url::parse("ftp://x.com/").is_none());
816    }
817
818    #[test]
819    fn base64_round_trip() {
820        for s in ["a", "ab", "abc", "user:pa55word", "x-access-token:ghp_abc"] {
821            assert_eq!(b64_decode(&b64_encode(s.as_bytes())).unwrap(), s.as_bytes());
822        }
823    }
824
825    #[test]
826    fn credentials_match_only_their_origin_and_never_print() {
827        let u = Url::parse("https://npm.corp.example/").unwrap();
828        let mut creds = Creds::default();
829        creds.push(Credential::new(u.origin(), Auth::Bearer("SECRET-TOKEN-1".into())));
830        assert!(creds.for_url(&u).is_some());
831        for other in [
832            "https://npm.corp.example:8443/",
833            "http://npm.corp.example/",
834            "https://evil.example/",
835            "https://npm.corp.example.evil.example/",
836        ] {
837            assert!(creds.for_url(&Url::parse(other).unwrap()).is_none(), "{other}");
838        }
839        assert!(!format!("{creds:?}").contains("SECRET"));
840        assert_eq!(
841            creds.redact("failed with SECRET-TOKEN-1 at https://u:pw@h.example/x"),
842            "failed with *** at https://***@h.example/x"
843        );
844    }
845
846    #[test]
847    fn netrc_entries() {
848        let e = parse_netrc("machine a.example login u1 password p1\nmachine b.example\n  login u2\n  password p2\ndefault login d password dp\n");
849        assert_eq!(e.len(), 3);
850        assert_eq!(e[1], (Some("b.example".into()), "u2".into(), "p2".into()));
851        assert_eq!(e[2].0, None);
852    }
853
854    #[test]
855    fn redirect_to_another_host_drops_the_credential() {
856        // The "evil" server records what it receives; the "good" one redirects to it.
857        let evil = serve(|_, _| (200, "text/plain".into(), b"ok".to_vec(), vec![]));
858        let target = format!("http://localhost:{}/steal", evil.host.rsplit(':').next().unwrap());
859        let good = serve(move |p, _| {
860            if p == "/start" {
861                (302, "text/plain".into(), vec![], vec![("Location".into(), target.clone())])
862            } else {
863                (404, "text/plain".into(), vec![], vec![])
864            }
865        });
866        let mut creds = Creds::default();
867        creds.push(Credential::new(Url::parse(&good.base).unwrap().origin(), Auth::Bearer("SECRET-TOKEN-2".into())));
868        let http = Http::new(creds);
869        let r = http.get(&format!("{}/start", good.base), None).unwrap();
870        assert_eq!(r.status, 200);
871        let good_seen = good.seen.lock().unwrap().clone();
872        assert_eq!(good_seen[0].1.as_deref(), Some("Bearer SECRET-TOKEN-2"));
873        let evil_seen = evil.seen.lock().unwrap().clone();
874        assert_eq!(evil_seen.len(), 1);
875        assert_eq!(evil_seen[0].1, None, "credential followed a redirect to another host");
876    }
877
878    #[test]
879    fn rejected_credential_error_names_host_not_secret() {
880        let m = serve(|_, _| (401, "text/plain".into(), vec![], vec![]));
881        let mut creds = Creds::default();
882        creds.push(Credential::new(Url::parse(&m.base).unwrap().origin(), Auth::Bearer("SECRET-TOKEN-3".into())));
883        let e = Http::new(creds).get_ok(&format!("{}/x", m.base), None).unwrap_err().to_string();
884        assert!(e.contains("rejected") && !e.contains("SECRET"), "{e}");
885    }
886}