Skip to content

Commit 1fec27a

Browse files
mikolalysenkoclaude
andcommitted
Merge lock-model/2a-pnpm-readers into lock-model/2b-pnpm-planner
Brings in #2002's review fixes and #2001's later counter fixes through 2a. Restores crates/socket-patch-core/src/origin.rs to the lock-readers group (lost in the earlier 1-origin merge). Budget at this branch's counts: lock-readers 30766, lock-writers 88114, both below 2a's 30767 and 88315. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 parents cf29118 + 097a538 commit 1fec27a

15 files changed

Lines changed: 179 additions & 83 deletions

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,19 @@ pub use registry::registry;
5151
mod architecture_tests {
5252
use std::path::Path;
5353

54-
const IMPURE: [&str; 9] = [
54+
const IMPURE: [&str; 12] = [
5555
"tokio::fs",
5656
"std::fs",
5757
"read_regular_",
5858
"File::open",
5959
"OpenOptions",
60+
// The host policy (`crate::origin`). Its pure path grammar
61+
// (`origin::vendor_ref`) is allowed: it applies no host policy.
6062
"hosted_patch_uuid",
6163
"hosted_patch_url_uuids",
64+
"hosted_uuid(",
65+
"OriginPolicy",
66+
"SOCKET_PATCH_SERVER_HOST",
6267
"async fn",
6368
".await",
6469
];

‎crates/socket-patch-core/src/origin.rs‎

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
//! Socket origin: whether a string a lockfile or package-manager config
22
//! records routes a package to a Socket patch, and to which one.
33
//!
4-
//! This is the one classifier every reader applies:
4+
//! The goal is one classifier that every reader applies. Today discovery and
5+
//! the redirect's hosted unwind apply it, but some readers still classify
6+
//! on their own: the lock inventory's PyPI view
7+
//! (`vendor::lock_inventory::pypi::hosted_pypi_reference`) adds its own
8+
//! url-tail grammar, discovery's PyPI diagnostics (`vex::discover::pypi_other`)
9+
//! compare hosts directly, the upstream restore keeps its own known-origin
10+
//! list, and the inventory matches hosted urls on any host. The lock-model
11+
//! stack moves them here (`docs/design/lock-model-unification.md`).
512
//!
613
//! * **hosted** — [`hosted_patch_uuid`]: an `http(s)` url on Socket's patch
714
//! server or on an operator-configured origin, the patch uuid its last
8-
//! canonical-uuid path segment. [`OriginPolicy::hosted_uuid`] answers the
15+
//! canonical-uuid path segment. `OriginPolicy::hosted_uuid` answers the
916
//! same question with a memoized fast path for the plain registry urls
1017
//! every lock is full of;
1118
//! * **vendored** — [`vendor_ref`] / [`vendor_ref_decorated`] /
1219
//! [`vendor_uuid_dir`]: a path that STARTS at this project's
1320
//! `.socket/vendor/<eco>/<uuid>/`, its leaf traversal-safe;
14-
//! * **names** — [`socket_patch_name_uuid`]: the `socket-patch-<uuid>`
21+
//! * **names** — `socket_patch_name_uuid`: the `socket-patch-<uuid>`
1522
//! registry, repository and source names.
1623
//!
17-
//! [`OriginPolicy::locate`] computes the hosted and vendored answers for one
24+
//! `OriginPolicy::locate` computes the hosted and vendored answers for one
1825
//! string at once.
1926
//!
2027
//! The format models ([`crate::formats`]) stay pure: they hand out the raw
@@ -74,7 +81,6 @@ pub fn hosted_patch_uuid(url: &str, extra_origins: &[String]) -> Option<String>
7481
/// uses it to RECOGNIZE every Socket identity a lockfile mentions, including
7582
/// the malformed or rejected shapes whose "last uuid" is not a patch.
7683
pub fn hosted_patch_url_uuids(url: &str, extra_origins: &[String]) -> Option<Vec<String>> {
77-
use crate::patch::path_safety::is_canonical_uuid;
7884
use crate::utils::purl::percent_decode_purl_component;
7985

8086
let unescaped = url.trim().replace("\\/", "/");
@@ -174,15 +180,19 @@ fn plain_url_domain(url: &str) -> Option<String> {
174180
#[derive(Debug)]
175181
pub(crate) struct OriginPolicy<'a> {
176182
origins: &'a [String],
177-
/// The hosts a Socket-hosted url can name (see [`Self::hosted_uuid`]).
183+
/// The hosts a Socket-hosted url can name, lowercased (see
184+
/// [`Self::hosts`]).
178185
hosts: OnceLock<BTreeSet<String>>,
186+
/// [`Self::hosts`] as an ASCII-case-insensitive matcher.
187+
host_finder: OnceLock<aho_corasick::AhoCorasick>,
179188
}
180189

181190
impl<'a> OriginPolicy<'a> {
182191
pub(crate) fn new(origins: &'a [String]) -> Self {
183192
OriginPolicy {
184193
origins,
185194
hosts: OnceLock::new(),
195+
host_finder: OnceLock::new(),
186196
}
187197
}
188198

@@ -191,6 +201,33 @@ impl<'a> OriginPolicy<'a> {
191201
self.origins
192202
}
193203

204+
/// The hosts a Socket-hosted url can name: Socket's patch server and
205+
/// each configured origin's host, lowercased. Built once per policy.
206+
fn hosts(&self) -> &BTreeSet<String> {
207+
self.hosts.get_or_init(|| {
208+
let mut hosts = BTreeSet::from([SOCKET_PATCH_SERVER_HOST.to_string()]);
209+
hosts.extend(self.origins.iter().filter_map(|o| {
210+
reqwest::Url::parse(o.trim())
211+
.ok()?
212+
.host_str()
213+
.map(str::to_ascii_lowercase)
214+
}));
215+
hosts
216+
})
217+
}
218+
219+
/// A matcher for any of [`Self::hosts`] in any ASCII case: the cheap
220+
/// pre-filter of discovery's identity sweep (only a token naming an
221+
/// accepted host can be ours). Built once per policy.
222+
pub(crate) fn host_finder(&self) -> &aho_corasick::AhoCorasick {
223+
self.host_finder.get_or_init(|| {
224+
aho_corasick::AhoCorasick::builder()
225+
.ascii_case_insensitive(true)
226+
.build(self.hosts())
227+
.expect("literal host patterns")
228+
})
229+
}
230+
194231
/// The patch uuid of a Socket-HOSTED url, or `None` for anything else
195232
/// (see [`hosted_patch_uuid`] for the accepted
196233
/// spellings and the host allowlist).
@@ -200,17 +237,7 @@ impl<'a> OriginPolicy<'a> {
200237
/// here.
201238
pub(crate) fn hosted_uuid(&self, url: &str) -> Option<String> {
202239
if let Some(host) = plain_url_domain(url) {
203-
let hosts = self.hosts.get_or_init(|| {
204-
let mut hosts = BTreeSet::from([SOCKET_PATCH_SERVER_HOST.to_string()]);
205-
hosts.extend(self.origins.iter().filter_map(|o| {
206-
reqwest::Url::parse(o.trim())
207-
.ok()?
208-
.host_str()
209-
.map(str::to_string)
210-
}));
211-
hosts
212-
});
213-
if !hosts.contains(&host) {
240+
if !self.hosts().contains(&host) {
214241
return None;
215242
}
216243
}
@@ -241,7 +268,7 @@ impl<'a> OriginPolicy<'a> {
241268
/// maven `<repository><id>`, nuget `<add key>`) or, with
242269
/// `vendored = true`, `socket-patch-vendor-<uuid>` (maven's vendored repo
243270
/// id). Exact grammar only — a suffix or a non-canonical uuid is not ours.
244-
pub fn socket_patch_name_uuid(name: &str, vendored: bool) -> Option<String> {
271+
pub(crate) fn socket_patch_name_uuid(name: &str, vendored: bool) -> Option<String> {
245272
crate::patch::redirect::generation::pin_name_uuid(name.trim(), vendored).map(str::to_string)
246273
}
247274

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub(crate) fn hosted_pin_name(uuid: &str) -> String {
5151
/// `socket-patch-vendor-<canonical-uuid>` (maven's vendored repository id).
5252
/// No trimming: the rewriter must never treat a user's padded pin as its
5353
/// own, while lockfile discovery trims at its call site
54-
/// (`vex::discover::socket_patch_name_uuid`).
54+
/// (`origin::socket_patch_name_uuid`).
5555
pub(crate) fn pin_name_uuid(name: &str, vendored: bool) -> Option<&str> {
5656
let prefix = if vendored {
5757
VENDOR_PIN_NAME_PREFIX

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7606,8 +7606,6 @@ pub fn grant_token_path_segment(url: &str, patch_uuid: &str) -> Option<String> {
76067606
(!token.is_empty()).then(|| token.to_string())
76077607
}
76087608

7609-
pub use crate::origin::{hosted_patch_url_uuids, hosted_patch_uuid, SOCKET_PATCH_SERVER_HOST};
7610-
76117609
/// A dep's Socket index URL as a regex source with the per-request rotating
76127610
/// segments (grant token, patch uuid) wildcarded — an exact-URL pattern
76137611
/// misses the URL a previous run wrote under an older grant. The grant token

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,11 @@ pub fn foreign_dep_origins<'a>(
476476
dep_origins(deps)
477477
.into_iter()
478478
.filter(|origin| {
479-
let known = std::iter::once(format!("https://{}", super::SOCKET_PATCH_SERVER_HOST))
480-
.chain(configured.iter().cloned());
479+
let known = std::iter::once(format!(
480+
"https://{}",
481+
crate::origin::SOCKET_PATCH_SERVER_HOST
482+
))
483+
.chain(configured.iter().cloned());
481484
!known
482485
.into_iter()
483486
.any(|k| dep_origins_of_url(&k).as_deref() == Some(origin.as_str()))
@@ -768,7 +771,7 @@ impl Ctx<'_> {
768771
/// The hosted patch uuid `url` names, under the same host allowlist
769772
/// discovery applies.
770773
pub(crate) fn hosted_uuid(&self, url: &str) -> Option<String> {
771-
super::hosted_patch_uuid(url, self.origins)
774+
crate::origin::hosted_patch_uuid(url, self.origins)
772775
}
773776
}
774777

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use std::path::{Path, PathBuf};
1414

1515
use serde_json::{Map, Value};
1616

17-
use super::hosted_patch_uuid;
1817
use crate::constants::npm_family::{VLT_HIDDEN_LOCK_REL, VLT_STORE_DIR};
1918
use crate::manifest::schema::PatchRecord;
19+
use crate::origin::hosted_patch_uuid;
2020
use crate::patch::apply::{is_safe_relative_subpath, normalize_file_path};
2121
use crate::patch::file_hash::compute_file_git_sha256;
2222
use crate::patch::package::read_archive_bytes_to_map_strict;

‎crates/socket-patch-core/src/vendor/lock_inventory/mod.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,17 @@ mod architecture_tests {
406406
const PURE_READER: &str = "// ── pure reader ──";
407407
/// Anything that reads content, does I/O, or applies the hosted-origin
408408
/// policy.
409-
const IMPURE: [&str; 9] = [
409+
const IMPURE: [&str; 12] = [
410410
"tokio::fs",
411411
"std::fs",
412412
"read_regular_",
413413
"File::open",
414414
"OpenOptions",
415415
"hosted_patch_uuid",
416416
"hosted_patch_url_uuids",
417+
"hosted_uuid(",
418+
"OriginPolicy",
419+
"crate::origin::",
417420
"async fn",
418421
".await",
419422
];

‎crates/socket-patch-core/src/vendor/lock_inventory/pypi.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,12 @@ pub(crate) fn hosted_artifact_url(url: &str) -> Result<HostedArtifactUrl, String
173173
/// Pipenv rotation (`redirect::pipenv`) and the vendored Pipenv guard
174174
/// (`vendor::pypi_pipenv`) share. Ours means both: served from an accepted
175175
/// origin (patch.socket.dev or one of `origins`, with no userinfo —
176-
/// [`crate::patch::redirect::hosted_patch_url_uuids`]'s origin policy), and
176+
/// [`crate::origin::hosted_patch_url_uuids`]'s origin policy), and
177177
/// a [`hosted_artifact_url`] whose `…/patch/pypi/<name>/<version>/<grant>/
178178
/// <uuid>/<artifact>` tail is matched from the END, so a path-prefixed
179179
/// `--patch-server-url` deployment and a hosted sdist are recognized too.
180180
pub(crate) fn hosted_pypi_reference(url: &str, origins: &[String]) -> Option<HostedArtifactUrl> {
181-
crate::patch::redirect::hosted_patch_url_uuids(url, origins)?;
181+
crate::origin::hosted_patch_url_uuids(url, origins)?;
182182
hosted_artifact_url(url).ok().filter(|coords| {
183183
coords
184184
.uuid_level

‎crates/socket-patch-core/src/vendor/path.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ fn split_nuget_leaf(stem: &str) -> Option<(&str, &str)> {
289289
///
290290
/// The leaf must be CLEAN (no lock-format suffix such as yarn's `#<sha1>` or
291291
/// berry's `::locator=…`) and pypi dist names come back un-canonicalized;
292-
/// `vex::discover::vendored_leaf_purl` strips the former and canonicalizes
292+
/// `origin::vendored_leaf_purl` strips the former and canonicalizes
293293
/// the latter for lockfile-discovered references.
294294
pub(crate) fn leaf_to_purl(eco: &str, leaf: &str) -> Option<String> {
295295
match eco {

0 commit comments

Comments
 (0)