Skip to content

Commit 1021784

Browse files
committed
Merge remote-tracking branch 'origin/fix/cargo-mixed-member-pins' into fix/cargo-vendored-twins-and-split-patches
# Conflicts: # crates/socket-patch-core/src/vex/discover/cargo.rs
2 parents 6592bf2 + ebe84e1 commit 1021784

3 files changed

Lines changed: 115 additions & 43 deletions

File tree

‎crates/socket-patch-core/src/patch/redirect/mod.rs‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7642,14 +7642,11 @@ pub fn hosted_patch_uuid(url: &str, extra_origins: &[String]) -> Option<String>
76427642
hosted_patch_url_uuids(url, extra_origins)?.pop()
76437643
}
76447644

7645-
/// EVERY canonical-uuid path segment of a Socket-HOSTED url, in path order
7646-
/// (the grant token first when it is uuid-shaped, the patch uuid last), or
7647-
/// `None` when `url` is not on an accepted origin — [`hosted_patch_uuid`]'s
7648-
/// exact acceptance rules, without electing one segment. `vex` discovery
7649-
/// uses it to RECOGNIZE every Socket identity a lockfile mentions, including
7650-
/// the malformed or rejected shapes whose "last uuid" is not a patch.
7651-
pub fn hosted_patch_url_uuids(url: &str, extra_origins: &[String]) -> Option<Vec<String>> {
7652-
use crate::patch::path_safety::is_canonical_uuid;
7645+
/// `url` normalized the way [`hosted_patch_url_uuids`] reads it: `\/`
7646+
/// unescaped, a percent-encoded `http(s)://` url decoded, and a Cargo
7647+
/// `sparse+` / `registry+` source prefix stripped. `None` without a
7648+
/// `://`.
7649+
pub(crate) fn normalized_hosted_url(url: &str) -> Option<String> {
76537650
use crate::utils::purl::percent_decode_purl_component;
76547651

76557652
let unescaped = url.trim().replace("\\/", "/");
@@ -7661,13 +7658,26 @@ pub fn hosted_patch_url_uuids(url: &str, extra_origins: &[String]) -> Option<Vec
76617658
};
76627659
// `sparse+https://…` / `registry+https://…` (Cargo.lock `source`).
76637660
let (scheme, _) = decoded.split_once("://")?;
7664-
let text = match scheme.rsplit_once('+') {
7661+
Some(match scheme.rsplit_once('+') {
76657662
Some((kind, _)) if !kind.is_empty() && kind.bytes().all(|b| b.is_ascii_alphabetic()) => {
7666-
&decoded[kind.len() + 1..]
7663+
decoded[kind.len() + 1..].to_string()
76677664
}
7668-
_ => decoded.as_str(),
7669-
};
7670-
let parsed = reqwest::Url::parse(text).ok()?;
7665+
_ => decoded,
7666+
})
7667+
}
7668+
7669+
/// EVERY canonical-uuid path segment of a Socket-HOSTED url, in path order
7670+
/// (the grant token first when it is uuid-shaped, the patch uuid last), or
7671+
/// `None` when `url` is not on an accepted origin — [`hosted_patch_uuid`]'s
7672+
/// exact acceptance rules, without electing one segment. `vex` discovery
7673+
/// uses it to RECOGNIZE every Socket identity a lockfile mentions, including
7674+
/// the malformed or rejected shapes whose "last uuid" is not a patch.
7675+
pub fn hosted_patch_url_uuids(url: &str, extra_origins: &[String]) -> Option<Vec<String>> {
7676+
use crate::patch::path_safety::is_canonical_uuid;
7677+
use crate::utils::purl::percent_decode_purl_component;
7678+
7679+
let text = normalized_hosted_url(url)?;
7680+
let parsed = reqwest::Url::parse(&text).ok()?;
76717681
if !matches!(parsed.scheme(), "https" | "http")
76727682
|| !parsed.username().is_empty()
76737683
|| parsed.password().is_some()

‎crates/socket-patch-core/src/vex/discover/cargo.rs‎

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,12 @@ pub(crate) async fn extract(ctx: &DiscoverCtx<'_>, out: &mut Discovery) {
197197
}
198198
let mut manifests: Vec<(String, DocumentMut)> = Vec::new();
199199
if let Some(doc) = root {
200-
let root_pins = manifest_has_socket_pin(&doc);
200+
// Any Socket pin the root or the config carries, a `[patch]`
201+
// entry included: members may hold the declarations it governs.
202+
let root_pins = doc.to_string().contains(PIN_NAME_PREFIX)
203+
|| config
204+
.as_ref()
205+
.is_some_and(|c| c.1.to_string().contains(PIN_NAME_PREFIX));
201206
// A vendored copy wired in the root or the config is weighed against
202207
// every member's declarations ([`patch_bypassed`]).
203208
let vendored = !wired_all.is_empty();
@@ -348,14 +353,14 @@ async fn read_toml(ctx: &DiscoverCtx<'_>, file: &str, out: &mut Discovery) -> Op
348353
/// not a declaration — or a build `target/`). Unparseable ones are skipped
349354
/// (diagnosed).
350355
///
351-
/// Members only ever decide about Socket wiring, so the walk is skipped
352-
/// when there is none they could decide about: a parsed lock with no
353-
/// Socket-sourced package, or an unparseable one (no ref either way). With
354-
/// no lock, a member matters only when some manifest carries a
355-
/// `socket-patch-` pin (a lockless pin, [`UnlockedPin`]); the walk has the
356-
/// texts, so the parse is skipped otherwise. `vendored` (the root manifest
357-
/// or the config wires a Socket copy through `[patch]`, whose lock entries
358-
/// are sourceless) always reads them, whatever the lock: a member's
356+
/// Members only ever decide about Socket wiring, so they are parsed only
357+
/// when there is some they could decide about: a lock with a
358+
/// Socket-sourced package, or a `socket-patch-` name in a member's own
359+
/// text, the root's or the config's (a pin to diagnose or recognize, a
360+
/// `[patch]` entry members declare, a lockless pin, [`UnlockedPin`]),
361+
/// whatever the lock says (stale, unparseable or absent). `vendored` (the
362+
/// root manifest or the config wires a Socket copy through `[patch]`,
363+
/// whose lock entries are sourceless) always reads them: a member's
359364
/// declaration from a source the `[patch]` does not redirect bypasses the
360365
/// copy ([`patch_bypassed`]).
361366
async fn read_members(
@@ -365,19 +370,15 @@ async fn read_members(
365370
vendored: bool,
366371
out: &mut Discovery,
367372
) -> Vec<(String, DocumentMut)> {
368-
let lockless = match lock {
369-
_ if vendored => false,
370-
Lock::Parsed(_) if has_socket_sourced(ctx, lock) => false,
371-
Lock::Parsed(_) | Lock::Unparseable => return Vec::new(),
372-
Lock::Absent => true,
373-
};
373+
let socket_sourced =
374+
vendored || (matches!(lock, Lock::Parsed(_)) && has_socket_sourced(ctx, lock));
374375
let texts: Vec<(String, String)> =
375376
crate::utils::cargo_workspace::member_manifest_texts_in(&ctx.view)
376377
.await
377378
.into_iter()
378379
.filter(|(rel, _)| crate::patch::redirect::is_cargo_member_manifest_key(rel))
379380
.collect();
380-
if lockless && !root_pins && !texts.iter().any(|(_, t)| t.contains(PIN_NAME_PREFIX)) {
381+
if !socket_sourced && !root_pins && !texts.iter().any(|(_, t)| t.contains(PIN_NAME_PREFIX)) {
381382
return Vec::new();
382383
}
383384
let mut members = Vec::new();
@@ -390,16 +391,6 @@ async fn read_members(
390391
members
391392
}
392393

393-
/// Whether some dependency declaration of `doc` names a `socket-patch-`
394-
/// registry.
395-
fn manifest_has_socket_pin(doc: &DocumentMut) -> bool {
396-
dependency_entries(doc).iter().any(|d| {
397-
d.registry
398-
.as_deref()
399-
.is_some_and(|r| r.trim().starts_with(PIN_NAME_PREFIX))
400-
})
401-
}
402-
403394
/// Whether the lock resolves some package from a Socket patch server.
404395
fn has_socket_sourced(ctx: &DiscoverCtx<'_>, lock: &Lock) -> bool {
405396
lock.packages().iter().any(|p| {
@@ -1050,7 +1041,10 @@ fn beside_the_pin(
10501041
let refused: Vec<String> = unpinned
10511042
.iter()
10521043
.filter_map(|(d, selects)| {
1053-
let why = match (&d.source, d.registry.as_deref(), selects) {
1044+
// `registry = "crates-io"` is crates.io itself, which the
1045+
// rewriter re-pins like a declaration with no `registry` (#386).
1046+
let registry = d.registry.as_deref().filter(|r| r.trim() != "crates-io");
1047+
let why = match (&d.source, registry, selects) {
10541048
(DeclSource::Registry, None, CargoReqMatch::Ours) => return None,
10551049
(DeclSource::Registry, None, _) => {
10561050
let also: Vec<&str> = others
@@ -2609,6 +2603,19 @@ mod tests {
26092603
"{}",
26102604
u.detail
26112605
);
2606+
// Review on #2019 (round 2): `registry = "crates-io"` is crates.io,
2607+
// which a re-scan re-pins (#386).
2608+
let p = mixed_workspace(
2609+
&pinned("serde", "=1.0.1", UUID_A),
2610+
"serde = { version = \"1\", registry = \"crates-io\" }",
2611+
&[],
2612+
);
2613+
let u = run(&p).await.unattested.remove(0);
2614+
assert!(
2615+
u.detail.contains("re-run `socket-patch scan`"),
2616+
"{}",
2617+
u.detail
2618+
);
26122619

26132620
// `"1"` also matches the crates.io 1.0.200 locked beside it.
26142621
let p = mixed_workspace(
@@ -3058,6 +3065,24 @@ mod tests {
30583065
let p = Project::new();
30593066
workspace_with_member(&p, &pinned("serde", "1", UUID_A));
30603067
assert!(member_read(&p.discover().await));
3068+
// Review on #2012 (round 2): a stale crates.io lock beside a member
3069+
// pin still reads the member, so the pin is diagnosed (a stale lock
3070+
// or a server outside the allowlist) and its uuid recognized.
3071+
let p = Project::new();
3072+
workspace_with_member(&p, &pinned("serde", "1", UUID_A));
3073+
p.write(
3074+
"Cargo.lock",
3075+
lock(&[("serde", "1.0.1", Some(CRATES_IO), Some(CKSUM))]),
3076+
);
3077+
let out = p.discover().await;
3078+
assert!(member_read(&out));
3079+
assert!(
3080+
out.diagnostics
3081+
.iter()
3082+
.any(|d| d.code == DIAG_REF_UNATTRIBUTABLE),
3083+
"{:?}",
3084+
out.diagnostics
3085+
);
30613086
}
30623087

30633088
/// A rotated grant token in the definition is the same patch.

‎crates/socket-patch-core/src/vex/discover/mod.rs‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,6 @@ impl<'a> DiscoverCtx<'a> {
16811681
}
16821682
}
16831683

1684-
/// Log a guarded read of `rel` ([`Discovery::read`]).
16851684
/// Account for `text`, which a walk through [`Self::view`] read from
16861685
/// `rel` on this ctx's behalf, exactly as [`Self::read_text`] accounts
16871686
/// for its own reads (rule 11 recognition and the read log).
@@ -1690,6 +1689,7 @@ impl<'a> DiscoverCtx<'a> {
16901689
self.recognize_text(rel, text);
16911690
}
16921691

1692+
/// Log a guarded read of `rel` ([`Discovery::read`]).
16931693
fn log_read(&self, rel: &str, readable: bool) {
16941694
self.read
16951695
.lock()
@@ -2948,9 +2948,21 @@ impl RecordLiveness {
29482948
/// ([`crate::patch::redirect::hosted_patch_uuid`]'s grammar — no userinfo,
29492949
/// the last canonical-uuid path segment — with the url's origin taken as
29502950
/// configured): what a lock entry on an origin this run was not given
2951-
/// claims to resolve from.
2951+
/// claims to resolve from. Only a url in a Socket patch server's path
2952+
/// shape (a `patch`, `patch-registry` or `gopatch` segment) counts: any
2953+
/// other url merely ending in the uuid (an artifact store's blob path)
2954+
/// resolves from elsewhere.
29522955
fn foreign_hosted_uuid(url: &str) -> Option<String> {
2953-
let parsed = reqwest::Url::parse(url.trim()).ok()?;
2956+
// Normalized the way `hosted_patch_uuid` reads it, so an escaped,
2957+
// percent-encoded or `sparse+` url still yields its origin.
2958+
let normalized = crate::patch::redirect::normalized_hosted_url(url)?;
2959+
let parsed = reqwest::Url::parse(&normalized).ok()?;
2960+
if !parsed
2961+
.path_segments()?
2962+
.any(|s| matches!(s, "patch" | "patch-registry" | "gopatch"))
2963+
{
2964+
return None;
2965+
}
29542966
let origin = parsed.origin().ascii_serialization();
29552967
crate::patch::redirect::hosted_patch_uuid(url, &[origin])
29562968
}
@@ -3635,6 +3647,31 @@ mod tests {
36353647
use super::testing::*;
36363648
use super::*;
36373649

3650+
/// A foreign hosted url reads its origin after the same normalization
3651+
/// `hosted_patch_uuid` applies: `\\/` escapes, a percent-encoded url
3652+
/// and a Cargo `sparse+` / `registry+` prefix.
3653+
#[test]
3654+
fn foreign_hosted_uuid_normalizes_like_hosted_patch_uuid() {
3655+
const U: &str = "11111111-2222-4333-8444-555555555555";
3656+
for url in [
3657+
format!("https://mirror.example/patch/{U}/x.tgz"),
3658+
format!("https:\\/\\/mirror.example\\/patch\\/{U}\\/x.tgz"),
3659+
format!("https%3A%2F%2Fmirror.example%2Fpatch%2F{U}%2Fx.tgz"),
3660+
format!("sparse+https://mirror.example/patch/{U}/"),
3661+
format!("registry+https://mirror.example/patch/{U}/"),
3662+
] {
3663+
assert_eq!(foreign_hosted_uuid(&url).as_deref(), Some(U), "{url}");
3664+
}
3665+
// Review on #2012 (round 2): a url merely ending in the uuid is not
3666+
// a patch server's.
3667+
for url in [
3668+
format!("https://artifacts.corp/blobs/{U}/x-1.0.0.tgz"),
3669+
format!("https://evil.example/{U}/x.tgz"),
3670+
] {
3671+
assert_eq!(foreign_hosted_uuid(&url), None, "{url}");
3672+
}
3673+
}
3674+
36383675
/// The read-only parse [`toml_or_diag`] hands extractors reads every
36393676
/// value the way the `DocumentMut` it replaced did (#1473): escaped,
36403677
/// literal and multi-line strings decode, quoted and dotted keys

0 commit comments

Comments
 (0)