socket-patch-core 2.0.0

Core library for socket-patch: manifest, hash, crawlers, patch engine, API client
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};

use super::types::{CrawledPackage, CrawlerOptions};

// ---------------------------------------------------------------------------
// Cargo.toml minimal parser
// ---------------------------------------------------------------------------

/// Parse `name` and `version` from a `Cargo.toml` `[package]` section.
///
/// Uses a simple line-based parser — no TOML crate dependency.
/// Handles `name = "..."` and `version = "..."` within the `[package]` table.
/// Returns `None` if `version.workspace = true` or fields are missing.
pub fn parse_cargo_toml_name_version(content: &str) -> Option<(String, String)> {
    let mut in_package = false;
    let mut name: Option<String> = None;
    let mut version: Option<String> = None;

    for line in content.lines() {
        let trimmed = line.trim();

        // Skip comments and empty lines
        if trimmed.starts_with('#') || trimmed.is_empty() {
            continue;
        }

        // Track table headers
        if trimmed.starts_with('[') {
            if trimmed == "[package]" {
                in_package = true;
            } else {
                // We left the [package] section
                if in_package {
                    break;
                }
            }
            continue;
        }

        if !in_package {
            continue;
        }

        if let Some(val) = extract_string_value(trimmed, "name") {
            name = Some(val);
        } else if let Some(val) = extract_string_value(trimmed, "version") {
            version = Some(val);
        } else if trimmed.starts_with("version") && trimmed.contains("workspace") {
            // version.workspace = true — cannot determine version from this file
            return None;
        }

        if name.is_some() && version.is_some() {
            break;
        }
    }

    match (name, version) {
        (Some(n), Some(v)) if !n.is_empty() && !v.is_empty() => Some((n, v)),
        _ => None,
    }
}

/// Extract a quoted string value from a `key = "value"` line.
fn extract_string_value(line: &str, key: &str) -> Option<String> {
    let rest = line.strip_prefix(key)?;
    let rest = rest.trim_start();
    let rest = rest.strip_prefix('=')?;
    let rest = rest.trim_start();
    let rest = rest.strip_prefix('"')?;
    let end = rest.find('"')?;
    Some(rest[..end].to_string())
}

// ---------------------------------------------------------------------------
// CargoCrawler
// ---------------------------------------------------------------------------

/// Cargo/Rust ecosystem crawler for discovering crates in the local
/// vendor directory or the Cargo registry cache (`$CARGO_HOME/registry/src/`).
pub struct CargoCrawler;

impl CargoCrawler {
    /// Create a new `CargoCrawler`.
    pub fn new() -> Self {
        Self
    }

    // ------------------------------------------------------------------
    // Public API
    // ------------------------------------------------------------------

    /// Get crate source paths based on options.
    ///
    /// In local mode, checks `<cwd>/vendor/` first, then falls back to
    /// `$CARGO_HOME/registry/src/` index directories — but only if the
    /// `cwd` actually contains a `Cargo.toml` or `Cargo.lock` (i.e. is a
    /// Rust project). This prevents scanning the global cargo registry
    /// when patching a non-Rust project.
    ///
    /// In global mode, returns `$CARGO_HOME/registry/src/` index directories
    /// (or the `--global-prefix` override).
    pub async fn get_crate_source_paths(
        &self,
        options: &CrawlerOptions,
    ) -> Result<Vec<PathBuf>, std::io::Error> {
        if options.global || options.global_prefix.is_some() {
            if let Some(ref custom) = options.global_prefix {
                return Ok(vec![custom.clone()]);
            }
            return Ok(Self::get_registry_src_paths().await);
        }

        // Local mode: check vendor first
        let vendor_dir = options.cwd.join("vendor");
        if is_dir(&vendor_dir).await {
            return Ok(vec![vendor_dir]);
        }

        // Only fall back to global registry if this looks like a Cargo project
        let has_cargo_toml = tokio::fs::metadata(options.cwd.join("Cargo.toml"))
            .await
            .is_ok();
        let has_cargo_lock = tokio::fs::metadata(options.cwd.join("Cargo.lock"))
            .await
            .is_ok();

        if has_cargo_toml || has_cargo_lock {
            return Ok(Self::get_registry_src_paths().await);
        }

        // Not a Cargo project — return empty
        Ok(Vec::new())
    }

