feluda 1.14.0

A CLI tool to check dependency licenses.
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
use cargo_metadata::{Metadata, Package, PackageId};
use rayon::prelude::*;
use std::collections::{BTreeSet, HashMap, HashSet, VecDeque};

use crate::debug::{log, log_debug, log_error, LogLevel};
use crate::licenses::{
    detect_license_from_content, detect_license_in_dir, fetch_licenses_from_github,
    is_license_restrictive, LicenseCompatibility, LicenseInfo,
};

/// Analyze the licenses of Rust dependencies from Cargo packages
#[allow(dead_code)]
pub fn analyze_rust_licenses(packages: Vec<Package>) -> Vec<LicenseInfo> {
    let config = crate::config::load_config().unwrap_or_default();
    analyze_rust_licenses_with_config(packages, &config, false)
}

/// Analyze Rust deps with full Metadata so workspace members can be attributed.
///
/// In a multi-member Cargo workspace, every dependency is tagged with the workspace
/// member(s) that pull it in, and workspace members themselves are excluded from the
/// dep report. Single-crate projects fall through to the existing behavior.
pub fn analyze_rust_licenses_with_metadata(
    metadata: Metadata,
    config: &crate::config::FeludaConfig,
    no_local: bool,
) -> Vec<LicenseInfo> {
    let workspace_members: HashSet<PackageId> =
        metadata.workspace_members.iter().cloned().collect();
    let is_workspace = workspace_members.len() > 1;

    log(
        LogLevel::Info,
        &format!(
            "Cargo metadata: {} workspace members, {} total packages",
            workspace_members.len(),
            metadata.packages.len()
        ),
    );

    if !is_workspace {
        log(
            LogLevel::Info,
            "Single-crate project; no workspace attribution",
        );
        return analyze_rust_licenses_with_config(metadata.packages, config, no_local);
    }

    let attribution = build_workspace_attribution(&metadata, &workspace_members);
    log_debug("Workspace attribution map", &attribution);

    let dep_packages: Vec<Package> = metadata
        .packages
        .into_iter()
        .filter(|p| !workspace_members.contains(&p.id))
        .collect();

    log(
        LogLevel::Info,
        &format!(
            "Analyzing {} non-workspace deps across workspace",
            dep_packages.len()
        ),
    );

    let mut infos = analyze_rust_licenses_with_config(dep_packages, config, no_local);
    for info in &mut infos {
        if let Some(member_names) = attribution.get(&(info.name.clone(), info.version.clone())) {
            if !member_names.is_empty() {
                info.sub_project =
                    Some(member_names.iter().cloned().collect::<Vec<_>>().join(", "));
            }
        }
    }
    infos
}

/// Build a map from (dep name, version) -> set of workspace member names that depend on it.
fn build_workspace_attribution(
    metadata: &Metadata,
    workspace_members: &HashSet<PackageId>,
) -> HashMap<(String, String), BTreeSet<String>> {
    let mut attribution: HashMap<(String, String), BTreeSet<String>> = HashMap::new();

    let resolve = match &metadata.resolve {
        Some(r) => r,
        None => {
            log(LogLevel::Warn, "No resolve graph in cargo metadata");
            return attribution;
        }
    };

    let nodes_by_id: HashMap<&PackageId, &cargo_metadata::Node> =
        resolve.nodes.iter().map(|n| (&n.id, n)).collect();
    let pkg_by_id: HashMap<&PackageId, &Package> =
        metadata.packages.iter().map(|p| (&p.id, p)).collect();

    for member_id in workspace_members {
        let member_name = match pkg_by_id.get(member_id) {
            Some(p) => p.name.to_string(),
            None => continue,
        };

        let mut visited: HashSet<&PackageId> = HashSet::new();
        let mut queue: VecDeque<&PackageId> = VecDeque::new();
        queue.push_back(member_id);
        visited.insert(member_id);

        while let Some(id) = queue.pop_front() {
            let node = match nodes_by_id.get(id) {
                Some(n) => *n,
                None => continue,
            };
            for dep_id in &node.dependencies {
                if !visited.insert(dep_id) {
                    continue;
                }
                queue.push_back(dep_id);
                if workspace_members.contains(dep_id) {
                    continue;
                }
                if let Some(pkg) = pkg_by_id.get(dep_id) {
                    attribution
                        .entry((pkg.name.to_string(), pkg.version.to_string()))
                        .or_default()
                        .insert(member_name.clone());
                }
            }
        }
    }

    attribution
}

