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
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};

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

// ---------------------------------------------------------------------------
// Case-encoding helpers
// ---------------------------------------------------------------------------

/// Encode a Go module path for the filesystem.
///
/// Go's module cache uses case-encoding: uppercase letters are replaced
/// with `!` followed by the lowercase letter.
/// e.g., `"github.com/Azure/azure-sdk"` -> `"github.com/!azure/azure-sdk"`
pub fn encode_module_path(path: &str) -> String {
    let mut encoded = String::with_capacity(path.len());
    for ch in path.chars() {
        if ch.is_ascii_uppercase() {
            encoded.push('!');
            encoded.push(ch.to_ascii_lowercase());
        } else {
            encoded.push(ch);
        }
    }
    encoded
}

/// Decode a case-encoded Go module path.
///
/// Reverses the encoding: `!` followed by a lowercase letter becomes the
/// uppercase letter.
/// e.g., `"github.com/!azure/azure-sdk"` -> `"github.com/Azure/azure-sdk"`
pub fn decode_module_path(encoded: &str) -> String {
    let mut decoded = String::with_capacity(encoded.len());
    let mut chars = encoded.chars();
    while let Some(ch) = chars.next() {
        if ch == '!' {
            if let Some(next) = chars.next() {
                decoded.push(next.to_ascii_uppercase());
            }
        } else {
            decoded.push(ch);
        }
    }
    decoded
}

/// Parse the `module` directive from a go.mod file.
///
/// Returns the module path, e.g., `"github.com/gin-gonic/gin"`.
pub fn parse_go_mod_module(content: &str) -> Option<String> {
    for line in content.lines() {
        let trimmed = line.trim();
        if let Some(rest) = trimmed.strip_prefix("module") {
            let rest = rest.trim();
            // Handle quoted module paths
            if rest.starts_with('"') && rest.ends_with('"') && rest.len() >= 2 {
                return Some(rest[1..rest.len() - 1].to_string());
            }
            // Unquoted module path
            if !rest.is_empty() {
                return Some(rest.to_string());
            }
        }
    }
    None
}

// ---------------------------------------------------------------------------
// GoCrawler
// ---------------------------------------------------------------------------

