phalus 0.7.0

Private Headless Automated License Uncoupling System — AI-powered clean room software reimplementation
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
/// Core scanning logic: walk a directory tree, collect packages from manifests
/// and SBOM files, resolve licenses via registries, and return a `ScanResult`.
use std::path::Path;

use anyhow::{Context, Result};
use tokio::sync::Semaphore;
use tokio::task::JoinSet;
use tracing::{debug, warn};
use uuid::Uuid;

use crate::license;
use crate::manifest;
use crate::registry::{
    crates::CratesResolver, golang::GoResolver, npm::NpmResolver, pypi::PypiResolver,
};
use crate::sbom;
use crate::{Ecosystem, LicenseClass, ScanResult, ScannedPackage};

/// Options for a scan run.
#[derive(Debug, Clone)]
pub struct ScanOptions {
    /// Maximum concurrent registry lookups.
    pub concurrency: usize,
    /// When true, skip registry lookups and rely only on manifest / SBOM data.
    pub offline: bool,
}

impl Default for ScanOptions {
    fn default() -> Self {
        Self {
            concurrency: 8,
            offline: false,
        }
    }
}

/// Walk `path` (directory or single manifest/SBOM file), collect all packages,
/// resolve their licenses, and return a `ScanResult`.
pub async fn run_scan(path: &Path, opts: ScanOptions) -> Result<ScanResult> {
    let abs_path = path
        .canonicalize()
        .with_context(|| format!("cannot access path: {}", path.display()))?;

    let mut manifest_files: Vec<String> = Vec::new();
    let mut sbom_files: Vec<String> = Vec::new();
    let mut package_refs: Vec<(crate::PackageRef, String /* source file */)> = Vec::new();
    let mut sbom_packages: Vec<ScannedPackage> = Vec::new();

    // ---------- Walk the directory ----------
    if abs_path.is_dir() {
        collect_files(
            &abs_path,
            &mut manifest_files,
            &mut sbom_files,
            &mut package_refs,
            &mut sbom_packages,
        )?;
    } else {
        // Single file — determine kind
        let file_name = abs_path.file_name().and_then(|n| n.to_str()).unwrap_or("");
        if sbom::is_sbom_filename(file_name) {
            let content = std::fs::read_to_string(&abs_path)?;
            match sbom::parse_sbom(&content) {
                Ok(pkgs) => {
                    sbom_files.push(abs_path.display().to_string());
                    sbom_packages.extend(pkgs);
                }
                Err(e) => warn!("Could not parse SBOM {}: {}", abs_path.display(), e),
            }
        } else {
            match manifest::parse_manifest(&abs_path) {
                Ok(parsed) => {
                    manifest_files.push(abs_path.display().to_string());
                    for pkg in parsed.packages {
                        package_refs.push((pkg, abs_path.display().to_string()));
                    }
                }
                Err(e) => warn!("Could not parse manifest {}: {}", abs_path.display(), e),
            }
        }
    }

    // ---------- Resolve licenses from registries ----------
    let mut registry_packages: Vec<ScannedPackage> = if opts.offline {
        // Offline: use package refs as-is with unknown license
        package_refs
            .into_iter()
            .map(|(pkg, _src)| ScannedPackage {
                name: pkg.name,
                version: pkg.version_constraint,
                ecosystem: pkg.ecosystem,
                raw_license: None,
                spdx_license: None,
                classification: LicenseClass::Unknown,
                source: "manifest".to_string(),
            })
            .collect()
    } else {
        resolve_licenses(package_refs, opts.concurrency).await
    };

    // Combine manifest-resolved packages and SBOM packages
    registry_packages.extend(sbom_packages);

    Ok(ScanResult {
        id: Uuid::new_v4().to_string(),
        path: abs_path.display().to_string(),
        scanned_at: chrono::Utc::now(),
        packages: registry_packages,
        manifest_files,
        sbom_files,
    })
}

// ---------------------------------------------------------------------------
// Directory walker
// ---------------------------------------------------------------------------