    /// Crawl all discovered crate source directories and return every
    /// package found.
    pub async fn crawl_all(&self, options: &CrawlerOptions) -> Vec<CrawledPackage> {
        let mut packages = Vec::new();
        let mut seen = HashSet::new();

        let src_paths = self.get_crate_source_paths(options).await.unwrap_or_default();

        for src_path in &src_paths {
            let found = self.scan_crate_source(src_path, &mut seen).await;
            packages.extend(found);
        }

        packages
    }

    /// Find specific packages by PURL inside a single crate source directory.
    ///
    /// Supports two layouts:
    /// - **Registry**: `<name>-<version>/Cargo.toml`
    /// - **Vendor**: `<name>/Cargo.toml` (version verified from file contents)
    pub async fn find_by_purls(
        &self,
        src_path: &Path,
        purls: &[String],
    ) -> Result<HashMap<String, CrawledPackage>, std::io::Error> {
        let mut result: HashMap<String, CrawledPackage> = HashMap::new();

        for purl in purls {
            if let Some((name, version)) = crate::utils::purl::parse_cargo_purl(purl) {
                // Try registry layout: <name>-<version>/
                let registry_dir = src_path.join(format!("{name}-{version}"));
                if self
                    .verify_crate_at_path(&registry_dir, name, version)
                    .await
                {
                    result.insert(
                        purl.clone(),
                        CrawledPackage {
                            name: name.to_string(),
                            version: version.to_string(),
                            namespace: None,
                            purl: purl.clone(),
                            path: registry_dir,
                        },
                    );
                    continue;
                }

                // Try vendor layout: <name>/
                let vendor_dir = src_path.join(name);
                if self
                    .verify_crate_at_path(&vendor_dir, name, version)
                    .await
                {
                    result.insert(
                        purl.clone(),
                        CrawledPackage {
                            name: name.to_string(),
                            version: version.to_string(),
                            namespace: None,
                            purl: purl.clone(),
                            path: vendor_dir,
                        },
                    );
                }
            }
        }

        Ok(result)
    }

    // ------------------------------------------------------------------
    // Private helpers
    // ------------------------------------------------------------------

    /// List subdirectories of `$CARGO_HOME/registry/src/`.
    ///
    /// Each subdirectory corresponds to a registry index
    /// (e.g. `index.crates.io-6f17d22bba15001f/`).
    async fn get_registry_src_paths() -> Vec<PathBuf> {
        let cargo_home = Self::cargo_home();
        let registry_src = cargo_home.join("registry").join("src");

        let mut paths = Vec::new();

        let mut entries = match tokio::fs::read_dir(&registry_src).await {
            Ok(rd) => rd,
            Err(_) => return paths,
        };

        while let Ok(Some(entry)) = entries.next_entry().await {
            let ft = match entry.file_type().await {
                Ok(ft) => ft,
                Err(_) => continue,
            };
            if ft.is_dir() {
                paths.push(registry_src.join(entry.file_name()));
            }
        }

        paths
    }

    /// Scan a crate source directory (either a registry index directory or
    /// a vendor directory) and return all valid crate packages found.
    async fn scan_crate_source(
        &self,
        src_path: &Path,
        seen: &mut HashSet<String>,
    ) -> Vec<CrawledPackage> {
        let mut results = Vec::new();

        let mut entries = match tokio::fs::read_dir(src_path).await {
            Ok(rd) => rd,
            Err(_) => return results,
        };

        let mut entry_list = Vec::new();
        while let Ok(Some(entry)) = entries.next_entry().await {
            entry_list.push(entry);
        }

        for entry in entry_list {
            let ft = match entry.file_type().await {
                Ok(ft) => ft,
                Err(_) => continue,
            };
            if !ft.is_dir() {
                continue;
            }

            let dir_name = entry.file_name();
            let dir_name_str = dir_name.to_string_lossy();

            // Skip hidden directories
            if dir_name_str.starts_with('.') {
                continue;
            }

            let crate_path = src_path.join(&*dir_name_str);
            if let Some(pkg) =
                self.read_crate_cargo_toml(&crate_path, &dir_name_str, seen).await
            {
                results.push(pkg);
            }
        }

        results
    }

