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.
7683pub 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 ) ]
175181pub ( 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
181190impl < ' 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
0 commit comments