fn collect_files(
    dir: &Path,
    manifest_files: &mut Vec<String>,
    sbom_files: &mut Vec<String>,
    package_refs: &mut Vec<(crate::PackageRef, String)>,
    sbom_packages: &mut Vec<ScannedPackage>,
) -> Result<()> {
    let entries = std::fs::read_dir(dir)
        .with_context(|| format!("cannot read directory: {}", dir.display()))?;

    for entry in entries {
        let entry = entry?;
        let path = entry.path();

        // Skip hidden directories and common non-source dirs
        let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
        if file_name.starts_with('.') || SKIP_DIRS.contains(&file_name) {
            continue;
        }

        if path.is_dir() {
            collect_files(
                &path,
                manifest_files,
                sbom_files,
                package_refs,
                sbom_packages,
            )?;
            continue;
        }

        // SBOM files take priority
        if sbom::is_sbom_filename(file_name) {
            let content = match std::fs::read_to_string(&path) {
                Ok(c) => c,
                Err(e) => {
                    warn!("Cannot read {}: {}", path.display(), e);
                    continue;
                }
            };
            match sbom::parse_sbom(&content) {
                Ok(pkgs) => {
                    debug!("SBOM {}: {} packages", path.display(), pkgs.len());
                    sbom_files.push(path.display().to_string());
                    sbom_packages.extend(pkgs);
                }
                Err(e) => warn!("Cannot parse SBOM {}: {}", path.display(), e),
            }
            continue;
        }

        // Manifest files
        if is_manifest_filename(file_name) {
            match manifest::parse_manifest(&path) {
                Ok(parsed) => {
                    debug!(
                        "Manifest {}: {} packages",
                        path.display(),
                        parsed.packages.len()
                    );
                    manifest_files.push(path.display().to_string());
                    let src = path.display().to_string();
                    for pkg in parsed.packages {
                        package_refs.push((pkg, src.clone()));
                    }
                }
                Err(e) => warn!("Cannot parse manifest {}: {}", path.display(), e),
            }
        }
    }

    Ok(())
}

/// Filenames that identify a package manifest.
fn is_manifest_filename(name: &str) -> bool {
    matches!(
        name,
        "package.json" | "requirements.txt" | "Cargo.toml" | "go.mod"
    )
}

/// Top-level directories to skip entirely (not recursed into).
const SKIP_DIRS: &[&str] = &[
    "node_modules",
    ".git",
    ".svn",
    "__pycache__",
    ".mypy_cache",
    ".pytest_cache",
    "dist",
    "build",
    "target", // Rust build artifacts
    ".cargo",
    "vendor",
    "venv",
    ".venv",
    "env",
];

// ---------------------------------------------------------------------------
// Registry license resolution
// ---------------------------------------------------------------------------

async fn resolve_licenses(
    refs: Vec<(crate::PackageRef, String)>,
    concurrency: usize,
) -> Vec<ScannedPackage> {
    let sem = std::sync::Arc::new(Semaphore::new(concurrency));
    let mut set: JoinSet<ScannedPackage> = JoinSet::new();

    for (pkg, src) in refs {
        let permit = sem.clone().acquire_owned().await.unwrap();
        set.spawn(async move {
            let result = fetch_license(&pkg).await;
            drop(permit);
            let (raw_license, spdx_license, classification, source) = match result {
                Ok(Some(raw)) => {
                    let (spdx, class) = license::normalize_and_classify(&raw);
                    (Some(raw), Some(spdx), class, src)
                }
                Ok(None) => (None, None, LicenseClass::Unknown, src),
                Err(e) => {
                    debug!(
                        "Registry lookup failed for {}@{}: {}",
                        pkg.name, pkg.version_constraint, e
                    );
                    (None, None, LicenseClass::Unknown, src)
                }
            };
            ScannedPackage {
                name: pkg.name,
                version: pkg.version_constraint,
                ecosystem: pkg.ecosystem,
                raw_license,
                spdx_license,
                classification,
                source,
            }
        });
    }

    let mut results = Vec::new();
    while let Some(res) = set.join_next().await {
        match res {
            Ok(pkg) => results.push(pkg),
            Err(e) => warn!("Task panicked: {}", e),
        }
    }
    results
}

async fn fetch_license(pkg: &crate::PackageRef) -> Result<Option<String>> {
    let meta = match &pkg.ecosystem {
        Ecosystem::Npm => {
            NpmResolver::default_registry()
                .resolve(&pkg.name, &pkg.version_constraint)
                .await?
        }
        Ecosystem::PyPI => {
            PypiResolver::default_registry()
                .resolve(&pkg.name, &pkg.version_constraint)
                .await?
        }
        Ecosystem::Crates => {
            CratesResolver::default_registry()
                .resolve(&pkg.name, &pkg.version_constraint)
                .await?
        }
        Ecosystem::Go => {
            GoResolver::default_registry()
                .resolve(&pkg.name, &pkg.version_constraint)
                .await?
        }
    };
    Ok(meta.license)
}

// ---------------------------------------------------------------------------
// Output helpers
// ---------------------------------------------------------------------------