    /// Read `Cargo.toml` from a crate directory, returning a `CrawledPackage`
    /// if valid. Falls back to parsing name+version from the directory name
    /// when the Cargo.toml has `version.workspace = true`.
    async fn read_crate_cargo_toml(
        &self,
        crate_path: &Path,
        dir_name: &str,
        seen: &mut HashSet<String>,
    ) -> Option<CrawledPackage> {
        let cargo_toml_path = crate_path.join("Cargo.toml");
        let content = tokio::fs::read_to_string(&cargo_toml_path).await.ok()?;

        let (name, version) = match parse_cargo_toml_name_version(&content) {
            Some(nv) => nv,
            None => {
                // Fallback: parse directory name as <name>-<version>
                Self::parse_dir_name_version(dir_name)?
            }
        };

        let purl = crate::utils::purl::build_cargo_purl(&name, &version);

        if seen.contains(&purl) {
            return None;
        }
        seen.insert(purl.clone());

        Some(CrawledPackage {
            name,
            version,
            namespace: None,
            purl,
            path: crate_path.to_path_buf(),
        })
    }

    /// Verify that a crate directory contains a Cargo.toml with the expected
    /// name and version.
    async fn verify_crate_at_path(&self, path: &Path, name: &str, version: &str) -> bool {
        let cargo_toml_path = path.join("Cargo.toml");
        let content = match tokio::fs::read_to_string(&cargo_toml_path).await {
            Ok(c) => c,
            Err(_) => return false,
        };

        match parse_cargo_toml_name_version(&content) {
            Some((n, v)) => n == name && v == version,
            None => {
                // Fallback: check directory name
                let dir_name = path
                    .file_name()
                    .map(|n| n.to_string_lossy().to_string())
                    .unwrap_or_default();
                if let Some((parsed_name, parsed_version)) =
                    Self::parse_dir_name_version(&dir_name)
                {
                    parsed_name == name && parsed_version == version
                } else {
                    false
                }
            }
        }
    }

    /// Parse a registry directory name into (name, version).
    ///
    /// Registry directories follow the pattern `<crate-name>-<version>`,
    /// where the version is the last `-`-separated component that starts with
    /// a digit (handles crate names with hyphens like `serde-json`).
    fn parse_dir_name_version(dir_name: &str) -> Option<(String, String)> {
        // Find the last '-' followed by a digit
        let mut split_idx = None;
        for (i, _) in dir_name.match_indices('-') {
            if dir_name[i + 1..].starts_with(|c: char| c.is_ascii_digit()) {
                split_idx = Some(i);
            }
        }
        let idx = split_idx?;
        let name = &dir_name[..idx];
        let version = &dir_name[idx + 1..];
        if name.is_empty() || version.is_empty() {
            return None;
        }
        Some((name.to_string(), version.to_string()))
    }

    /// Get `CARGO_HOME`, defaulting to `$HOME/.cargo`.
    fn cargo_home() -> PathBuf {
        if let Ok(cargo_home) = std::env::var("CARGO_HOME") {
            return PathBuf::from(cargo_home);
        }
        let home = std::env::var("HOME")
            .or_else(|_| std::env::var("USERPROFILE"))
            .unwrap_or_else(|_| "~".to_string());
        PathBuf::from(home).join(".cargo")
    }
}

impl Default for CargoCrawler {
    fn default() -> Self {
        Self::new()
    }
}