pub fn analyze_rust_licenses_with_config(
    packages: Vec<Package>,
    config: &crate::config::FeludaConfig,
    no_local: bool,
) -> Vec<LicenseInfo> {
    if packages.is_empty() {
        log(
            LogLevel::Warn,
            "No Rust packages found for license analysis",
        );
        return vec![];
    }

    log(
        LogLevel::Info,
        &format!("Analyzing licenses for {} Rust packages", packages.len()),
    );

    let known_licenses = match fetch_licenses_from_github() {
        Ok(licenses) => {
            log(
                LogLevel::Info,
                &format!("Fetched {} known licenses from GitHub", licenses.len()),
            );
            licenses
        }
        Err(err) => {
            log_error("Failed to fetch licenses from GitHub", &err);
            HashMap::new()
        }
    };

    packages
        .par_iter()
        .map(|package| {
            log(
                LogLevel::Info,
                &format!("Analyzing package: {} ({})", package.name, package.version),
            );

            let license = package.license.clone().or_else(|| {
                if no_local {
                    None
                } else {
                    get_license_from_manifest(&package.manifest_path)
                }
            });

            let is_restrictive = is_license_restrictive(&license, &known_licenses, config.strict);

            if is_restrictive {
                log(
                    LogLevel::Warn,
                    &format!(
                        "Restrictive license found: {:?} for {}",
                        license, package.name
                    ),
                );
            }

            LicenseInfo {
                name: package.name.to_string(),
                version: package.version.to_string(),
                license,
                is_restrictive,
                compatibility: LicenseCompatibility::Unknown,
                osi_status: match &package.license {
                    Some(license) => crate::licenses::get_osi_status(license),
                    None => crate::licenses::OsiStatus::Unknown,
                },
                sub_project: None,
            }
        })
        .collect()
}