/// Format a `ScanResult` as a human-readable text report.
pub fn format_report(result: &ScanResult) -> String {
    let mut out = String::new();
    out.push_str(&format!("Scan ID:  {}\n", result.id));
    out.push_str(&format!("Path:     {}\n", result.path));
    out.push_str(&format!(
        "Scanned:  {}\n",
        result.scanned_at.format("%Y-%m-%d %H:%M:%S UTC")
    ));
    out.push_str(&format!(
        "Manifests found: {}\n",
        result.manifest_files.len()
    ));
    out.push_str(&format!("SBOMs found:     {}\n\n", result.sbom_files.len()));

    if result.packages.is_empty() {
        out.push_str("No packages found.\n");
        return out;
    }

    out.push_str(&format!(
        "{:<40} {:<15} {:<12} {:<20} {}\n",
        "Package", "Version", "Ecosystem", "Classification", "License (SPDX)"
    ));
    out.push_str(&"-".repeat(100));
    out.push('\n');

    let mut sorted = result.packages.clone();
    sorted.sort_by(|a, b| a.name.cmp(&b.name));

    for pkg in &sorted {
        let license_str = pkg
            .spdx_license
            .as_deref()
            .or(pkg.raw_license.as_deref())
            .unwrap_or("unknown");
        out.push_str(&format!(
            "{:<40} {:<15} {:<12} {:<20} {}\n",
            truncate(&pkg.name, 39),
            truncate(&pkg.version, 14),
            format!("{}", pkg.ecosystem),
            format!("{}", pkg.classification),
            license_str,
        ));
    }

    // Summary by classification
    let mut counts = std::collections::HashMap::new();
    for pkg in &result.packages {
        *counts.entry(pkg.classification.clone()).or_insert(0usize) += 1;
    }
    out.push('\n');
    out.push_str("Summary:\n");
    for (class, count) in &counts {
        out.push_str(&format!("  {:20} {}\n", format!("{}:", class), count));
    }
    out.push_str(&format!("  {:20} {}\n", "Total:", result.packages.len()));

    out
}

fn truncate(s: &str, max: usize) -> &str {
    if s.len() <= max {
        s
    } else {
        &s[..max]
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    fn write_file(dir: &Path, name: &str, content: &str) {
        fs::write(dir.join(name), content).unwrap();
    }

    #[tokio::test]
    async fn scan_empty_directory() {
        let tmp = TempDir::new().unwrap();
        let result = run_scan(
            tmp.path(),
            ScanOptions {
                concurrency: 1,
                offline: true,
            },
        )
        .await
        .unwrap();
        assert_eq!(result.packages.len(), 0);
        assert_eq!(result.manifest_files.len(), 0);
    }

    #[tokio::test]
    async fn scan_package_json_offline() {
        let tmp = TempDir::new().unwrap();
        write_file(
            tmp.path(),
            "package.json",
            r#"{"dependencies":{"lodash":"^4.17.21","express":"~4.18.0"}}"#,
        );
        let result = run_scan(
            tmp.path(),
            ScanOptions {
                concurrency: 1,
                offline: true,
            },
        )
        .await
        .unwrap();
        assert_eq!(result.packages.len(), 2);
        assert_eq!(result.manifest_files.len(), 1);
        // Offline: licenses are unknown
        for pkg in &result.packages {
            assert_eq!(pkg.classification, LicenseClass::Unknown);
        }
    }

    #[tokio::test]
    async fn scan_sbom_file() {
        let tmp = TempDir::new().unwrap();
        let bom = r#"{
            "bomFormat": "CycloneDX",
            "components": [
                {"name":"lodash","version":"4.17.21","purl":"pkg:npm/lodash@4.17.21",
                 "licenses":[{"license":{"id":"MIT"}}]}
            ]
        }"#;
        write_file(tmp.path(), "bom.json", bom);
        let result = run_scan(
            tmp.path(),
            ScanOptions {
                concurrency: 1,
                offline: true,
            },
        )
        .await
        .unwrap();
        assert_eq!(result.sbom_files.len(), 1);
        assert_eq!(result.packages.len(), 1);
        assert_eq!(result.packages[0].spdx_license.as_deref(), Some("MIT"));
        assert_eq!(result.packages[0].classification, LicenseClass::Permissive);
    }

    #[tokio::test]
    async fn skip_node_modules() {
        let tmp = TempDir::new().unwrap();
        let nm = tmp.path().join("node_modules").join("some-lib");
        fs::create_dir_all(&nm).unwrap();
        write_file(
            &nm,
            "package.json",
            r#"{"dependencies":{"lodash":"^4.0.0"}}"#,
        );
        // Root has no manifest — only the nested one inside node_modules
        let result = run_scan(
            tmp.path(),
            ScanOptions {
                concurrency: 1,
                offline: true,
            },
        )
        .await
        .unwrap();
        // node_modules is skipped, so nothing should be found
        assert_eq!(result.packages.len(), 0);
    }

    #[test]
    fn format_report_non_empty() {
        use chrono::Utc;
        let result = ScanResult {
            id: "test-id".to_string(),
            path: "/tmp/foo".to_string(),
            scanned_at: Utc::now(),
            packages: vec![ScannedPackage {
                name: "lodash".to_string(),
                version: "4.17.21".to_string(),
                ecosystem: Ecosystem::Npm,
                raw_license: Some("MIT".to_string()),
                spdx_license: Some("MIT".to_string()),
                classification: LicenseClass::Permissive,
                source: "manifest".to_string(),
            }],
            manifest_files: vec!["/tmp/foo/package.json".to_string()],
            sbom_files: vec![],
        };
        let report = format_report(&result);
        assert!(report.contains("lodash"));
        assert!(report.contains("MIT"));
        assert!(report.contains("permissive"));
    }
}