Skip to content

Commit 64fce24

Browse files
committed
Merge remote-tracking branch 'origin/fix/cargo-members-liveness-origin' into fix/cargo-mixed-member-pins
2 parents 7837fb4 + d92dab4 commit 64fce24

3 files changed

Lines changed: 95 additions & 39 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: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,12 @@ pub(crate) async fn extract(ctx: &DiscoverCtx<'_>, out: &mut Discovery) {
173173
// hosted rewriter pins a crate in (each that declares it).
174174
let mut manifests: Vec<(String, DocumentMut)> = Vec::new();
175175
if let Some(doc) = root {
176-
let root_pins = manifest_has_socket_pin(&doc);
176+
// Any Socket pin the root or the config carries, a `[patch]`
177+
// entry included: members may hold the declarations it governs.
178+
let root_pins = doc.to_string().contains(PIN_NAME_PREFIX)
179+
|| config
180+
.as_ref()
181+
.is_some_and(|c| c.1.to_string().contains(PIN_NAME_PREFIX));
177182
manifests.push((CARGO_TOML.to_string(), doc));
178183
manifests.extend(read_members(ctx, &lock, root_pins, out).await);
179184
}
@@ -354,30 +359,26 @@ async fn read_toml(ctx: &DiscoverCtx<'_>, file: &str, out: &mut Discovery) -> Op
354359
/// not a declaration — or a build `target/`). Unparseable ones are skipped
355360
/// (diagnosed).
356361
///
357-
/// Members only ever decide about Socket wiring, so the walk is skipped
358-
/// when there is none they could decide about: a parsed lock with no
359-
/// Socket-sourced package, or an unparseable one (no ref either way). With
360-
/// no lock, a member matters only when some manifest carries a
361-
/// `socket-patch-` pin (a lockless pin, [`UnlockedPin`]); the walk has the
362-
/// texts, so the parse is skipped otherwise.
362+
/// Members only ever decide about Socket wiring, so they are parsed only
363+
/// when there is some they could decide about: a lock with a
364+
/// Socket-sourced package, or a `socket-patch-` name in a member's own
365+
/// text, the root's or the config's (a pin to diagnose or recognize, a
366+
/// `[patch]` entry members declare, a lockless pin, [`UnlockedPin`]),
367+
/// whatever the lock says (stale, unparseable or absent).
363368
async fn read_members(
364369
ctx: &DiscoverCtx<'_>,
365370
lock: &Lock,
366371
root_pins: bool,
367372
out: &mut Discovery,
368373
) -> Vec<(String, DocumentMut)> {
369-
let lockless = match lock {
370-
Lock::Parsed(_) if has_socket_sourced(ctx, lock) => false,
371-
Lock::Parsed(_) | Lock::Unparseable => return Vec::new(),
372-
Lock::Absent => true,
373-
};
374+
let socket_sourced = 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| {
@@ -2536,6 +2527,24 @@ mod tests {
25362527
let p = Project::new();
25372528
workspace_with_member(&p, &pinned("serde", "1", UUID_A));
25382529
assert!(member_read(&p.discover().await));
2530+
// Review on #2012 (round 2): a stale crates.io lock beside a member
2531+
// pin still reads the member, so the pin is diagnosed (a stale lock
2532+
// or a server outside the allowlist) and its uuid recognized.
2533+
let p = Project::new();
2534+
workspace_with_member(&p, &pinned("serde", "1", UUID_A));
2535+
p.write(
2536+
"Cargo.lock",
2537+
lock(&[("serde", "1.0.1", Some(CRATES_IO), Some(CKSUM))]),
2538+
);
2539+
let out = p.discover().await;
2540+
assert!(member_read(&out));
2541+
assert!(
2542+
out.diagnostics
2543+
.iter()
2544+
.any(|d| d.code == DIAG_REF_UNATTRIBUTABLE),
2545+
"{:?}",
2546+
out.diagnostics
2547+
);
25392548
}
25402549

25412550
/// 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
@@ -1674,7 +1674,6 @@ impl<'a> DiscoverCtx<'a> {
16741674
}
16751675
}
16761676

1677-
/// Log a guarded read of `rel` ([`Discovery::read`]).
16781677
/// Account for `text`, which a walk through [`Self::view`] read from
16791678
/// `rel` on this ctx's behalf, exactly as [`Self::read_text`] accounts
16801679
/// for its own reads (rule 11 recognition and the read log).
@@ -1683,6 +1682,7 @@ impl<'a> DiscoverCtx<'a> {
16831682
self.recognize_text(rel, text);
16841683
}
16851684

1685+
/// Log a guarded read of `rel` ([`Discovery::read`]).
16861686
fn log_read(&self, rel: &str, readable: bool) {
16871687
self.read
16881688
.lock()
@@ -2941,9 +2941,21 @@ impl RecordLiveness {
29412941
/// ([`crate::patch::redirect::hosted_patch_uuid`]'s grammar — no userinfo,
29422942
/// the last canonical-uuid path segment — with the url's origin taken as
29432943
/// configured): what a lock entry on an origin this run was not given
2944-
/// claims to resolve from.
2944+
/// claims to resolve from. Only a url in a Socket patch server's path
2945+
/// shape (a `patch`, `patch-registry` or `gopatch` segment) counts: any
2946+
/// other url merely ending in the uuid (an artifact store's blob path)
2947+
/// resolves from elsewhere.
29452948
fn foreign_hosted_uuid(url: &str) -> Option<String> {
2946-
let parsed = reqwest::Url::parse(url.trim()).ok()?;
2949+
// Normalized the way `hosted_patch_uuid` reads it, so an escaped,
2950+
// percent-encoded or `sparse+` url still yields its origin.
2951+
let normalized = crate::patch::redirect::normalized_hosted_url(url)?;
2952+
let parsed = reqwest::Url::parse(&normalized).ok()?;
2953+
if !parsed
2954+
.path_segments()?
2955+
.any(|s| matches!(s, "patch" | "patch-registry" | "gopatch"))
2956+
{
2957+
return None;
2958+
}
29472959
let origin = parsed.origin().ascii_serialization();
29482960
crate::patch::redirect::hosted_patch_uuid(url, &[origin])
29492961
}
@@ -3628,6 +3640,31 @@ mod tests {
36283640
use super::testing::*;
36293641
use super::*;
36303642

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

0 commit comments

Comments
 (0)