diff --git a/.codex/hooks.json b/.codex/hooks.json new file mode 100644 index 0000000..9884114 --- /dev/null +++ b/.codex/hooks.json @@ -0,0 +1,14 @@ +{ + "hooks": { + "Stop": [ + { + "hooks": [ + { + "type": "command", + "command": "./harness post-edit codex" + } + ] + } + ] + } +} diff --git a/Cargo.lock b/Cargo.lock index 69fd9e9..2b9c8e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -362,6 +362,7 @@ dependencies = [ "serde", "serde_derive", "serde_json", + "serde_yaml_ng", "tempfile", "termcolor", "tokio", @@ -1823,6 +1824,19 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_yaml_ng" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b4db627b98b36d4203a7b458cf3573730f2bb591b28871d916dfa9efabfd41f" +dependencies = [ + "indexmap", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + [[package]] name = "sha1" version = "0.10.6" @@ -2233,6 +2247,12 @@ version = "1.0.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f63a545481291138910575129486daeaf8ac54aee4387fe7906919f7830c7d9d" +[[package]] +name = "unsafe-libyaml" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" + [[package]] name = "url" version = "2.5.7" diff --git a/Cargo.toml b/Cargo.toml index dd14348..d60edad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,6 +41,7 @@ http-body-util = "0.1" url = "2.5" open = "5.0" urlencoding = "2.1" +serde_yaml_ng = "0.10" [target.'cfg(not(target_os = "windows"))'.dependencies] openssl = { version = "0.10", features = ["vendored"] } diff --git a/README.md b/README.md index b242ebe..03b116d 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,29 @@ Once the binary is installed, login with your token from the Corgea app. corgea login ``` +## Dependency Inventory (offline) + +`corgea deps` builds a dependency inventory from npm, Python, and Java manifests +and lockfiles, then evaluates a pinning policy (DEP rules). Runs fully offline — +no token or network required. + +```bash +corgea deps scan # table report for the current directory +corgea deps scan --format agent # compact TSV for coding agents +corgea deps scan --format json # JSON inventory on stdout +corgea deps scan --format quiet # no stdout; exit code still applies +corgea deps scan --fail-on high # exit 1 if any finding is >= high +corgea deps scan --out-format json # machine-readable (json or sarif) +corgea deps graph --format json # print the resolved dependency graph +corgea deps explain --format agent # show why a package is present +corgea deps diff --base origin/main --format json +corgea deps sbom --format cyclonedx # emit a CycloneDX SBOM +corgea deps policy init --exist-ok # write starter policy, or keep existing file +``` + +`corgea deps` defaults to `--format agent` when an agent environment is detected (`AI_AGENT`, `CODEX_SANDBOX`, `CLAUDECODE`, and related agent variables). Use `--format human` to force the normal terminal output. + +See [Dependency Scanning (CLI)](https://docs.corgea.app/cli/deps) for the full flag and exit-code reference. ## Development Setup diff --git a/examples/deps_skill.rs b/examples/deps_skill.rs new file mode 100644 index 0000000..f4a5b91 --- /dev/null +++ b/examples/deps_skill.rs @@ -0,0 +1,31 @@ +use std::path::Path; +use std::process::ExitCode; + +use corgea::deps::skill::{check_skill_file, generated_marked_section, update_skill_file}; + +const SKILL_PATH: &str = "skills/corgea/SKILL.md"; + +fn main() -> ExitCode { + let mode = std::env::args() + .nth(1) + .unwrap_or_else(|| "print".to_string()); + let skill_path = Path::new(SKILL_PATH); + + let result = match mode.as_str() { + "print" => { + println!("{}", generated_marked_section()); + Ok(()) + } + "check" => check_skill_file(skill_path), + "update" => update_skill_file(skill_path), + _ => Err("usage: cargo run --example deps_skill -- [print|check|update]".to_string()), + }; + + match result { + Ok(()) => ExitCode::SUCCESS, + Err(message) => { + eprintln!("{message}"); + ExitCode::FAILURE + } + } +} diff --git a/harness b/harness index 61cc0b1..84b5076 100755 --- a/harness +++ b/harness @@ -3,7 +3,8 @@ # Usage: ./harness [--verbose] [--min=N] # # Commands: check, fix, lint, test, audit, coverage, pre-commit, ci, -# post-edit, setup-hooks, suppressions +# post-edit, setup-hooks, suppressions, deps-skill +# Use `./harness post-edit codex` for Codex hooks that require JSON-safe stdout. set -u @@ -97,6 +98,20 @@ run_with_summary() { return 0 } +json_escape() { + local value="$1" + value="${value//\\/\\\\}" + value="${value//\"/\\\"}" + value="${value//$'\n'/\\n}" + value="${value//$'\r'/\\r}" + value="${value//$'\t'/\\t}" + printf "%s" "$value" +} + +codex_system_message() { + printf '{"systemMessage":"%s"}\n' "$(json_escape "$1")" +} + # ── Git helpers ───────────────────────────────────────────────────── staged_rs_files() { @@ -160,6 +175,10 @@ cmd_test() { run_with_summary "Tests" 0 -- cargo test } +cmd_deps_skill() { + run "Deps skill drift" 0 -- cargo run --example deps_skill -- check +} + cmd_audit() { _cmd_audit_inner 0 } @@ -192,8 +211,49 @@ cmd_coverage() { } cmd_post_edit() { + local codex=0 + local hook_input="" + local arg + for arg in "$@"; do + case "$arg" in + codex) codex=1 ;; + "") + ;; + *) + printf "Unknown post-edit option: %s\n" "$arg" >&2 + return 1 + ;; + esac + done + + if [ ! -t 0 ]; then + hook_input="$(cat || true)" + if printf "%s" "$hook_input" | grep -qE '"hook_event_name"[[:space:]]*:'; then + codex=1 + fi + fi + local changed; changed="$(changed_rs_files)" [ -z "$changed" ] && return 0 + + if [ "$codex" -eq 1 ]; then + local tmp; tmp="$(mktemp)" + cargo fmt >"$tmp" 2>&1 + local rc=$? + local output; output="$(tail -40 "$tmp")" + rm -f "$tmp" + + [ "$rc" -eq 0 ] && return 0 + + if [ -n "$output" ]; then + codex_system_message "cargo fmt failed: +$output" + else + codex_system_message "cargo fmt failed without output." + fi + return 0 + fi + # Never fail the Stop hook. run "Format" 1 -- cargo fmt || true return 0 @@ -226,6 +286,8 @@ cmd_check() { [ $? -eq 0 ] && passed=$(( passed + 1 )) || failed=$(( failed + 1 )) run_with_summary "Tests" 1 -- cargo test [ $? -eq 0 ] && passed=$(( passed + 1 )) || failed=$(( failed + 1 )) + run "Deps skill drift" 1 -- cargo run --example deps_skill -- check + [ $? -eq 0 ] && passed=$(( passed + 1 )) || failed=$(( failed + 1 )) cmd_suppressions @@ -278,13 +340,15 @@ case "$cmd" in coverage) cmd_coverage ;; pre-commit) cmd_pre_commit ;; ci) cmd_ci ;; - post-edit) cmd_post_edit ;; + post-edit) cmd_post_edit "${@:2}" ;; setup-hooks) cmd_setup_hooks ;; suppressions) cmd_suppressions ;; + deps-skill) cmd_deps_skill ;; -h|--help|help) printf "Usage: ./harness [--verbose] [--min=N]\n\n" printf "Commands: check, fix, lint, test, audit, coverage, pre-commit,\n" - printf " ci, post-edit, setup-hooks, suppressions\n" + printf " ci, post-edit, setup-hooks, suppressions, deps-skill\n" + printf "\nCodex hook mode: ./harness post-edit codex\n" ;; *) printf "Unknown command: %s\n" "$cmd" >&2 diff --git a/skills/corgea/SKILL.md b/skills/corgea/SKILL.md index 2429d9c..f23293f 100644 --- a/skills/corgea/SKILL.md +++ b/skills/corgea/SKILL.md @@ -109,6 +109,28 @@ corgea setup-hooks --default-config # Default: secrets + PII, fail on Installs a pre-commit hook running `corgea scan blast --only-uncommitted`. Bypass with `git commit --no-verify`. + +### Deps — `corgea deps ` + +Offline dependency inventory and policy checks. No Corgea token or network required. +Agent environments default to compact TSV; force output with `--format human|agent|json|quiet`. + +- `corgea deps scan [PATH]` — Scan manifests and lockfiles, build inventory, evaluate policy. Flags: `--fail-on`, `--out-format`, `--out-file`, `--format` + Examples: `corgea deps scan --format agent`; `corgea deps scan --format quiet --fail-on high` +- `corgea deps graph [PATH]` — Print the dependency graph. Flags: `--format` + Examples: `corgea deps graph --format agent`; `corgea deps graph tests/fixtures/node-app --format json` +- `corgea deps explain [PATH]` — Explain why a package is present. Flags: `--format` + Examples: `corgea deps explain lodash --format agent`; `corgea deps explain left-pad tests/fixtures/node-app --format json` +- `corgea deps diff --base [PATH]` — Compare dependency graph against a git ref. Flags: `--base`, `--fail-on-new`, `--format` + Examples: `corgea deps diff --base origin/main --format json`; `corgea deps diff --base HEAD . --fail-on-new high` +- `corgea deps sbom [PATH]` — Generate an SBOM. Flags: `--format`, `--out` + Examples: `corgea deps sbom --format cyclonedx`; `corgea deps sbom --format cyclonedx --out bom.json` +- `corgea deps policy init [PATH]` — Write a starter `.corgea/deps.yml` policy file. Flags: `--exist-ok`, `--format` + Examples: `corgea deps policy init`; `corgea deps policy init --exist-ok --format quiet` + +Notes: `deps scan --out-format table|json|sarif` is the report/export selector; do not combine it with `deps scan --format`. + + ## Common Workflows ### Scan full project diff --git a/src/deps/detect.rs b/src/deps/detect.rs new file mode 100644 index 0000000..bf3636c --- /dev/null +++ b/src/deps/detect.rs @@ -0,0 +1,103 @@ +use std::path::{Path, PathBuf}; + +use crate::deps::model::Ecosystem; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub enum DepFileKind { + NpmManifest, + NpmLockfile, + YarnLockfile, + PnpmLockfile, + PipRequirements, + PipConstraints, + PyProject, + PoetryLock, + UvLock, + MavenPom, + GradleBuild, + GradleLockfile, + GoMod, + GoSum, + CargoManifest, + CargoLock, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct DetectedFile { + pub path: PathBuf, + pub kind: DepFileKind, + pub ecosystem: Ecosystem, +} + +const SKIP_DIRS: &[&str] = &[ + "node_modules", + ".git", + "vendor", + "target", + ".venv", + "venv", + "__pycache__", + "dist", + "build", +]; + +/// Recursively detect supported dependency files; skip vendored/VCS dirs. +pub fn detect_dependency_files(root: &Path) -> Vec { + let mut out = Vec::new(); + detect_recursive(root, &mut out); + out.sort_by(|a, b| a.path.cmp(&b.path)); + out +} + +fn detect_recursive(dir: &Path, out: &mut Vec) { + let entries = match std::fs::read_dir(dir) { + Ok(e) => e, + Err(_) => return, + }; + + for entry in entries.flatten() { + let path = entry.path(); + let file_name = entry.file_name(); + let name = file_name.to_string_lossy(); + + if path.is_dir() { + if SKIP_DIRS.iter().any(|s| name == *s) { + continue; + } + detect_recursive(&path, out); + continue; + } + + if let Some(detected) = classify_file(&path) { + out.push(detected); + } + } +} + +fn classify_file(path: &Path) -> Option { + let name = path.file_name()?.to_string_lossy(); + let kind_eco = match name.as_ref() { + "package.json" => (DepFileKind::NpmManifest, Ecosystem::Npm), + "package-lock.json" | "npm-shrinkwrap.json" => (DepFileKind::NpmLockfile, Ecosystem::Npm), + "yarn.lock" => (DepFileKind::YarnLockfile, Ecosystem::Npm), + "pnpm-lock.yaml" => (DepFileKind::PnpmLockfile, Ecosystem::Npm), + "requirements.txt" => (DepFileKind::PipRequirements, Ecosystem::PyPI), + "constraints.txt" => (DepFileKind::PipConstraints, Ecosystem::PyPI), + "pyproject.toml" => (DepFileKind::PyProject, Ecosystem::PyPI), + "poetry.lock" => (DepFileKind::PoetryLock, Ecosystem::PyPI), + "uv.lock" => (DepFileKind::UvLock, Ecosystem::PyPI), + "pom.xml" => (DepFileKind::MavenPom, Ecosystem::Maven), + "build.gradle" | "build.gradle.kts" => (DepFileKind::GradleBuild, Ecosystem::Maven), + "gradle.lockfile" => (DepFileKind::GradleLockfile, Ecosystem::Maven), + "go.mod" => (DepFileKind::GoMod, Ecosystem::Go), + "go.sum" => (DepFileKind::GoSum, Ecosystem::Go), + "Cargo.toml" => (DepFileKind::CargoManifest, Ecosystem::Cargo), + "Cargo.lock" => (DepFileKind::CargoLock, Ecosystem::Cargo), + _ => return None, + }; + Some(DetectedFile { + path: path.to_path_buf(), + kind: kind_eco.0, + ecosystem: kind_eco.1, + }) +} diff --git a/src/deps/diff.rs b/src/deps/diff.rs new file mode 100644 index 0000000..4db62f8 --- /dev/null +++ b/src/deps/diff.rs @@ -0,0 +1,151 @@ +use std::collections::{BTreeMap, HashSet}; + +use crate::deps::model::{DependencyGraph, DependencyNode}; + +#[derive(Debug)] +pub struct VersionChange { + pub name: String, + pub from: String, + pub to: String, +} + +#[derive(Debug)] +pub struct ArtifactChange { + pub name: String, + pub version: String, + pub old_resolved: Option, + pub new_resolved: Option, + pub old_integrity: Option, + pub new_integrity: Option, +} + +#[derive(Debug)] +pub struct GraphDiff { + pub added: Vec, + pub removed: Vec, + pub changed: Vec, + pub artifact_changed: Vec, +} + +pub fn diff_graphs(base: &DependencyGraph, head: &DependencyGraph) -> GraphDiff { + let mut base_map: BTreeMap = BTreeMap::new(); + for n in &base.nodes { + if n.version.is_some() { + base_map.insert(n.id().0.clone(), n); + } + } + let mut head_map: BTreeMap = BTreeMap::new(); + for n in &head.nodes { + if n.version.is_some() { + head_map.insert(n.id().0.clone(), n); + } + } + + let mut added = Vec::new(); + let mut artifact_changed = Vec::new(); + for n in &head.nodes { + if n.version.is_none() { + continue; + } + match base_map.get(&n.id().0) { + None => added.push(n.clone()), + Some(old) if artifact_metadata_changed(old, n) => { + if let Some(version) = &n.version { + artifact_changed.push(ArtifactChange { + name: n.name.clone(), + version: version.clone(), + old_resolved: old.lock_resolved.clone(), + new_resolved: n.lock_resolved.clone(), + old_integrity: old.lock_integrity_hash.clone(), + new_integrity: n.lock_integrity_hash.clone(), + }); + } + } + _ => {} + } + } + + let mut removed = Vec::new(); + for n in &base.nodes { + if n.version.is_some() && !head_map.contains_key(&n.id().0) { + removed.push(n.clone()); + } + } + + let (added, removed, changed) = pair_version_changes(added, removed); + + GraphDiff { + added, + removed, + changed, + artifact_changed, + } +} + +fn artifact_metadata_changed(base: &DependencyNode, head: &DependencyNode) -> bool { + base.lock_resolved != head.lock_resolved || base.lock_integrity_hash != head.lock_integrity_hash +} + +fn pair_version_changes( + added: Vec, + removed: Vec, +) -> (Vec, Vec, Vec) { + let mut added_by_package: BTreeMap> = BTreeMap::new(); + for (idx, node) in added.iter().enumerate() { + added_by_package + .entry(versionless_identity(node)) + .or_default() + .push(idx); + } + + let mut removed_by_package: BTreeMap> = BTreeMap::new(); + for (idx, node) in removed.iter().enumerate() { + removed_by_package + .entry(versionless_identity(node)) + .or_default() + .push(idx); + } + + let mut paired_added = HashSet::new(); + let mut paired_removed = HashSet::new(); + let mut changed = Vec::new(); + for (key, removed_indices) in &removed_by_package { + let Some(added_indices) = added_by_package.get(key) else { + continue; + }; + if removed_indices.len() != 1 || added_indices.len() != 1 { + continue; + } + let removed_idx = removed_indices[0]; + let added_idx = added_indices[0]; + let old = &removed[removed_idx]; + let new = &added[added_idx]; + changed.push(VersionChange { + name: new.name().to_string(), + from: old.version().unwrap_or("").to_string(), + to: new.version().unwrap_or("").to_string(), + }); + paired_removed.insert(removed_idx); + paired_added.insert(added_idx); + } + + let added = added + .into_iter() + .enumerate() + .filter_map(|(idx, node)| (!paired_added.contains(&idx)).then_some(node)) + .collect(); + let removed = removed + .into_iter() + .enumerate() + .filter_map(|(idx, node)| (!paired_removed.contains(&idx)).then_some(node)) + .collect(); + (added, removed, changed) +} + +fn versionless_identity(node: &DependencyNode) -> String { + node.id() + .0 + .rsplit_once('@') + .map(|(package, _)| package.to_string()) + .unwrap_or_else(|| node.id().0.clone()) +} diff --git a/src/deps/ecosystems/evaluate.rs b/src/deps/ecosystems/evaluate.rs new file mode 100644 index 0000000..6c76a5f --- /dev/null +++ b/src/deps/ecosystems/evaluate.rs @@ -0,0 +1,304 @@ +use std::collections::{HashMap, HashSet}; +use std::path::{Path, PathBuf}; + +use serde_json::Value; + +use crate::deps::detect::{DepFileKind, DetectedFile}; +use crate::deps::ecosystems::classify_constraint; +use crate::deps::findings::Finding; +use crate::deps::model::{ + ConstraintKind, DependencyGraph, DependencyNode, Ecosystem, PackageId, Severity, SourceType, +}; +use crate::deps::policy::Policy; +use crate::deps::DepsError; + +pub struct ScanContext<'a> { + pub root: &'a Path, + pub policy: &'a Policy, + pub detected: &'a [DetectedFile], + pub graph: &'a mut DependencyGraph, + pub findings: &'a mut Vec, +} + +pub fn scan_all(ctx: &mut ScanContext<'_>) -> Result<(), DepsError> { + super::npm::scan_npm_projects(ctx)?; + super::pypi::scan_pypi_projects(ctx)?; + super::maven::scan_maven_projects(ctx)?; + ctx.graph.sort_nodes(); + crate::deps::findings::sort_findings(ctx.findings); + Ok(()) +} + +#[allow(clippy::too_many_arguments)] +pub fn add_pinning_finding( + findings: &mut Vec, + code: &str, + severity: Severity, + title: &str, + package: Option, + source_file: &str, + declared: Option<&str>, + resolved: Option<&str>, + reproducible: bool, + recommendation: &str, +) { + findings.push(Finding { + id: code.into(), + severity, + title: title.into(), + package, + source_file: source_file.into(), + declared_constraint: declared.map(str::to_string), + resolved_version: resolved.map(str::to_string), + recommendation: recommendation.into(), + reproducible, + paths: vec![vec![PackageId::root()]], + }); +} + +#[allow(clippy::too_many_arguments)] +pub fn constraint_to_findings( + policy: &Policy, + kind: &ConstraintKind, + is_direct: bool, + _name: &str, + declared: &str, + resolved: Option<&str>, + source_file: &str, + package_id: Option, + reproducible: bool, +) -> Vec { + if !is_direct && reproducible { + return vec![]; + } + + let mut out = Vec::new(); + match kind { + ConstraintKind::Exact => {} + ConstraintKind::BoundedRange if is_direct && policy.warn_on_semver_range => { + add_pinning_finding( + &mut out, + "DEP003", + Severity::Medium, + "Direct dependency uses broad range", + package_id, + source_file, + Some(declared), + resolved, + reproducible, + "Pin to the resolved version or allow by policy because the lockfile resolves it.", + ); + } + ConstraintKind::BoundedRange => {} + ConstraintKind::Unbounded + if is_direct && (policy.fail_on_wildcard || policy.fail_on_latest) => + { + add_pinning_finding( + &mut out, + "DEP004", + Severity::High, + "Wildcard or latest dependency", + package_id, + source_file, + Some(declared), + resolved, + reproducible, + "Pin to an exact version instead of using wildcard, latest, or unbounded ranges.", + ); + } + ConstraintKind::Mutable if is_direct && policy.fail_on_mutable_sources => { + add_pinning_finding( + &mut out, + "DEP021", + Severity::High, + "Mutable artifact version", + package_id, + source_file, + Some(declared), + resolved, + false, + "Avoid SNAPSHOT or other mutable artifact versions; pin to an immutable release.", + ); + } + ConstraintKind::GitRef { mutable: true } if is_direct && policy.fail_on_mutable_sources => { + add_pinning_finding( + &mut out, + "DEP005", + Severity::High, + "Mutable Git branch dependency", + package_id, + source_file, + Some(declared), + resolved, + false, + "Pin to a commit SHA or immutable release tag instead of a branch ref.", + ); + } + ConstraintKind::GitRef { .. } => {} + ConstraintKind::Url { checksum: false } if is_direct => { + add_pinning_finding( + &mut out, + "DEP006", + Severity::High, + "URL/tarball dependency without checksum", + package_id, + source_file, + Some(declared), + resolved, + false, + "Add an integrity checksum or pin to a registry package.", + ); + } + ConstraintKind::Url { .. } => {} + _ => {} + } + out +} + +pub fn dep001( + findings: &mut Vec, + policy: &Policy, + source_file: &str, + ecosystem_label: &str, +) { + if policy.fail_on_missing_lockfile { + add_pinning_finding( + findings, + "DEP001", + Severity::High, + "Missing lockfile", + None, + source_file, + None, + None, + false, + &format!( + "Generate a {ecosystem_label} lockfile and commit it for reproducible installs." + ), + ); + } +} + +pub fn dep002(findings: &mut Vec, policy: &Policy, manifest_file: &str, missing: &str) { + if policy.fail_on_stale_lockfile { + add_pinning_finding( + findings, + "DEP002", + Severity::High, + "Stale lockfile", + None, + manifest_file, + Some(missing), + None, + false, + &format!( + "Regenerate the lockfile — `{missing}` is declared in the manifest but missing from the lockfile." + ), + ); + } +} + +pub fn dep019_unsupported_lockfile( + findings: &mut Vec, + source_file: &str, + ecosystem_label: &str, +) { + add_pinning_finding( + findings, + "DEP019", + Severity::Medium, + "Unsupported lockfile", + None, + source_file, + None, + None, + false, + &format!( + "{ecosystem_label} lockfile support is not implemented yet; use a supported lockfile or wait for parser support." + ), + ); +} + +pub fn dep008(findings: &mut Vec, policy: &Policy, node: &DependencyNode) { + if !policy.require_integrity_hashes { + return; + } + if node.lock_integrity == Some(false) { + add_pinning_finding( + findings, + "DEP008", + Severity::Medium, + "Lockfile integrity hash missing", + Some(node.id.clone()), + node.lockfile.as_deref().unwrap_or("lockfile"), + node.declared_constraint.as_deref(), + node.version.as_deref(), + true, + "Add an integrity hash to the lockfile entry for this package.", + ); + } +} + +pub fn read_json(path: &Path) -> Result { + let content = std::fs::read_to_string(path) + .map_err(|e| DepsError(format!("read {}: {e}", path.display())))?; + serde_json::from_str(&content) + .map_err(|e| DepsError(format!("parse JSON {}: {e}", path.display()))) +} + +pub fn parent_dir(path: &Path) -> PathBuf { + path.parent().unwrap_or(path).to_path_buf() +} + +pub fn has_kind_in_dir(detected: &[DetectedFile], dir: &Path, kind: DepFileKind) -> bool { + detected + .iter() + .any(|f| f.kind == kind && parent_dir(&f.path) == dir) +} + +pub fn file_in_dir(detected: &[DetectedFile], dir: &Path, kind: DepFileKind) -> Option { + detected + .iter() + .find(|f| f.kind == kind && parent_dir(&f.path) == dir) + .map(|f| f.path.clone()) +} + +pub fn source_type_from_declared(declared: &str) -> SourceType { + match classify_constraint(Ecosystem::Npm, declared) { + ConstraintKind::GitRef { mutable: true } => SourceType::GitBranch, + ConstraintKind::GitRef { mutable: false } => SourceType::GitCommit, + ConstraintKind::Url { .. } => SourceType::Url, + _ => SourceType::Registry, + } +} + +pub fn dep014(findings: &mut Vec, graph: &DependencyGraph) { + let mut versions: HashMap> = HashMap::new(); + for n in &graph.nodes { + if let Some(v) = &n.version { + versions + .entry(n.name.clone()) + .or_default() + .insert(v.clone()); + } + } + for (name, vers) in versions { + if vers.len() > 1 { + add_pinning_finding( + findings, + "DEP014", + Severity::Low, + "Duplicate versions of same package", + Some(PackageId::npm(&name, vers.iter().next().unwrap())), + "lockfile", + None, + None, + true, + &format!( + "Multiple versions of {name} present: {}", + vers.iter().cloned().collect::>().join(", ") + ), + ); + } + } +} diff --git a/src/deps/ecosystems/maven.rs b/src/deps/ecosystems/maven.rs new file mode 100644 index 0000000..a5d2469 --- /dev/null +++ b/src/deps/ecosystems/maven.rs @@ -0,0 +1,247 @@ +use std::path::Path; + +use crate::deps::detect::DepFileKind; +use crate::deps::ecosystems::classify_constraint; +use crate::deps::ecosystems::evaluate::{ + constraint_to_findings, dep001, file_in_dir, parent_dir, ScanContext, +}; +use crate::deps::model::{DependencyNode, Ecosystem, PackageId, Scope, SourceType}; +use crate::deps::DepsError; + +pub fn scan_maven_projects(ctx: &mut ScanContext<'_>) -> Result<(), DepsError> { + for f in ctx.detected { + match f.kind { + DepFileKind::MavenPom => { + let dir = parent_dir(&f.path); + scan_maven_pom(ctx, &dir, &f.path)?; + } + DepFileKind::GradleBuild => { + let dir = parent_dir(&f.path); + scan_gradle(ctx, &dir, &f.path)?; + } + _ => {} + } + } + Ok(()) +} + +#[derive(Clone)] +struct MavenDep { + group: String, + artifact: String, + version: String, + scope: Scope, +} + +fn scan_maven_pom(ctx: &mut ScanContext<'_>, dir: &Path, pom_path: &Path) -> Result<(), DepsError> { + let rel = pom_path + .strip_prefix(ctx.root) + .unwrap_or(pom_path) + .display() + .to_string(); + + let content = + std::fs::read_to_string(pom_path).map_err(|e| DepsError(format!("read pom: {e}")))?; + if !content.trim_start().starts_with('<') { + return Err(DepsError(format!( + "parse XML {}: not valid XML", + pom_path.display() + ))); + } + + dep001(ctx.findings, ctx.policy, &rel, "Maven"); + + let deps = parse_pom_dependencies(&content)?; + for dep in deps { + let name = dep.artifact.clone(); + let declared = dep.version.clone(); + let kind = classify_constraint(Ecosystem::Maven, &declared); + let package_id = PackageId::maven(&dep.group, &dep.artifact, &dep.version); + ctx.findings.extend(constraint_to_findings( + ctx.policy, + &kind, + true, + &name, + &declared, + Some(&dep.version), + &rel, + Some(package_id.clone()), + false, + )); + ctx.graph.nodes.push(DependencyNode { + id: package_id, + name, + ecosystem: Ecosystem::Maven, + version: Some(dep.version), + direct: true, + scope: dep.scope, + depth: 1, + source_type: SourceType::Registry, + manifest_file: Some(rel.clone()), + lockfile: None, + declared_constraint: Some(declared), + lock_integrity: None, + lock_resolved: None, + lock_integrity_hash: None, + }); + } + let _ = dir; + Ok(()) +} + +fn parse_pom_dependencies(content: &str) -> Result, DepsError> { + Ok(parse_pom_regex(content)) +} + +fn parse_pom_regex(content: &str) -> Vec { + let mut deps = Vec::new(); + let dep_blocks: Vec<&str> = content.split("").skip(1).collect(); + for block in dep_blocks { + let group = extract_xml_tag(block, "groupId"); + let artifact = extract_xml_tag(block, "artifactId"); + let version = extract_xml_tag(block, "version"); + let scope = extract_xml_tag(block, "scope"); + if artifact.is_empty() { + continue; + } + deps.push(MavenDep { + group, + artifact: artifact.clone(), + version: version.clone(), + scope: if scope == "test" { + Scope::Development + } else { + Scope::Production + }, + }); + } + deps +} + +fn extract_xml_tag(block: &str, tag: &str) -> String { + let open = format!("<{tag}>"); + let close = format!(""); + if let Some(start) = block.find(&open) { + let rest = &block[start + open.len()..]; + if let Some(end) = rest.find(&close) { + return rest[..end].trim().to_string(); + } + } + String::new() +} + +fn scan_gradle(ctx: &mut ScanContext<'_>, dir: &Path, gradle_path: &Path) -> Result<(), DepsError> { + let rel = gradle_path + .strip_prefix(ctx.root) + .unwrap_or(gradle_path) + .display() + .to_string(); + let content = + std::fs::read_to_string(gradle_path).map_err(|e| DepsError(format!("read gradle: {e}")))?; + + let lock_path = file_in_dir(ctx.detected, dir, DepFileKind::GradleLockfile); + let locked = lock_path + .as_ref() + .map(|p| parse_gradle_lockfile(p)) + .transpose()? + .unwrap_or_default(); + + if lock_path.is_none() { + dep001(ctx.findings, ctx.policy, &rel, "Gradle"); + } + + let deps = parse_gradle_deps(&content); + for (coords, declared, scope) in deps { + let parts: Vec<&str> = coords.split(':').collect(); + if parts.len() < 2 { + continue; + } + let group = parts[0]; + let artifact = parts[1]; + let name = artifact.to_string(); + let resolved = locked + .get(&format!("{group}:{artifact}")) + .cloned() + .or_else(|| { + if !declared.contains('+') && !declared.eq_ignore_ascii_case("latest.release") { + Some(declared.clone()) + } else { + locked.get(&format!("{group}:{artifact}")).cloned() + } + }); + let version = resolved.clone().unwrap_or_else(|| declared.clone()); + let kind = classify_constraint(Ecosystem::Maven, &declared); + let reproducible = lock_path.is_some() && resolved.is_some(); + let package_id = PackageId::maven(group, artifact, &version); + ctx.findings.extend(constraint_to_findings( + ctx.policy, + &kind, + true, + &name, + &declared, + resolved.as_deref(), + &rel, + Some(package_id.clone()), + reproducible, + )); + ctx.graph.nodes.push(DependencyNode { + id: package_id, + name, + ecosystem: Ecosystem::Maven, + version: Some(version), + direct: true, + scope, + depth: 1, + source_type: SourceType::Registry, + manifest_file: Some(rel.clone()), + lockfile: lock_path.as_ref().map(|p| p.display().to_string()), + declared_constraint: Some(declared), + lock_integrity: None, + lock_resolved: None, + lock_integrity_hash: None, + }); + } + Ok(()) +} + +fn parse_gradle_deps(content: &str) -> Vec<(String, String, Scope)> { + let mut out = Vec::new(); + for line in content.lines() { + let line = line.trim(); + if line.starts_with("implementation ") || line.starts_with("testImplementation ") { + let scope = if line.starts_with("test") { + Scope::Development + } else { + Scope::Production + }; + if let Some(spec) = line.split('\'').nth(1) { + let parts: Vec<&str> = spec.split(':').collect(); + if parts.len() >= 3 { + let coord = format!("{}:{}", parts[0], parts[1]); + out.push((coord, parts[2].to_string(), scope)); + } + } + } + } + out +} + +fn parse_gradle_lockfile( + path: &Path, +) -> Result, DepsError> { + let content = std::fs::read_to_string(path) + .map_err(|e| DepsError(format!("read gradle.lockfile: {e}")))?; + let mut out = std::collections::HashMap::new(); + for line in content.lines() { + if line.starts_with('#') || line.starts_with("empty=") { + continue; + } + if let Some((coord, _)) = line.split_once('=') { + let parts: Vec<&str> = coord.split(':').collect(); + if parts.len() >= 3 { + out.insert(format!("{}:{}", parts[0], parts[1]), parts[2].to_string()); + } + } + } + Ok(out) +} diff --git a/src/deps/ecosystems/mod.rs b/src/deps/ecosystems/mod.rs new file mode 100644 index 0000000..02de0ce --- /dev/null +++ b/src/deps/ecosystems/mod.rs @@ -0,0 +1,139 @@ +pub mod evaluate; +pub mod maven; +pub mod npm; +pub mod pypi; + +use crate::deps::ecosystems::evaluate::ScanContext; +use crate::deps::DepsError; + +pub fn scan_all(ctx: &mut ScanContext<'_>) -> Result<(), DepsError> { + evaluate::scan_all(ctx) +} + +use crate::deps::model::{ConstraintKind, Ecosystem}; + +/// Classify a raw declared constraint string. +pub fn classify_constraint(ecosystem: Ecosystem, raw: &str) -> ConstraintKind { + let trimmed = raw.trim(); + if trimmed.is_empty() { + return ConstraintKind::Unbounded; + } + + match ecosystem { + Ecosystem::Npm => classify_npm(trimmed), + Ecosystem::PyPI => classify_pypi(trimmed), + Ecosystem::Maven => classify_maven(trimmed), + _ => classify_generic(trimmed), + } +} + +fn classify_npm(raw: &str) -> ConstraintKind { + if raw.starts_with("git+") || raw.starts_with("git:") || raw.starts_with("git@") { + return git_ref_kind(raw); + } + if raw.starts_with("http://") || raw.starts_with("https://") { + return ConstraintKind::Url { checksum: false }; + } + if raw == "*" || raw.eq_ignore_ascii_case("latest") || raw.eq_ignore_ascii_case("x") { + return ConstraintKind::Unbounded; + } + if raw.starts_with('^') || raw.starts_with('~') || raw.starts_with('=') { + return ConstraintKind::BoundedRange; + } + if raw.starts_with('>') || raw.starts_with('<') { + return ConstraintKind::Unbounded; + } + if looks_like_exact_version(raw) { + return ConstraintKind::Exact; + } + ConstraintKind::Unbounded +} + +fn classify_pypi(raw: &str) -> ConstraintKind { + if raw.contains("git+") || raw.contains("@git") { + return git_ref_kind(raw); + } + if raw.starts_with("http://") || raw.starts_with("https://") { + return ConstraintKind::Url { checksum: false }; + } + if raw.starts_with("==") { + return ConstraintKind::Exact; + } + if let Some((_name, ver)) = raw.split_once("==") { + let ver = ver.trim(); + if looks_like_exact_version(ver) { + return ConstraintKind::Exact; + } + } + if raw.starts_with("~=") { + return ConstraintKind::BoundedRange; + } + if raw.starts_with('^') || raw.starts_with('~') { + return ConstraintKind::BoundedRange; + } + if raw.starts_with(">=") || raw.starts_with('>') || raw.starts_with('<') { + return ConstraintKind::Unbounded; + } + if looks_like_exact_version(raw) { + return ConstraintKind::Exact; + } + // Bare package name + ConstraintKind::Unbounded +} + +fn classify_maven(raw: &str) -> ConstraintKind { + if raw.ends_with("-SNAPSHOT") { + return ConstraintKind::Mutable; + } + if raw.eq_ignore_ascii_case("LATEST") + || raw.eq_ignore_ascii_case("RELEASE") + || raw.eq_ignore_ascii_case("latest.release") + { + return ConstraintKind::Unbounded; + } + if raw.ends_with(".+") || raw.contains('+') && raw.ends_with('.') { + return ConstraintKind::BoundedRange; + } + if raw.starts_with('[') || raw.starts_with('(') { + return ConstraintKind::BoundedRange; + } + if looks_like_exact_version(raw) || raw.contains('-') || raw.contains('.') { + return ConstraintKind::Exact; + } + ConstraintKind::Unbounded +} + +fn classify_generic(raw: &str) -> ConstraintKind { + if raw.starts_with("git+") { + return git_ref_kind(raw); + } + if raw == "*" || raw.eq_ignore_ascii_case("latest") { + return ConstraintKind::Unbounded; + } + if looks_like_exact_version(raw) { + return ConstraintKind::Exact; + } + ConstraintKind::BoundedRange +} + +fn git_ref_kind(raw: &str) -> ConstraintKind { + let ref_part = raw + .rsplit_once('#') + .or_else(|| raw.rsplit_once('@')) + .map(|(_, r)| r) + .unwrap_or(""); + if ref_part.len() == 40 && ref_part.chars().all(|c| c.is_ascii_hexdigit()) { + ConstraintKind::GitRef { mutable: false } + } else { + ConstraintKind::GitRef { mutable: true } + } +} + +fn looks_like_exact_version(raw: &str) -> bool { + let s = raw.trim_start_matches('='); + if s.is_empty() { + return false; + } + let first = s.chars().next().unwrap(); + first.is_ascii_digit() || first == 'v' +} diff --git a/src/deps/ecosystems/npm.rs b/src/deps/ecosystems/npm.rs new file mode 100644 index 0000000..edbc7cf --- /dev/null +++ b/src/deps/ecosystems/npm.rs @@ -0,0 +1,350 @@ +use std::collections::{HashMap, HashSet}; +use std::path::Path; + +use crate::deps::detect::DepFileKind; +use crate::deps::ecosystems::classify_constraint; +use crate::deps::ecosystems::evaluate::{ + constraint_to_findings, dep001, dep002, dep008, dep019_unsupported_lockfile, file_in_dir, + parent_dir, read_json, source_type_from_declared, ScanContext, +}; +use crate::deps::model::{ + ConstraintKind, DependencyEdge, DependencyNode, Ecosystem, PackageId, Scope, SourceType, +}; +use crate::deps::DepsError; + +pub fn scan_npm_projects(ctx: &mut ScanContext<'_>) -> Result<(), DepsError> { + let manifests: Vec<_> = ctx + .detected + .iter() + .filter(|f| f.kind == DepFileKind::NpmManifest) + .collect(); + + for manifest in manifests { + let dir = parent_dir(&manifest.path); + let rel_manifest = manifest + .path + .strip_prefix(ctx.root) + .unwrap_or(&manifest.path) + .display() + .to_string(); + scan_one_npm(ctx, &dir, &manifest.path, &rel_manifest)?; + } + Ok(()) +} + +fn scan_one_npm( + ctx: &mut ScanContext<'_>, + dir: &Path, + manifest_path: &Path, + rel_manifest: &str, +) -> Result<(), DepsError> { + let pkg = read_json(manifest_path)?; + let lock_path = file_in_dir(ctx.detected, dir, DepFileKind::NpmLockfile); + let unsupported_lock_path = file_in_dir(ctx.detected, dir, DepFileKind::YarnLockfile) + .or_else(|| file_in_dir(ctx.detected, dir, DepFileKind::PnpmLockfile)); + + let mut direct_prod: HashMap = HashMap::new(); + let mut direct_dev: HashMap = HashMap::new(); + if let Some(deps) = pkg.get("dependencies").and_then(|v| v.as_object()) { + for (k, v) in deps { + if let Some(s) = v.as_str() { + direct_prod.insert(k.clone(), s.to_string()); + } + } + } + if let Some(deps) = pkg.get("devDependencies").and_then(|v| v.as_object()) { + for (k, v) in deps { + if let Some(s) = v.as_str() { + direct_dev.insert(k.clone(), s.to_string()); + } + } + } + + let lock_packages: HashMap = if let Some(ref lp) = lock_path { + parse_npm_lock(lp)? + } else { + HashMap::new() + }; + if lock_path.is_none() { + if let Some(lock) = unsupported_lock_path.as_ref() { + let rel_lock = lock + .strip_prefix(ctx.root) + .unwrap_or(lock) + .display() + .to_string(); + dep019_unsupported_lockfile(ctx.findings, &rel_lock, "npm"); + } else { + dep001(ctx.findings, ctx.policy, rel_manifest, "npm"); + } + } + + let lock_has = |name: &str| -> bool { lock_packages.contains_key(&top_level_lock_key(name)) }; + + if ctx.policy.fail_on_stale_lockfile && lock_path.is_some() { + for name in direct_prod.keys().chain(direct_dev.keys()) { + let declared = direct_prod + .get(name) + .or_else(|| direct_dev.get(name)) + .map(String::as_str) + .unwrap_or(""); + if declared.starts_with("git") || declared.contains("git+") { + continue; + } + if !lock_has(name) { + dep002(ctx.findings, ctx.policy, rel_manifest, name); + } + } + } + + let mut seen_node_ids: HashSet = HashSet::new(); + let mut direct_lock_keys: HashSet = HashSet::new(); + let mut node_ids_by_lock_key: HashMap = HashMap::new(); + + for (name, declared) in direct_prod.iter().chain(direct_dev.iter()) { + let scope = if direct_dev.contains_key(name) { + Scope::Development + } else { + Scope::Production + }; + let lock_key = top_level_lock_key(name); + let lock_package = lock_packages.get(&lock_key).cloned(); + if lock_package.is_some() { + direct_lock_keys.insert(lock_key.clone()); + } + let resolved = lock_package.as_ref().map(|p| p.version.clone()); + let reproducible = resolved.is_some() && lock_path.is_some(); + let kind = classify_constraint(Ecosystem::Npm, declared); + let package_id = resolved + .as_ref() + .map(|v| PackageId::npm(name, v)) + .or_else(|| { + if matches!(kind, ConstraintKind::GitRef { .. }) { + Some(PackageId::npm(name, "git")) + } else { + None + } + }); + ctx.findings.extend(constraint_to_findings( + ctx.policy, + &kind, + true, + name, + declared, + resolved.as_deref(), + rel_manifest, + package_id.clone(), + reproducible, + )); + + let source_type = source_type_from_declared(declared); + let version = resolved.clone().or_else(|| { + if matches!(kind, ConstraintKind::GitRef { .. }) { + Some("git".into()) + } else { + None + } + }); + let node_id = package_id + .clone() + .unwrap_or_else(|| PackageId::npm(name, version.as_deref().unwrap_or("?"))); + if seen_node_ids.insert(node_id.0.clone()) { + let has_integrity = lock_package.as_ref().map(|p| p.has_integrity); + let node = DependencyNode { + id: node_id.clone(), + name: name.clone(), + ecosystem: Ecosystem::Npm, + version, + direct: true, + scope, + depth: 1, + source_type, + manifest_file: Some(rel_manifest.into()), + lockfile: lock_path.as_ref().map(|p| p.display().to_string()), + declared_constraint: Some(declared.clone()), + lock_integrity: has_integrity, + lock_resolved: lock_package.as_ref().and_then(|p| p.resolved.clone()), + lock_integrity_hash: lock_package.as_ref().and_then(|p| p.integrity.clone()), + }; + dep008(ctx.findings, ctx.policy, &node); + ctx.graph.nodes.push(node.clone()); + if lock_package.is_some() { + node_ids_by_lock_key.insert(lock_key, node.id.clone()); + } + ctx.graph.edges.push(DependencyEdge { + from: PackageId::root(), + to: node.id.clone(), + declared_constraint: declared.clone(), + resolved_version: resolved.clone(), + scope, + source_file: rel_manifest.into(), + }); + } + } + + // Transitive nodes from lockfile. Nested lock keys may carry the same package name + // at different versions, so de-duplicate by package identity instead of name. + for (key, lp) in &lock_packages { + if !key.starts_with("node_modules/") { + continue; + } + if direct_lock_keys.contains(key) { + continue; + } + let name = package_name_from_lock_key(key); + let id = PackageId::npm(name, &lp.version); + if !seen_node_ids.insert(id.0.clone()) { + continue; + } + let node = DependencyNode { + id, + name: name.to_string(), + ecosystem: Ecosystem::Npm, + version: Some(lp.version.clone()), + direct: false, + scope: Scope::Production, + depth: 2, + source_type: SourceType::Registry, + manifest_file: None, + lockfile: lock_path.as_ref().map(|p| p.display().to_string()), + declared_constraint: lp.declared.clone(), + lock_integrity: Some(lp.has_integrity), + lock_resolved: lp.resolved.clone(), + lock_integrity_hash: lp.integrity.clone(), + }; + dep008(ctx.findings, ctx.policy, &node); + node_ids_by_lock_key.insert(key.clone(), node.id.clone()); + ctx.graph.nodes.push(node); + } + + for (key, lp) in &lock_packages { + if !key.starts_with("node_modules/") { + continue; + } + if let Some(parent_key) = &lp.parent_key { + let Some(child_id) = node_ids_by_lock_key.get(key) else { + continue; + }; + let Some(parent_id) = node_ids_by_lock_key.get(parent_key) else { + continue; + }; + ctx.graph.edges.push(DependencyEdge { + from: parent_id.clone(), + to: child_id.clone(), + declared_constraint: lp.declared.clone().unwrap_or_else(|| lp.version.clone()), + resolved_version: Some(lp.version.clone()), + scope: Scope::Production, + source_file: rel_manifest.into(), + }); + } + } + + Ok(()) +} + +#[derive(Clone)] +struct LockPackage { + version: String, + has_integrity: bool, + resolved: Option, + integrity: Option, + declared: Option, + parent_key: Option, +} + +fn parse_npm_lock(path: &Path) -> Result, DepsError> { + let v = read_json(path)?; + let mut out = HashMap::new(); + + if let Some(packages) = v.get("packages").and_then(|p| p.as_object()) { + for (key, entry) in packages { + if key.is_empty() { + continue; + } + let version = entry + .get("version") + .and_then(|x| x.as_str()) + .unwrap_or("?") + .to_string(); + let has_integrity = entry.get("integrity").is_some(); + let resolved = entry + .get("resolved") + .and_then(|x| x.as_str()) + .map(str::to_string); + let integrity = entry + .get("integrity") + .and_then(|x| x.as_str()) + .map(str::to_string); + out.insert( + key.clone(), + LockPackage { + version: version.clone(), + has_integrity, + resolved: resolved.clone(), + integrity: integrity.clone(), + declared: None, + parent_key: None, + }, + ); + } + + for (parent_key, entry) in packages { + let Some(deps) = entry.get("dependencies").and_then(|d| d.as_object()) else { + continue; + }; + for (child_name, spec) in deps { + let Some(spec) = spec.as_str() else { + continue; + }; + if let Some(child_key) = child_lock_key(parent_key, child_name, packages) { + if let Some(lp) = out.get_mut(&child_key) { + lp.declared = Some(spec.to_string()); + lp.parent_key = if parent_key.is_empty() { + None + } else { + Some(parent_key.clone()) + }; + } + } + } + } + } + + Ok(out) +} + +fn package_name_from_lock_key(key: &str) -> &str { + let package_path = key + .rsplit_once("node_modules/") + .map(|(_, name)| name) + .unwrap_or(key); + let mut parts = package_path.split('/'); + let first = parts.next().unwrap_or(package_path); + if first.starts_with('@') { + if let Some(second) = parts.next() { + let scoped_len = first.len() + 1 + second.len(); + return &package_path[..scoped_len]; + } + } + first +} + +fn top_level_lock_key(name: &str) -> String { + format!("node_modules/{name}") +} + +fn child_lock_key( + parent_key: &str, + child_name: &str, + packages: &serde_json::Map, +) -> Option { + let nested = if parent_key.is_empty() { + format!("node_modules/{child_name}") + } else { + format!("{parent_key}/node_modules/{child_name}") + }; + if packages.contains_key(&nested) { + return Some(nested); + } + let hoisted = format!("node_modules/{child_name}"); + packages.contains_key(&hoisted).then_some(hoisted) +} diff --git a/src/deps/ecosystems/pypi.rs b/src/deps/ecosystems/pypi.rs new file mode 100644 index 0000000..062f13c --- /dev/null +++ b/src/deps/ecosystems/pypi.rs @@ -0,0 +1,471 @@ +use std::collections::{HashMap, HashSet}; +use std::path::Path; + +use crate::deps::detect::DepFileKind; +use crate::deps::ecosystems::classify_constraint; +use crate::deps::ecosystems::evaluate::{ + constraint_to_findings, dep001, file_in_dir, parent_dir, ScanContext, +}; +use crate::deps::model::{DependencyEdge, DependencyNode, Ecosystem, PackageId, Scope, SourceType}; +use crate::deps::DepsError; + +pub fn scan_pypi_projects(ctx: &mut ScanContext<'_>) -> Result<(), DepsError> { + let mut handled_dirs: HashSet<_> = HashSet::new(); + + for f in ctx.detected { + if f.kind == DepFileKind::PyProject { + let dir = parent_dir(&f.path); + if file_in_dir(ctx.detected, &dir, DepFileKind::PoetryLock).is_some() { + if !handled_dirs.insert(dir.clone()) { + continue; + } + scan_poetry(ctx, &dir)?; + } else if scan_pyproject_manifest(ctx, &dir, &f.path)? { + handled_dirs.insert(dir); + } + } + } + + for f in ctx.detected { + if f.kind == DepFileKind::PipRequirements { + let dir = parent_dir(&f.path); + let has_lock = ctx + .detected + .iter() + .any(|x| parent_dir(&x.path) == dir && matches!(x.kind, DepFileKind::PoetryLock)); + if !has_lock && !handled_dirs.contains(&dir) { + scan_requirements(ctx, &dir, &f.path)?; + } + } + } + Ok(()) +} + +fn scan_poetry(ctx: &mut ScanContext<'_>, dir: &Path) -> Result<(), DepsError> { + let pyproject = file_in_dir(ctx.detected, dir, DepFileKind::PyProject).unwrap(); + let poetry_lock = file_in_dir(ctx.detected, dir, DepFileKind::PoetryLock).unwrap(); + let rel_py = pyproject + .strip_prefix(ctx.root) + .unwrap_or(&pyproject) + .display() + .to_string(); + + let content = std::fs::read_to_string(&pyproject) + .map_err(|e| DepsError(format!("read pyproject: {e}")))?; + let toml: toml::Value = + toml::from_str(&content).map_err(|e| DepsError(format!("parse pyproject: {e}")))?; + + let direct = poetry_manifest_dependencies(&toml); + + let locked = parse_poetry_lock(&poetry_lock)?; + let mut seen = HashSet::new(); + + for (name, (declared, scope)) in &direct { + let resolved = locked.get(name).map(|p| p.version.as_str()); + let reproducible = resolved.is_some(); + let kind = classify_constraint(Ecosystem::PyPI, declared); + ctx.findings.extend(constraint_to_findings( + ctx.policy, + &kind, + true, + name, + declared, + resolved, + &rel_py, + resolved.map(|v| PackageId::pypi(name, v)), + reproducible, + )); + if seen.insert(name.clone()) { + let node = DependencyNode { + id: resolved + .map(|v| PackageId::pypi(name, v)) + .unwrap_or_else(|| PackageId::pypi(name, "?")), + name: name.clone(), + ecosystem: Ecosystem::PyPI, + version: resolved.map(str::to_string), + direct: true, + scope: *scope, + depth: 1, + source_type: SourceType::Registry, + manifest_file: Some(rel_py.clone()), + lockfile: Some(poetry_lock.display().to_string()), + declared_constraint: Some(declared.clone()), + lock_integrity: None, + lock_resolved: None, + lock_integrity_hash: None, + }; + ctx.graph.edges.push(DependencyEdge { + from: PackageId::root(), + to: node.id().clone(), + declared_constraint: declared.clone(), + resolved_version: resolved.map(str::to_string), + scope: *scope, + source_file: rel_py.clone(), + }); + ctx.graph.nodes.push(node); + } + } + + for (name, package) in &locked { + if direct.contains_key(name) { + continue; + } + if !seen.insert(name.clone()) { + continue; + } + ctx.graph.nodes.push(DependencyNode { + id: PackageId::pypi(name, &package.version), + name: name.clone(), + ecosystem: Ecosystem::PyPI, + version: Some(package.version.clone()), + direct: false, + scope: Scope::Production, + depth: 2, + source_type: SourceType::Registry, + manifest_file: None, + lockfile: Some(poetry_lock.display().to_string()), + declared_constraint: first_parent_constraint(&locked, name), + lock_integrity: None, + lock_resolved: None, + lock_integrity_hash: None, + }); + } + + for (parent_name, parent_package) in &locked { + for (child_name, declared) in &parent_package.dependencies { + let Some(child_package) = locked.get(child_name) else { + continue; + }; + ctx.graph.edges.push(DependencyEdge { + from: PackageId::pypi(parent_name, &parent_package.version), + to: PackageId::pypi(child_name, &child_package.version), + declared_constraint: declared.clone(), + resolved_version: Some(child_package.version.clone()), + scope: Scope::Production, + source_file: rel_py.clone(), + }); + } + } + + Ok(()) +} + +fn scan_pyproject_manifest( + ctx: &mut ScanContext<'_>, + _dir: &Path, + pyproject: &Path, +) -> Result { + let rel = pyproject + .strip_prefix(ctx.root) + .unwrap_or(pyproject) + .display() + .to_string(); + let content = std::fs::read_to_string(pyproject) + .map_err(|e| DepsError(format!("read pyproject: {e}")))?; + let toml: toml::Value = + toml::from_str(&content).map_err(|e| DepsError(format!("parse pyproject: {e}")))?; + let deps = pyproject_manifest_dependencies(&toml); + if deps.is_empty() { + return Ok(false); + } + + dep001(ctx.findings, ctx.policy, &rel, "Python"); + for (name, declared, scope) in deps { + add_unlocked_pypi_dependency(ctx, &rel, &name, &declared, scope); + } + Ok(true) +} + +fn scan_requirements( + ctx: &mut ScanContext<'_>, + dir: &Path, + req_path: &Path, +) -> Result<(), DepsError> { + let rel = req_path + .strip_prefix(ctx.root) + .unwrap_or(req_path) + .display() + .to_string(); + dep001(ctx.findings, ctx.policy, &rel, "Python"); + + let content = std::fs::read_to_string(req_path) + .map_err(|e| DepsError(format!("read requirements: {e}")))?; + for line in content.lines() { + let line = line.trim(); + if line.is_empty() || line.starts_with('#') { + continue; + } + let (name, declared) = parse_requirement_line(line); + add_unlocked_pypi_dependency(ctx, &rel, &name, &declared, Scope::Production); + } + let _ = dir; + Ok(()) +} + +fn add_unlocked_pypi_dependency( + ctx: &mut ScanContext<'_>, + source_file: &str, + name: &str, + declared: &str, + scope: Scope, +) { + let name = normalize_pypi_name(name); + let kind = classify_constraint(Ecosystem::PyPI, declared); + let is_exact = matches!(kind, crate::deps::model::ConstraintKind::Exact); + let exact_version = if is_exact { + exact_version_from_declared(&name, declared) + } else { + None + }; + let package_id = exact_version + .as_deref() + .map(|version| PackageId::pypi(&name, version)) + .or_else(|| { + if declared.contains("git+") { + Some(PackageId::pypi(&name, "git")) + } else { + Some(PackageId::pypi(&name, "?")) + } + }); + + ctx.findings.extend(constraint_to_findings( + ctx.policy, + &kind, + true, + &name, + declared, + exact_version.as_deref(), + source_file, + package_id.clone(), + false, + )); + + if let Some(version) = exact_version { + ctx.graph.nodes.push(DependencyNode { + id: PackageId::pypi(&name, &version), + name: name.clone(), + ecosystem: Ecosystem::PyPI, + version: Some(version), + direct: true, + scope, + depth: 1, + source_type: SourceType::Registry, + manifest_file: Some(source_file.to_string()), + lockfile: None, + declared_constraint: Some(declared.to_string()), + lock_integrity: None, + lock_resolved: None, + lock_integrity_hash: None, + }); + } else if declared.contains("git+") { + ctx.graph.nodes.push(DependencyNode { + id: PackageId::pypi(&name, "git"), + name: name.clone(), + ecosystem: Ecosystem::PyPI, + version: Some("git".into()), + direct: true, + scope, + depth: 1, + source_type: SourceType::GitBranch, + manifest_file: Some(source_file.to_string()), + lockfile: None, + declared_constraint: Some(declared.to_string()), + lock_integrity: None, + lock_resolved: None, + lock_integrity_hash: None, + }); + } +} + +fn parse_requirement_line(line: &str) -> (String, String) { + let line = line.trim(); + if let Some((name, _rest)) = line.split_once('@') { + return (name.trim().to_string(), line.to_string()); + } + if line.contains("==") { + let name = line.split("==").next().unwrap_or(line).trim(); + return (name.to_string(), line.to_string()); + } + if let Some(idx) = line.find(">=") { + let name = line[..idx].trim(); + return (name.to_string(), line.to_string()); + } + (line.to_string(), line.to_string()) +} + +fn poetry_manifest_dependencies(toml: &toml::Value) -> HashMap { + let mut direct = HashMap::new(); + if let Some(deps) = toml + .get("tool") + .and_then(|t| t.get("poetry")) + .and_then(|p| p.get("dependencies")) + .and_then(|d| d.as_table()) + { + for (name, value) in deps { + let name = normalize_pypi_name(name); + if name == "python" { + continue; + } + direct.insert(name, (toml_dependency_spec(value), Scope::Production)); + } + } + if let Some(deps) = toml + .get("tool") + .and_then(|t| t.get("poetry")) + .and_then(|p| p.get("group")) + .and_then(|g| g.get("dev")) + .and_then(|d| d.get("dependencies")) + .and_then(|d| d.as_table()) + { + for (name, value) in deps { + direct.insert( + normalize_pypi_name(name), + (toml_dependency_spec(value), Scope::Development), + ); + } + } + direct +} + +fn pyproject_manifest_dependencies(toml: &toml::Value) -> Vec<(String, String, Scope)> { + let mut deps = Vec::new(); + if let Some(project_deps) = toml + .get("project") + .and_then(|project| project.get("dependencies")) + .and_then(|dependencies| dependencies.as_array()) + { + for value in project_deps { + if let Some(raw) = value.as_str() { + let (name, declared) = parse_requirement_line(raw); + deps.push((name, declared, Scope::Production)); + } + } + } + for (name, (declared, scope)) in poetry_manifest_dependencies(toml) { + deps.push((name, declared, scope)); + } + deps +} + +fn toml_dependency_spec(value: &toml::Value) -> String { + value + .as_str() + .map(str::to_string) + .unwrap_or_else(|| value.to_string()) +} + +fn exact_version_from_declared(name: &str, declared: &str) -> Option { + let declared = declared.trim(); + if let Some(version) = declared.strip_prefix("==") { + return Some(version.trim().to_string()); + } + if let Some((left, version)) = declared.split_once("==") { + if normalize_pypi_name(left.trim()) == name { + return Some(version.trim().to_string()); + } + } + Some(declared.trim_start_matches('=').trim().to_string()) +} + +fn normalize_pypi_name(name: &str) -> String { + let mut out = String::new(); + let mut last_was_separator = false; + for c in name.trim().chars() { + if matches!(c, '-' | '_' | '.') { + if !last_was_separator { + out.push('-'); + last_was_separator = true; + } + } else { + out.push(c.to_ascii_lowercase()); + last_was_separator = false; + } + } + out +} + +#[derive(Debug, Clone)] +struct PoetryLockPackage { + version: String, + dependencies: HashMap, +} + +fn parse_poetry_lock(path: &Path) -> Result, DepsError> { + let content = + std::fs::read_to_string(path).map_err(|e| DepsError(format!("read poetry.lock: {e}")))?; + if content.trim().is_empty() || !content.contains("[[package]]") { + return Err(DepsError(format!( + "parse poetry.lock {}: truncated or invalid", + path.display() + ))); + } + let mut out = HashMap::new(); + let mut current_name: Option = None; + let mut current_version: Option = None; + let mut current_dependencies: HashMap = HashMap::new(); + let mut in_dependencies = false; + for line in content.lines() { + let line = line.trim(); + if line == "[[package]]" { + insert_poetry_package( + &mut out, + current_name.take(), + current_version.take(), + std::mem::take(&mut current_dependencies), + ); + in_dependencies = false; + continue; + } + if line.starts_with('[') { + in_dependencies = line == "[package.dependencies]"; + continue; + } + if let Some(rest) = line.strip_prefix("name = ") { + current_name = Some(rest.trim_matches('"').to_string()); + } + if let Some(rest) = line.strip_prefix("version = ") { + current_version = Some(rest.trim_matches('"').to_string()); + } + if in_dependencies { + if let Some((name, spec)) = line.split_once('=') { + current_dependencies.insert( + normalize_pypi_name(name.trim().trim_matches('"')), + spec.trim().trim_matches('"').to_string(), + ); + } + } + } + insert_poetry_package( + &mut out, + current_name, + current_version, + current_dependencies, + ); + Ok(out) +} + +fn insert_poetry_package( + packages: &mut HashMap, + name: Option, + version: Option, + dependencies: HashMap, +) { + if let (Some(name), Some(version)) = (name, version) { + packages.insert( + normalize_pypi_name(&name), + PoetryLockPackage { + version, + dependencies, + }, + ); + } +} + +fn first_parent_constraint( + packages: &HashMap, + child_name: &str, +) -> Option { + packages + .values() + .find_map(|package| package.dependencies.get(child_name).cloned()) +} diff --git a/src/deps/explain.rs b/src/deps/explain.rs new file mode 100644 index 0000000..cc6be5d --- /dev/null +++ b/src/deps/explain.rs @@ -0,0 +1,82 @@ +use std::collections::{HashMap, VecDeque}; + +use crate::deps::model::{DependencyGraph, PackageId}; + +#[derive(Debug)] +pub struct Explanation { + pub package: PackageId, + pub direct: bool, + pub depth: u32, + pub paths: Vec>, +} + +pub fn explain(graph: &DependencyGraph, package: &str) -> Option { + let node = graph.node(package)?; + let paths = find_paths_for(graph, package); + Some(Explanation { + package: node.id.clone(), + direct: node.is_direct(), + depth: node.depth(), + paths, + }) +} + +pub fn find_paths_for(graph: &DependencyGraph, package: &str) -> Vec> { + find_paths(graph, package) +} + +fn find_paths(graph: &DependencyGraph, target: &str) -> Vec> { + let target_id = graph.node(target).map(|n| n.id.clone()); + let Some(target_id) = target_id else { + return vec![]; + }; + + let mut adj: HashMap> = HashMap::new(); + for edge in &graph.edges { + let from_key = if edge.from.0 == "root" { + "root".to_string() + } else { + edge.from.name().to_string() + }; + adj.entry(from_key).or_default().push(edge.to.clone()); + } + + let mut paths = Vec::new(); + let mut queue: VecDeque> = VecDeque::new(); + queue.push_back(vec![PackageId::root()]); + + while let Some(path) = queue.pop_front() { + let last = path.last().unwrap(); + if last.name() == target || &target_id == last { + paths.push(path); + continue; + } + if path.len() > 10 { + continue; + } + let key = if last.0 == "root" { + "root".to_string() + } else { + last.name().to_string() + }; + if let Some(children) = adj.get(&key) { + for child in children { + if path.iter().any(|p| p == child) { + continue; + } + let mut next = path.clone(); + next.push(child.clone()); + queue.push_back(next); + } + } else if last.name() == target { + paths.push(path); + } + } + + if paths.is_empty() && graph.node(target).is_some() { + paths.push(vec![PackageId::root(), target_id]); + } + + paths.sort_by_key(|a| a.len()); + paths +} diff --git a/src/deps/findings.rs b/src/deps/findings.rs new file mode 100644 index 0000000..f75e50e --- /dev/null +++ b/src/deps/findings.rs @@ -0,0 +1,38 @@ +use crate::deps::model::{PackageId, Severity}; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Finding { + pub id: String, + pub severity: Severity, + pub title: String, + pub package: Option, + pub source_file: String, + pub declared_constraint: Option, + pub resolved_version: Option, + pub recommendation: String, + pub reproducible: bool, + pub paths: Vec>, +} + +pub trait FindingSource { + fn enrich(&self, graph: &crate::deps::model::DependencyGraph) -> Vec; +} + +pub fn sort_findings(findings: &mut [Finding]) { + findings.sort_by(|a, b| { + a.id.cmp(&b.id) + .then_with(|| a.severity.cmp(&b.severity)) + .then_with(|| { + a.package + .as_ref() + .map(|p| p.name().to_string()) + .unwrap_or_default() + .cmp( + &b.package + .as_ref() + .map(|p| p.name().to_string()) + .unwrap_or_default(), + ) + }) + }); +} diff --git a/src/deps/mod.rs b/src/deps/mod.rs new file mode 100644 index 0000000..91a6790 --- /dev/null +++ b/src/deps/mod.rs @@ -0,0 +1,95 @@ +//! Offline dependency inventory, policy evaluation, and graph analysis. + +#![allow(dead_code)] // library surface exceeds current bin wiring (Slice 8 vuln-api deferred) + +pub mod detect; +pub mod diff; +pub mod ecosystems; +pub mod explain; +pub mod findings; +pub mod model; +pub mod parse; +pub mod policy; +pub mod report; +pub mod run; +pub mod skill; + +use std::path::{Path, PathBuf}; + +use detect::DetectedFile; +use ecosystems::evaluate::ScanContext; +use findings::Finding; +use model::DependencyGraph; +use policy::Policy; + +#[derive(Debug)] +pub struct DepsError(pub String); + +impl std::fmt::Display for DepsError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} + +impl std::error::Error for DepsError {} + +/// Full result of a dependency scan of one directory tree. +#[derive(Debug)] +pub struct Inventory { + pub root: PathBuf, + pub detected_files: Vec, + pub graph: DependencyGraph, + pub findings: Vec, +} + +impl Inventory { + pub fn with_code(&self, code: &str) -> Vec<&Finding> { + self.findings.iter().filter(|f| f.id == code).collect() + } + + pub fn findings_for(&self, name: &str) -> Vec<&Finding> { + self.findings + .iter() + .filter(|f| f.package.as_ref().is_some_and(|p| p.name() == name)) + .collect() + } + + pub fn node(&self, name: &str) -> Option<&model::DependencyNode> { + self.graph.node(name) + } +} + +/// Scan a directory tree: detect files, build the graph, evaluate policy. +pub fn scan(root: &Path, policy: &Policy) -> Result { + let detected = detect::detect_dependency_files(root); + let mut graph = DependencyGraph::default(); + let mut findings = Vec::new(); + + // Invalid npm lockfile in tree + for f in &detected { + if f.kind == detect::DepFileKind::NpmLockfile { + ecosystems::evaluate::read_json(&f.path)?; + } + } + + let mut ctx = ScanContext { + root, + policy, + detected: &detected, + graph: &mut graph, + findings: &mut findings, + }; + ecosystems::scan_all(&mut ctx)?; + + ecosystems::evaluate::dep014(&mut findings, &graph); + + Ok(Inventory { + root: root.to_path_buf(), + detected_files: detected, + graph, + findings, + }) +} + +#[cfg(test)] +mod tests; diff --git a/src/deps/model.rs b/src/deps/model.rs new file mode 100644 index 0000000..b7a3309 --- /dev/null +++ b/src/deps/model.rs @@ -0,0 +1,239 @@ +use std::cmp::Ordering; +use std::fmt; + +/// Canonical package identity: a Package URL, e.g. `pkg:npm/express@4.18.2`. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct PackageId(pub String); + +impl PackageId { + pub fn npm(name: &str, version: &str) -> Self { + Self(format!("pkg:npm/{name}@{version}")) + } + + pub fn pypi(name: &str, version: &str) -> Self { + Self(format!("pkg:pypi/{name}@{version}")) + } + + pub fn maven(group: &str, artifact: &str, version: &str) -> Self { + Self(format!("pkg:maven/{group}/{artifact}@{version}")) + } + + pub fn root() -> Self { + Self("root".into()) + } + + /// The package-name component (`express`, `guava`, `commons-lang3`). + pub fn name(&self) -> &str { + if self.0 == "root" { + return "root"; + } + let before_at = self.0.rsplit_once('@').map(|(l, _)| l).unwrap_or(&self.0); + if let Some(name) = before_at.strip_prefix("pkg:npm/") { + return name; + } + if let Some(name) = before_at.strip_prefix("pkg:pypi/") { + return name; + } + before_at + .rsplit_once('/') + .map(|(_, r)| r) + .unwrap_or(before_at) + } + + /// The resolved-version component, if the purl carries one. + pub fn version(&self) -> Option<&str> { + self.0.rsplit_once('@').map(|(_, v)| v) + } +} + +impl From for PackageId { + fn from(s: String) -> Self { + Self(s) + } +} + +impl fmt::Display for PackageId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum Ecosystem { + Npm, + PyPI, + Maven, + Go, + Cargo, + Unknown, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub enum Scope { + Production, + Development, + Optional, + Peer, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum SourceType { + Registry, + PrivateRegistry, + GitCommit, + GitBranch, + GitTag, + LocalPath, + RemoteTarball, + Url, + Workspace, + Unknown, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub enum Severity { + Info, + Low, + Medium, + High, + Critical, +} + +impl Severity { + pub fn parse(s: &str) -> Option { + match s.to_lowercase().as_str() { + "info" => Some(Severity::Info), + "low" => Some(Severity::Low), + "medium" | "med" => Some(Severity::Medium), + "high" => Some(Severity::High), + "critical" | "crit" => Some(Severity::Critical), + _ => None, + } + } + + pub fn at_least(self, threshold: Severity) -> bool { + self >= threshold + } +} + +/// How a declared version constraint behaves — drives finding classification. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum ConstraintKind { + Exact, + BoundedRange, + Unbounded, + Mutable, + GitRef { mutable: bool }, + Url { checksum: bool }, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct DependencyNode { + pub(crate) id: PackageId, + pub(crate) name: String, + pub(crate) ecosystem: Ecosystem, + pub(crate) version: Option, + pub(crate) direct: bool, + pub(crate) scope: Scope, + pub(crate) depth: u32, + pub(crate) source_type: SourceType, + pub(crate) manifest_file: Option, + pub(crate) lockfile: Option, + pub(crate) declared_constraint: Option, + pub(crate) lock_integrity: Option, + pub(crate) lock_resolved: Option, + pub(crate) lock_integrity_hash: Option, +} + +impl DependencyNode { + pub fn new_npm(name: &str, version: &str) -> Self { + Self { + id: PackageId::npm(name, version), + name: name.to_string(), + ecosystem: Ecosystem::Npm, + version: Some(version.to_string()), + direct: true, + scope: Scope::Production, + depth: 1, + source_type: SourceType::Registry, + manifest_file: None, + lockfile: None, + declared_constraint: None, + lock_integrity: None, + lock_resolved: None, + lock_integrity_hash: None, + } + } + + pub fn id(&self) -> &PackageId { + &self.id + } + + pub fn name(&self) -> &str { + &self.name + } + + pub fn is_direct(&self) -> bool { + self.direct + } + + pub fn scope(&self) -> Scope { + self.scope + } + + pub fn version(&self) -> Option<&str> { + self.version.as_deref() + } + + pub fn depth(&self) -> u32 { + self.depth + } + + pub fn source_type(&self) -> SourceType { + self.source_type + } + + pub fn ecosystem(&self) -> Ecosystem { + self.ecosystem + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct DependencyEdge { + pub(crate) from: PackageId, + pub(crate) to: PackageId, + pub(crate) declared_constraint: String, + pub(crate) resolved_version: Option, + pub(crate) scope: Scope, + pub(crate) source_file: String, +} + +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct DependencyGraph { + pub(crate) nodes: Vec, + pub(crate) edges: Vec, +} + +impl DependencyGraph { + pub fn node(&self, name: &str) -> Option<&DependencyNode> { + self.nodes.iter().find(|n| n.name == name) + } + + pub fn nodes_named(&self, name: &str) -> Vec<&DependencyNode> { + self.nodes.iter().filter(|n| n.name == name).collect() + } + + pub fn node_by_id(&self, id: &PackageId) -> Option<&DependencyNode> { + self.nodes.iter().find(|n| &n.id == id) + } + + pub fn sort_nodes(&mut self) { + self.nodes.sort_by(|a, b| a.id.0.cmp(&b.id.0)); + self.edges + .sort_by(|a, b| a.from.0.cmp(&b.from.0).then_with(|| a.to.0.cmp(&b.to.0))); + } +} + +pub fn compare_versions(a: &str, b: &str) -> Ordering { + a.cmp(b) +} diff --git a/src/deps/parse/mod.rs b/src/deps/parse/mod.rs new file mode 100644 index 0000000..a4e62b7 --- /dev/null +++ b/src/deps/parse/mod.rs @@ -0,0 +1,4 @@ +//! Shared lockfile and manifest parsers for `corgea deps` inventory. + +pub mod npm_lock; +pub mod python_lock; diff --git a/src/deps/parse/npm_lock.rs b/src/deps/parse/npm_lock.rs new file mode 100644 index 0000000..247148c --- /dev/null +++ b/src/deps/parse/npm_lock.rs @@ -0,0 +1,10 @@ +//! npm / yarn / pnpm lockfile parsing — shared-parser module boundary placeholder. + +#![allow(dead_code)] + +use std::path::Path; + +/// Placeholder for the shared npm lockfile parser (not yet extracted). +pub fn parse_package_lock(_path: &Path) -> Result<(), String> { + unimplemented!("deps::parse::npm_lock") +} diff --git a/src/deps/parse/python_lock.rs b/src/deps/parse/python_lock.rs new file mode 100644 index 0000000..4953ec4 --- /dev/null +++ b/src/deps/parse/python_lock.rs @@ -0,0 +1,10 @@ +//! Python lockfile parsing — shared-parser module boundary placeholder. + +#![allow(dead_code)] + +use std::path::Path; + +/// Placeholder for the shared Python lockfile parser (not yet extracted). +pub fn parse_poetry_lock(_path: &Path) -> Result<(), String> { + unimplemented!("deps::parse::python_lock") +} diff --git a/src/deps/policy.rs b/src/deps/policy.rs new file mode 100644 index 0000000..782286a --- /dev/null +++ b/src/deps/policy.rs @@ -0,0 +1,92 @@ +#[derive(Debug, Clone)] +pub struct Policy { + pub require_lockfile: bool, + pub fail_on_missing_lockfile: bool, + pub fail_on_stale_lockfile: bool, + pub fail_on_wildcard: bool, + pub fail_on_latest: bool, + pub fail_on_mutable_sources: bool, + pub warn_on_semver_range: bool, + pub require_integrity_hashes: bool, +} + +impl Default for Policy { + fn default() -> Self { + Self { + require_lockfile: true, + fail_on_missing_lockfile: true, + fail_on_stale_lockfile: true, + fail_on_wildcard: true, + fail_on_latest: true, + fail_on_mutable_sources: true, + warn_on_semver_range: true, + require_integrity_hashes: true, + } + } +} + +#[derive(Debug)] +pub struct PolicyError(pub String); + +#[derive(serde::Deserialize)] +struct PolicyFile { + dependency_policy: Option, +} + +#[derive(serde::Deserialize)] +struct PolicyYaml { + require_lockfile: Option, + fail_on_missing_lockfile: Option, + fail_on_stale_lockfile: Option, + direct_dependencies: Option, +} + +#[derive(serde::Deserialize)] +struct DirectDepsYaml { + fail_on_wildcard: Option, + fail_on_latest: Option, + warn_on_semver_range: Option, +} + +impl Policy { + pub fn from_yaml(yaml: &str) -> Result { + let parsed: PolicyFile = serde_yaml_ng::from_str(yaml) + .map_err(|e| PolicyError(format!("invalid policy YAML: {e}")))?; + let mut policy = Policy::default(); + if let Some(dp) = parsed.dependency_policy { + if let Some(v) = dp.require_lockfile { + policy.require_lockfile = v; + } + if let Some(v) = dp.fail_on_missing_lockfile { + policy.fail_on_missing_lockfile = v; + } + if let Some(v) = dp.fail_on_stale_lockfile { + policy.fail_on_stale_lockfile = v; + } + if let Some(dd) = dp.direct_dependencies { + if let Some(v) = dd.fail_on_wildcard { + policy.fail_on_wildcard = v; + } + if let Some(v) = dd.fail_on_latest { + policy.fail_on_latest = v; + } + if let Some(v) = dd.warn_on_semver_range { + policy.warn_on_semver_range = v; + } + } + } + Ok(policy) + } + + pub fn default_yaml() -> &'static str { + r#"dependency_policy: + require_lockfile: true + fail_on_missing_lockfile: true + fail_on_stale_lockfile: true + direct_dependencies: + fail_on_wildcard: true + fail_on_latest: true + warn_on_semver_range: true +"# + } +} diff --git a/src/deps/report.rs b/src/deps/report.rs new file mode 100644 index 0000000..810f466 --- /dev/null +++ b/src/deps/report.rs @@ -0,0 +1,172 @@ +use serde_json::{json, Value}; +use std::fmt::Write as _; + +use crate::deps::model::DependencyGraph; +use crate::deps::Inventory; + +pub fn to_json(inv: &Inventory) -> Value { + inventory_to_json(inv) +} + +pub fn to_sarif(inv: &Inventory) -> Value { + let rules: Vec = inv + .findings + .iter() + .map(|f| { + json!({ + "id": f.id, + "name": f.title, + "shortDescription": { "text": f.title }, + }) + }) + .collect(); + + let results: Vec = inv + .findings + .iter() + .map(|f| { + json!({ + "ruleId": f.id, + "level": severity_to_sarif(f.severity), + "message": { "text": f.recommendation }, + }) + }) + .collect(); + + json!({ + "version": "2.1.0", + "runs": [{ + "tool": { + "driver": { + "name": "corgea-deps", + "rules": rules, + } + }, + "results": results, + }] + }) +} + +fn severity_to_sarif(sev: crate::deps::model::Severity) -> &'static str { + use crate::deps::model::Severity; + match sev { + Severity::Critical | Severity::High => "error", + Severity::Medium => "warning", + Severity::Low | Severity::Info => "note", + } +} + +pub fn to_cyclonedx(graph: &DependencyGraph) -> Value { + let components: Vec = graph + .nodes + .iter() + .filter(|n| n.name() != "root") + .map(|n| { + json!({ + "type": "library", + "name": n.name(), + "version": n.version(), + "purl": n.id().0, + }) + }) + .collect(); + + let deps: Vec = graph + .edges + .iter() + .map(|e| { + json!({ + "ref": e.from.0, + "dependsOn": [e.to.0], + }) + }) + .collect(); + + json!({ + "bomFormat": "CycloneDX", + "specVersion": "1.4", + "version": 1, + "components": components, + "dependencies": deps, + }) +} + +pub fn graph_nodes_json(graph: &DependencyGraph) -> Vec { + graph + .nodes + .iter() + .map(|n| { + json!({ + "id": n.id().0, + "name": n.name(), + "version": n.version(), + "direct": n.is_direct(), + "scope": format!("{:?}", n.scope()), + "depth": n.depth(), + }) + }) + .collect() +} + +pub fn inventory_to_json(inv: &Inventory) -> Value { + let nodes = graph_nodes_json(&inv.graph); + + let findings: Vec = inv + .findings + .iter() + .map(|f| { + json!({ + "id": f.id, + "severity": format!("{:?}", f.severity), + "title": f.title, + "package": f.package.as_ref().map(|p| p.0.clone()), + "reproducible": f.reproducible, + "recommendation": f.recommendation, + }) + }) + .collect(); + + json!({ + "root": inv.root, + "nodes": nodes, + "findings": findings, + }) +} + +pub fn table_output(inv: &Inventory) -> String { + let mut out = String::new(); + writeln!(&mut out, "Corgea dependency inventory\n").unwrap(); + writeln!( + &mut out, + "Detected {} dependency file(s)", + inv.detected_files.len() + ) + .unwrap(); + writeln!( + &mut out, + "Inventory: {} packages, {} findings\n", + inv.graph.nodes.len(), + inv.findings.len() + ) + .unwrap(); + + let mut by_sev: std::collections::BTreeMap = std::collections::BTreeMap::new(); + for f in &inv.findings { + *by_sev.entry(format!("{:?}", f.severity)).or_default() += 1; + } + for (sev, count) in by_sev { + writeln!(&mut out, " {sev}: {count}").unwrap(); + } + + for f in &inv.findings { + let pkg = f.package.as_ref().map(|p| p.name()).unwrap_or("project"); + writeln!(&mut out, "\n {} {:?} {}", f.id, f.severity, f.title).unwrap(); + writeln!(&mut out, " package: {pkg}").unwrap(); + writeln!(&mut out, " {}", f.recommendation).unwrap(); + } + out +} + +pub fn print_table(inv: &Inventory) { + print!("{}", table_output(inv)); +} diff --git a/src/deps/run.rs b/src/deps/run.rs new file mode 100644 index 0000000..3084214 --- /dev/null +++ b/src/deps/run.rs @@ -0,0 +1,872 @@ +use std::collections::HashSet; +use std::ffi::OsString; +use std::path::{Path, PathBuf}; +use std::process::Command; + +use clap::{Args, Subcommand}; +use serde_json::{json, Value}; + +use crate::deps::findings::Finding; +use crate::deps::model::{DependencyNode, Severity}; +use crate::deps::policy::Policy; +use crate::deps::report::{graph_nodes_json, table_output, to_cyclonedx, to_json, to_sarif}; +use crate::deps::{scan, DepsError}; + +#[derive(Subcommand, Debug, Clone)] +pub enum DepsSubcommand { + /// Scan manifests and lockfiles, build inventory, evaluate policy + #[command( + after_help = "Examples:\n corgea deps scan --format agent\n corgea deps scan --format quiet --fail-on high\n corgea deps scan --out-format sarif --out-file deps.sarif" + )] + Scan { + #[arg(default_value = ".")] + path: String, + #[arg(long, help = "Fail (exit 1) at or above this severity")] + fail_on: Option, + #[arg(long, help = "Output format: table, json, sarif")] + out_format: Option, + #[arg(long, help = "Write output to this file")] + out_file: Option, + #[command(flatten)] + render: RenderArgs, + }, + /// Print the dependency graph + #[command( + after_help = "Examples:\n corgea deps graph --format agent\n corgea deps graph tests/fixtures/node-app --format json" + )] + Graph { + #[arg(default_value = ".")] + path: String, + #[command(flatten)] + render: RenderArgs, + }, + /// Explain why a package is present + #[command( + after_help = "Examples:\n corgea deps explain lodash --format agent\n corgea deps explain left-pad tests/fixtures/node-app --format json" + )] + Explain { + package: String, + #[arg(default_value = ".")] + path: String, + #[command(flatten)] + render: RenderArgs, + }, + /// Compare dependency graph against a git ref + #[command( + after_help = "Examples:\n corgea deps diff --base origin/main --format json\n corgea deps diff --base HEAD . --fail-on-new high" + )] + Diff { + #[arg(long)] + base: String, + #[arg(default_value = ".")] + path: String, + #[arg(long)] + fail_on_new: Option, + #[command(flatten)] + render: RenderArgs, + }, + /// Generate an SBOM + #[command( + after_help = "Examples:\n corgea deps sbom --format cyclonedx\n corgea deps sbom --format cyclonedx --out bom.json" + )] + Sbom { + #[arg(long, default_value = "cyclonedx")] + format: String, + #[arg(default_value = ".")] + path: String, + #[arg(long)] + out: Option, + }, + /// Policy commands + Policy { + #[command(subcommand)] + command: DepsPolicySubcommand, + }, +} + +#[derive(Subcommand, Debug, Clone)] +pub enum DepsPolicySubcommand { + /// Write a starter `.corgea/deps.yml` policy file + #[command( + after_help = "Examples:\n corgea deps policy init\n corgea deps policy init --exist-ok --format quiet" + )] + Init { + #[arg(default_value = ".")] + path: String, + #[arg( + long, + help = "Succeed without rewriting when .corgea/deps.yml already exists" + )] + exist_ok: bool, + #[command(flatten)] + render: RenderArgs, + }, +} + +#[derive(Args, Debug, Clone, Default)] +pub struct RenderArgs { + #[arg( + long, + value_name = "human|agent|json|quiet", + help = "Render output for humans, agents, JSON parsers, or suppress stdout" + )] + format: Option, +} + +impl RenderArgs { + fn is_set(&self) -> bool { + self.format.is_some() + } + + fn resolve(&self) -> Result { + RenderFormat::resolve(self.format.as_deref()) + } +} + +pub fn run(sub: DepsSubcommand) -> u8 { + match run_inner(sub) { + Ok(code) => code, + Err(e) => { + eprintln!("deps failed: {e}"); + 2 + } + } +} + +fn run_inner(sub: DepsSubcommand) -> Result { + match sub { + DepsSubcommand::Scan { + path, + fail_on, + out_format, + out_file, + render, + } => { + if out_format.is_some() && render.is_set() { + return Err(DepsError( + "--format cannot be used with --out-format; choose one output selector" + .to_string(), + )); + } + let fail_threshold = fail_on + .as_deref() + .map(|threshold| parse_severity(threshold, "--fail-on")) + .transpose()?; + let root = Path::new(&path); + let policy = load_policy(root)?; + let inv = scan(root, &policy)?; + let render_format = if out_format.is_some() { + None + } else { + Some(render.resolve()?) + }; + let output = if let Some(out_format) = out_format.as_deref() { + match ReportFormat::parse(out_format)? { + ReportFormat::Table => table_output(&inv), + ReportFormat::Json => json_line(to_json(&inv)), + ReportFormat::Sarif => json_line(to_sarif(&inv)), + } + } else { + render_scan(&inv, render_format.expect("render format resolved")) + }; + + emit_output(&output, out_file.as_deref())?; + if let Some(format) = render_format { + emit_scan_hints(&path, &inv, format); + } + + if let Some(threshold) = fail_threshold { + if should_fail(&inv, threshold) { + return Ok(1); + } + } + Ok(0) + } + DepsSubcommand::Graph { path, render } => { + let root = Path::new(&path); + let policy = load_policy(root)?; + let inv = scan(root, &policy)?; + let output = render_graph(&inv, render.resolve()?); + emit_output(&output, None)?; + Ok(0) + } + DepsSubcommand::Explain { + package, + path, + render, + } => { + let root = Path::new(&path); + let policy = load_policy(root)?; + let inv = scan(root, &policy)?; + match crate::deps::explain::explain(&inv.graph, &package) { + Some(e) => { + let output = render_explanation(&package, &e, render.resolve()?); + emit_output(&output, None)?; + } + None => { + return Err(DepsError(format!("package not found: {package}"))); + } + } + Ok(0) + } + DepsSubcommand::Diff { + base, + path, + fail_on_new, + render, + } => { + let new_threshold = fail_on_new + .as_deref() + .map(|threshold| parse_severity(threshold, "--fail-on-new")) + .transpose()?; + let root = Path::new(&path); + let policy = load_policy(root)?; + let head = scan(root, &policy)?; + let base_inv = scan_base_ref(root, &base)?; + let diff = crate::deps::diff::diff_graphs(&base_inv.graph, &head.graph); + let output = render_diff(&base, &diff, render.resolve()?); + emit_output(&output, None)?; + if let Some(threshold) = new_threshold { + if has_new_findings_at_or_above(&base_inv, &head, threshold) + || (Severity::High.at_least(threshold) && !diff.artifact_changed.is_empty()) + { + return Ok(1); + } + } + Ok(0) + } + DepsSubcommand::Sbom { format, path, out } => { + let root = Path::new(&path); + let policy = load_policy(root)?; + let inv = scan(root, &policy)?; + if format != "cyclonedx" { + return Err(DepsError(format!("unsupported SBOM format: {format}"))); + } + let sbom = to_cyclonedx(&inv.graph).to_string(); + if let Some(out_path) = out { + std::fs::write(&out_path, sbom) + .map_err(|e| DepsError(format!("write sbom: {e}")))?; + } else { + println!("{sbom}"); + } + Ok(0) + } + DepsSubcommand::Policy { command } => match command { + DepsPolicySubcommand::Init { + path, + exist_ok, + render, + } => { + let render_format = render.resolve()?; + let dir = PathBuf::from(&path).join(".corgea"); + std::fs::create_dir_all(&dir) + .map_err(|e| DepsError(format!("create .corgea: {e}")))?; + let policy_path = dir.join("deps.yml"); + let created = if policy_path.exists() && exist_ok { + false + } else { + std::fs::write(&policy_path, Policy::default_yaml()) + .map_err(|e| DepsError(format!("write policy: {e}")))?; + true + }; + let output = render_policy_init(&policy_path, created, render_format); + emit_output(&output, None)?; + emit_policy_init_hint(&path, render_format); + Ok(0) + } + }, + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum ReportFormat { + Table, + Json, + Sarif, +} + +impl ReportFormat { + fn parse(value: &str) -> Result { + match value { + "table" => Ok(Self::Table), + "json" => Ok(Self::Json), + "sarif" => Ok(Self::Sarif), + other => Err(DepsError(format!( + "unsupported --out-format: {other}; expected table, json, or sarif" + ))), + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum RenderFormat { + Human, + Agent, + Json, + Quiet, +} + +impl RenderFormat { + fn resolve(value: Option<&str>) -> Result { + match value { + Some("human") => Ok(Self::Human), + Some("agent") => Ok(Self::Agent), + Some("json") => Ok(Self::Json), + Some("quiet") => Ok(Self::Quiet), + Some(other) => Err(DepsError(format!( + "unsupported --format: {other}; expected human, agent, json, or quiet" + ))), + None if agent_env_detected() => Ok(Self::Agent), + None => Ok(Self::Human), + } + } + + fn as_str(self) -> &'static str { + match self { + Self::Human => "human", + Self::Agent => "agent", + Self::Json => "json", + Self::Quiet => "quiet", + } + } +} + +fn agent_env_detected() -> bool { + [ + "AI_AGENT", + "CODEX_SANDBOX", + "CLAUDECODE", + "CLAUDE_CODE", + "CURSOR_AGENT", + "CURSOR_TRACE_ID", + "GEMINI_CLI", + "PI_AGENT", + ] + .iter() + .any(|name| match std::env::var(name) { + Ok(value) => { + let normalized = value.trim().to_ascii_lowercase(); + !normalized.is_empty() && normalized != "0" && normalized != "false" + } + Err(_) => false, + }) +} + +fn emit_output(output: &str, out_file: Option<&str>) -> Result<(), DepsError> { + if let Some(file) = out_file { + std::fs::write(file, output).map_err(|e| DepsError(format!("write out-file: {e}")))?; + } else if !output.is_empty() { + print!("{output}"); + } + Ok(()) +} + +fn json_line(value: Value) -> String { + format!("{value}\n") +} + +fn emit_scan_hints(path: &str, inv: &crate::deps::Inventory, format: RenderFormat) { + if format == RenderFormat::Quiet { + return; + } + + let format = format.as_str(); + if let Some(package) = inv + .findings + .iter() + .filter_map(|finding| finding.package.as_ref()) + .map(|package| package.name()) + .find(|name| *name != "project") + { + eprintln!( + "Hint: Run `corgea deps explain {} {} --format {}` to inspect why this package is present.", + shell_word(package), + shell_word(path), + format + ); + } + + eprintln!( + "Hint: Run `corgea deps diff --base origin/main {} --format {}` before merging dependency changes.", + shell_word(path), + format + ); +} + +fn emit_policy_init_hint(path: &str, format: RenderFormat) { + if format == RenderFormat::Quiet { + return; + } + + eprintln!( + "Hint: Run `corgea deps scan {} --format json` to verify the policy.", + shell_word(path) + ); +} + +fn render_scan(inv: &crate::deps::Inventory, format: RenderFormat) -> String { + match format { + RenderFormat::Human => table_output(inv), + RenderFormat::Agent => agent_scan_output(inv), + RenderFormat::Json => json_line(to_json(inv)), + RenderFormat::Quiet => String::new(), + } +} + +fn agent_scan_output(inv: &crate::deps::Inventory) -> String { + let mut out = String::new(); + out.push_str("record\troot\tdetected_files\tpackages\tfindings\n"); + out.push_str(&format!( + "summary\t{}\t{}\t{}\t{}\n", + tsv_cell(&inv.root.display().to_string()), + inv.detected_files.len(), + inv.graph.nodes.len(), + inv.findings.len() + )); + out.push_str("record\tid\tseverity\tpackage\ttitle\trecommendation\n"); + for finding in &inv.findings { + let package = finding + .package + .as_ref() + .map(|package| package.0.as_str()) + .unwrap_or("project"); + out.push_str(&format!( + "finding\t{}\t{:?}\t{}\t{}\t{}\n", + tsv_cell(&finding.id), + finding.severity, + tsv_cell(package), + tsv_cell(&finding.title), + tsv_cell(&finding.recommendation) + )); + } + out +} + +fn render_graph(inv: &crate::deps::Inventory, format: RenderFormat) -> String { + match format { + RenderFormat::Human => human_graph_output(inv), + RenderFormat::Agent => agent_graph_output(inv), + RenderFormat::Json => json_line(json!({ "nodes": graph_nodes_json(&inv.graph) })), + RenderFormat::Quiet => String::new(), + } +} + +fn human_graph_output(inv: &crate::deps::Inventory) -> String { + let mut out = String::new(); + for node in &inv.graph.nodes { + out.push_str(&format!( + "{} {} direct={} scope={:?} depth={}\n", + node.name(), + node.version().unwrap_or("?"), + node.is_direct(), + node.scope(), + node.depth() + )); + } + out +} + +fn agent_graph_output(inv: &crate::deps::Inventory) -> String { + let mut out = String::from("id\tname\tversion\tdirect\tscope\tdepth\n"); + for node in &inv.graph.nodes { + out.push_str(&format!( + "{}\t{}\t{}\t{}\t{:?}\t{}\n", + tsv_cell(&node.id().0), + tsv_cell(node.name()), + tsv_cell(node.version().unwrap_or("")), + node.is_direct(), + node.scope(), + node.depth() + )); + } + out +} + +fn render_explanation( + package: &str, + explanation: &crate::deps::explain::Explanation, + format: RenderFormat, +) -> String { + match format { + RenderFormat::Human => human_explanation_output(package, explanation), + RenderFormat::Agent => agent_explanation_output(package, explanation), + RenderFormat::Json => json_line(explanation_json(package, explanation)), + RenderFormat::Quiet => String::new(), + } +} + +fn human_explanation_output( + package: &str, + explanation: &crate::deps::explain::Explanation, +) -> String { + let mut out = format!( + "{} direct={} depth={}\n", + package, explanation.direct, explanation.depth + ); + for path in &explanation.paths { + let line: Vec<_> = path.iter().map(|package| package.name()).collect(); + out.push_str(&format!(" path: {}\n", line.join(" -> "))); + } + out +} + +fn agent_explanation_output( + package: &str, + explanation: &crate::deps::explain::Explanation, +) -> String { + let mut out = String::from("record\tpackage\tdirect\tdepth\tpath\n"); + for path in &explanation.paths { + let line: Vec<_> = path.iter().map(|package| package.name()).collect(); + out.push_str(&format!( + "path\t{}\t{}\t{}\t{}\n", + tsv_cell(package), + explanation.direct, + explanation.depth, + tsv_cell(&line.join(" -> ")) + )); + } + out +} + +fn explanation_json(package: &str, explanation: &crate::deps::explain::Explanation) -> Value { + let paths: Vec> = explanation + .paths + .iter() + .map(|path| path.iter().map(|package| package.name()).collect()) + .collect(); + json!({ + "package": package, + "direct": explanation.direct, + "depth": explanation.depth, + "paths": paths, + }) +} + +fn render_diff(base: &str, diff: &crate::deps::diff::GraphDiff, format: RenderFormat) -> String { + match format { + RenderFormat::Human => human_diff_output(base, diff), + RenderFormat::Agent => agent_diff_output(diff), + RenderFormat::Json => json_line(diff_json(base, diff)), + RenderFormat::Quiet => String::new(), + } +} + +fn human_diff_output(base: &str, diff: &crate::deps::diff::GraphDiff) -> String { + let mut out = format!("Dependency diff against {base}\n"); + for node in &diff.added { + out.push_str(&format!( + " + {}@{}\n", + node.name(), + node.version().unwrap_or("?") + )); + } + for node in &diff.removed { + out.push_str(&format!( + " - {}@{}\n", + node.name(), + node.version().unwrap_or("?") + )); + } + for change in &diff.changed { + out.push_str(&format!( + " ~ {} {} -> {}\n", + change.name, change.from, change.to + )); + } + for change in &diff.artifact_changed { + out.push_str(&format!( + " ~ {} {} {} changed\n", + change.name, + change.version, + artifact_change_label(change) + )); + } + out +} + +fn agent_diff_output(diff: &crate::deps::diff::GraphDiff) -> String { + let mut out = String::from("change\tname\tfrom\tto\n"); + for node in &diff.added { + out.push_str(&format!( + "added\t{}\t\t{}\n", + tsv_cell(node.name()), + tsv_cell(node.version().unwrap_or("")) + )); + } + for node in &diff.removed { + out.push_str(&format!( + "removed\t{}\t{}\t\n", + tsv_cell(node.name()), + tsv_cell(node.version().unwrap_or("")) + )); + } + for change in &diff.changed { + out.push_str(&format!( + "changed\t{}\t{}\t{}\n", + tsv_cell(&change.name), + tsv_cell(&change.from), + tsv_cell(&change.to) + )); + } + for change in &diff.artifact_changed { + out.push_str(&format!( + "artifact_changed\t{}\t{}\t{}\n", + tsv_cell(&change.name), + tsv_cell(&artifact_summary( + &change.version, + change.old_resolved.as_deref(), + change.old_integrity.as_deref() + )), + tsv_cell(&artifact_summary( + &change.version, + change.new_resolved.as_deref(), + change.new_integrity.as_deref() + )) + )); + } + out +} + +fn diff_json(base: &str, diff: &crate::deps::diff::GraphDiff) -> Value { + let changed: Vec = diff + .changed + .iter() + .map(|change| { + json!({ + "name": change.name, + "from": change.from, + "to": change.to, + }) + }) + .collect(); + let artifact_changed: Vec = diff + .artifact_changed + .iter() + .map(|change| { + json!({ + "name": change.name, + "version": change.version, + "resolved": { + "from": change.old_resolved, + "to": change.new_resolved, + }, + "integrity": { + "from": change.old_integrity, + "to": change.new_integrity, + }, + }) + }) + .collect(); + json!({ + "base": base, + "added": diff_nodes_json(&diff.added), + "removed": diff_nodes_json(&diff.removed), + "changed": changed, + "artifact_changed": artifact_changed, + }) +} + +fn artifact_change_label(change: &crate::deps::diff::ArtifactChange) -> &'static str { + match ( + change.old_resolved != change.new_resolved, + change.old_integrity != change.new_integrity, + ) { + (true, true) => "artifact", + (true, false) => "resolved", + (false, true) => "integrity", + (false, false) => "artifact", + } +} + +fn artifact_summary(version: &str, resolved: Option<&str>, integrity: Option<&str>) -> String { + let mut parts = vec![version.to_string()]; + if let Some(resolved) = resolved { + parts.push(format!("resolved={resolved}")); + } + if let Some(integrity) = integrity { + parts.push(format!("integrity={integrity}")); + } + parts.join(" ") +} + +fn diff_nodes_json(nodes: &[DependencyNode]) -> Vec { + nodes + .iter() + .map(|node| { + json!({ + "name": node.name(), + "version": node.version(), + }) + }) + .collect() +} + +fn render_policy_init(path: &Path, created: bool, format: RenderFormat) -> String { + let path = path.display().to_string(); + match format { + RenderFormat::Human if created => format!("Wrote {path}\n"), + RenderFormat::Human => format!("Exists {path}\n"), + RenderFormat::Agent => format!("path\n{}\n", tsv_cell(&path)), + RenderFormat::Json => json_line(json!({ "path": path, "created": created })), + RenderFormat::Quiet => String::new(), + } +} + +fn tsv_cell(value: &str) -> String { + value.replace(['\t', '\r', '\n'], " ") +} + +fn shell_word(value: &str) -> String { + if !value.is_empty() + && value + .chars() + .all(|c| c.is_ascii_alphanumeric() || matches!(c, '/' | '.' | '_' | '-' | ':' | '@')) + { + return value.to_string(); + } + + format!("'{}'", value.replace('\'', "'\\''")) +} + +fn load_policy(root: &Path) -> Result { + let policy_path = root.join(".corgea").join("deps.yml"); + if !policy_path.exists() { + return Ok(Policy::default()); + } + let yaml = std::fs::read_to_string(&policy_path) + .map_err(|e| DepsError(format!("read policy {}: {e}", policy_path.display())))?; + Policy::from_yaml(&yaml) + .map_err(|e| DepsError(format!("load policy {}: {}", policy_path.display(), e.0))) +} + +fn parse_severity(value: &str, option: &str) -> Result { + Severity::parse(value).ok_or_else(|| { + DepsError(format!( + "unsupported severity for {option}: {value}; expected info, low, medium, high, or critical" + )) + }) +} + +fn should_fail(inv: &crate::deps::Inventory, threshold: Severity) -> bool { + inv.findings.iter().any(|f| f.severity.at_least(threshold)) +} + +fn has_new_findings_at_or_above( + base: &crate::deps::Inventory, + head: &crate::deps::Inventory, + threshold: Severity, +) -> bool { + let base_keys: HashSet = base.findings.iter().map(finding_key).collect(); + head.findings + .iter() + .any(|f| f.severity.at_least(threshold) && !base_keys.contains(&finding_key(f))) +} + +fn finding_key(finding: &Finding) -> String { + format!( + "{}\0{}\0{}\0{}\0{}", + finding.id, + finding.package.as_ref().map(|p| p.0.as_str()).unwrap_or(""), + finding.source_file, + finding.declared_constraint.as_deref().unwrap_or(""), + finding.resolved_version.as_deref().unwrap_or("") + ) +} + +fn scan_base_ref(path: &Path, base: &str) -> Result { + let head_path = std::fs::canonicalize(path) + .map_err(|e| DepsError(format!("resolve scan path {}: {e}", path.display())))?; + let repo_root_raw = git_output(&head_path, &["rev-parse", "--show-toplevel"])?; + let repo_root = std::fs::canonicalize(PathBuf::from(repo_root_raw.trim())) + .map_err(|e| DepsError(format!("resolve git root: {e}")))?; + let rel_path = head_path.strip_prefix(&repo_root).map_err(|_| { + DepsError(format!( + "scan path {} is outside git root {}", + head_path.display(), + repo_root.display() + )) + })?; + + let temp = tempfile::TempDir::new() + .map_err(|e| DepsError(format!("create temporary worktree directory: {e}")))?; + let worktree = temp.path().join("base"); + git_output_os( + &repo_root, + vec![ + OsString::from("worktree"), + OsString::from("add"), + OsString::from("--detach"), + OsString::from("--quiet"), + worktree.as_os_str().to_os_string(), + OsString::from(base), + ], + )?; + + let base_path = if rel_path.as_os_str().is_empty() { + worktree.clone() + } else { + worktree.join(rel_path) + }; + let result = if base_path.exists() { + load_policy(&base_path).and_then(|policy| scan(&base_path, &policy)) + } else { + Err(DepsError(format!( + "path {} does not exist at base ref {base}", + path.display() + ))) + }; + let cleanup = git_output_os( + &repo_root, + vec![ + OsString::from("worktree"), + OsString::from("remove"), + OsString::from("--force"), + worktree.as_os_str().to_os_string(), + ], + ); + + match (result, cleanup) { + (Ok(inv), Ok(_)) => Ok(inv), + (Err(e), _) => Err(e), + (Ok(_), Err(e)) => Err(DepsError(format!("cleanup base worktree: {e}"))), + } +} + +fn git_output(repo: &Path, args: &[&str]) -> Result { + git_output_os(repo, args.iter().map(OsString::from).collect()) +} + +fn git_output_os(repo: &Path, args: Vec) -> Result { + let mut command = Command::new("git"); + for var in GIT_LOCAL_ENV_VARS { + command.env_remove(var); + } + let output = command + .current_dir(repo) + .args(args) + .output() + .map_err(|e| DepsError(format!("run git: {e}")))?; + if !output.status.success() { + let stderr = String::from_utf8_lossy(&output.stderr); + return Err(DepsError(format!("git failed: {}", stderr.trim()))); + } + String::from_utf8(output.stdout).map_err(|e| DepsError(format!("read git output: {e}"))) +} + +const GIT_LOCAL_ENV_VARS: &[&str] = &[ + "GIT_ALTERNATE_OBJECT_DIRECTORIES", + "GIT_CONFIG", + "GIT_CONFIG_PARAMETERS", + "GIT_CONFIG_COUNT", + "GIT_OBJECT_DIRECTORY", + "GIT_DIR", + "GIT_WORK_TREE", + "GIT_IMPLICIT_WORK_TREE", + "GIT_GRAFT_FILE", + "GIT_INDEX_FILE", + "GIT_NO_REPLACE_OBJECTS", + "GIT_REPLACE_REF_BASE", + "GIT_PREFIX", + "GIT_SHALLOW_FILE", + "GIT_COMMON_DIR", +]; diff --git a/src/deps/skill.rs b/src/deps/skill.rs new file mode 100644 index 0000000..cf326b3 --- /dev/null +++ b/src/deps/skill.rs @@ -0,0 +1,185 @@ +use std::path::Path; + +use clap::{Command, CommandFactory, Parser}; + +use crate::deps::run::DepsSubcommand; + +pub const BEGIN_MARKER: &str = ""; +pub const END_MARKER: &str = ""; + +#[derive(Parser)] +#[command( + name = "corgea deps", + about = "Offline dependency inventory and policy checks" +)] +struct DepsSkillCli { + #[command(subcommand)] + command: DepsSubcommand, +} + +struct SkillCommand<'a> { + path: &'a [&'a str], + signature: &'a str, + examples: &'a [&'a str], +} + +const COMMANDS: &[SkillCommand<'_>] = &[ + SkillCommand { + path: &["scan"], + signature: "corgea deps scan [PATH]", + examples: &[ + "corgea deps scan --format agent", + "corgea deps scan --format quiet --fail-on high", + ], + }, + SkillCommand { + path: &["graph"], + signature: "corgea deps graph [PATH]", + examples: &[ + "corgea deps graph --format agent", + "corgea deps graph tests/fixtures/node-app --format json", + ], + }, + SkillCommand { + path: &["explain"], + signature: "corgea deps explain [PATH]", + examples: &[ + "corgea deps explain lodash --format agent", + "corgea deps explain left-pad tests/fixtures/node-app --format json", + ], + }, + SkillCommand { + path: &["diff"], + signature: "corgea deps diff --base [PATH]", + examples: &[ + "corgea deps diff --base origin/main --format json", + "corgea deps diff --base HEAD . --fail-on-new high", + ], + }, + SkillCommand { + path: &["sbom"], + signature: "corgea deps sbom [PATH]", + examples: &[ + "corgea deps sbom --format cyclonedx", + "corgea deps sbom --format cyclonedx --out bom.json", + ], + }, + SkillCommand { + path: &["policy", "init"], + signature: "corgea deps policy init [PATH]", + examples: &[ + "corgea deps policy init", + "corgea deps policy init --exist-ok --format quiet", + ], + }, +]; + +pub fn generated_marked_section() -> String { + format!( + "{BEGIN_MARKER}\n{}\n{END_MARKER}", + generated_deps_skill_section().trim_end() + ) +} + +pub fn generated_deps_skill_section() -> String { + let root = DepsSkillCli::command(); + let mut out = String::new(); + out.push_str("### Deps \u{2014} `corgea deps `\n\n"); + out.push_str( + "Offline dependency inventory and policy checks. No Corgea token or network required.\n", + ); + out.push_str( + "Agent environments default to compact TSV; force output with `--format human|agent|json|quiet`.\n\n", + ); + + for spec in COMMANDS { + let command = find_command(&root, spec.path); + let about = command + .get_about() + .map(|about| about.to_string()) + .unwrap_or_else(|| "No description".to_string()); + let flags = important_flags(command); + + out.push_str(&format!("- `{}` \u{2014} {}", spec.signature, about)); + if !flags.is_empty() { + out.push_str(&format!(". Flags: {}", flags.join(", "))); + } + out.push('\n'); + out.push_str(" Examples: "); + out.push_str( + &spec + .examples + .iter() + .map(|example| format!("`{example}`")) + .collect::>() + .join("; "), + ); + out.push('\n'); + } + + out.push_str( + "\nNotes: `deps scan --out-format table|json|sarif` is the report/export selector; do not combine it with `deps scan --format`.\n", + ); + out +} + +pub fn check_skill_file(path: &Path) -> Result<(), String> { + let current = + std::fs::read_to_string(path).map_err(|e| format!("read {}: {e}", path.display()))?; + let expected = replace_generated_section(¤t)?; + if current == expected { + return Ok(()); + } + + Err(format!( + "{} is out of date. Run `cargo run --example deps_skill -- update`.", + path.display() + )) +} + +pub fn update_skill_file(path: &Path) -> Result<(), String> { + let current = + std::fs::read_to_string(path).map_err(|e| format!("read {}: {e}", path.display()))?; + let updated = replace_generated_section(¤t)?; + std::fs::write(path, updated).map_err(|e| format!("write {}: {e}", path.display())) +} + +fn replace_generated_section(content: &str) -> Result { + let start = content + .find(BEGIN_MARKER) + .ok_or_else(|| format!("missing {BEGIN_MARKER}"))?; + let end = content + .find(END_MARKER) + .ok_or_else(|| format!("missing {END_MARKER}"))?; + if end < start { + return Err(format!("{END_MARKER} appears before {BEGIN_MARKER}")); + } + + let after_end = end + END_MARKER.len(); + Ok(format!( + "{}{}{}", + &content[..start], + generated_marked_section(), + &content[after_end..] + )) +} + +fn find_command<'a>(root: &'a Command, path: &[&str]) -> &'a Command { + let mut current = root; + for part in path { + current = current + .get_subcommands() + .find(|command| command.get_name() == *part) + .unwrap_or_else(|| panic!("missing deps skill command metadata: {part}")); + } + current +} + +fn important_flags(command: &Command) -> Vec { + command + .get_arguments() + .filter_map(|arg| arg.get_long()) + .filter(|long| *long != "help" && *long != "version") + .map(|long| format!("`--{long}`")) + .collect() +} diff --git a/src/deps/tests/common.rs b/src/deps/tests/common.rs new file mode 100644 index 0000000..a2d8c37 --- /dev/null +++ b/src/deps/tests/common.rs @@ -0,0 +1,15 @@ +use std::path::PathBuf; + +use crate::deps::policy::Policy; +use crate::deps::{scan, Inventory}; + +pub fn fixture(name: &str) -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("tests/fixtures") + .join(name) +} + +pub fn scan_fixture(name: &str) -> Inventory { + scan(&fixture(name), &Policy::default()) + .unwrap_or_else(|e| panic!("scan of fixture {name} failed: {e:?}")) +} diff --git a/src/deps/tests/correctness_tests.rs b/src/deps/tests/correctness_tests.rs new file mode 100644 index 0000000..4db3947 --- /dev/null +++ b/src/deps/tests/correctness_tests.rs @@ -0,0 +1,46 @@ +use super::common::scan_fixture; +use crate::deps::model::Severity; + +#[test] +fn node_locked_transitive_range_yields_no_finding() { + let inv = scan_fixture("node-app"); + assert!( + inv.findings_for("qs") + .iter() + .all(|f| f.id != "DEP003" && f.id != "DEP004"), + "locked transitive qs must not raise pinning finding" + ); +} + +#[test] +fn node_direct_locked_range_is_medium_not_high() { + let inv = scan_fixture("node-app"); + let dep003 = inv + .findings_for("express") + .into_iter() + .find(|f| f.id == "DEP003") + .expect("expected DEP003 for express"); + assert_eq!(dep003.severity, Severity::Medium); + assert!(dep003.reproducible); +} + +#[test] +fn pypi_locked_transitive_range_yields_no_finding() { + let inv = scan_fixture("python-poetry"); + assert!( + inv.findings_for("urllib3").is_empty(), + "locked transitive urllib3 must produce no findings" + ); +} + +#[test] +fn gradle_locked_dynamic_version_is_reproducible() { + let inv = scan_fixture("java-gradle"); + let dep003 = inv + .findings_for("commons-lang3") + .into_iter() + .find(|f| f.id == "DEP003") + .expect("dynamic direct version should warn DEP003"); + assert_eq!(dep003.severity, Severity::Medium); + assert!(dep003.reproducible); +} diff --git a/src/deps/tests/detect_tests.rs b/src/deps/tests/detect_tests.rs new file mode 100644 index 0000000..b4ee1aa --- /dev/null +++ b/src/deps/tests/detect_tests.rs @@ -0,0 +1,50 @@ +use super::common::fixture; +use crate::deps::detect::{detect_dependency_files, DepFileKind}; +use crate::deps::model::Ecosystem; + +fn kinds(root: &str) -> Vec { + let mut k: Vec<_> = detect_dependency_files(&fixture(root)) + .into_iter() + .map(|f| f.kind) + .collect(); + k.sort_by_key(|x| format!("{x:?}")); + k +} + +#[test] +fn detect_finds_npm_files() { + let k = kinds("node-app"); + assert!(k.contains(&DepFileKind::NpmManifest)); + assert!(k.contains(&DepFileKind::NpmLockfile)); +} + +#[test] +fn detect_finds_python_poetry_files() { + let k = kinds("python-poetry"); + assert!(k.contains(&DepFileKind::PyProject)); + assert!(k.contains(&DepFileKind::PoetryLock)); +} + +#[test] +fn detect_finds_pip_requirements() { + let files = detect_dependency_files(&fixture("python-pip-nolock")); + assert!(files.iter().any(|f| f.kind == DepFileKind::PipRequirements)); + assert!(files.iter().all(|f| f.ecosystem == Ecosystem::PyPI)); +} + +#[test] +fn detect_finds_maven_pom() { + assert!(kinds("java-maven").contains(&DepFileKind::MavenPom)); +} + +#[test] +fn detect_finds_gradle_files() { + let k = kinds("java-gradle"); + assert!(k.contains(&DepFileKind::GradleBuild)); + assert!(k.contains(&DepFileKind::GradleLockfile)); +} + +#[test] +fn detect_finds_go_mod_smoke() { + assert!(kinds("go-mod-smoke").contains(&DepFileKind::GoMod)); +} diff --git a/src/deps/tests/diff_tests.rs b/src/deps/tests/diff_tests.rs new file mode 100644 index 0000000..121642a --- /dev/null +++ b/src/deps/tests/diff_tests.rs @@ -0,0 +1,132 @@ +use crate::deps::diff::diff_graphs; +use crate::deps::model::{DependencyGraph, DependencyNode, Ecosystem, PackageId}; + +fn graph(nodes: Vec) -> DependencyGraph { + DependencyGraph { + nodes, + edges: vec![], + } +} + +fn npm_with_artifact( + name: &str, + version: &str, + resolved: Option<&str>, + integrity: Option<&str>, +) -> DependencyNode { + let mut node = DependencyNode::new_npm(name, version); + node.lock_resolved = resolved.map(str::to_string); + node.lock_integrity_hash = integrity.map(str::to_string); + node +} + +fn pypi_node(name: &str, version: &str) -> DependencyNode { + let mut node = DependencyNode::new_npm(name, version); + node.id = PackageId::pypi(name, version); + node.ecosystem = Ecosystem::PyPI; + node +} + +#[test] +fn diff_detects_added_removed_changed() { + let base = graph(vec![ + DependencyNode::new_npm("lodash", "4.17.20"), + DependencyNode::new_npm("request", "2.88.2"), + ]); + let head = graph(vec![ + DependencyNode::new_npm("lodash", "4.17.21"), + DependencyNode::new_npm("axios", "1.8.2"), + ]); + let d = diff_graphs(&base, &head); + assert!(d.added.iter().any(|n| n.name() == "axios")); + assert!(d.removed.iter().any(|n| n.name() == "request")); + assert!(d + .changed + .iter() + .any(|c| c.name == "lodash" && c.from == "4.17.20" && c.to == "4.17.21")); + assert!(d.added.iter().all(|n| n.name() != "lodash")); +} + +#[test] +fn diff_detects_same_version_integrity_change() { + let base = graph(vec![npm_with_artifact( + "lodash", + "4.17.21", + Some("https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz"), + Some("sha512-old"), + )]); + let head = graph(vec![npm_with_artifact( + "lodash", + "4.17.21", + Some("https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz"), + Some("sha512-new"), + )]); + + let d = diff_graphs(&base, &head); + + assert!(d.changed.is_empty()); + assert_eq!(d.artifact_changed.len(), 1); + let change = &d.artifact_changed[0]; + assert_eq!(change.name, "lodash"); + assert_eq!(change.version, "4.17.21"); + assert_eq!(change.old_integrity.as_deref(), Some("sha512-old")); + assert_eq!(change.new_integrity.as_deref(), Some("sha512-new")); +} + +#[test] +fn diff_detects_same_version_resolved_change() { + let base = graph(vec![npm_with_artifact( + "lodash", + "4.17.21", + Some("https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz"), + Some("sha512-same"), + )]); + let head = graph(vec![npm_with_artifact( + "lodash", + "4.17.21", + Some("https://mirror.example/lodash/-/lodash-4.17.21.tgz"), + Some("sha512-same"), + )]); + + let d = diff_graphs(&base, &head); + + assert!(d.changed.is_empty()); + assert_eq!(d.artifact_changed.len(), 1); + let change = &d.artifact_changed[0]; + assert_eq!( + change.old_resolved.as_deref(), + Some("https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz") + ); + assert_eq!( + change.new_resolved.as_deref(), + Some("https://mirror.example/lodash/-/lodash-4.17.21.tgz") + ); +} + +#[test] +fn diff_does_not_collapse_same_name_across_ecosystems() { + let base = graph(vec![DependencyNode::new_npm("foo", "1.0.0")]); + let head = graph(vec![ + DependencyNode::new_npm("foo", "1.0.0"), + pypi_node("foo", "2.0.0"), + ]); + + let d = diff_graphs(&base, &head); + + assert!(d.changed.is_empty()); + assert!(d.added.iter().any(|n| n.id().0 == "pkg:pypi/foo@2.0.0")); +} + +#[test] +fn diff_reports_removed_duplicate_version() { + let base = graph(vec![ + DependencyNode::new_npm("foo", "1.0.0"), + DependencyNode::new_npm("foo", "2.0.0"), + ]); + let head = graph(vec![DependencyNode::new_npm("foo", "2.0.0")]); + + let d = diff_graphs(&base, &head); + + assert!(d.changed.is_empty()); + assert!(d.removed.iter().any(|n| n.id().0 == "pkg:npm/foo@1.0.0")); +} diff --git a/src/deps/tests/explain_tests.rs b/src/deps/tests/explain_tests.rs new file mode 100644 index 0000000..49efb9b --- /dev/null +++ b/src/deps/tests/explain_tests.rs @@ -0,0 +1,20 @@ +use super::common::scan_fixture; +use crate::deps::explain::explain; + +#[test] +fn explain_transitive_shows_path() { + let inv = scan_fixture("node-app"); + let e = explain(&inv.graph, "qs").expect("qs should be explainable"); + assert!(!e.direct); + assert_eq!(e.depth, 2); + let path = e.paths.first().expect("at least one path"); + assert_eq!(path.first().map(|id| id.0.as_str()), Some("root")); + assert!(path.iter().any(|id| id.name() == "express")); + assert_eq!(path.last().map(|id| id.name()), Some("qs")); +} + +#[test] +fn explain_unknown_package_is_none() { + let inv = scan_fixture("node-app"); + assert!(explain(&inv.graph, "does-not-exist").is_none()); +} diff --git a/src/deps/tests/findings_tests.rs b/src/deps/tests/findings_tests.rs new file mode 100644 index 0000000..5663d37 --- /dev/null +++ b/src/deps/tests/findings_tests.rs @@ -0,0 +1,25 @@ +use super::common::scan_fixture; +use crate::deps::model::Severity; + +#[test] +fn pip_no_lockfile_is_dep001() { + let inv = scan_fixture("python-pip-nolock"); + let f = inv.with_code("DEP001"); + assert!(!f.is_empty()); + assert_eq!(f[0].severity, Severity::High); +} + +#[test] +fn poetry_lock_present_no_dep001() { + assert!(scan_fixture("python-poetry").with_code("DEP001").is_empty()); +} + +#[test] +fn maven_no_lockfile_is_dep001() { + assert!(!scan_fixture("java-maven").with_code("DEP001").is_empty()); +} + +#[test] +fn gradle_lock_present_no_dep001() { + assert!(scan_fixture("java-gradle").with_code("DEP001").is_empty()); +} diff --git a/src/deps/tests/maven_tests.rs b/src/deps/tests/maven_tests.rs new file mode 100644 index 0000000..6d6390d --- /dev/null +++ b/src/deps/tests/maven_tests.rs @@ -0,0 +1,129 @@ +use crate::deps::ecosystems::classify_constraint; +use crate::deps::model::{ConstraintKind, Ecosystem::Maven}; + +#[test] +fn maven_classify_hard_version_is_exact() { + assert_eq!( + classify_constraint(Maven, "32.1.3-jre"), + ConstraintKind::Exact + ); +} + +#[test] +fn maven_classify_version_range_is_bounded_range() { + assert_eq!( + classify_constraint(Maven, "[3.0,4.0)"), + ConstraintKind::BoundedRange + ); +} + +#[test] +fn maven_classify_latest_keyword_is_unbounded() { + assert_eq!( + classify_constraint(Maven, "LATEST"), + ConstraintKind::Unbounded + ); + assert_eq!( + classify_constraint(Maven, "RELEASE"), + ConstraintKind::Unbounded + ); +} + +#[test] +fn maven_classify_snapshot_is_mutable() { + assert_eq!( + classify_constraint(Maven, "2.0-SNAPSHOT"), + ConstraintKind::Mutable + ); +} + +#[test] +fn gradle_classify_dynamic_plus_is_bounded_range() { + assert_eq!( + classify_constraint(Maven, "3.+"), + ConstraintKind::BoundedRange + ); +} + +#[test] +fn gradle_classify_latest_release_is_unbounded() { + assert_eq!( + classify_constraint(Maven, "latest.release"), + ConstraintKind::Unbounded + ); +} + +use super::common::scan_fixture; +use crate::deps::model::{PackageId, Severity}; + +#[test] +fn maven_graph_lists_all_direct_dependencies() { + let inv = scan_fixture("java-maven"); + for name in ["guava", "commons-lang3", "slf4j-api", "internal-bom"] { + let n = inv + .node(name) + .unwrap_or_else(|| panic!("{name} node missing")); + assert!(n.is_direct(), "{name} is direct"); + } +} + +#[test] +fn maven_purl_identity_includes_group() { + assert_eq!( + *scan_fixture("java-gradle").node("guava").unwrap().id(), + PackageId("pkg:maven/com.google.guava/guava@32.1.3-jre".into()) + ); +} + +#[test] +fn gradle_graph_resolves_dynamic_version_from_lockfile() { + assert_eq!( + scan_fixture("java-gradle") + .node("commons-lang3") + .expect("commons-lang3 node missing") + .version(), + Some("3.14.0") + ); +} + +#[test] +fn maven_range_direct_dep_is_dep003() { + assert!(scan_fixture("java-maven") + .findings_for("commons-lang3") + .iter() + .any(|f| f.id == "DEP003")); +} + +#[test] +fn maven_exact_dep_has_no_pinning_finding() { + assert!(scan_fixture("java-maven") + .findings_for("guava") + .iter() + .all(|f| f.id != "DEP003" && f.id != "DEP004")); +} + +#[test] +fn maven_latest_keyword_is_dep004() { + let inv = scan_fixture("java-maven"); + let f = inv + .findings_for("slf4j-api") + .into_iter() + .find(|f| f.id == "DEP004") + .expect("slf4j-api LATEST must raise DEP004"); + assert_eq!(f.severity, Severity::High); +} + +#[test] +fn maven_snapshot_is_dep021_high() { + let inv = scan_fixture("java-maven"); + let f = inv + .findings_for("internal-bom") + .into_iter() + .find(|f| f.id == "DEP021") + .expect("2.0-SNAPSHOT must raise DEP021"); + assert_eq!(f.severity, Severity::High); + assert!( + f.recommendation.to_lowercase().contains("snapshot"), + "recommendation should name SNAPSHOT" + ); +} diff --git a/src/deps/tests/mod.rs b/src/deps/tests/mod.rs new file mode 100644 index 0000000..4bd37a0 --- /dev/null +++ b/src/deps/tests/mod.rs @@ -0,0 +1,13 @@ +mod common; +mod correctness_tests; +mod detect_tests; +mod diff_tests; +mod explain_tests; +mod findings_tests; +mod maven_tests; +mod npm_tests; +mod policy_tests; +mod pypi_tests; +mod report_tests; +mod robustness_tests; +mod slice0_tests; diff --git a/src/deps/tests/npm_tests.rs b/src/deps/tests/npm_tests.rs new file mode 100644 index 0000000..b06be85 --- /dev/null +++ b/src/deps/tests/npm_tests.rs @@ -0,0 +1,350 @@ +use crate::deps::ecosystems::classify_constraint; +use crate::deps::model::{ConstraintKind, Ecosystem::Npm}; + +#[test] +fn npm_classify_exact_version() { + assert_eq!(classify_constraint(Npm, "4.18.2"), ConstraintKind::Exact); +} + +#[test] +fn npm_classify_caret_is_bounded_range() { + assert_eq!( + classify_constraint(Npm, "^4.18.2"), + ConstraintKind::BoundedRange + ); +} + +#[test] +fn npm_classify_wildcard_is_unbounded() { + assert_eq!(classify_constraint(Npm, "*"), ConstraintKind::Unbounded); +} + +#[test] +fn npm_classify_latest_is_unbounded() { + assert_eq!( + classify_constraint(Npm, "latest"), + ConstraintKind::Unbounded + ); +} + +#[test] +fn npm_classify_git_branch_is_mutable_ref() { + assert_eq!( + classify_constraint(Npm, "git+https://github.com/acme/x.git#main"), + ConstraintKind::GitRef { mutable: true } + ); +} + +#[test] +fn npm_classify_git_commit_sha_is_immutable_ref() { + let sha = "git+https://github.com/acme/x.git#0bc1a2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9"; + assert_eq!( + classify_constraint(Npm, sha), + ConstraintKind::GitRef { mutable: false } + ); +} + +use super::common::scan_fixture; +use crate::deps::explain::explain; +use crate::deps::model::{PackageId, Scope, Severity, SourceType}; +use crate::deps::policy::Policy; +use crate::deps::scan; + +#[test] +fn npm_graph_classifies_express_as_direct_production() { + let inv = scan_fixture("node-app"); + let express = inv.node("express").expect("express node missing"); + assert!(express.is_direct()); + assert_eq!(express.scope(), Scope::Production); + assert_eq!(express.version(), Some("4.18.2")); +} + +#[test] +fn npm_graph_classifies_qs_as_transitive() { + let inv = scan_fixture("node-app"); + let qs = inv.node("qs").expect("qs node missing"); + assert!(!qs.is_direct()); + assert!(qs.depth() >= 2); +} + +#[test] +fn npm_graph_classifies_jest_as_development_scope() { + let inv = scan_fixture("node-app"); + assert_eq!( + inv.node("jest").expect("jest node missing").scope(), + Scope::Development + ); +} + +#[test] +fn npm_graph_marks_git_dep_source_type() { + let inv = scan_fixture("node-app"); + let git_dep = inv + .node("internal-utils") + .expect("internal-utils node missing"); + assert_eq!(git_dep.source_type(), SourceType::GitBranch); +} + +#[test] +fn npm_purl_identity_is_canonical() { + let inv = scan_fixture("node-app"); + assert_eq!( + *inv.node("lodash").unwrap().id(), + PackageId("pkg:npm/lodash@4.17.21".into()) + ); +} + +#[test] +fn npm_caret_direct_dep_is_dep003() { + let inv = scan_fixture("node-app"); + assert!( + !inv.findings_for("express").is_empty() + && inv.findings_for("express").iter().any(|f| f.id == "DEP003") + ); +} + +#[test] +fn npm_exact_dev_dep_has_no_pinning_finding() { + let inv = scan_fixture("node-app"); + assert!(inv + .findings_for("jest") + .iter() + .all(|f| f.id != "DEP003" && f.id != "DEP004")); +} + +#[test] +fn npm_wildcard_direct_dep_is_dep004_high() { + let inv = scan_fixture("node-app"); + let f = inv + .findings_for("lodash") + .into_iter() + .find(|f| f.id == "DEP004") + .expect("lodash `*` must raise DEP004"); + assert_eq!(f.severity, Severity::High); +} + +#[test] +fn npm_latest_direct_dep_is_dep004() { + let inv = scan_fixture("node-app"); + assert!( + inv.findings_for("left-pad") + .iter() + .any(|f| f.id == "DEP004"), + "left-pad `latest` must raise DEP004" + ); +} + +#[test] +fn npm_git_branch_dep_is_dep005() { + let inv = scan_fixture("node-app"); + let f = inv + .findings_for("internal-utils") + .into_iter() + .find(|f| f.id == "DEP005") + .expect("internal-utils @ #main is DEP005"); + assert_eq!(f.severity, Severity::High); +} + +#[test] +fn git_commit_sha_is_not_dep005() { + let pinned = "git+https://github.com/acme/x.git#0bc1a2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9"; + assert_eq!( + classify_constraint(Npm, pinned), + ConstraintKind::GitRef { mutable: false } + ); +} + +#[test] +fn npm_url_dep_without_checksum_is_dep006() { + assert_eq!( + classify_constraint(Npm, "https://example.com/pkg/foo-1.0.0.tgz"), + ConstraintKind::Url { checksum: false } + ); +} + +#[test] +fn npm_lock_entry_without_integrity_is_dep008() { + let inv = scan_fixture("node-app"); + assert!( + inv.findings_for("left-pad") + .iter() + .any(|f| f.id == "DEP008"), + "left-pad lacks integrity — DEP008" + ); +} + +#[test] +fn npm_lock_entry_with_integrity_no_dep008() { + let inv = scan_fixture("node-app"); + for pkg in ["express", "qs", "lodash"] { + assert!( + inv.findings_for(pkg).iter().all(|f| f.id != "DEP008"), + "{pkg} has integrity — no DEP008" + ); + } +} + +#[test] +fn npm_lock_entry_preserves_artifact_evidence() { + let inv = scan_fixture("node-app"); + let express = inv.node("express").expect("express node missing"); + assert_eq!( + express.lock_resolved.as_deref(), + Some("https://registry.npmjs.org/express/-/express-4.18.2.tgz") + ); + assert_eq!( + express.lock_integrity_hash.as_deref(), + Some("sha512-5/PsL6iGPdfQ/lKM1UuielYgv3BUoJfz1aUwU9vHZ+J7gyvwdQXFEBIEIaxeGf0GIcreATNyBExtalisDbuMqQ==") + ); +} + +#[test] +fn node_manifest_dep_missing_from_lock_is_dep002() { + let inv = scan_fixture("node-stale"); + let f = inv.with_code("DEP002"); + assert!(!f.is_empty(), "manifest/lock drift must raise DEP002"); + assert_eq!(f[0].severity, Severity::High); +} + +#[test] +fn node_app_lock_in_sync_no_dep002() { + let inv = scan_fixture("node-app"); + assert!(inv.with_code("DEP002").is_empty()); +} + +#[test] +fn npm_graph_uses_generic_lockfile_edges() { + let inv = scan_fixture("node-transitive"); + let child = inv.node("child-lib").expect("child-lib node missing"); + assert!(!child.is_direct()); + let explanation = explain(&inv.graph, "child-lib").expect("child-lib explain"); + let paths: Vec> = explanation + .paths + .iter() + .map(|path| path.iter().map(|p| p.name().to_string()).collect()) + .collect(); + assert!( + paths + .iter() + .any(|path| path == &["root", "parent-lib", "child-lib"]), + "expected parent-lib -> child-lib path, got {paths:?}" + ); +} + +#[test] +fn npm_scoped_transitive_package_keeps_full_name() { + let inv = scan_fixture("node-transitive"); + let node = inv.node("@types/node").expect("@types/node node missing"); + assert_eq!(node.name(), "@types/node"); + assert_eq!(node.id().name(), "@types/node"); + assert_eq!(*node.id(), PackageId("pkg:npm/@types/node@18.19.0".into())); +} + +#[test] +fn yarn_lock_is_explicitly_unsupported_without_stale_noise() { + let inv = scan_fixture("node-yarn"); + assert!(!inv.with_code("DEP019").is_empty()); + assert!(inv.with_code("DEP002").is_empty()); +} + +#[test] +fn pnpm_lock_is_explicitly_unsupported_without_stale_noise() { + let inv = scan_fixture("node-pnpm"); + assert!(!inv.with_code("DEP019").is_empty()); + assert!(inv.with_code("DEP002").is_empty()); +} + +#[test] +fn npm_manifest_without_lockfile_is_dep001_not_dep002() { + let tmp = tempfile::TempDir::new().expect("temp project"); + std::fs::write( + tmp.path().join("package.json"), + r#"{ + "name": "npm-no-lock", + "version": "1.0.0", + "dependencies": { + "left-pad": "*" + } +} +"#, + ) + .expect("write package.json"); + + let inv = scan(tmp.path(), &Policy::default()).expect("scan"); + + assert!(!inv.with_code("DEP001").is_empty()); + assert!(inv.with_code("DEP002").is_empty()); + assert!(!inv.with_code("DEP004").is_empty()); +} + +#[test] +fn npm_lock_preserves_duplicate_transitive_versions() { + let tmp = tempfile::TempDir::new().expect("temp project"); + std::fs::write( + tmp.path().join("package.json"), + r#"{ + "name": "npm-duplicates", + "version": "1.0.0", + "dependencies": { + "a": "1.0.0", + "b": "1.0.0" + } +} +"#, + ) + .expect("write package.json"); + std::fs::write( + tmp.path().join("package-lock.json"), + r#"{ + "name": "npm-duplicates", + "version": "1.0.0", + "lockfileVersion": 3, + "packages": { + "": { + "name": "npm-duplicates", + "version": "1.0.0", + "dependencies": { + "a": "1.0.0", + "b": "1.0.0" + } + }, + "node_modules/a": { + "version": "1.0.0", + "integrity": "sha512-a", + "dependencies": { + "foo": "1.0.0" + } + }, + "node_modules/b": { + "version": "1.0.0", + "integrity": "sha512-b", + "dependencies": { + "foo": "2.0.0" + } + }, + "node_modules/a/node_modules/foo": { + "version": "1.0.0", + "integrity": "sha512-foo1" + }, + "node_modules/b/node_modules/foo": { + "version": "2.0.0", + "integrity": "sha512-foo2" + } + } +} +"#, + ) + .expect("write package-lock.json"); + + let inv = scan(tmp.path(), &Policy::default()).expect("scan"); + let mut versions: Vec = inv + .graph + .nodes_named("foo") + .into_iter() + .filter_map(|node| node.version().map(str::to_string)) + .collect(); + versions.sort(); + + assert_eq!(versions, vec!["1.0.0".to_string(), "2.0.0".to_string()]); + assert!(!inv.with_code("DEP014").is_empty()); +} diff --git a/src/deps/tests/policy_tests.rs b/src/deps/tests/policy_tests.rs new file mode 100644 index 0000000..ed3b12b --- /dev/null +++ b/src/deps/tests/policy_tests.rs @@ -0,0 +1,44 @@ +use super::common::{fixture, scan_fixture}; +use crate::deps::policy::Policy; +use crate::deps::scan; + +#[test] +fn default_policy_fails_on_wildcard() { + assert!(!scan_fixture("node-app").with_code("DEP004").is_empty()); +} + +#[test] +fn policy_from_yaml_parses_supported_example() { + let yaml = r#" +dependency_policy: + require_lockfile: true + fail_on_missing_lockfile: true + fail_on_stale_lockfile: true + direct_dependencies: + fail_on_wildcard: true + fail_on_latest: true + warn_on_semver_range: true +"#; + assert!(Policy::from_yaml(yaml).is_ok()); +} + +#[test] +fn default_yaml_only_contains_supported_fields() { + let yaml = Policy::default_yaml(); + assert!(!yaml.contains("allow_exact_versions")); + assert!(!yaml.contains("transitive_dependencies")); + assert!(!yaml.contains("ci:")); +} + +#[test] +fn policy_disabling_rule_silences_finding() { + let yaml = r#" +dependency_policy: + direct_dependencies: + fail_on_wildcard: false + fail_on_latest: false +"#; + let policy = Policy::from_yaml(yaml).expect("policy parses"); + let inv = scan(&fixture("node-app"), &policy).expect("scan"); + assert!(inv.with_code("DEP004").is_empty()); +} diff --git a/src/deps/tests/pypi_tests.rs b/src/deps/tests/pypi_tests.rs new file mode 100644 index 0000000..c1f4878 --- /dev/null +++ b/src/deps/tests/pypi_tests.rs @@ -0,0 +1,183 @@ +use crate::deps::ecosystems::classify_constraint; +use crate::deps::model::{ConstraintKind, Ecosystem::PyPI}; + +#[test] +fn pypi_classify_exact_pin() { + assert_eq!(classify_constraint(PyPI, "==2.3.3"), ConstraintKind::Exact); +} + +#[test] +fn pypi_classify_bare_name_is_unbounded() { + assert_eq!( + classify_constraint(PyPI, "requests"), + ConstraintKind::Unbounded + ); +} + +#[test] +fn pypi_classify_open_greater_equal_is_unbounded() { + assert_eq!( + classify_constraint(PyPI, ">=1.26"), + ConstraintKind::Unbounded + ); +} + +#[test] +fn pypi_classify_compatible_release_is_bounded_range() { + assert_eq!( + classify_constraint(PyPI, "~=2.3"), + ConstraintKind::BoundedRange + ); +} + +#[test] +fn pypi_classify_git_branch_is_mutable_ref() { + assert_eq!( + classify_constraint(PyPI, "git+https://github.com/acme/x.git@main"), + ConstraintKind::GitRef { mutable: true } + ); +} + +use super::common::scan_fixture; +use crate::deps::explain::explain; +use crate::deps::model::Scope; +use crate::deps::policy::Policy; +use crate::deps::scan; + +#[test] +fn pypi_graph_classifies_pytest_as_development_scope() { + assert_eq!( + scan_fixture("python-poetry") + .node("pytest") + .expect("pytest node missing") + .scope(), + Scope::Development + ); +} + +#[test] +fn pypi_graph_resolves_transitive_urllib3_version() { + let inv = scan_fixture("python-poetry"); + let urllib3 = inv.node("urllib3").expect("urllib3 should be in the graph"); + assert!(!urllib3.is_direct()); + assert_eq!(urllib3.version(), Some("2.0.7")); +} + +#[test] +fn pypi_exact_pin_has_no_pinning_finding() { + let inv = scan_fixture("python-pip-nolock"); + assert!(inv + .findings_for("flask") + .iter() + .all(|f| f.id != "DEP003" && f.id != "DEP004")); +} + +#[test] +fn pypi_bare_name_is_dep004() { + assert!(scan_fixture("python-pip-nolock") + .findings_for("requests") + .iter() + .any(|f| f.id == "DEP004")); +} + +#[test] +fn pypi_open_ended_range_is_dep004_high() { + use crate::deps::model::Severity; + let inv = scan_fixture("python-pip-nolock"); + let f = inv + .findings_for("urllib3") + .into_iter() + .find(|f| f.id == "DEP004") + .expect("urllib3>=1.26 must raise DEP004"); + assert_eq!(f.severity, Severity::High); +} + +#[test] +fn pypi_git_branch_dep_is_dep005() { + assert!(scan_fixture("python-pip-nolock") + .findings_for("internal-lib") + .iter() + .any(|f| f.id == "DEP005")); +} + +#[test] +fn poetry_graph_uses_dependency_tables_for_multiple_parents() { + let inv = scan_fixture("python-poetry-multi"); + for (target, parent) in [("gamma", "alpha"), ("delta", "beta")] { + let node = inv.node(target).expect("transitive node missing"); + assert!(!node.is_direct()); + let explanation = explain(&inv.graph, target).expect("transitive explain"); + let paths: Vec> = explanation + .paths + .iter() + .map(|path| path.iter().map(|p| p.name().to_string()).collect()) + .collect(); + assert!( + paths.iter().any(|path| path == &["root", parent, target]), + "expected {parent} -> {target} path, got {paths:?}" + ); + } +} + +#[test] +fn uv_lock_does_not_suppress_requirements_scan() { + let inv = scan_fixture("python-uv-requirements"); + assert!(!inv.with_code("DEP001").is_empty()); + assert!(inv + .findings_for("requests") + .iter() + .any(|f| f.id == "DEP004")); +} + +#[test] +fn pyproject_only_dependencies_are_scanned_without_lockfile() { + let tmp = tempfile::TempDir::new().expect("temp project"); + std::fs::write( + tmp.path().join("pyproject.toml"), + r#"[project] +name = "pyproject-only" +version = "0.1.0" +dependencies = ["requests>=2"] +"#, + ) + .expect("write pyproject.toml"); + + let inv = scan(tmp.path(), &Policy::default()).expect("scan"); + + assert!(!inv.with_code("DEP001").is_empty()); + assert!(!inv.with_code("DEP004").is_empty()); +} + +#[test] +fn poetry_manifest_and_lock_names_are_normalized() { + let tmp = tempfile::TempDir::new().expect("temp project"); + std::fs::write( + tmp.path().join("pyproject.toml"), + r#"[tool.poetry] +name = "poetry-normalized" +version = "0.1.0" + +[tool.poetry.dependencies] +python = "^3.12" +Django = "^4.2" +"#, + ) + .expect("write pyproject.toml"); + std::fs::write( + tmp.path().join("poetry.lock"), + r#"[[package]] +name = "django" +version = "4.2.7" +"#, + ) + .expect("write poetry.lock"); + + let inv = scan(tmp.path(), &Policy::default()).expect("scan"); + let nodes = inv.graph.nodes_named("django"); + + assert_eq!(nodes.len(), 1); + assert_eq!(nodes[0].version(), Some("4.2.7")); + assert!(nodes[0].is_direct()); + assert!(inv.graph.nodes_named("Django").is_empty()); + assert!(inv.with_code("DEP001").is_empty()); +} diff --git a/src/deps/tests/report_tests.rs b/src/deps/tests/report_tests.rs new file mode 100644 index 0000000..038abdb --- /dev/null +++ b/src/deps/tests/report_tests.rs @@ -0,0 +1,29 @@ +use super::common::scan_fixture; +use crate::deps::report::{to_cyclonedx, to_json, to_sarif}; + +#[test] +fn report_json_has_findings_and_graph() { + let v = to_json(&scan_fixture("node-app")); + assert!(v.get("nodes").and_then(|n| n.as_array()).is_some()); + assert!(v.get("findings").and_then(|f| f.as_array()).is_some()); +} + +#[test] +fn report_sarif_has_rules_and_results() { + let v = to_sarif(&scan_fixture("node-app")); + assert_eq!(v["runs"][0]["tool"]["driver"]["name"], "corgea-deps"); + let results = v["runs"][0]["results"].as_array().expect("results array"); + assert!(results.iter().any(|r| r["ruleId"] == "DEP004")); +} + +#[test] +fn report_cyclonedx_has_components_and_deps() { + let inv = scan_fixture("node-app"); + let v = to_cyclonedx(&inv.graph); + assert_eq!(v["bomFormat"], "CycloneDX"); + let components = v["components"].as_array().expect("components array"); + assert!(components + .iter() + .any(|c| c["purl"] == "pkg:npm/express@4.18.2")); + assert!(v.get("dependencies").is_some()); +} diff --git a/src/deps/tests/robustness_tests.rs b/src/deps/tests/robustness_tests.rs new file mode 100644 index 0000000..e18aac0 --- /dev/null +++ b/src/deps/tests/robustness_tests.rs @@ -0,0 +1,105 @@ +use super::common::{fixture, scan_fixture}; +use crate::deps::ecosystems::classify_constraint; +use crate::deps::model::Ecosystem; +use crate::deps::policy::Policy; +use crate::deps::report::to_json; +use crate::deps::scan; + +#[test] +fn robust_malformed_npm_lockfile_is_error_not_panic() { + let result = scan(&fixture("malformed"), &Policy::default()); + assert!(result.is_err()); +} + +#[test] +fn robust_truncated_poetry_lock_is_error_not_panic() { + let result = std::panic::catch_unwind(|| scan(&fixture("malformed"), &Policy::default())); + assert!(result.is_ok()); +} + +#[test] +fn robust_classify_never_panics_on_adversarial_input() { + let corpus = [ + "", + " ", + "\t\n", + "^", + "~", + ">=", + "@", + "git+", + "#", + "[", + "[,]", + "999999999999999999999999999999", + "v1.2.3", + "==", + "*.*.*", + "latest.latest", + "-SNAPSHOT", + "💥", + "../../etc/passwd", + ]; + for raw in corpus { + for eco in [Ecosystem::Npm, Ecosystem::PyPI, Ecosystem::Maven] { + let _ = classify_constraint(eco, raw); + } + } + let long = "a".repeat(10_000); + for eco in [Ecosystem::Npm, Ecosystem::PyPI, Ecosystem::Maven] { + let _ = classify_constraint(eco, &long); + } +} + +#[test] +fn robust_graph_order_deterministic() { + let a = scan_fixture("node-app"); + let b = scan_fixture("node-app"); + let names = |inv: &crate::deps::Inventory| -> Vec { + inv.graph.nodes.iter().map(|n| n.id().0.clone()).collect() + }; + assert_eq!(names(&a), names(&b)); +} + +#[test] +fn robust_json_output_byte_stable() { + let a = to_json(&scan_fixture("node-app")).to_string(); + let b = to_json(&scan_fixture("node-app")).to_string(); + assert_eq!(a, b); +} + +#[test] +fn robust_monorepo_detects_all_workspace_manifests() { + let inv = scan_fixture("node-monorepo"); + use crate::deps::detect::DepFileKind::NpmManifest; + let manifests = inv + .detected_files + .iter() + .filter(|f| f.kind == NpmManifest) + .count(); + assert!(manifests >= 3, "expected >=3 manifests, got {manifests}"); +} + +#[test] +fn robust_scan_skips_node_modules() { + use std::fs; + let tmp = tempfile::TempDir::new().expect("temp dir"); + fs::write( + tmp.path().join("package.json"), + r#"{"name":"x","version":"1.0.0","dependencies":{}}"#, + ) + .unwrap(); + let nested = tmp.path().join("node_modules/inner"); + fs::create_dir_all(&nested).unwrap(); + fs::write( + nested.join("package.json"), + r#"{"name":"inner","version":"9.9.9"}"#, + ) + .unwrap(); + + let files = crate::deps::detect::detect_dependency_files(tmp.path()); + assert!(files.iter().all(|f| !f + .path + .components() + .any(|c| { c.as_os_str() == "node_modules" }))); +} diff --git a/src/deps/tests/slice0_tests.rs b/src/deps/tests/slice0_tests.rs new file mode 100644 index 0000000..62b6c2f --- /dev/null +++ b/src/deps/tests/slice0_tests.rs @@ -0,0 +1,16 @@ +//! Slice 0 → 1 handoff: classification tests target `classify_constraint` in +//! `src/deps/ecosystems/mod.rs` (PRD_DEPS_TESTING.md §8.2, §9.4). + +use crate::deps::ecosystems::classify_constraint; +use crate::deps::model::{ConstraintKind, Ecosystem::Npm}; + +#[test] +fn slice1_classify_boundary_is_implemented() { + // When stubbing for Slice 0-only PRs, this test fails at classify_constraint + // with `unimplemented!()` — the correct red state for Slice 1. + assert_eq!(classify_constraint(Npm, "*"), ConstraintKind::Unbounded); + assert_eq!( + classify_constraint(Npm, "^4.18.2"), + ConstraintKind::BoundedRange + ); +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..49bc6d0 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod deps; diff --git a/src/main.rs b/src/main.rs index 09a8ae7..442c5a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -194,6 +194,11 @@ enum Commands { )] default_config: bool, }, + /// Offline dependency inventory: scan, graph, explain, diff, sbom, policy + Deps { + #[command(subcommand)] + command: corgea::deps::run::DepsSubcommand, + }, } #[derive(Subcommand, Debug, Clone, PartialEq)] @@ -495,6 +500,10 @@ fn main() { Some(Commands::SetupHooks { default_config }) => { setup_hooks::setup_pre_commit_hook(*default_config); } + Some(Commands::Deps { command }) => { + // Offline: no token / network. Exit code propagates fail-on policy. + std::process::exit(i32::from(corgea::deps::run::run(command.clone()))); + } None => { utils::terminal::show_welcome_message(); let _ = Cli::command().print_help(); diff --git a/tests/cli_deps.rs b/tests/cli_deps.rs new file mode 100644 index 0000000..7723e8c --- /dev/null +++ b/tests/cli_deps.rs @@ -0,0 +1,913 @@ +use std::process::Command; +use tempfile::TempDir; + +fn corgea_isolated() -> (Command, TempDir) { + let home = TempDir::new().expect("temp HOME"); + let mut cmd = Command::new(env!("CARGO_BIN_EXE_corgea")); + cmd.env("HOME", home.path()) + .env("USERPROFILE", home.path()) + .env_remove("CORGEA_TOKEN") + .env_remove("CORGEA_URL") + .env_remove("AI_AGENT") + .env_remove("CODEX_SANDBOX") + .env_remove("CLAUDECODE") + .env_remove("CLAUDE_CODE") + .env_remove("CURSOR_AGENT") + .env_remove("CURSOR_TRACE_ID") + .env_remove("GEMINI_CLI") + .env_remove("PI_AGENT"); + (cmd, home) +} + +fn fixture(name: &str) -> String { + format!("{}/tests/fixtures/{}", env!("CARGO_MANIFEST_DIR"), name) +} + +#[test] +fn cli_scan_runs_without_token_or_config() { + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .args([ + "deps", + "scan", + &fixture("python-poetry"), + "--out-format", + "json", + ]) + .output() + .expect("failed to run corgea"); + assert!( + out.status.success(), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); + let parsed: serde_json::Value = + serde_json::from_slice(&out.stdout).expect("stdout must be valid JSON"); + assert!(parsed.get("findings").is_some()); +} + +#[test] +fn cli_scan_does_not_write_outside_home() { + let (mut cmd, home) = corgea_isolated(); + cmd.args(["deps", "scan", &fixture("node-app")]) + .output() + .expect("failed to run corgea"); + assert!(home.path().exists()); +} + +#[test] +fn cli_scan_fail_on_high_exits_one() { + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .args(["deps", "scan", &fixture("node-app"), "--fail-on", "high"]) + .output() + .expect("failed to run corgea"); + assert_eq!(out.status.code(), Some(1)); +} + +#[test] +fn cli_scan_clean_fixture_fail_on_high_exits_zero() { + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .args([ + "deps", + "scan", + &fixture("python-poetry"), + "--fail-on", + "high", + ]) + .output() + .expect("failed to run corgea"); + assert_eq!(out.status.code(), Some(0)); +} + +#[test] +fn cli_scan_agent_env_defaults_to_agent_format() { + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .env("AI_AGENT", "1") + .args(["deps", "scan", &fixture("node-app")]) + .output() + .expect("failed to run corgea"); + assert!( + out.status.success(), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); + let stdout = String::from_utf8_lossy(&out.stdout); + assert!(stdout.starts_with("record\troot\t"), "stdout: {stdout}"); + assert!( + stdout.contains("\nfinding\tDEP004\tHigh\tpkg:npm/lodash@4.17.21\t"), + "stdout: {stdout}" + ); +} + +#[test] +fn cli_scan_format_human_overrides_agent_env() { + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .env("AI_AGENT", "1") + .args(["deps", "scan", &fixture("node-app"), "--format", "human"]) + .output() + .expect("failed to run corgea"); + assert!( + out.status.success(), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); + let stdout = String::from_utf8_lossy(&out.stdout); + assert!( + stdout.contains("Corgea dependency inventory"), + "stdout: {stdout}" + ); +} + +#[test] +fn cli_scan_format_json_outputs_parseable_inventory() { + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .args(["deps", "scan", &fixture("node-app"), "--format", "json"]) + .output() + .expect("failed to run corgea"); + assert!( + out.status.success(), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); + let parsed: serde_json::Value = + serde_json::from_slice(&out.stdout).expect("stdout must be valid JSON"); + assert!(parsed.get("nodes").is_some()); + assert!(parsed.get("findings").is_some()); + assert!( + !String::from_utf8_lossy(&out.stdout).contains("Hint:"), + "stdout: {}", + String::from_utf8_lossy(&out.stdout) + ); + assert!( + String::from_utf8_lossy(&out.stderr).contains("Hint: Run `corgea deps explain"), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); + assert!( + String::from_utf8_lossy(&out.stderr).contains("Hint: Run `corgea deps diff"), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); +} + +#[test] +fn cli_scan_format_quiet_suppresses_stdout_and_preserves_fail_code() { + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .args([ + "deps", + "scan", + &fixture("node-app"), + "--format", + "quiet", + "--fail-on", + "high", + ]) + .output() + .expect("failed to run corgea"); + assert_eq!(out.status.code(), Some(1)); + assert_eq!(out.stdout, b""); +} + +#[test] +fn cli_scan_agent_hints_go_to_stderr_only() { + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .args(["deps", "scan", &fixture("node-app"), "--format", "agent"]) + .output() + .expect("failed to run corgea"); + assert!( + out.status.success(), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); + let stdout = String::from_utf8_lossy(&out.stdout); + let stderr = String::from_utf8_lossy(&out.stderr); + assert!(stdout.starts_with("record\troot\t"), "stdout: {stdout}"); + assert!(!stdout.contains("Hint:"), "stdout: {stdout}"); + assert!( + stderr.contains("Hint: Run `corgea deps explain"), + "stderr: {stderr}" + ); + assert!( + stderr.contains("Hint: Run `corgea deps diff"), + "stderr: {stderr}" + ); +} + +#[test] +fn cli_graph_format_json_outputs_parseable_nodes() { + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .args(["deps", "graph", &fixture("node-app"), "--format", "json"]) + .output() + .expect("failed to run corgea"); + assert!( + out.status.success(), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); + let parsed: serde_json::Value = + serde_json::from_slice(&out.stdout).expect("stdout must be valid JSON"); + let nodes = parsed["nodes"].as_array().expect("nodes array"); + assert!(nodes + .iter() + .any(|node| node["id"] == "pkg:npm/left-pad@1.3.0")); +} + +#[test] +fn cli_deps_help_includes_copy_paste_examples() { + let cases = [ + ( + vec!["deps", "scan", "--help"], + vec![ + "Examples:", + "corgea deps scan --format agent", + "corgea deps scan --out-format sarif --out-file deps.sarif", + ], + ), + ( + vec!["deps", "graph", "--help"], + vec![ + "Examples:", + "corgea deps graph --format agent", + "corgea deps graph tests/fixtures/node-app --format json", + ], + ), + ( + vec!["deps", "explain", "--help"], + vec![ + "Examples:", + "corgea deps explain lodash --format agent", + "corgea deps explain left-pad tests/fixtures/node-app --format json", + ], + ), + ( + vec!["deps", "diff", "--help"], + vec![ + "Examples:", + "corgea deps diff --base origin/main --format json", + "corgea deps diff --base HEAD . --fail-on-new high", + ], + ), + ( + vec!["deps", "sbom", "--help"], + vec![ + "Examples:", + "corgea deps sbom --format cyclonedx", + "corgea deps sbom --format cyclonedx --out bom.json", + ], + ), + ( + vec!["deps", "policy", "init", "--help"], + vec![ + "Examples:", + "corgea deps policy init", + "corgea deps policy init --exist-ok --format quiet", + ], + ), + ]; + + for (args, expected) in cases { + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .args(args.clone()) + .output() + .expect("failed to run corgea"); + assert!( + out.status.success(), + "args: {:?}\nstderr: {}", + args, + String::from_utf8_lossy(&out.stderr) + ); + let stdout = String::from_utf8_lossy(&out.stdout); + for needle in expected { + assert!( + stdout.contains(needle), + "args: {:?}\nmissing: {needle}\nstdout: {stdout}", + args + ); + } + } +} + +#[test] +fn cli_deps_rejects_invalid_render_format() { + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .args(["deps", "graph", &fixture("node-app"), "--format", "typo"]) + .output() + .expect("failed to run corgea"); + assert_ne!(out.status.code(), Some(0)); + assert!( + String::from_utf8_lossy(&out.stderr).contains("unsupported --format"), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); +} + +#[test] +fn cli_deps_without_subcommand_exits_nonzero() { + let (mut cmd, _home) = corgea_isolated(); + let out = cmd.args(["deps"]).output().expect("failed to run corgea"); + assert_ne!(out.status.code(), Some(0)); +} + +#[test] +fn cli_scan_out_file_writes_json() { + let (mut cmd, home) = corgea_isolated(); + let out_file = home.path().join("deps.json"); + let out = cmd + .args([ + "deps", + "scan", + &fixture("java-gradle"), + "--out-format", + "json", + "--out-file", + out_file.to_str().unwrap(), + ]) + .output() + .expect("failed to run corgea"); + assert!( + out.status.success(), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); + let written = std::fs::read_to_string(&out_file).expect("out-file should exist"); + let _: serde_json::Value = serde_json::from_str(&written).expect("valid JSON"); +} + +#[test] +fn cli_scan_rejects_invalid_out_format() { + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .args(["deps", "scan", &fixture("node-app"), "--out-format", "typo"]) + .output() + .expect("failed to run corgea"); + assert_ne!(out.status.code(), Some(0)); + assert!( + String::from_utf8_lossy(&out.stderr).contains("unsupported --out-format"), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); +} + +#[test] +fn cli_scan_rejects_render_format_with_out_format() { + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .args([ + "deps", + "scan", + &fixture("node-app"), + "--format", + "agent", + "--out-format", + "json", + ]) + .output() + .expect("failed to run corgea"); + assert_ne!(out.status.code(), Some(0)); + assert!( + String::from_utf8_lossy(&out.stderr).contains("--format cannot be used with --out-format"), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); +} + +#[test] +fn cli_scan_rejects_invalid_fail_on_severity() { + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .args(["deps", "scan", &fixture("node-app"), "--fail-on", "hihg"]) + .output() + .expect("failed to run corgea"); + assert_ne!(out.status.code(), Some(0)); + assert!( + String::from_utf8_lossy(&out.stderr).contains("unsupported severity for --fail-on"), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); +} + +#[test] +fn cli_scan_loads_policy_created_by_policy_init() { + let project = TempDir::new().expect("temp project"); + write_exact_node_project(project.path(), "lodash", "*", "4.17.21"); + + let (mut default_cmd, _home) = corgea_isolated(); + let default_out = default_cmd + .args([ + "deps", + "scan", + project.path().to_str().unwrap(), + "--out-format", + "json", + ]) + .output() + .expect("failed to run corgea"); + assert!(default_out.status.success()); + let default_json: serde_json::Value = + serde_json::from_slice(&default_out.stdout).expect("valid JSON"); + assert!(finding_ids(&default_json).contains(&"DEP004".to_string())); + + let (mut init_cmd, _home) = corgea_isolated(); + let init_out = init_cmd + .args(["deps", "policy", "init", project.path().to_str().unwrap()]) + .output() + .expect("failed to run corgea"); + assert!( + init_out.status.success(), + "stderr: {}", + String::from_utf8_lossy(&init_out.stderr) + ); + let policy_path = project.path().join(".corgea").join("deps.yml"); + let policy = std::fs::read_to_string(&policy_path) + .expect("policy init should create .corgea/deps.yml") + .replace("fail_on_wildcard: true", "fail_on_wildcard: false") + .replace("fail_on_latest: true", "fail_on_latest: false"); + std::fs::write(&policy_path, policy).expect("write edited policy"); + + let (mut scan_cmd, _home) = corgea_isolated(); + let out = scan_cmd + .args([ + "deps", + "scan", + project.path().to_str().unwrap(), + "--out-format", + "json", + ]) + .output() + .expect("failed to run corgea"); + assert!( + out.status.success(), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); + let parsed: serde_json::Value = serde_json::from_slice(&out.stdout).expect("valid JSON"); + assert!(!finding_ids(&parsed).contains(&"DEP004".to_string())); +} + +#[test] +fn cli_policy_init_exist_ok_preserves_existing_policy() { + let project = TempDir::new().expect("temp project"); + let policy_dir = project.path().join(".corgea"); + std::fs::create_dir_all(&policy_dir).expect("create policy dir"); + let policy_path = policy_dir.join("deps.yml"); + std::fs::write(&policy_path, "custom: true\n").expect("write existing policy"); + + let (mut init_cmd, _home) = corgea_isolated(); + let init_out = init_cmd + .args([ + "deps", + "policy", + "init", + project.path().to_str().unwrap(), + "--exist-ok", + "--format", + "json", + ]) + .output() + .expect("failed to run corgea"); + assert!( + init_out.status.success(), + "stderr: {}", + String::from_utf8_lossy(&init_out.stderr) + ); + let parsed: serde_json::Value = + serde_json::from_slice(&init_out.stdout).expect("stdout must be valid JSON"); + assert_eq!(parsed["created"], false); + assert!( + !String::from_utf8_lossy(&init_out.stdout).contains("Hint:"), + "stdout: {}", + String::from_utf8_lossy(&init_out.stdout) + ); + assert!( + String::from_utf8_lossy(&init_out.stderr).contains("Hint: Run `corgea deps scan"), + "stderr: {}", + String::from_utf8_lossy(&init_out.stderr) + ); + assert_eq!( + std::fs::read_to_string(&policy_path).expect("read existing policy"), + "custom: true\n" + ); +} + +#[test] +fn cli_scan_fails_closed_on_invalid_policy_yaml() { + let project = TempDir::new().expect("temp project"); + write_exact_node_project(project.path(), "lodash", "4.17.21", "4.17.21"); + let policy_dir = project.path().join(".corgea"); + std::fs::create_dir_all(&policy_dir).expect("create policy dir"); + std::fs::write(policy_dir.join("deps.yml"), "dependency_policy: [") + .expect("write invalid policy"); + + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .args(["deps", "scan", project.path().to_str().unwrap()]) + .output() + .expect("failed to run corgea"); + assert_ne!(out.status.code(), Some(0)); + assert!( + String::from_utf8_lossy(&out.stderr).contains("invalid policy YAML") + || String::from_utf8_lossy(&out.stderr).contains("invalid policy"), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); +} + +#[test] +fn cli_diff_same_ref_has_empty_diff() { + let repo = TempDir::new().expect("temp repo"); + init_git_repo(repo.path()); + write_exact_node_project(repo.path(), "left-pad", "1.3.0", "1.3.0"); + commit_all(repo.path(), "base"); + + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .current_dir(repo.path()) + .args(["deps", "diff", "--base", "HEAD", "."]) + .output() + .expect("failed to run corgea"); + assert!( + out.status.success(), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); + let stdout = String::from_utf8_lossy(&out.stdout); + assert!(!stdout.contains("\n + "), "stdout: {stdout}"); + assert!(!stdout.contains("\n - "), "stdout: {stdout}"); + assert!(!stdout.contains("\n ~ "), "stdout: {stdout}"); +} + +#[test] +fn cli_diff_reports_real_version_change_from_base_ref() { + let repo = TempDir::new().expect("temp repo"); + init_git_repo(repo.path()); + write_exact_node_project(repo.path(), "left-pad", "1.3.0", "1.3.0"); + commit_all(repo.path(), "base"); + write_exact_node_project(repo.path(), "left-pad", "1.3.1", "1.3.1"); + + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .current_dir(repo.path()) + .args(["deps", "diff", "--base", "HEAD", "."]) + .output() + .expect("failed to run corgea"); + assert!( + out.status.success(), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); + let stdout = String::from_utf8_lossy(&out.stdout); + assert!( + stdout.contains("~ left-pad 1.3.0 -> 1.3.1"), + "stdout: {stdout}" + ); +} + +#[test] +fn cli_diff_format_json_reports_real_version_change_from_base_ref() { + let repo = TempDir::new().expect("temp repo"); + init_git_repo(repo.path()); + write_exact_node_project(repo.path(), "left-pad", "1.3.0", "1.3.0"); + commit_all(repo.path(), "base"); + write_exact_node_project(repo.path(), "left-pad", "1.3.1", "1.3.1"); + + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .current_dir(repo.path()) + .args(["deps", "diff", "--base", "HEAD", ".", "--format", "json"]) + .output() + .expect("failed to run corgea"); + assert!( + out.status.success(), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); + let parsed: serde_json::Value = + serde_json::from_slice(&out.stdout).expect("stdout must be valid JSON"); + assert_eq!(parsed["changed"][0]["name"], "left-pad"); + assert_eq!(parsed["changed"][0]["from"], "1.3.0"); + assert_eq!(parsed["changed"][0]["to"], "1.3.1"); +} + +#[test] +fn cli_diff_reports_same_version_integrity_change_from_base_ref() { + let repo = TempDir::new().expect("temp repo"); + init_git_repo(repo.path()); + write_exact_node_project_with_artifact(repo.path(), "left-pad", "1.3.0", "1.3.0", "sha512-old"); + commit_all(repo.path(), "base"); + write_exact_node_project_with_artifact(repo.path(), "left-pad", "1.3.0", "1.3.0", "sha512-new"); + + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .current_dir(repo.path()) + .args(["deps", "diff", "--base", "HEAD", "."]) + .output() + .expect("failed to run corgea"); + assert!( + out.status.success(), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); + let stdout = String::from_utf8_lossy(&out.stdout); + assert!( + stdout.contains("~ left-pad 1.3.0 integrity changed"), + "stdout: {stdout}" + ); +} + +#[test] +fn cli_diff_format_json_reports_same_version_integrity_change_from_base_ref() { + let repo = TempDir::new().expect("temp repo"); + init_git_repo(repo.path()); + write_exact_node_project_with_artifact(repo.path(), "left-pad", "1.3.0", "1.3.0", "sha512-old"); + commit_all(repo.path(), "base"); + write_exact_node_project_with_artifact(repo.path(), "left-pad", "1.3.0", "1.3.0", "sha512-new"); + + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .current_dir(repo.path()) + .args(["deps", "diff", "--base", "HEAD", ".", "--format", "json"]) + .output() + .expect("failed to run corgea"); + assert!( + out.status.success(), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); + let parsed: serde_json::Value = + serde_json::from_slice(&out.stdout).expect("stdout must be valid JSON"); + assert_eq!(parsed["artifact_changed"][0]["name"], "left-pad"); + assert_eq!(parsed["artifact_changed"][0]["version"], "1.3.0"); + assert_eq!( + parsed["artifact_changed"][0]["integrity"]["from"], + "sha512-old" + ); + assert_eq!( + parsed["artifact_changed"][0]["integrity"]["to"], + "sha512-new" + ); +} + +#[test] +fn cli_diff_fail_on_new_ignores_existing_high_findings() { + let repo = TempDir::new().expect("temp repo"); + init_git_repo(repo.path()); + write_exact_node_project(repo.path(), "lodash", "*", "4.17.21"); + commit_all(repo.path(), "base"); + + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .current_dir(repo.path()) + .args([ + "deps", + "diff", + "--base", + "HEAD", + ".", + "--fail-on-new", + "high", + ]) + .output() + .expect("failed to run corgea"); + assert_eq!( + out.status.code(), + Some(0), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); +} + +#[test] +fn cli_diff_fail_on_new_applies_severity_to_new_findings() { + let repo = TempDir::new().expect("temp repo"); + init_git_repo(repo.path()); + write_exact_node_project(repo.path(), "left-pad", "1.3.0", "1.3.0"); + commit_all(repo.path(), "base"); + + write_exact_node_project(repo.path(), "left-pad", "^1.3.0", "1.3.0"); + let (mut medium_cmd, _home) = corgea_isolated(); + let medium_out = medium_cmd + .current_dir(repo.path()) + .args([ + "deps", + "diff", + "--base", + "HEAD", + ".", + "--fail-on-new", + "high", + ]) + .output() + .expect("failed to run corgea"); + assert_eq!( + medium_out.status.code(), + Some(0), + "stderr: {}", + String::from_utf8_lossy(&medium_out.stderr) + ); + + write_exact_node_project(repo.path(), "left-pad", "*", "1.3.0"); + let (mut high_cmd, _home) = corgea_isolated(); + let high_out = high_cmd + .current_dir(repo.path()) + .args([ + "deps", + "diff", + "--base", + "HEAD", + ".", + "--fail-on-new", + "high", + ]) + .output() + .expect("failed to run corgea"); + assert_eq!( + high_out.status.code(), + Some(1), + "stderr: {}", + String::from_utf8_lossy(&high_out.stderr) + ); +} + +#[test] +fn cli_diff_fail_on_new_blocks_same_version_integrity_change() { + let repo = TempDir::new().expect("temp repo"); + init_git_repo(repo.path()); + write_exact_node_project_with_artifact(repo.path(), "left-pad", "1.3.0", "1.3.0", "sha512-old"); + commit_all(repo.path(), "base"); + write_exact_node_project_with_artifact(repo.path(), "left-pad", "1.3.0", "1.3.0", "sha512-new"); + + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .current_dir(repo.path()) + .args([ + "deps", + "diff", + "--base", + "HEAD", + ".", + "--fail-on-new", + "high", + ]) + .output() + .expect("failed to run corgea"); + assert_eq!( + out.status.code(), + Some(1), + "stdout: {}\nstderr: {}", + String::from_utf8_lossy(&out.stdout), + String::from_utf8_lossy(&out.stderr) + ); +} + +#[test] +fn cli_diff_rejects_invalid_fail_on_new_severity() { + let repo = TempDir::new().expect("temp repo"); + init_git_repo(repo.path()); + write_exact_node_project(repo.path(), "left-pad", "1.3.0", "1.3.0"); + commit_all(repo.path(), "base"); + + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .current_dir(repo.path()) + .args([ + "deps", + "diff", + "--base", + "HEAD", + ".", + "--fail-on-new", + "hihg", + ]) + .output() + .expect("failed to run corgea"); + assert_ne!(out.status.code(), Some(0)); + assert!( + String::from_utf8_lossy(&out.stderr).contains("unsupported severity for --fail-on-new"), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); +} + +#[test] +fn cli_diff_rejects_unknown_base_ref() { + let repo = TempDir::new().expect("temp repo"); + init_git_repo(repo.path()); + write_exact_node_project(repo.path(), "left-pad", "1.3.0", "1.3.0"); + commit_all(repo.path(), "base"); + + let (mut cmd, _home) = corgea_isolated(); + let out = cmd + .current_dir(repo.path()) + .args(["deps", "diff", "--base", "missing-ref", "."]) + .output() + .expect("failed to run corgea"); + assert_ne!(out.status.code(), Some(0)); + assert!( + String::from_utf8_lossy(&out.stderr).contains("git failed"), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); +} + +fn finding_ids(json: &serde_json::Value) -> Vec { + json["findings"] + .as_array() + .expect("findings array") + .iter() + .filter_map(|finding| finding["id"].as_str().map(str::to_string)) + .collect() +} + +fn write_exact_node_project(root: &std::path::Path, name: &str, declared: &str, resolved: &str) { + write_exact_node_project_with_artifact(root, name, declared, resolved, "sha512-example"); +} + +fn write_exact_node_project_with_artifact( + root: &std::path::Path, + name: &str, + declared: &str, + resolved: &str, + integrity: &str, +) { + let package_json = format!( + r#"{{ + "name": "diff-project", + "version": "1.0.0", + "dependencies": {{ + "{name}": "{declared}" + }} +}} +"# + ); + let package_lock = format!( + r#"{{ + "name": "diff-project", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": {{ + "": {{ + "name": "diff-project", + "version": "1.0.0", + "dependencies": {{ + "{name}": "{declared}" + }} + }}, + "node_modules/{name}": {{ + "version": "{resolved}", + "resolved": "https://registry.npmjs.org/{name}/-/{name}-{resolved}.tgz", + "integrity": "{integrity}" + }} + }} +}} +"# + ); + std::fs::write(root.join("package.json"), package_json).expect("write package.json"); + std::fs::write(root.join("package-lock.json"), package_lock).expect("write package-lock.json"); +} + +fn init_git_repo(repo: &std::path::Path) { + run_git(repo, &["init", "-q"]); +} + +fn commit_all(repo: &std::path::Path, message: &str) { + run_git(repo, &["add", "."]); + run_git( + repo, + &[ + "-c", + "user.email=test@example.com", + "-c", + "user.name=Test User", + "commit", + "-q", + "-m", + message, + ], + ); +} + +fn run_git(repo: &std::path::Path, args: &[&str]) { + let output = Command::new("git") + .current_dir(repo) + .args(args) + .output() + .expect("run git"); + assert!( + output.status.success(), + "git {:?} failed\nstdout: {}\nstderr: {}", + args, + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); +} diff --git a/tests/cli_deps_skill.rs b/tests/cli_deps_skill.rs new file mode 100644 index 0000000..3bbf897 --- /dev/null +++ b/tests/cli_deps_skill.rs @@ -0,0 +1,10 @@ +use std::path::Path; + +#[test] +fn generated_deps_skill_block_is_current() { + let path = Path::new(env!("CARGO_MANIFEST_DIR")) + .join("skills") + .join("corgea") + .join("SKILL.md"); + corgea::deps::skill::check_skill_file(&path).expect("deps skill block should be current"); +} diff --git a/tests/fixtures/README.md b/tests/fixtures/README.md new file mode 100644 index 0000000..e09abb5 --- /dev/null +++ b/tests/fixtures/README.md @@ -0,0 +1,23 @@ +# Dependency scan fixtures (`tests/fixtures/`) + +Offline fixture projects for `corgea deps` unit and CLI tests per `docs/PRD_DEPS_TESTING.md` §4.2. + +- Pins are **intentional** — do not bump versions without updating advisory-backed tests. +- Used by `cargo test deps` and `tests/cli_deps.rs` (hermetic `HOME`, no network). +- Dogfood fixtures for freshness/CVE live under `fixtures/deps/` and use `corgea deps verify`. + +| Directory | Role | +|-----------|------| +| `node-app` | npm graph + DEP003/004/005/008 | +| `node-stale` | DEP002 stale lockfile | +| `node-transitive` | npm generic transitive edges + scoped package names | +| `node-yarn` / `node-pnpm` | unsupported npm-family lockfiles | +| `node-monorepo` | workspace detection | +| `python-poetry` | Poetry lock + transitive urllib3 | +| `python-poetry-multi` | Poetry dependency tables for multiple parents | +| `python-pip-nolock` | DEP001 + requirements.txt | +| `python-uv-requirements` | `uv.lock` does not suppress requirements scanning | +| `java-maven` / `java-gradle` | Maven/Gradle parsers | +| `go-mod-smoke` | detection only | +| `malformed/` | graceful parse errors | +| `vuln-db.json` | mock DEP010 advisories | diff --git a/tests/fixtures/go-mod-smoke/go.mod b/tests/fixtures/go-mod-smoke/go.mod new file mode 100644 index 0000000..9c50f56 --- /dev/null +++ b/tests/fixtures/go-mod-smoke/go.mod @@ -0,0 +1,5 @@ +module example.com/go-mod-smoke + +go 1.21 + +require github.com/stretchr/testify v1.8.4 diff --git a/tests/fixtures/go-mod-smoke/go.sum b/tests/fixtures/go-mod-smoke/go.sum new file mode 100644 index 0000000..3ff42b4 --- /dev/null +++ b/tests/fixtures/go-mod-smoke/go.sum @@ -0,0 +1,2 @@ +github.com/stretchr/testify v1.8.4 h1:1234567890abcdef= +github.com/stretchr/testify v1.8.4/go.mod h1:abcdef= diff --git a/tests/fixtures/java-gradle/build.gradle b/tests/fixtures/java-gradle/build.gradle new file mode 100644 index 0000000..f501628 --- /dev/null +++ b/tests/fixtures/java-gradle/build.gradle @@ -0,0 +1,10 @@ +plugins { + id 'java' +} + +dependencies { + implementation 'com.google.guava:guava:32.1.3-jre' + implementation 'org.apache.commons:commons-lang3:3.+' + implementation 'org.slf4j:slf4j-api:latest.release' + testImplementation 'org.junit.jupiter:junit-jupiter:5.10.1' +} diff --git a/tests/fixtures/java-gradle/gradle.lockfile b/tests/fixtures/java-gradle/gradle.lockfile new file mode 100644 index 0000000..80236b7 --- /dev/null +++ b/tests/fixtures/java-gradle/gradle.lockfile @@ -0,0 +1,6 @@ +# This is a Gradle generated file for dependency locking. +com.google.guava:guava:32.1.3-jre=compileClasspath,runtimeClasspath +org.apache.commons:commons-lang3:3.14.0=compileClasspath,runtimeClasspath +org.slf4j:slf4j-api:2.0.9=compileClasspath,runtimeClasspath +org.junit.jupiter:junit-jupiter:5.10.1=testCompileClasspath,testRuntimeClasspath +empty=annotationProcessor diff --git a/tests/fixtures/java-maven/pom.xml b/tests/fixtures/java-maven/pom.xml new file mode 100644 index 0000000..1ad0329 --- /dev/null +++ b/tests/fixtures/java-maven/pom.xml @@ -0,0 +1,35 @@ + + + 4.0.0 + com.acme + java-maven-app + 1.0.0 + + + com.google.guava + guava + 32.1.3-jre + + + org.apache.commons + commons-lang3 + [3.0,4.0) + + + org.slf4j + slf4j-api + LATEST + + + com.acme + internal-bom + 2.0-SNAPSHOT + + + org.junit.jupiter + junit-jupiter + 5.10.1 + test + + + diff --git a/tests/fixtures/malformed/not-xml-pom.xml b/tests/fixtures/malformed/not-xml-pom.xml new file mode 100644 index 0000000..d6a395c --- /dev/null +++ b/tests/fixtures/malformed/not-xml-pom.xml @@ -0,0 +1 @@ +not xml at all diff --git a/tests/fixtures/malformed/package-lock.json b/tests/fixtures/malformed/package-lock.json new file mode 100644 index 0000000..81ec3ba --- /dev/null +++ b/tests/fixtures/malformed/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "malformed", + "packages": { + "": { "dependencies": { "x": "1.0.0" } , + } +} diff --git a/tests/fixtures/malformed/package.json b/tests/fixtures/malformed/package.json new file mode 100644 index 0000000..d29c7ae --- /dev/null +++ b/tests/fixtures/malformed/package.json @@ -0,0 +1,4 @@ +{ + "name": "malformed", + "dependencies": {} +} diff --git a/tests/fixtures/malformed/poetry.lock b/tests/fixtures/malformed/poetry.lock new file mode 100644 index 0000000..fc620d7 --- /dev/null +++ b/tests/fixtures/malformed/poetry.lock @@ -0,0 +1,3 @@ +[[package]] +name = "truncated" +version = "1.0.0 diff --git a/tests/fixtures/malformed/pyproject.toml b/tests/fixtures/malformed/pyproject.toml new file mode 100644 index 0000000..513277c --- /dev/null +++ b/tests/fixtures/malformed/pyproject.toml @@ -0,0 +1,6 @@ +[tool.poetry] +name = "malformed-poetry" +version = "0.1.0" + +[tool.poetry.dependencies] +python = "^3.12" diff --git a/tests/fixtures/malformed/truncated-poetry.lock b/tests/fixtures/malformed/truncated-poetry.lock new file mode 100644 index 0000000..fc620d7 --- /dev/null +++ b/tests/fixtures/malformed/truncated-poetry.lock @@ -0,0 +1,3 @@ +[[package]] +name = "truncated" +version = "1.0.0 diff --git a/tests/fixtures/node-app/.corgea/deps.yml b/tests/fixtures/node-app/.corgea/deps.yml new file mode 100644 index 0000000..0cc5523 --- /dev/null +++ b/tests/fixtures/node-app/.corgea/deps.yml @@ -0,0 +1,8 @@ +dependency_policy: + require_lockfile: true + fail_on_missing_lockfile: true + fail_on_stale_lockfile: true + direct_dependencies: + fail_on_wildcard: true + fail_on_latest: true + warn_on_semver_range: true diff --git a/tests/fixtures/node-app/package-lock.json b/tests/fixtures/node-app/package-lock.json new file mode 100644 index 0000000..97640a8 --- /dev/null +++ b/tests/fixtures/node-app/package-lock.json @@ -0,0 +1,44 @@ +{ + "name": "node-app", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "node-app", + "version": "1.0.0", + "dependencies": { + "express": "^4.18.2", + "lodash": "*", + "left-pad": "latest", + "internal-utils": "git+https://github.com/acme/internal-utils.git#main" + }, + "devDependencies": { "jest": "29.7.0" } + }, + "node_modules/express": { + "version": "4.18.2", + "resolved": "https://registry.npmjs.org/express/-/express-4.18.2.tgz", + "integrity": "sha512-5/PsL6iGPdfQ/lKM1UuielYgv3BUoJfz1aUwU9vHZ+J7gyvwdQXFEBIEIaxeGf0GIcreATNyBExtalisDbuMqQ==", + "dependencies": { "qs": "6.11.0" } + }, + "node_modules/qs": { + "version": "6.11.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", + "integrity": "sha512-MvjoMCJwEarSbUYk5O+nmoSzSutSsTwF85zcHPQ9OrlFoZOYIjaqBAJIqIXjptyD5vThxGq52Xu/MaJzRkDtA==" + }, + "node_modules/lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvKw==" + }, + "node_modules/left-pad": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/left-pad/-/left-pad-1.3.0.tgz" + }, + "node_modules/jest": { + "version": "29.7.0", + "resolved": "https://registry.npmjs.org/jest/-/jest-29.7.0.tgz", + "integrity": "sha512-example" + } + } +} diff --git a/tests/fixtures/node-app/package.json b/tests/fixtures/node-app/package.json new file mode 100644 index 0000000..5161dd4 --- /dev/null +++ b/tests/fixtures/node-app/package.json @@ -0,0 +1,13 @@ +{ + "name": "node-app", + "version": "1.0.0", + "dependencies": { + "express": "^4.18.2", + "lodash": "*", + "left-pad": "latest", + "internal-utils": "git+https://github.com/acme/internal-utils.git#main" + }, + "devDependencies": { + "jest": "29.7.0" + } +} diff --git a/tests/fixtures/node-monorepo/package-lock.json b/tests/fixtures/node-monorepo/package-lock.json new file mode 100644 index 0000000..33f2aa8 --- /dev/null +++ b/tests/fixtures/node-monorepo/package-lock.json @@ -0,0 +1,11 @@ +{ + "name": "node-monorepo", + "lockfileVersion": 3, + "packages": { + "": { "name": "node-monorepo", "dependencies": { "lodash": "4.17.21" } }, + "node_modules/lodash": { + "version": "4.17.21", + "integrity": "sha512-x" + } + } +} diff --git a/tests/fixtures/node-monorepo/package.json b/tests/fixtures/node-monorepo/package.json new file mode 100644 index 0000000..24582bd --- /dev/null +++ b/tests/fixtures/node-monorepo/package.json @@ -0,0 +1,6 @@ +{ + "name": "node-monorepo", + "version": "1.0.0", + "workspaces": ["packages/*"], + "dependencies": { "lodash": "4.17.21" } +} diff --git a/tests/fixtures/node-monorepo/packages/a/package.json b/tests/fixtures/node-monorepo/packages/a/package.json new file mode 100644 index 0000000..ddfddd3 --- /dev/null +++ b/tests/fixtures/node-monorepo/packages/a/package.json @@ -0,0 +1 @@ +{ "name": "pkg-a", "version": "1.0.0", "dependencies": { "axios": "1.8.2" } } diff --git a/tests/fixtures/node-monorepo/packages/b/package.json b/tests/fixtures/node-monorepo/packages/b/package.json new file mode 100644 index 0000000..ccc903d --- /dev/null +++ b/tests/fixtures/node-monorepo/packages/b/package.json @@ -0,0 +1 @@ +{ "name": "pkg-b", "version": "1.0.0", "dependencies": { "chalk": "5.3.0" } } diff --git a/tests/fixtures/node-pnpm/package.json b/tests/fixtures/node-pnpm/package.json new file mode 100644 index 0000000..76f846f --- /dev/null +++ b/tests/fixtures/node-pnpm/package.json @@ -0,0 +1,7 @@ +{ + "name": "node-pnpm", + "version": "1.0.0", + "dependencies": { + "left-pad": "1.3.0" + } +} diff --git a/tests/fixtures/node-pnpm/pnpm-lock.yaml b/tests/fixtures/node-pnpm/pnpm-lock.yaml new file mode 100644 index 0000000..a6cf0ea --- /dev/null +++ b/tests/fixtures/node-pnpm/pnpm-lock.yaml @@ -0,0 +1,5 @@ +lockfileVersion: '9.0' +packages: + left-pad@1.3.0: + resolution: + integrity: sha512-example diff --git a/tests/fixtures/node-stale/package-lock.json b/tests/fixtures/node-stale/package-lock.json new file mode 100644 index 0000000..87ed96e --- /dev/null +++ b/tests/fixtures/node-stale/package-lock.json @@ -0,0 +1,15 @@ +{ + "name": "node-stale", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { "name": "node-stale", "version": "1.0.0", + "dependencies": { "express": "^4.18.2" } }, + "node_modules/express": { + "version": "4.18.2", + "resolved": "https://registry.npmjs.org/express/-/express-4.18.2.tgz", + "integrity": "sha512-5/PsL6iGPdfQ/lKM1UuielYgv3BUoJfz1aUwU9vHZ+J7gyvwdQXFEBIEIaxeGf0GIcreATNyBExtalisDbuMqQ==" + } + } +} diff --git a/tests/fixtures/node-stale/package.json b/tests/fixtures/node-stale/package.json new file mode 100644 index 0000000..3e78ebd --- /dev/null +++ b/tests/fixtures/node-stale/package.json @@ -0,0 +1,5 @@ +{ + "name": "node-stale", + "version": "1.0.0", + "dependencies": { "express": "^4.18.2", "chalk": "^5.3.0" } +} diff --git a/tests/fixtures/node-transitive/package-lock.json b/tests/fixtures/node-transitive/package-lock.json new file mode 100644 index 0000000..c3c1544 --- /dev/null +++ b/tests/fixtures/node-transitive/package-lock.json @@ -0,0 +1,34 @@ +{ + "name": "node-transitive", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "node-transitive", + "version": "1.0.0", + "dependencies": { + "parent-lib": "1.0.0" + } + }, + "node_modules/parent-lib": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/parent-lib/-/parent-lib-1.0.0.tgz", + "integrity": "sha512-parent", + "dependencies": { + "child-lib": "^2.0.0", + "@types/node": "^18.19.0" + } + }, + "node_modules/child-lib": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/child-lib/-/child-lib-2.1.0.tgz", + "integrity": "sha512-child" + }, + "node_modules/@types/node": { + "version": "18.19.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.19.0.tgz", + "integrity": "sha512-types" + } + } +} diff --git a/tests/fixtures/node-transitive/package.json b/tests/fixtures/node-transitive/package.json new file mode 100644 index 0000000..8d88ff9 --- /dev/null +++ b/tests/fixtures/node-transitive/package.json @@ -0,0 +1,7 @@ +{ + "name": "node-transitive", + "version": "1.0.0", + "dependencies": { + "parent-lib": "1.0.0" + } +} diff --git a/tests/fixtures/node-yarn/package.json b/tests/fixtures/node-yarn/package.json new file mode 100644 index 0000000..228f257 --- /dev/null +++ b/tests/fixtures/node-yarn/package.json @@ -0,0 +1,7 @@ +{ + "name": "node-yarn", + "version": "1.0.0", + "dependencies": { + "left-pad": "1.3.0" + } +} diff --git a/tests/fixtures/node-yarn/yarn.lock b/tests/fixtures/node-yarn/yarn.lock new file mode 100644 index 0000000..bdb1f57 --- /dev/null +++ b/tests/fixtures/node-yarn/yarn.lock @@ -0,0 +1,4 @@ +left-pad@1.3.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.3.0.tgz" + integrity sha512-example diff --git a/tests/fixtures/python-pip-nolock/requirements.txt b/tests/fixtures/python-pip-nolock/requirements.txt new file mode 100644 index 0000000..ea658aa --- /dev/null +++ b/tests/fixtures/python-pip-nolock/requirements.txt @@ -0,0 +1,4 @@ +flask==2.3.3 +requests +urllib3>=1.26 +internal-lib @ git+https://github.com/acme/internal-lib.git@main diff --git a/tests/fixtures/python-poetry-multi/poetry.lock b/tests/fixtures/python-poetry-multi/poetry.lock new file mode 100644 index 0000000..11a3c79 --- /dev/null +++ b/tests/fixtures/python-poetry-multi/poetry.lock @@ -0,0 +1,34 @@ +[[package]] +name = "alpha" +version = "1.0.1" +optional = false +python-versions = ">=3.8" + +[package.dependencies] +gamma = ">=3,<4" + +[[package]] +name = "beta" +version = "2.0.1" +optional = false +python-versions = ">=3.8" + +[package.dependencies] +delta = "^4.0" + +[[package]] +name = "gamma" +version = "3.2.1" +optional = false +python-versions = ">=3.8" + +[[package]] +name = "delta" +version = "4.1.0" +optional = false +python-versions = ">=3.8" + +[metadata] +lock-version = "2.0" +python-versions = "^3.12" +content-hash = "0000000000000000000000000000000000000000000000000000000000000000" diff --git a/tests/fixtures/python-poetry-multi/pyproject.toml b/tests/fixtures/python-poetry-multi/pyproject.toml new file mode 100644 index 0000000..f97bc94 --- /dev/null +++ b/tests/fixtures/python-poetry-multi/pyproject.toml @@ -0,0 +1,8 @@ +[tool.poetry] +name = "python-poetry-multi" +version = "0.1.0" + +[tool.poetry.dependencies] +python = "^3.12" +alpha = "^1.0.0" +beta = "^2.0.0" diff --git a/tests/fixtures/python-poetry/poetry.lock b/tests/fixtures/python-poetry/poetry.lock new file mode 100644 index 0000000..426f247 --- /dev/null +++ b/tests/fixtures/python-poetry/poetry.lock @@ -0,0 +1,31 @@ +[[package]] +name = "requests" +version = "2.31.0" +optional = false +python-versions = ">=3.7" + +[package.dependencies] +urllib3 = ">=1.21.1,<3" + +[[package]] +name = "urllib3" +version = "2.0.7" +optional = false +python-versions = ">=3.7" + +[[package]] +name = "flask" +version = "2.3.3" +optional = false +python-versions = ">=3.8" + +[[package]] +name = "pytest" +version = "8.0.0" +optional = false +python-versions = ">=3.8" + +[metadata] +lock-version = "2.0" +python-versions = "^3.12" +content-hash = "0000000000000000000000000000000000000000000000000000000000000000" diff --git a/tests/fixtures/python-poetry/pyproject.toml b/tests/fixtures/python-poetry/pyproject.toml new file mode 100644 index 0000000..72f3ad6 --- /dev/null +++ b/tests/fixtures/python-poetry/pyproject.toml @@ -0,0 +1,11 @@ +[tool.poetry] +name = "python-poetry-app" +version = "0.1.0" + +[tool.poetry.dependencies] +python = "^3.12" +requests = "^2.31.0" +flask = "2.3.3" + +[tool.poetry.group.dev.dependencies] +pytest = "^8.0.0" diff --git a/tests/fixtures/python-uv-requirements/pyproject.toml b/tests/fixtures/python-uv-requirements/pyproject.toml new file mode 100644 index 0000000..5510875 --- /dev/null +++ b/tests/fixtures/python-uv-requirements/pyproject.toml @@ -0,0 +1,3 @@ +[project] +name = "python-uv-requirements" +version = "0.1.0" diff --git a/tests/fixtures/python-uv-requirements/requirements.txt b/tests/fixtures/python-uv-requirements/requirements.txt new file mode 100644 index 0000000..f229360 --- /dev/null +++ b/tests/fixtures/python-uv-requirements/requirements.txt @@ -0,0 +1 @@ +requests diff --git a/tests/fixtures/python-uv-requirements/uv.lock b/tests/fixtures/python-uv-requirements/uv.lock new file mode 100644 index 0000000..d0e17e4 --- /dev/null +++ b/tests/fixtures/python-uv-requirements/uv.lock @@ -0,0 +1,2 @@ +version = 1 +revision = 1