deps-core 5.0.0

Core abstractions for deps-lsp: caching, errors, and traits
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//! The one URL-alone registry trust rule (spec 084, #1822).
//!
//! A registry URL is [`RegistryTrust::Trusted`] if and only if its canonical form equals a URL
//! declared in an operator-owned file, whichever tier declared it in the current workspace;
//! otherwise it is [`RegistryTrust::WorkspaceDeclared`]. Trust is therefore a function of the
//! URL alone, so two workspaces sharing a URL never differ in transport or policy.
//!
//! The canonical form is the parsed URL after `${VAR}`/`%VAR%` expansion with the trailing `/`
//! trimmed; equality is per URL, never per host.
//!
//! This module decides **egress and workspace policy only**. A consumer may additionally bind a
//! credential to a Trusted URL (`deps-swift`, `deps-nuget`, `deps-cargo`); that rule lives with
//! the consumer and is only ever applied to a URL this rule classified Trusted.

use std::collections::HashSet;
use std::hash::{Hash, Hasher};

use super::{
    BlockedHost, CanonicalUrl, IndexUrlError, PolicyGate, RedactedUrl, RegistryAccessPolicy,
    RegistryUrlKind, TrustedConstantRegistryUrl, ValidatedRegistryUrl, WorkspaceRegistryAccess,
    classify_host, enforce_policy, validate_index_url,
};
use crate::EgressTier;
use crate::config_trust::{OperatorEnvValue, OperatorPath};

/// Whether a registry URL is the operator's own or only workspace-declared.
///
/// **Exhaustive**: a wildcard arm at a consuming match would silently trust workspace input.
///
/// # Examples
///
/// ```
/// use deps_core::EgressTier;
/// use deps_core::net_policy::RegistryTrust;
///
/// assert_eq!(RegistryTrust::Trusted.egress(), EgressTier::Baseline);
/// assert_eq!(RegistryTrust::WorkspaceDeclared.egress(), EgressTier::Guarded);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RegistryTrust {
    /// Equal to a URL declared in an operator-owned file: the workspace policy is skipped at URL
    /// time and at connect time, and requests use the system-proxy Baseline egress.
    Trusted,
    /// Declared only by the workspace: policy-gated and fetched over the Guarded egress.
    WorkspaceDeclared,
}

impl RegistryTrust {
    /// The egress tier requests to a URL of this trust use.
    #[must_use]
    pub const fn egress(self) -> EgressTier {
        match self {
            Self::Trusted => EgressTier::Baseline,
            Self::WorkspaceDeclared => EgressTier::Guarded,
        }
    }
}

/// The never-a-registry floor: the block for `url` when its host is of a class no setting
/// unblocks (loopback, link-local, cloud metadata, unspecified, reserved), else `None`.
///
/// Applies to Trusted registries too: operator ownership lifts the workspace policy, never this
/// floor. Test builds let `http` loopback through (the mock-server shape, which
/// `validate_index_url` admits only there); `https` loopback stays blocked everywhere.
///
/// # Examples
///
/// ```
/// use deps_core::net_policy::{WorkspaceRegistryAccess, floor_block};
///
/// let metadata = url::Url::parse("https://169.254.169.254/").unwrap();
/// assert!(floor_block(&metadata, WorkspaceRegistryAccess::All).is_some());
/// let public = url::Url::parse("https://index.crates.io/").unwrap();
/// assert!(floor_block(&public, WorkspaceRegistryAccess::All).is_none());
/// ```
#[must_use]
pub fn floor_block(url: &url::Url, level: WorkspaceRegistryAccess) -> Option<BlockedHost> {
    let class = classify_host(url);
    let test_http_loopback = cfg!(any(test, feature = "test-util")) && url.scheme() == "http";
    (class.never_a_registry() && !test_http_loopback).then(|| BlockedHost::new(class, level))
}