/// Go module ecosystem crawler for discovering modules in the Go module cache
/// (`$GOMODCACHE` or `$GOPATH/pkg/mod/`).
pub struct GoCrawler;

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

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

    /// Get the Go module cache paths.
    ///
    /// In global mode (or with `--global-prefix`), returns the module cache
    /// directory directly.
    ///
    /// In local mode, only returns the cache path if the cwd contains a
    /// `go.mod` or `go.sum` file (i.e., is a Go project).
    pub async fn get_module_cache_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_gomodcache().map_or_else(Vec::new, |p| vec![p]));
        }

        // Local mode: only scan if this looks like a Go project
        let has_go_mod = tokio::fs::metadata(options.cwd.join("go.mod"))
            .await
            .is_ok();
        let has_go_sum = tokio::fs::metadata(options.cwd.join("go.sum"))
            .await
            .is_ok();

        if has_go_mod || has_go_sum {
            return Ok(Self::get_gomodcache().map_or_else(Vec::new, |p| vec![p]));
        }

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

    /// Crawl the Go module cache and return all discovered packages.
    pub async fn crawl_all(&self, options: &CrawlerOptions) -> Vec<CrawledPackage> {
        let mut packages = Vec::new();
        let mut seen = HashSet::new();

        let cache_paths = self
            .get_module_cache_paths(options)
            .await
            .unwrap_or_default();

        for cache_path in &cache_paths {
            let found = self.scan_module_cache(cache_path, &mut seen).await;
            packages.extend(found);
        }

        packages
    }

    /// Find specific packages by PURL in the module cache.
    pub async fn find_by_purls(
        &self,
        cache_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((module_path, version)) = crate::utils::purl::parse_golang_purl(purl) {
                // Encode the module path for the filesystem
                let encoded = encode_module_path(module_path);

                // Go module cache layout: <encoded-module-path>@<version>/
                let module_dir = cache_path.join(format!("{encoded}@{version}"));

                if is_dir(&module_dir).await {
                    // Split module_path into namespace and name
                    let (namespace, name) = split_module_path(module_path);

                    result.insert(
                        purl.clone(),
                        CrawledPackage {
                            name: name.to_string(),
                            version: version.to_string(),
                            namespace: Some(namespace.to_string()),
                            purl: purl.clone(),
                            path: module_dir,
                        },
                    );
                }
            }
        }

        Ok(result)
    }

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

    /// Get `GOMODCACHE`, falling back to `$GOPATH/pkg/mod/` or `$HOME/go/pkg/mod/`.
    fn get_gomodcache() -> Option<PathBuf> {
        if let Ok(cache) = std::env::var("GOMODCACHE") {
            let p = PathBuf::from(cache);
            if !p.as_os_str().is_empty() {
                return Some(p);
            }
        }
        if let Ok(gopath) = std::env::var("GOPATH") {
            let p = PathBuf::from(gopath);
            if !p.as_os_str().is_empty() {
                return Some(p.join("pkg").join("mod"));
            }
        }
        let home = std::env::var("HOME")
            .or_else(|_| std::env::var("USERPROFILE"))
            .ok()?;
        Some(PathBuf::from(home).join("go").join("pkg").join("mod"))
    }

    /// Recursively scan the module cache directory tree.
    ///
    /// Go module cache has a hierarchical structure:
    /// `<cache>/github.com/user/project@v1.0.0/`
    ///
    /// We walk the tree looking for directories whose name contains `@`
    /// (the version separator), which marks a versioned module.
    async fn scan_module_cache(
        &self,
        cache_path: &Path,
        seen: &mut HashSet<String>,
    ) -> Vec<CrawledPackage> {
        let mut results = Vec::new();
        self.scan_dir_recursive(cache_path, cache_path, seen, &mut results)
            .await;
        results
    }

    fn scan_dir_recursive<'a>(
        &'a self,
        base_path: &'a Path,
        current_path: &'a Path,
        seen: &'a mut HashSet<String>,
        results: &'a mut Vec<CrawledPackage>,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + 'a>> {
        Box::pin(async move {
            let mut entries = match tokio::fs::read_dir(current_path).await {
                Ok(rd) => rd,
                Err(_) => return,
            };

            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 and the cache metadata directory
                if dir_name_str.starts_with('.') || dir_name_str == "cache" {
                    continue;
                }

                let full_path = current_path.join(&*dir_name_str);

                // Check if this directory has `@` in its name (versioned module)
                if dir_name_str.contains('@') {
                    if let Some(pkg) =
                        self.parse_versioned_dir(base_path, &full_path, &dir_name_str, seen)
                    {
                        results.push(pkg);
                    }
                } else {
                    // Recurse into subdirectories
                    self.scan_dir_recursive(base_path, &full_path, seen, results)
                        .await;
                }
            }
        })
    }

    /// Parse a versioned directory (containing `@`) into a `CrawledPackage`.
    fn parse_versioned_dir(
        &self,
        base_path: &Path,
        dir_path: &Path,
        _dir_name: &str,
        seen: &mut HashSet<String>,
    ) -> Option<CrawledPackage> {
        // Get the relative path from the cache root.
        // Normalize to forward slashes so PURLs are correct on Windows.
        let rel_path = dir_path.strip_prefix(base_path).ok()?;
        let rel_str = rel_path.to_string_lossy().replace('\\', "/");

        // Find the last `@` to split module path and version
        let at_idx = rel_str.rfind('@')?;
        let encoded_module_path = &rel_str[..at_idx];
        let version = &rel_str[at_idx + 1..];

        if encoded_module_path.is_empty() || version.is_empty() {
            return None;
        }

        // Decode case-encoded path
        let module_path = decode_module_path(encoded_module_path);

        let purl = crate::utils::purl::build_golang_purl(&module_path, version);

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

        let (namespace, name) = split_module_path(&module_path);

        Some(CrawledPackage {
            name: name.to_string(),
            version: version.to_string(),
            namespace: Some(namespace.to_string()),
            purl,
            path: dir_path.to_path_buf(),
        })
    }
}

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