fn get_license_from_manifest<P: AsRef<std::path::Path>>(manifest_path: P) -> Option<String> {
    use std::fs;
    use toml::Value;

    let manifest_path = manifest_path.as_ref();

    log(
        crate::debug::LogLevel::Info,
        &format!("Checking manifest for license: {}", manifest_path.display()),
    );

    if !manifest_path.exists() {
        return None;
    }

    let content = fs::read_to_string(manifest_path).ok()?;
    let manifest = toml::from_str::<Value>(&content).ok()?;
    let package = manifest.get("package");

    // 1. Explicit SPDX expression in the `license` field.
    if let Some(license) = package
        .and_then(|pkg| pkg.get("license"))
        .and_then(|license| license.as_str())
    {
        log(
            crate::debug::LogLevel::Info,
            &format!("Found license in manifest: {license}"),
        );
        return Some(license.to_string());
    }

    let crate_dir = manifest_path.parent();

    // 2. `license-file` field: a relative path to a bundled license text, which may use a
    //    non-standard filename (e.g. `LICENSE-MIT`), so read it directly and content-detect.
    if let (Some(dir), Some(rel)) = (
        crate_dir,
        package
            .and_then(|pkg| pkg.get("license-file"))
            .and_then(|license_file| license_file.as_str()),
    ) {
        if let Ok(text) = fs::read_to_string(dir.join(rel)) {
            if let Some(spdx) = detect_license_from_content(&text) {
                log(
                    crate::debug::LogLevel::Info,
                    &format!("Detected {spdx} license from license-file: {rel}"),
                );
                return Some(spdx);
            }
        }
    }

    // 3. Probe conventional license files (LICENSE, COPYING, …) in the crate root.
    if let Some(spdx) = crate_dir.and_then(detect_license_in_dir) {
        log(
            crate::debug::LogLevel::Info,
            &format!("Detected {spdx} license from crate license file"),
        );
        return Some(spdx);
    }

    None
}

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

    fn setup() -> TempDir {
        tempfile::tempdir().unwrap()
    }

    #[test]
    fn test_analyze_rust_licenses_empty() {
        let packages = vec![];
        let result = analyze_rust_licenses(packages);
        assert!(result.is_empty());
    }

    #[test]
    fn test_license_restrictive_with_default_config() {
        temp_env::with_var("FELUDA_LICENSES_RESTRICTIVE", None::<&str>, || {
            let dir = setup();
            std::env::set_current_dir(dir.path()).unwrap();

            let known_licenses = HashMap::new();
            assert!(is_license_restrictive(
                &Some("GPL-3.0".to_string()),
                &known_licenses,
                false
            ));
            assert!(!is_license_restrictive(
                &Some("MIT".to_string()),
                &known_licenses,
                false
            ));
        });
    }

    #[test]
    fn test_license_restrictive_no_license() {
        temp_env::with_var("FELUDA_LICENSES_RESTRICTIVE", None::<&str>, || {
            let dir = setup();
            std::env::set_current_dir(dir.path()).unwrap();

            let known_licenses = HashMap::new();
            assert!(is_license_restrictive(
                &Some("No License".to_string()),
                &known_licenses,
                false
            ));
        });
    }

    #[test]
    fn test_get_license_from_manifest() {
        let temp_dir = TempDir::new().unwrap();
        let manifest_path = temp_dir.path().join("Cargo.toml");

        let manifest_content = r#"[package]
name = "test-crate"
version = "0.1.0"
license = "MIT"
"#;

        std::fs::write(&manifest_path, manifest_content).unwrap();

        let result = get_license_from_manifest(&manifest_path);
        assert_eq!(result, Some("MIT".to_string()));
    }

    #[test]
    fn test_get_license_from_manifest_apache() {
        let temp_dir = TempDir::new().unwrap();
        let manifest_path = temp_dir.path().join("Cargo.toml");

        let manifest_content = r#"[package]
name = "test-crate"
version = "0.1.0"
license = "Apache-2.0"
"#;

        std::fs::write(&manifest_path, manifest_content).unwrap();

        let result = get_license_from_manifest(&manifest_path);
        assert_eq!(result, Some("Apache-2.0".to_string()));
    }

    #[test]
    fn test_get_license_from_manifest_missing() {
        let temp_dir = TempDir::new().unwrap();
        let manifest_path = temp_dir.path().join("Cargo.toml");

        let manifest_content = r#"[package]
name = "test-crate"
version = "0.1.0"
"#;

        std::fs::write(&manifest_path, manifest_content).unwrap();

        let result = get_license_from_manifest(&manifest_path);
        assert_eq!(result, None);
    }

    #[test]
    fn test_get_license_from_manifest_not_found() {
        let temp_dir = TempDir::new().unwrap();
        let manifest_path = temp_dir.path().join("nonexistent.toml");

        let result = get_license_from_manifest(&manifest_path);
        assert_eq!(result, None);
    }

    #[test]
    fn test_get_license_from_manifest_license_file_field() {
        let temp_dir = TempDir::new().unwrap();
        let manifest_path = temp_dir.path().join("Cargo.toml");

        // A crate with no `license` field, only a `license-file` pointing at a
        // non-standard filename — previously this resolved to "No License".
        let manifest_content = r#"[package]
name = "test-crate"
version = "0.1.0"
license-file = "LICENSE-MIT"
"#;
        std::fs::write(&manifest_path, manifest_content).unwrap();
        std::fs::write(
            temp_dir.path().join("LICENSE-MIT"),
            "MIT License\n\nPermission is hereby granted, free of charge, to any person",
        )
        .unwrap();

        let result = get_license_from_manifest(&manifest_path);
        assert_eq!(result, Some("MIT".to_string()));
    }

    #[test]
    fn test_get_license_from_manifest_crate_dir_fallback() {
        let temp_dir = TempDir::new().unwrap();
        let manifest_path = temp_dir.path().join("Cargo.toml");

        // No `license` and no `license-file` field, but a conventional LICENSE file
        // ships in the crate root.
        let manifest_content = r#"[package]
name = "test-crate"
version = "0.1.0"
"#;
        std::fs::write(&manifest_path, manifest_content).unwrap();
        std::fs::write(
            temp_dir.path().join("LICENSE"),
            "Apache License\nVersion 2.0, January 2004",
        )
        .unwrap();

        let result = get_license_from_manifest(&manifest_path);
        assert_eq!(result, Some("Apache-2.0".to_string()));
    }

    #[test]
    fn test_get_license_from_manifest_license_field_wins_over_file() {
        let temp_dir = TempDir::new().unwrap();
        let manifest_path = temp_dir.path().join("Cargo.toml");

        // When both are present the explicit SPDX expression takes precedence.
        let manifest_content = r#"[package]
name = "test-crate"
version = "0.1.0"
license = "MIT"
license-file = "LICENSE"
"#;
        std::fs::write(&manifest_path, manifest_content).unwrap();
        std::fs::write(
            temp_dir.path().join("LICENSE"),
            "Apache License\nVersion 2.0, January 2004",
        )
        .unwrap();

        let result = get_license_from_manifest(&manifest_path);
        assert_eq!(result, Some("MIT".to_string()));
    }
}