/// The registry URLs one ecosystem's operator-owned file declares.
///
/// Built only from an [`OperatorPath`], so a repository cannot populate it. Misuse-resistant, not
/// unforgeable: the caller asserts that `candidates` are the file's entries.
///
/// # Examples
///
/// ```
/// use deps_core::config_trust::OperatorPath;
/// use deps_core::net_policy::OperatorRegistries;
///
/// let file = OperatorPath::for_test("/home/me/.npmrc");
/// let set = OperatorRegistries::declared_in(&file, ["https://npm.corp.example/"]);
/// assert!(set.contains_str("https://npm.corp.example"));
/// assert!(!set.contains_str("https://npm.corp.example/other"));
/// ```
#[derive(Clone, Default, PartialEq, Eq)]
pub struct OperatorRegistries {
    urls: HashSet<CanonicalUrl>,
}

/// Prints the size only: the set can hold query strings that carry tokens, so no URL is ever
/// rendered.
impl std::fmt::Debug for OperatorRegistries {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("OperatorRegistries")
            .field("urls", &self.urls.len())
            .finish()
    }
}

impl OperatorRegistries {
    /// The URLs declared in `file`, given as the post-expansion strings the ecosystem resolved
    /// them to. An unparseable candidate, or one carrying userinfo, is skipped: neither could
    /// ever equal a validated URL, and a credential is never stored.
    #[must_use]
    pub fn declared_in<S: AsRef<str>>(
        _file: &OperatorPath,
        candidates: impl IntoIterator<Item = S>,
    ) -> Self {
        // `_file` is a provenance token only: holding one says `candidates` come from a file
        // located through the operator's own environment.
        Self {
            urls: Self::canonical_urls(candidates),
        }
    }