/// Split a module path into (namespace, name).
///
/// e.g., `"github.com/gin-gonic/gin"` -> `("github.com/gin-gonic", "gin")`
/// e.g., `"golang.org/x/text"` -> `("golang.org/x", "text")`
fn split_module_path(module_path: &str) -> (&str, &str) {
    match module_path.rfind('/') {
        Some(idx) => (&module_path[..idx], &module_path[idx + 1..]),
        None => ("", module_path),
    }
}

/// 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_encode_module_path_no_uppercase() {
        assert_eq!(
            encode_module_path("github.com/gin-gonic/gin"),
            "github.com/gin-gonic/gin"
        );
    }

    #[test]
    fn test_encode_module_path_with_uppercase() {
        assert_eq!(
            encode_module_path("github.com/Azure/azure-sdk-for-go"),
            "github.com/!azure/azure-sdk-for-go"
        );
    }

    #[test]
    fn test_encode_module_path_multiple_uppercase() {
        assert_eq!(
            encode_module_path("github.com/BurntSushi/toml"),
            "github.com/!burnt!sushi/toml"
        );
    }

    #[test]
    fn test_decode_module_path_no_encoding() {
        assert_eq!(
            decode_module_path("github.com/gin-gonic/gin"),
            "github.com/gin-gonic/gin"
        );
    }

    #[test]
    fn test_decode_module_path_with_encoding() {
        assert_eq!(
            decode_module_path("github.com/!azure/azure-sdk-for-go"),
            "github.com/Azure/azure-sdk-for-go"
        );
    }

    #[test]
    fn test_encode_decode_roundtrip() {
        let original = "github.com/Azure/azure-sdk-for-go";
        assert_eq!(decode_module_path(&encode_module_path(original)), original);

        let original2 = "github.com/BurntSushi/toml";
        assert_eq!(
            decode_module_path(&encode_module_path(original2)),
            original2
        );

        let original3 = "github.com/gin-gonic/gin";
        assert_eq!(
            decode_module_path(&encode_module_path(original3)),
            original3
        );
    }

    #[test]
    fn test_parse_go_mod_module_basic() {
        let content = "module github.com/gin-gonic/gin\n\ngo 1.21\n";
        assert_eq!(
            parse_go_mod_module(content),
            Some("github.com/gin-gonic/gin".to_string())
        );
    }

    #[test]
    fn test_parse_go_mod_module_quoted() {
        let content = "module \"github.com/gin-gonic/gin\"\n\ngo 1.21\n";
        assert_eq!(
            parse_go_mod_module(content),
            Some("github.com/gin-gonic/gin".to_string())
        );
    }

    #[test]
    fn test_parse_go_mod_module_missing() {
        let content = "go 1.21\n\nrequire (\n\tgithub.com/gin-gonic/gin v1.9.1\n)\n";
        assert_eq!(parse_go_mod_module(content), None);
    }

    #[test]
    fn test_split_module_path() {
        let (ns, name) = split_module_path("github.com/gin-gonic/gin");
        assert_eq!(ns, "github.com/gin-gonic");
        assert_eq!(name, "gin");

        let (ns, name) = split_module_path("golang.org/x/text");
        assert_eq!(ns, "golang.org/x");
        assert_eq!(name, "text");

        let (ns, name) = split_module_path("gopkg.in/yaml.v3");
        assert_eq!(ns, "gopkg.in");
        assert_eq!(name, "yaml.v3");
    }

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

        // Create a fake module directory: github.com/gin-gonic/gin@v1.9.1
        let module_dir = dir
            .path()
            .join("github.com")
            .join("gin-gonic")
            .join("gin@v1.9.1");
        tokio::fs::create_dir_all(&module_dir).await.unwrap();

        let crawler = GoCrawler::new();
        let purls = vec![
            "pkg:golang/github.com/gin-gonic/gin@v1.9.1".to_string(),
            "pkg:golang/github.com/missing/pkg@v0.1.0".to_string(),
        ];
        let result = crawler.find_by_purls(dir.path(), &purls).await.unwrap();

        assert_eq!(result.len(), 1);
        assert!(result.contains_key("pkg:golang/github.com/gin-gonic/gin@v1.9.1"));
        assert!(!result.contains_key("pkg:golang/github.com/missing/pkg@v0.1.0"));

        let pkg = &result["pkg:golang/github.com/gin-gonic/gin@v1.9.1"];
        assert_eq!(pkg.name, "gin");
        assert_eq!(pkg.version, "v1.9.1");
        assert_eq!(pkg.namespace, Some("github.com/gin-gonic".to_string()));
    }

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

        // Create a case-encoded module directory
        let module_dir = dir
            .path()
            .join("github.com")
            .join("!azure")
            .join("azure-sdk-for-go@v1.0.0");
        tokio::fs::create_dir_all(&module_dir).await.unwrap();

        let crawler = GoCrawler::new();
        let purls = vec!["pkg:golang/github.com/Azure/azure-sdk-for-go@v1.0.0".to_string()];
        let result = crawler.find_by_purls(dir.path(), &purls).await.unwrap();

        assert_eq!(result.len(), 1);
        let pkg = &result["pkg:golang/github.com/Azure/azure-sdk-for-go@v1.0.0"];
        assert_eq!(pkg.name, "azure-sdk-for-go");
        assert_eq!(pkg.namespace, Some("github.com/Azure".to_string()));
    }

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

        // Create fake module directories
        let gin_dir = dir
            .path()
            .join("github.com")
            .join("gin-gonic")
            .join("gin@v1.9.1");
        tokio::fs::create_dir_all(&gin_dir).await.unwrap();

        let text_dir = dir.path().join("golang.org").join("x").join("text@v0.14.0");
        tokio::fs::create_dir_all(&text_dir).await.unwrap();

        let crawler = GoCrawler::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:golang/github.com/gin-gonic/gin@v1.9.1"));
        assert!(purls.contains("pkg:golang/golang.org/x/text@v0.14.0"));
    }

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

        // Create a single module
        let gin_dir = dir
            .path()
            .join("github.com")
            .join("gin-gonic")
            .join("gin@v1.9.1");
        tokio::fs::create_dir_all(&gin_dir).await.unwrap();

        let crawler = GoCrawler::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:golang/github.com/gin-gonic/gin@v1.9.1"
        );
    }

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

        // Create a real module
        let gin_dir = dir
            .path()
            .join("github.com")
            .join("gin-gonic")
            .join("gin@v1.9.1");
        tokio::fs::create_dir_all(&gin_dir).await.unwrap();

        // Create a "cache" dir (should be skipped)
        let cache_dir = dir.path().join("cache").join("download").join("sumdb");
        tokio::fs::create_dir_all(&cache_dir).await.unwrap();

        let crawler = GoCrawler::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);
    }

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

        // No go.mod or go.sum in cwd
        let crawler = GoCrawler::new();
        let options = CrawlerOptions {
            cwd: dir.path().to_path_buf(),
            global: false,
            global_prefix: None,
            batch_size: 100,
        };

        let paths = crawler.get_module_cache_paths(&options).await.unwrap();
        assert!(paths.is_empty());
    }

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

        // Create case-encoded module
        let azure_dir = dir
            .path()
            .join("github.com")
            .join("!azure")
            .join("azure-sdk-for-go@v1.0.0");
        tokio::fs::create_dir_all(&azure_dir).await.unwrap();

        let crawler = GoCrawler::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:golang/github.com/Azure/azure-sdk-for-go@v1.0.0"
        );
        assert_eq!(packages[0].name, "azure-sdk-for-go");
        assert_eq!(
            packages[0].namespace,
            Some("github.com/Azure".to_string())
        );
    }
}