/// Check whether a path is a directory.
async fn is_dir(path: &Path) -> bool {
    tokio::fs::metadata(path)
        .await
        .map(|m| m.is_dir())
        .unwrap_or(false)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_cargo_toml_basic() {
        let content = r#"
[package]
name = "serde"
version = "1.0.200"
edition = "2021"
"#;
        let (name, version) = parse_cargo_toml_name_version(content).unwrap();
        assert_eq!(name, "serde");
        assert_eq!(version, "1.0.200");
    }

    #[test]
    fn test_parse_cargo_toml_with_comments() {
        let content = r#"
# This is a comment
[package]
name = "tokio" # inline comment ignored since we stop at first "
version = "1.38.0"
"#;
        let (name, version) = parse_cargo_toml_name_version(content).unwrap();
        assert_eq!(name, "tokio");
        assert_eq!(version, "1.38.0");
    }

    #[test]
    fn test_parse_cargo_toml_workspace_version() {
        let content = r#"
[package]
name = "my-crate"
version.workspace = true
"#;
        assert!(parse_cargo_toml_name_version(content).is_none());
    }

    #[test]
    fn test_parse_cargo_toml_missing_fields() {
        let content = r#"
[package]
name = "incomplete"
"#;
        assert!(parse_cargo_toml_name_version(content).is_none());
    }

    #[test]
    fn test_parse_cargo_toml_no_package_section() {
        let content = r#"
[dependencies]
serde = "1.0"
"#;
        assert!(parse_cargo_toml_name_version(content).is_none());
    }

    #[test]
    fn test_parse_cargo_toml_stops_at_next_section() {
        let content = r#"
[package]
name = "foo"

[dependencies]
version = "fake"
"#;
        // Should not find version since it's under [dependencies]
        assert!(parse_cargo_toml_name_version(content).is_none());
    }

    #[test]
    fn test_parse_dir_name_version() {
        assert_eq!(
            CargoCrawler::parse_dir_name_version("serde-1.0.200"),
            Some(("serde".to_string(), "1.0.200".to_string()))
        );
        assert_eq!(
            CargoCrawler::parse_dir_name_version("serde-json-1.0.120"),
            Some(("serde-json".to_string(), "1.0.120".to_string()))
        );
        assert_eq!(
            CargoCrawler::parse_dir_name_version("tokio-1.38.0"),
            Some(("tokio".to_string(), "1.38.0".to_string()))
        );
        assert!(CargoCrawler::parse_dir_name_version("no-version-here").is_none());
        assert!(CargoCrawler::parse_dir_name_version("noversion").is_none());
    }

    #[tokio::test]
    async fn test_find_by_purls_registry_layout() {
        let dir = tempfile::tempdir().unwrap();
        let serde_dir = dir.path().join("serde-1.0.200");
        tokio::fs::create_dir_all(&serde_dir).await.unwrap();
        tokio::fs::write(
            serde_dir.join("Cargo.toml"),
            "[package]\nname = \"serde\"\nversion = \"1.0.200\"\n",
        )
        .await
        .unwrap();

        let crawler = CargoCrawler::new();
        let purls = vec![
            "pkg:cargo/serde@1.0.200".to_string(),
            "pkg:cargo/tokio@1.38.0".to_string(),
        ];
        let result = crawler.find_by_purls(dir.path(), &purls).await.unwrap();

        assert_eq!(result.len(), 1);
        assert!(result.contains_key("pkg:cargo/serde@1.0.200"));
        assert!(!result.contains_key("pkg:cargo/tokio@1.38.0"));
    }

    #[tokio::test]
    async fn test_find_by_purls_vendor_layout() {
        let dir = tempfile::tempdir().unwrap();
        let serde_dir = dir.path().join("serde");
        tokio::fs::create_dir_all(&serde_dir).await.unwrap();
        tokio::fs::write(
            serde_dir.join("Cargo.toml"),
            "[package]\nname = \"serde\"\nversion = \"1.0.200\"\n",
        )
        .await
        .unwrap();

        let crawler = CargoCrawler::new();
        let purls = vec!["pkg:cargo/serde@1.0.200".to_string()];
        let result = crawler.find_by_purls(dir.path(), &purls).await.unwrap();

        assert_eq!(result.len(), 1);
        assert!(result.contains_key("pkg:cargo/serde@1.0.200"));
    }

    #[tokio::test]
    async fn test_crawl_all_tempdir() {
        let dir = tempfile::tempdir().unwrap();

        // Create fake crate directories
        let serde_dir = dir.path().join("serde-1.0.200");
        tokio::fs::create_dir_all(&serde_dir).await.unwrap();
        tokio::fs::write(
            serde_dir.join("Cargo.toml"),
            "[package]\nname = \"serde\"\nversion = \"1.0.200\"\n",
        )
        .await
        .unwrap();

        let tokio_dir = dir.path().join("tokio-1.38.0");
        tokio::fs::create_dir_all(&tokio_dir).await.unwrap();
        tokio::fs::write(
            tokio_dir.join("Cargo.toml"),
            "[package]\nname = \"tokio\"\nversion = \"1.38.0\"\n",
        )
        .await
        .unwrap();

        let crawler = CargoCrawler::new();
        let options = CrawlerOptions {
            cwd: dir.path().to_path_buf(),
            global: false,
            global_prefix: Some(dir.path().to_path_buf()),
            batch_size: 100,
        };

        let packages = crawler.crawl_all(&options).await;
        assert_eq!(packages.len(), 2);

        let purls: HashSet<_> = packages.iter().map(|p| p.purl.as_str()).collect();
        assert!(purls.contains("pkg:cargo/serde@1.0.200"));
        assert!(purls.contains("pkg:cargo/tokio@1.38.0"));
    }

    #[tokio::test]
    async fn test_crawl_all_deduplication() {
        let dir = tempfile::tempdir().unwrap();

        // Create two directories that would resolve to the same PURL
        let dir1 = dir.path().join("serde-1.0.200");
        tokio::fs::create_dir_all(&dir1).await.unwrap();
        tokio::fs::write(
            dir1.join("Cargo.toml"),
            "[package]\nname = \"serde\"\nversion = \"1.0.200\"\n",
        )
        .await
        .unwrap();

        // This would be found if we scan the parent twice
        let crawler = CargoCrawler::new();
        let options = CrawlerOptions {
            cwd: dir.path().to_path_buf(),
            global: false,
            global_prefix: Some(dir.path().to_path_buf()),
            batch_size: 100,
        };

        let packages = crawler.crawl_all(&options).await;
        assert_eq!(packages.len(), 1);
        assert_eq!(packages[0].purl, "pkg:cargo/serde@1.0.200");
    }

    #[tokio::test]
    async fn test_crawl_workspace_version_fallback() {
        let dir = tempfile::tempdir().unwrap();

        // Create a crate with workspace version — should fall back to dir name parsing
        let crate_dir = dir.path().join("my-crate-0.5.0");
        tokio::fs::create_dir_all(&crate_dir).await.unwrap();
        tokio::fs::write(
            crate_dir.join("Cargo.toml"),
            "[package]\nname = \"my-crate\"\nversion.workspace = true\n",
        )
        .await
        .unwrap();

        let crawler = CargoCrawler::new();
        let options = CrawlerOptions {
            cwd: dir.path().to_path_buf(),
            global: false,
            global_prefix: Some(dir.path().to_path_buf()),
            batch_size: 100,
        };

        let packages = crawler.crawl_all(&options).await;
        assert_eq!(packages.len(), 1);
        assert_eq!(packages[0].purl, "pkg:cargo/my-crate@0.5.0");
    }

    #[tokio::test]
    async fn test_vendor_layout_via_get_crate_source_paths() {
        let dir = tempfile::tempdir().unwrap();
        let vendor = dir.path().join("vendor");
        tokio::fs::create_dir_all(&vendor).await.unwrap();

        let serde_dir = vendor.join("serde");
        tokio::fs::create_dir_all(&serde_dir).await.unwrap();
        tokio::fs::write(
            serde_dir.join("Cargo.toml"),
            "[package]\nname = \"serde\"\nversion = \"1.0.200\"\n",
        )
        .await
        .unwrap();

        let crawler = CargoCrawler::new();
        let options = CrawlerOptions {
            cwd: dir.path().to_path_buf(),
            global: false,
            global_prefix: None,
            batch_size: 100,
        };

        let paths = crawler.get_crate_source_paths(&options).await.unwrap();
        assert_eq!(paths.len(), 1);
        assert_eq!(paths[0], vendor);
    }
}