    /// The URLs a process environment variable declares: `urls` extracts the candidate URL texts
    /// from `value` (one for a Cargo index, a chain for `GOPROXY`).
    ///
    /// `value` is an [`OperatorEnvValue`], so the URLs provably came from the environment, which a
    /// repository cannot set. `urls` is a non-capturing function pointer, so it cannot close over
    /// workspace data, but it can still return literals: callers must derive the candidates
    /// from the value (both callers, Cargo and Go, do). Unparseable candidates and candidates
    /// carrying userinfo are skipped, as in [`Self::declared_in`].
    ///
    /// # Examples
    ///
    /// ```
    /// use deps_core::config_trust::{OperatorEnvName, OperatorEnvValue};
    /// use deps_core::net_policy::{MapEnv, OperatorRegistries};
    ///
    /// let env = MapEnv::new().with_var("REGISTRY_INDEX", "https://index.corp.example");
    /// let name = OperatorEnvName::fixed("REGISTRY_INDEX");
    /// let value = OperatorEnvValue::read(&env, &name).unwrap();
    /// let set = OperatorRegistries::declared_by_env(&value, |raw| vec![raw]);
    /// assert!(set.contains_str("https://index.corp.example/"));
    /// ```
    #[must_use]
    pub fn declared_by_env(
        value: &OperatorEnvValue,
        urls: for<'a> fn(&'a str) -> Vec<&'a str>,
    ) -> Self {
        Self {
            urls: Self::canonical_urls(urls(value.as_str())),
        }
    }

    /// Adds every URL `other` declares to this set.
    pub fn extend(&mut self, other: Self) {
        self.urls.extend(other.urls);
    }

    fn canonical_urls<S: AsRef<str>>(
        candidates: impl IntoIterator<Item = S>,
    ) -> HashSet<CanonicalUrl> {
        candidates
            .into_iter()
            .filter_map(|candidate| url::Url::parse(candidate.as_ref()).ok())
            .filter(|url| url.username().is_empty() && url.password().is_none())
            .map(|url| CanonicalUrl::of(&url))
            .collect()
    }

    /// Whether the set declares no URL.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.urls.is_empty()
    }

    /// Whether `candidate` (any spelling that parses to the same URL) is declared.
    #[must_use]
    pub fn contains_str(&self, candidate: &str) -> bool {
        url::Url::parse(candidate).is_ok_and(|url| self.contains(&url))
    }

    fn contains(&self, url: &url::Url) -> bool {
        self.urls.contains(&CanonicalUrl::of(url))
    }

    /// Validates `candidate` for kind `K` and classifies its trust.
    ///
    /// A URL in the set skips the workspace policy (the operator's own configuration) but never
    /// the never-a-registry floor; any other URL is validated under `policy`. `raw_for_log` is
    /// the pre-expansion text errors and logs name, always redacted.
    ///
    /// # Errors
    ///
    /// Returns `K::Error` when `candidate` is not a valid https registry URL for `K`, when a
    /// workspace-declared host is blocked by `policy`, or when a Trusted host is of a class that
    /// is never a registry.
    ///
    /// # Examples
    ///
    /// ```
    /// use deps_core::config_trust::OperatorPath;
    /// use deps_core::net_policy::{
    ///     OperatorRegistries, RegistryAccessPolicy, RegistryTrust, WorkspaceRegistryAccess,
    /// };
    /// # use deps_core::EcosystemId;
    /// # use deps_core::net_policy::{IndexUrlError, RegistryUrlKind};
    /// # pub enum Kind {}
    /// # impl deps_core::net_policy::private::Sealed for Kind {}
    /// # impl RegistryUrlKind for Kind {
    /// #     const ECOSYSTEM: EcosystemId = EcosystemId::Npm;
    /// #     const REJECT_QUERY_FRAGMENT: bool = false;
    /// #     type Error = IndexUrlError;
    /// # }
    ///
    /// let file = OperatorPath::for_test("/home/me/.npmrc");
    /// let operator = OperatorRegistries::declared_in(&file, ["https://npm.corp.example"]);
    /// let off = RegistryAccessPolicy::new(WorkspaceRegistryAccess::Off);
    ///
    /// let trusted = operator
    ///     .classify::<Kind>("https://npm.corp.example/", "https://npm.corp.example/", &off)
    ///     .unwrap();
    /// assert_eq!(trusted.trust(), RegistryTrust::Trusted);
    ///
    /// // The policy still gates every other URL.
    /// assert!(operator
    ///     .classify::<Kind>("https://elsewhere.example", "https://elsewhere.example", &off)
    ///     .is_err());
    /// ```
    pub fn classify<K: RegistryUrlKind>(
        &self,
        candidate: &str,
        raw_for_log: &str,
        policy: &RegistryAccessPolicy,
    ) -> Result<DeclaredRegistry<K>, K::Error> {
        let parsed = validate_index_url(candidate, raw_for_log, K::ECOSYSTEM, PolicyGate::Skip)?;
        let shape = ValidatedRegistryUrl::<K>::from_validated(&parsed, raw_for_log)?;
        if self.contains(&parsed) {
            if let Some(block) = floor_block(&parsed, policy.get()) {
                tracing::warn!(
                    url = %RedactedUrl::new(raw_for_log),
                    class = ?block.class(),
                    ecosystem = %K::ECOSYSTEM,
                    "operator-declared registry host is never a registry; rejected"
                );
                return Err(IndexUrlError::BlockedHost { block }.into());
            }
            return Ok(DeclaredRegistry {
                url: shape,
                trust: RegistryTrust::Trusted,
            });
        }
        enforce_policy(&parsed, raw_for_log, K::ECOSYSTEM, policy)?;
        Ok(DeclaredRegistry {
            url: shape,
            trust: RegistryTrust::WorkspaceDeclared,
        })
    }
}

/// A validated registry URL together with the trust the URL-alone rule gave it.
///
/// Fields are private and the constructors are [`OperatorRegistries::classify`] and, for a
/// compile-time constant, [`Self::trusted_constant`], so trust is not assigned by a caller.
/// Misuse-resistant rather than sealed: the operator set a caller classifies against is the
/// caller's claim about which file it came from.
pub struct DeclaredRegistry<K> {
    url: ValidatedRegistryUrl<K>,
    trust: RegistryTrust,
}

impl<K> DeclaredRegistry<K> {
    /// The validated, normalized URL.
    #[must_use]
    pub const fn url(&self) -> &ValidatedRegistryUrl<K> {
        &self.url
    }

