@@ -11,6 +11,7 @@ use socket_patch_core::api::types::{
1111} ;
1212use socket_patch_core:: crawlers:: fuzzy_match:: fuzzy_match_packages;
1313use socket_patch_core:: crawlers:: { CrawlerOptions , Ecosystem } ;
14+ use socket_patch_core:: formats:: pnpm:: PnpmLock ;
1415use socket_patch_core:: manifest:: operations:: { read_manifest, write_manifest} ;
1516use socket_patch_core:: manifest:: schema:: {
1617 PatchFileInfo , PatchManifest , PatchRecord , VulnerabilityInfo ,
@@ -1498,47 +1499,6 @@ fn purl_has_version(purl: &str) -> bool {
14981499 } )
14991500}
15001501
1501- /// Does the raw pnpm-lock text RESOLVE `name@version`? Boundary-anchored
1502- /// probes over the three lock grammars — a plain `contains` collides on
1503- /// version prefixes (`left-pad@1.3.0` matches inside
1504- /// `left-pad@1.3.0-beta.1`), name suffixes (`pad@1.3.0` inside
1505- /// `left-pad@1.3.0`), and unscoped-inside-scoped names (`name@1.0.0` inside
1506- /// `@scope/name@1.0.0`). The needles cover v6/v9's `name@version` and v5's
1507- /// `/name/version` key spellings; a match counts only when the preceding
1508- /// char cannot extend the name (start/whitespace/quote, or a `/` delimiter
1509- /// itself preceded by such a boundary) and the following char cannot extend
1510- /// the version (so `:`, `'`, `(`, and v5's `_peer` suffix all accept).
1511- /// Heuristic by design: a false negative degrades to a calm skip, a false
1512- /// positive costs one grant request the rewriter's per-dep confirmation
1513- /// then ignores.
1514- fn pnpm_lock_resolves ( text : & str , name : & str , version : & str ) -> bool {
1515- let version_boundary = |c : char | !( c. is_ascii_alphanumeric ( ) || matches ! ( c, '.' | '-' | '+' ) ) ;
1516- let name_boundary = |c : char | matches ! ( c, ' ' | '\t' | '\n' | '\r' | '\'' | '"' ) ;
1517- for needle in [ format ! ( "{name}@{version}" ) , format ! ( "/{name}/{version}" ) ] {
1518- for ( pos, _) in text. match_indices ( needle. as_str ( ) ) {
1519- let before_ok = match text[ ..pos] . chars ( ) . next_back ( ) {
1520- None => true ,
1521- // v5/v6's leading key delimiter — legitimate only when the
1522- // char before it is itself a boundary (otherwise this is a
1523- // scoped `@scope/<name>` tail: a DIFFERENT package).
1524- Some ( '/' ) => text[ ..pos - 1 ]
1525- . chars ( )
1526- . next_back ( )
1527- . is_none_or ( name_boundary) ,
1528- Some ( c) => name_boundary ( c) ,
1529- } ;
1530- let after_ok = text[ pos + needle. len ( ) ..]
1531- . chars ( )
1532- . next ( )
1533- . is_none_or ( version_boundary) ;
1534- if before_ok && after_ok {
1535- return true ;
1536- }
1537- }
1538- }
1539- false
1540- }
1541-
15421502/// Outcome of the coarse installed-VERSION narrowing over a CVE/GHSA/PURL
15431503/// search fan-out (see [`filter_to_installed_purls`]).
15441504struct InstalledNarrowing {
@@ -1576,7 +1536,7 @@ struct InstalledNarrowing {
15761536/// `yarn_pnp_unsupported`, not a false "not installed"). pnpm PnP skips
15771537/// carry `pnpm_pnp_unsupported` in agent/vendored modes; hosted mode — the
15781538/// refusal's own remedy — keeps the versions the raw pnpm-lock.yaml text
1579- /// resolves ([`pnpm_lock_resolves `]), labels a judged miss
1539+ /// resolves ([`PnpmLock::resolves `]), labels a judged miss
15801540/// `package_not_installed` like any other mode, and reserves the layout
15811541/// code for an unreadable lock (no judgment possible).
15821542///
@@ -1645,6 +1605,7 @@ async fn filter_to_installed_purls(
16451605 let pnpm_pnp_lock_text: Option < String > = ( pnp_pnpm && mode == super :: scan:: ScanMode :: Hosted )
16461606 . then ( || std:: fs:: read_to_string ( common. cwd . join ( "pnpm-lock.yaml" ) ) . ok ( ) )
16471607 . flatten ( ) ;
1608+ let pnpm_pnp_lock = pnpm_pnp_lock_text. as_deref ( ) . map ( PnpmLock :: parse) ;
16481609
16491610 let mut out = InstalledNarrowing {
16501611 kept : Vec :: new ( ) ,
@@ -1674,8 +1635,8 @@ async fn filter_to_installed_purls(
16741635 // The pnpm PnP refusal's own remedy is the hosted lockfile
16751636 // rewrite — but only for versions the lock ACTUALLY resolves:
16761637 // keeping the whole fan-out would request grants for every
1677- // version ever patched. Anchored probe over the raw lock text
1678- // (see `pnpm_lock_resolves `); a hit is kept (the rewriter's
1638+ // version ever patched. The lock model's key probe
1639+ // (`PnpmLock::resolves `); a hit is kept (the rewriter's
16791640 // per-dep confirmation still decides). A judged MISS is a
16801641 // genuine "version not resolved" verdict — the layout blocked
16811642 // nothing — so it carries the same `package_not_installed` code
@@ -1684,9 +1645,9 @@ async fn filter_to_installed_purls(
16841645 let decoded = canon ( & result. purl ) ;
16851646 let coord = decoded. strip_prefix ( "pkg:npm/" ) . unwrap_or ( & decoded) ;
16861647 if mode == super :: scan:: ScanMode :: Hosted {
1687- match ( pnpm_pnp_lock_text . as_deref ( ) , coord. rsplit_once ( '@' ) ) {
1688- ( Some ( text ) , Some ( ( name, version) ) ) => {
1689- if pnpm_lock_resolves ( text , name, version) {
1648+ match ( & pnpm_pnp_lock , coord. rsplit_once ( '@' ) ) {
1649+ ( Some ( lock ) , Some ( ( name, version) ) ) => {
1650+ if lock . resolves ( name, version) {
16901651 out. kept . push ( result. clone ( ) ) ;
16911652 continue ;
16921653 }
@@ -3985,77 +3946,6 @@ pub(crate) fn base64_decode(input: &str) -> Result<Vec<u8>, String> {
39853946mod tests {
39863947 use super :: * ;
39873948
3988- /// The pnpm-PnP hosted lock probe must be boundary-anchored: plain
3989- /// substring matching collides on version prefixes, name suffixes, and
3990- /// unscoped-inside-scoped names.
3991- #[ test]
3992- fn pnpm_lock_resolves_is_boundary_anchored ( ) {
3993- // v9/v6/v5 key spellings all resolve.
3994- assert ! ( pnpm_lock_resolves(
3995- "lockfileVersion: '9.0'\n \n snapshots:\n \n left-pad@1.3.0:\n " ,
3996- "left-pad" ,
3997- "1.3.0"
3998- ) ) ;
3999- assert ! ( pnpm_lock_resolves(
4000- " /left-pad@1.3.0:\n resolution: {}\n " ,
4001- "left-pad" ,
4002- "1.3.0"
4003- ) ) ;
4004- assert ! ( pnpm_lock_resolves(
4005- " /left-pad/1.3.0:\n resolution: {}\n " ,
4006- "left-pad" ,
4007- "1.3.0"
4008- ) ) ;
4009- // Peer-qualified keys still resolve: v9 `(peer)` and v5 `_peer`.
4010- assert ! ( pnpm_lock_resolves(
4011- " 'left-pad@1.3.0(react@18.0.0)':\n " ,
4012- "left-pad" ,
4013- "1.3.0"
4014- ) ) ;
4015- assert ! ( pnpm_lock_resolves(
4016- " /left-pad/1.3.0_react@18.0.0:\n " ,
4017- "left-pad" ,
4018- "1.3.0"
4019- ) ) ;
4020- // Scoped names resolve in both quoted-v9 and v6 spellings.
4021- assert ! ( pnpm_lock_resolves(
4022- " '@scope/name@1.0.0':\n " ,
4023- "@scope/name" ,
4024- "1.0.0"
4025- ) ) ;
4026- assert ! ( pnpm_lock_resolves(
4027- " /@scope/name@1.0.0:\n " ,
4028- "@scope/name" ,
4029- "1.0.0"
4030- ) ) ;
4031-
4032- // Version-prefix collision: 1.3.0 must NOT match 1.3.0-beta.1.
4033- assert ! ( !pnpm_lock_resolves(
4034- " left-pad@1.3.0-beta.1:\n " ,
4035- "left-pad" ,
4036- "1.3.0"
4037- ) ) ;
4038- // Name-suffix collision: `pad` must NOT match inside `left-pad`.
4039- assert ! ( !pnpm_lock_resolves( " left-pad@1.3.0:\n " , "pad" , "1.3.0" ) ) ;
4040- assert ! ( !pnpm_lock_resolves( " /left-pad/1.3.0:\n " , "pad" , "1.3.0" ) ) ;
4041- // Unscoped-inside-scoped: `name` must NOT match `@scope/name`.
4042- assert ! ( !pnpm_lock_resolves(
4043- " '@scope/name@1.0.0':\n " ,
4044- "name" ,
4045- "1.0.0"
4046- ) ) ;
4047- assert ! ( !pnpm_lock_resolves(
4048- " /@scope/name@1.0.0:\n " ,
4049- "name" ,
4050- "1.0.0"
4051- ) ) ;
4052- // Absent version: never resolves.
4053- assert ! ( !pnpm_lock_resolves(
4054- " left-pad@1.3.0:\n " ,
4055- "left-pad" ,
4056- "2.0.0"
4057- ) ) ;
4058- }
40593949 use socket_patch_core:: api:: types:: { PatchFileResponse , VulnerabilityResponse } ;
40603950 use std:: collections:: HashMap ;
40613951
@@ -5034,36 +4924,6 @@ mod tests {
50344924 ) ;
50354925 }
50364926
5037- // --- pnpm_lock_resolves: needle at byte 0 ------------------------------
5038- // The boundary probe reads the char BEFORE the match; a match at the very
5039- // start of the text has none (`None => true`). A regression that indexes
5040- // `text[..pos - 1]` unconditionally would underflow/panic here.
5041-
5042- #[ test]
5043- fn pnpm_lock_resolves_needle_at_start_of_text ( ) {
5044- // pos == 0, plain v9 spelling: no preceding char is a valid boundary.
5045- assert ! ( pnpm_lock_resolves( "left-pad@1.3.0:\n " , "left-pad" , "1.3.0" ) ) ;
5046- // pos == 0, v5/v6 `/name/version` and `/name@version` spellings: the
5047- // leading `/` delimiter itself has nothing before it.
5048- assert ! ( pnpm_lock_resolves(
5049- "/left-pad/1.3.0:\n " ,
5050- "left-pad" ,
5051- "1.3.0"
5052- ) ) ;
5053- assert ! ( pnpm_lock_resolves(
5054- "/left-pad@1.3.0:\n " ,
5055- "left-pad" ,
5056- "1.3.0"
5057- ) ) ;
5058- // Still boundary-checked at the start of text: a scoped tail whose
5059- // name begins mid-token must NOT match.
5060- assert ! ( !pnpm_lock_resolves(
5061- "@scope/left-pad@1.3.0:\n " ,
5062- "left-pad" ,
5063- "1.3.0"
5064- ) ) ;
5065- }
5066-
50674927 // --- write_all_patch_blobs ---------------------------------------------
50684928 // The per-patch fan-out over write_blob_entry: the FIRST bad entry must
50694929 // fail the whole patch (Err(())) and leave nothing outside the blobs
0 commit comments