Files
pruefwerk/compliance-agent/src/classify/dotnet.rs
T
Sharang ParnerkarandClaude Opus 5.5 2bafe12b2a feat(dotnet): .NET detection, NuGet SBOM, C# rules and config checks
Minimal .NET/C# support for epic #445 (N1-N5; N6 out of scope):

- N1: solutions and .csproj/.vbproj/.fsproj at any depth, old vs SDK
  style, TargetFramework(s)/TargetFrameworkVersion; recorded as target
  facts (dotnet_runtime, dotnet_solution, dotnet_project).
- N2: NuGet components from packages.config, PackageReference (attribute,
  child element, VersionOverride, Directory.Packages.props),
  packages.lock.json, obj/project.assets.json and packages/<Id>.<Version>/
  with pkg:nuget purls for OSV. PackageReference projects without lockfile
  or assets are reported as unresolved (sbom_dependencies). Target
  frameworks past Microsoft's end of support are findings (CWE-1104).
- N3-lite: own semgrep rule pack cra_semgrep_csharp.yaml (SQLi, command
  injection, deserialization, XXE, path traversal, weak crypto/ECB, TLS
  validation off, hard-coded credentials) with semgrep --test cases;
  semgrep.rs runs every bundled pack.
- N4: web.config/app.config checks (clear-text passwords, debug,
  customErrors, httpCookies, WCF security) and committed key/publish files.
- N5: bin/obj beside projects, NuGet packages/, .vs, .nuget, TestResults
  are not own code.

Control map bindings, fixtures/dotnet/framework48 with a test, and docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
2026-10-05 16:27:00 +02:00

128 lines
4.4 KiB
Rust

//! The .NET stack of a code tree as classification facts (#445).
//!
//! Facts only: which target type a .NET tree is (desktop, web, service) is the
//! language classifier's call. Recorded per tree:
//!
//! - `dotnet_runtime`: `dotnet-framework`, `dotnet` (.NET Core / 5+) or
//! `dotnet-standard`, once each;
//! - `dotnet_solution`: every solution, a component of its own;
//! - `dotnet_project`: every project with its style and target frameworks,
//! e.g. `src/Hmi/Hmi.csproj (legacy; net48)`.
use std::path::Path;
use compliance_core::models::DetectedFact;
use crate::pipeline::dotnet::{inventory, Inventory};
/// What produced the facts.
const SOURCE: &str = "dotnet-inventory";
/// At most this many solution and project facts each: a large codebase has
/// dozens of projects, and the facts are a summary, not an inventory.
const MAX_FACTS: usize = 100;
/// Add the .NET facts of the tree at `root` to `facts`, skipping any already
/// there.
pub(super) fn add_facts(facts: &mut Vec<DetectedFact>, root: &Path) {
for fact in dotnet_facts(&inventory(root)) {
if !facts
.iter()
.any(|f| f.key == fact.key && f.value == fact.value)
{
facts.push(fact);
}
}
}
/// The facts of an [`Inventory`]; none for a tree without .NET.
fn dotnet_facts(inv: &Inventory) -> Vec<DetectedFact> {
let mut facts = Vec::new();
for runtime in inv.runtimes() {
facts.push(DetectedFact::new(
"dotnet_runtime",
runtime.as_str(),
SOURCE,
));
}
for sln in inv.solutions.iter().take(MAX_FACTS) {
facts.push(DetectedFact::new("dotnet_solution", sln, SOURCE));
}
for project in inv.projects.iter().take(MAX_FACTS) {
let style = if project.sdk_style { "sdk" } else { "legacy" };
let frameworks: Vec<&str> = project
.frameworks
.iter()
.map(|f| f.moniker.as_str())
.collect();
let frameworks = if frameworks.is_empty() {
"inherited".to_string()
} else {
frameworks.join(",")
};
facts.push(DetectedFact::new(
"dotnet_project",
format!("{} ({style}; {frameworks})", project.path),
SOURCE,
));
}
facts
}
#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
use super::*;
#[test]
fn solutions_projects_and_runtimes_are_recorded_at_any_depth() {
let root = std::env::temp_dir().join(format!("cs-dotnet-facts-{}", uuid::Uuid::new_v4()));
let deep = root.join("src/plant/hmi/app");
std::fs::create_dir_all(&deep).expect("mkdir");
std::fs::write(root.join("src/plant/Plant.sln"), "").expect("write");
std::fs::write(root.join("src/Tools.sln"), "").expect("write");
std::fs::write(
deep.join("Hmi.csproj"),
"<Project ToolsVersion=\"15.0\"><PropertyGroup><TargetFrameworkVersion>v4.8</TargetFrameworkVersion></PropertyGroup></Project>",
)
.expect("write");
std::fs::write(
root.join("src/Api.csproj"),
"<Project Sdk=\"Microsoft.NET.Sdk.Web\"><PropertyGroup><TargetFramework>net8.0</TargetFramework></PropertyGroup></Project>",
)
.expect("write");
let mut facts = Vec::new();
add_facts(&mut facts, &root);
add_facts(&mut facts, &root);
let _ = std::fs::remove_dir_all(&root);
let got: Vec<String> = facts
.iter()
.map(|f| format!("{}={}", f.key, f.value))
.collect();
assert_eq!(
got,
[
"dotnet_runtime=dotnet-framework",
"dotnet_runtime=dotnet",
"dotnet_solution=src/Tools.sln",
"dotnet_solution=src/plant/Plant.sln",
"dotnet_project=src/Api.csproj (sdk; net8.0)",
"dotnet_project=src/plant/hmi/app/Hmi.csproj (legacy; net48)",
]
);
}
#[test]
fn a_tree_without_dotnet_adds_nothing() {
let root = std::env::temp_dir().join(format!("cs-dotnet-facts-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&root).expect("mkdir");
std::fs::write(root.join("main.go"), "package main").expect("write");
let mut facts = Vec::new();
add_facts(&mut facts, &root);
let _ = std::fs::remove_dir_all(&root);
assert!(facts.is_empty());
}
}