    /// The trust the rule assigned.
    #[must_use]
    pub const fn trust(&self) -> RegistryTrust {
        self.trust
    }

    /// The normalized URL text; never carries a trailing `/`.
    #[must_use]
    pub fn as_str(&self) -> &str
    where
        K: RegistryUrlKind,
    {
        self.url.as_str()
    }

    /// The egress tier requests to this URL use.
    #[must_use]
    pub const fn egress(&self) -> EgressTier {
        self.trust.egress()
    }
}

impl<K: TrustedConstantRegistryUrl> DeclaredRegistry<K> {
    /// A compile-time constant registry URL (the public registry), which is Trusted by
    /// construction: it is not workspace-declared input and needs no operator file to vouch for it.
    ///
    /// # Errors
    ///
    /// Returns `K::Error` when `raw` fails a non-policy check (https, userinfo, query/fragment).
    pub fn trusted_constant(raw: &'static str) -> Result<Self, K::Error> {
        Ok(Self {
            url: ValidatedRegistryUrl::<K>::new_trusted_constant(raw)?,
            trust: RegistryTrust::Trusted,
        })
    }
}

impl<K> Clone for DeclaredRegistry<K> {
    fn clone(&self) -> Self {
        Self {
            url: self.url.clone(),
            trust: self.trust,
        }
    }
}

impl<K: RegistryUrlKind> std::fmt::Debug for DeclaredRegistry<K> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DeclaredRegistry")
            .field("url", &self.url)
            .field("trust", &self.trust)
            .finish()
    }
}

impl<K> PartialEq for DeclaredRegistry<K> {
    fn eq(&self, other: &Self) -> bool {
        self.url == other.url && self.trust == other.trust
    }
}

impl<K> Eq for DeclaredRegistry<K> {}

impl<K> Hash for DeclaredRegistry<K> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.url.hash(state);
        self.trust.hash(state);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::assert_matches;

    use crate::EcosystemId;
    use crate::net_policy::{BlockingPolicy, HostClass, WorkspaceRegistryAccess};

    #[test]
    fn declared_by_env_collects_chain_and_skips_userinfo() {
        use crate::net_policy::MapEnv;
        let env = MapEnv::new().with_var(
            "CHAIN",
            "https://a.example,https://u:p@b.example,not a url,https://c.example/",
        );
        let name = crate::config_trust::OperatorEnvName::fixed("CHAIN");
        let value = OperatorEnvValue::read(&env, &name).unwrap();
        let set = OperatorRegistries::declared_by_env(&value, |raw| raw.split(',').collect());
        assert!(set.contains_str("https://a.example"));
        assert!(set.contains_str("https://c.example"));
        assert!(!set.contains_str("https://b.example"));
    }

    enum TestKind {}
    impl crate::net_policy::private::Sealed for TestKind {}
    impl RegistryUrlKind for TestKind {
        const ECOSYSTEM: EcosystemId = EcosystemId::Npm;
        const REJECT_QUERY_FRAGMENT: bool = false;
        type Error = IndexUrlError;
    }

    fn file() -> OperatorPath {
        OperatorPath::for_test("/home/me/.npmrc")
    }

    fn policy(level: WorkspaceRegistryAccess) -> RegistryAccessPolicy {
        RegistryAccessPolicy::new(level)
    }

    fn classify(
        set: &OperatorRegistries,
        url: &str,
        level: WorkspaceRegistryAccess,
    ) -> Result<DeclaredRegistry<TestKind>, IndexUrlError> {
        set.classify::<TestKind>(url, url, &policy(level))
    }

    #[test]
    fn egress_follows_trust() {
        assert_eq!(RegistryTrust::Trusted.egress(), EgressTier::Baseline);
        assert_eq!(
            RegistryTrust::WorkspaceDeclared.egress(),
            EgressTier::Guarded
        );
    }

    #[test]
    fn operator_url_is_trusted_whatever_spelling_and_policy() {
        let set = OperatorRegistries::declared_in(&file(), ["https://Npm.Corp.example:443/"]);
        for spelling in [
            "https://npm.corp.example",
            "https://npm.corp.example/",
            "https://NPM.corp.example:443",
        ] {
            let declared = classify(&set, spelling, WorkspaceRegistryAccess::Off).unwrap();
            assert_eq!(declared.trust(), RegistryTrust::Trusted, "{spelling}");
            assert_eq!(declared.egress(), EgressTier::Baseline);
        }
    }

    #[test]
    fn workspace_only_url_is_workspace_declared_and_policy_gated() {
        let set = OperatorRegistries::declared_in(&file(), ["https://npm.corp.example"]);

        let declared = classify(
            &set,
            "https://repo.example",
            WorkspaceRegistryAccess::PublicOnly,
        )
        .unwrap();
        assert_eq!(declared.trust(), RegistryTrust::WorkspaceDeclared);

        let blocked = classify(&set, "https://repo.example", WorkspaceRegistryAccess::Off);
        assert_matches!(blocked, Err(IndexUrlError::BlockedHost { .. }));
    }

    #[test]
    fn same_host_different_path_is_not_trusted() {
        let set = OperatorRegistries::declared_in(&file(), ["https://npm.corp.example/team-a"]);

        let other = classify(
            &set,
            "https://npm.corp.example/team-b",
            WorkspaceRegistryAccess::PublicOnly,
        )
        .unwrap();
        assert_eq!(other.trust(), RegistryTrust::WorkspaceDeclared);

        let off = classify(
            &set,
            "https://npm.corp.example/team-b",
            WorkspaceRegistryAccess::Off,
        );
        assert_matches!(off, Err(IndexUrlError::BlockedHost { .. }));
    }

    #[test]
    fn trusted_never_a_registry_host_is_rejected_at_the_floor() {
        let set = OperatorRegistries::declared_in(&file(), ["https://169.254.169.254/"]);
        let error = classify(
            &set,
            "https://169.254.169.254",
            WorkspaceRegistryAccess::All,
        )
        .unwrap_err();
        let IndexUrlError::BlockedHost { block } = error else {
            panic!("expected a blocked host, got {error:?}");
        };
        assert_eq!(block.class(), HostClass::CloudMetadata);
        assert_eq!(block.policy(), BlockingPolicy::Floor);
    }

    #[test]
    fn userinfo_and_http_are_rejected_even_for_an_operator_url() {
        let set = OperatorRegistries::declared_in(
            &file(),
            [
                "https://user:pw@npm.corp.example",
                "http://npm.corp.example",
            ],
        );
        assert!(
            classify(
                &set,
                "https://user:pw@npm.corp.example",
                WorkspaceRegistryAccess::All
            )
            .is_err()
        );
        assert!(
            classify(
                &set,
                "http://npm.corp.example",
                WorkspaceRegistryAccess::All
            )
            .is_err()
        );
    }

    #[test]
    fn empty_set_trusts_nothing() {
        let set = OperatorRegistries::default();
        assert!(set.is_empty());
        let declared = classify(
            &set,
            "https://npm.corp.example",
            WorkspaceRegistryAccess::All,
        )
        .unwrap();
        assert_eq!(declared.trust(), RegistryTrust::WorkspaceDeclared);
    }

    #[test]
    fn debug_never_renders_a_url_or_credential() {
        let set = OperatorRegistries::declared_in(
            &file(),
            [
                "https://registry.corp.example/npm?_authToken=tok-secret",
                "https://user:s3cr3t@registry.corp.example/",
            ],
        );
        let rendered = format!("{set:?}");
        for leak in ["tok-secret", "s3cr3t", "registry.corp.example"] {
            assert!(!rendered.contains(leak), "{rendered}");
        }
        assert!(!set.contains_str("https://user:s3cr3t@registry.corp.example/"));
    }

    #[test]
    fn unparseable_candidates_are_skipped() {
        let set = OperatorRegistries::declared_in(&file(), ["not a url", "https://a.example"]);
        assert!(set.contains_str("https://a.example"));
        assert!(!set.is_empty());
    }
}