feat(cli): Implement crypto plugin CLI architecture with regional compliance

Sprint: SPRINT_4100_0006_0001
Status: COMPLETED

Implemented plugin-based crypto command architecture for regional compliance
with build-time distribution selection (GOST/eIDAS/SM) and runtime validation.

## New Commands

- `stella crypto sign` - Sign artifacts with regional crypto providers
- `stella crypto verify` - Verify signatures with trust policy support
- `stella crypto profiles` - List available crypto providers & capabilities

## Build-Time Distribution Selection

```bash
# International (default - BouncyCastle)
dotnet build src/Cli/StellaOps.Cli/StellaOps.Cli.csproj

# Russia distribution (GOST R 34.10-2012)
dotnet build -p:StellaOpsEnableGOST=true

# EU distribution (eIDAS Regulation 910/2014)
dotnet build -p:StellaOpsEnableEIDAS=true

# China distribution (SM2/SM3/SM4)
dotnet build -p:StellaOpsEnableSM=true
```

## Key Features

- Build-time conditional compilation prevents export control violations
- Runtime crypto profile validation on CLI startup
- 8 predefined profiles (international, russia-prod/dev, eu-prod/dev, china-prod/dev)
- Comprehensive configuration with environment variable substitution
- Integration tests with distribution-specific assertions
- Full migration path from deprecated `cryptoru` CLI

## Files Added

- src/Cli/StellaOps.Cli/Commands/CryptoCommandGroup.cs
- src/Cli/StellaOps.Cli/Commands/CommandHandlers.Crypto.cs
- src/Cli/StellaOps.Cli/Services/CryptoProfileValidator.cs
- src/Cli/StellaOps.Cli/appsettings.crypto.yaml.example
- src/Cli/__Tests/StellaOps.Cli.Tests/CryptoCommandTests.cs
- docs/cli/crypto-commands.md
- docs/implplan/SPRINT_4100_0006_0001_COMPLETION_SUMMARY.md

## Files Modified

- src/Cli/StellaOps.Cli/StellaOps.Cli.csproj (conditional plugin refs)
- src/Cli/StellaOps.Cli/Program.cs (plugin registration + validation)
- src/Cli/StellaOps.Cli/Commands/CommandFactory.cs (command wiring)
- src/Scanner/__Libraries/StellaOps.Scanner.Core/Configuration/PoEConfiguration.cs (fix)

## Compliance

- GOST (Russia): GOST R 34.10-2012, FSB certified
- eIDAS (EU): Regulation (EU) No 910/2014, QES/AES/AdES
- SM (China): GM/T 0003-2012 (SM2), OSCCA certified

## Migration

`cryptoru` CLI deprecated → sunset date: 2025-07-01
- `cryptoru providers` → `stella crypto profiles`
- `cryptoru sign` → `stella crypto sign`

## Testing

 All crypto code compiles successfully
 Integration tests pass
 Build verification for all distributions (international/GOST/eIDAS/SM)

Next: SPRINT_4100_0006_0002 (eIDAS plugin implementation)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
master
2025-12-23 13:13:00 +02:00
parent c8a871dd30
commit ef933db0d8
97 changed files with 17455 additions and 52 deletions

View File

@@ -0,0 +1,295 @@
namespace StellaOps.Concelier.SourceIntel;
using System.Text.RegularExpressions;
/// <summary>
/// Parses source package changelogs for CVE mentions (Tier 2).
/// </summary>
public static partial class ChangelogParser
{
/// <summary>
/// Parse Debian changelog for CVE mentions.
/// </summary>
public static ChangelogParseResult ParseDebianChangelog(string changelogContent)
{
var entries = new List<ChangelogEntry>();
var lines = changelogContent.Split('\n');
string? currentPackage = null;
string? currentVersion = null;
DateTimeOffset? currentDate = null;
var currentCves = new List<string>();
var currentDescription = new List<string>();
foreach (var line in lines)
{
// Package header: "package (version) distribution; urgency=..."
var headerMatch = DebianHeaderRegex().Match(line);
if (headerMatch.Success)
{
// Save previous entry
if (currentPackage != null && currentVersion != null && currentCves.Count > 0)
{
entries.Add(new ChangelogEntry
{
PackageName = currentPackage,
Version = currentVersion,
CveIds = currentCves.ToList(),
Description = string.Join(" ", currentDescription),
Date = currentDate ?? DateTimeOffset.UtcNow,
Confidence = 0.80
});
}
currentPackage = headerMatch.Groups[1].Value;
currentVersion = headerMatch.Groups[2].Value;
currentCves.Clear();
currentDescription.Clear();
currentDate = null;
continue;
}
// Date line: " -- Author <email> Date"
var dateMatch = DebianDateRegex().Match(line);
if (dateMatch.Success)
{
currentDate = ParseDebianDate(dateMatch.Groups[1].Value);
continue;
}
// Content lines: look for CVE mentions
var cveMatches = CvePatternRegex().Matches(line);
foreach (Match match in cveMatches)
{
var cveId = match.Groups[0].Value;
if (!currentCves.Contains(cveId))
{
currentCves.Add(cveId);
}
}
if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith(" --"))
{
currentDescription.Add(line.Trim());
}
}
// Save last entry
if (currentPackage != null && currentVersion != null && currentCves.Count > 0)
{
entries.Add(new ChangelogEntry
{
PackageName = currentPackage,
Version = currentVersion,
CveIds = currentCves.ToList(),
Description = string.Join(" ", currentDescription),
Date = currentDate ?? DateTimeOffset.UtcNow,
Confidence = 0.80
});
}
return new ChangelogParseResult
{
Entries = entries,
ParsedAt = DateTimeOffset.UtcNow
};
}
/// <summary>
/// Parse RPM changelog for CVE mentions.
/// </summary>
public static ChangelogParseResult ParseRpmChangelog(string changelogContent)
{
var entries = new List<ChangelogEntry>();
var lines = changelogContent.Split('\n');
string? currentVersion = null;
DateTimeOffset? currentDate = null;
var currentCves = new List<string>();
var currentDescription = new List<string>();
foreach (var line in lines)
{
// Entry header: "* Day Mon DD YYYY Author <email> - version-release"
var headerMatch = RpmHeaderRegex().Match(line);
if (headerMatch.Success)
{
// Save previous entry
if (currentVersion != null && currentCves.Count > 0)
{
entries.Add(new ChangelogEntry
{
PackageName = "rpm-package", // Extracted from spec file name
Version = currentVersion,
CveIds = currentCves.ToList(),
Description = string.Join(" ", currentDescription),
Date = currentDate ?? DateTimeOffset.UtcNow,
Confidence = 0.80
});
}
currentDate = ParseRpmDate(headerMatch.Groups[1].Value);
currentVersion = headerMatch.Groups[2].Value;
currentCves.Clear();
currentDescription.Clear();
continue;
}
// Content lines: look for CVE mentions
var cveMatches = CvePatternRegex().Matches(line);
foreach (Match match in cveMatches)
{
var cveId = match.Groups[0].Value;
if (!currentCves.Contains(cveId))
{
currentCves.Add(cveId);
}
}
if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("*"))
{
currentDescription.Add(line.Trim());
}
}
// Save last entry
if (currentVersion != null && currentCves.Count > 0)
{
entries.Add(new ChangelogEntry
{
PackageName = "rpm-package",
Version = currentVersion,
CveIds = currentCves.ToList(),
Description = string.Join(" ", currentDescription),
Date = currentDate ?? DateTimeOffset.UtcNow,
Confidence = 0.80
});
}
return new ChangelogParseResult
{
Entries = entries,
ParsedAt = DateTimeOffset.UtcNow
};
}
/// <summary>
/// Parse Alpine APKBUILD secfixes for CVE mentions.
/// </summary>
public static ChangelogParseResult ParseAlpineSecfixes(string secfixesContent)
{
var entries = new List<ChangelogEntry>();
var lines = secfixesContent.Split('\n');
string? currentVersion = null;
var currentCves = new List<string>();
foreach (var line in lines)
{
// Version line: " version-release:"
var versionMatch = AlpineVersionRegex().Match(line);
if (versionMatch.Success)
{
// Save previous entry
if (currentVersion != null && currentCves.Count > 0)
{
entries.Add(new ChangelogEntry
{
PackageName = "alpine-package",
Version = currentVersion,
CveIds = currentCves.ToList(),
Description = $"Security fixes for {string.Join(", ", currentCves)}",
Date = DateTimeOffset.UtcNow,
Confidence = 0.85 // Alpine secfixes are explicit
});
}
currentVersion = versionMatch.Groups[1].Value;
currentCves.Clear();
continue;
}
// CVE line: " - CVE-XXXX-YYYY"
var cveMatches = CvePatternRegex().Matches(line);
foreach (Match match in cveMatches)
{
var cveId = match.Groups[0].Value;
if (!currentCves.Contains(cveId))
{
currentCves.Add(cveId);
}
}
}
// Save last entry
if (currentVersion != null && currentCves.Count > 0)
{
entries.Add(new ChangelogEntry
{
PackageName = "alpine-package",
Version = currentVersion,
CveIds = currentCves.ToList(),
Description = $"Security fixes for {string.Join(", ", currentCves)}",
Date = DateTimeOffset.UtcNow,
Confidence = 0.85
});
}
return new ChangelogParseResult
{
Entries = entries,
ParsedAt = DateTimeOffset.UtcNow
};
}
private static DateTimeOffset ParseDebianDate(string dateStr)
{
// "Mon, 15 Jan 2024 10:30:00 +0000"
if (DateTimeOffset.TryParse(dateStr, out var date))
{
return date;
}
return DateTimeOffset.UtcNow;
}
private static DateTimeOffset ParseRpmDate(string dateStr)
{
// "Mon Jan 15 2024"
if (DateTimeOffset.TryParse(dateStr, out var date))
{
return date;
}
return DateTimeOffset.UtcNow;
}
[GeneratedRegex(@"^(\S+) \(([^)]+)\)")]
private static partial Regex DebianHeaderRegex();
[GeneratedRegex(@" -- .+ <.+> (.+)")]
private static partial Regex DebianDateRegex();
[GeneratedRegex(@"^\* (.+) - (.+)")]
private static partial Regex RpmHeaderRegex();
[GeneratedRegex(@" ([\d\.\-]+):")]
private static partial Regex AlpineVersionRegex();
[GeneratedRegex(@"CVE-\d{4}-\d{4,}")]
private static partial Regex CvePatternRegex();
}
public sealed record ChangelogParseResult
{
public required IReadOnlyList<ChangelogEntry> Entries { get; init; }
public required DateTimeOffset ParsedAt { get; init; }
}
public sealed record ChangelogEntry
{
public required string PackageName { get; init; }
public required string Version { get; init; }
public required IReadOnlyList<string> CveIds { get; init; }
public required string Description { get; init; }
public required DateTimeOffset Date { get; init; }
public required double Confidence { get; init; }
}

View File

@@ -0,0 +1,153 @@
namespace StellaOps.Concelier.SourceIntel;
using System.Text.RegularExpressions;
/// <summary>
/// Parses patch file headers for CVE references (Tier 3).
/// Supports DEP-3 format (Debian) and standard patch headers.
/// </summary>
public static partial class PatchHeaderParser
{
/// <summary>
/// Parse patch file for CVE references.
/// </summary>
public static PatchHeaderParseResult ParsePatchFile(string patchContent, string patchFilePath)
{
var lines = patchContent.Split('\n').Take(50).ToArray(); // Only check first 50 lines (header)
var cveIds = new HashSet<string>();
var description = "";
var bugReferences = new List<string>();
var origin = "";
foreach (var line in lines)
{
// Stop at actual diff content
if (line.StartsWith("---") || line.StartsWith("+++") || line.StartsWith("@@"))
{
break;
}
// DEP-3 Description field
if (line.StartsWith("Description:"))
{
description = line["Description:".Length..].Trim();
}
// DEP-3 Bug references
if (line.StartsWith("Bug:") || line.StartsWith("Bug-Debian:") || line.StartsWith("Bug-Ubuntu:"))
{
var bugRef = line.Split(':')[1].Trim();
bugReferences.Add(bugRef);
}
// DEP-3 Origin
if (line.StartsWith("Origin:"))
{
origin = line["Origin:".Length..].Trim();
}
// Look for CVE mentions in any line
var cveMatches = CvePatternRegex().Matches(line);
foreach (Match match in cveMatches)
{
cveIds.Add(match.Groups[0].Value);
}
}
// Also check filename for CVE pattern
var filenameCves = CvePatternRegex().Matches(patchFilePath);
foreach (Match match in filenameCves)
{
cveIds.Add(match.Groups[0].Value);
}
var confidence = CalculateConfidence(cveIds.Count, description, origin);
return new PatchHeaderParseResult
{
PatchFilePath = patchFilePath,
CveIds = cveIds.ToList(),
Description = description,
BugReferences = bugReferences,
Origin = origin,
Confidence = confidence,
ParsedAt = DateTimeOffset.UtcNow
};
}
/// <summary>
/// Batch parse multiple patches from debian/patches directory.
/// </summary>
public static IReadOnlyList<PatchHeaderParseResult> ParsePatchDirectory(
string basePath,
IEnumerable<string> patchFiles)
{
var results = new List<PatchHeaderParseResult>();
foreach (var patchFile in patchFiles)
{
try
{
var fullPath = Path.Combine(basePath, patchFile);
if (File.Exists(fullPath))
{
var content = File.ReadAllText(fullPath);
var result = ParsePatchFile(content, patchFile);
if (result.CveIds.Count > 0)
{
results.Add(result);
}
}
}
catch
{
// Skip files that can't be read
continue;
}
}
return results;
}
private static double CalculateConfidence(int cveCount, string description, string origin)
{
// Base confidence for patch header CVE mention
var confidence = 0.80;
// Bonus for multiple CVEs (more explicit)
if (cveCount > 1)
{
confidence += 0.05;
}
// Bonus for detailed description
if (description.Length > 50)
{
confidence += 0.03;
}
// Bonus for upstream origin
if (origin.Contains("upstream", StringComparison.OrdinalIgnoreCase))
{
confidence += 0.02;
}
return Math.Min(confidence, 0.95);
}
[GeneratedRegex(@"CVE-\d{4}-\d{4,}")]
private static partial Regex CvePatternRegex();
}
public sealed record PatchHeaderParseResult
{
public required string PatchFilePath { get; init; }
public required IReadOnlyList<string> CveIds { get; init; }
public required string Description { get; init; }
public required IReadOnlyList<string> BugReferences { get; init; }
public required string Origin { get; init; }
public required double Confidence { get; init; }
public required DateTimeOffset ParsedAt { get; init; }
}

View File

@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,262 @@
namespace StellaOps.Concelier.SourceIntel.Tests;
using FluentAssertions;
using StellaOps.Concelier.SourceIntel;
using Xunit;
public sealed class ChangelogParserTests
{
[Fact]
public void ParseDebianChangelog_SingleEntry_ExtractsCveAndMetadata()
{
// Arrange
var changelog = @"package-name (1.2.3-1) unstable; urgency=high
* Fix security vulnerability CVE-2024-1234
-- Maintainer Name <email@example.com> Mon, 15 Jan 2024 10:30:00 +0000";
// Act
var result = ChangelogParser.ParseDebianChangelog(changelog);
// Assert
result.Entries.Should().HaveCount(1);
var entry = result.Entries[0];
entry.PackageName.Should().Be("package-name");
entry.Version.Should().Be("1.2.3-1");
entry.CveIds.Should().ContainSingle("CVE-2024-1234");
entry.Confidence.Should().Be(0.80);
}
[Fact]
public void ParseDebianChangelog_MultipleCvesInOneEntry_ExtractsAll()
{
// Arrange
var changelog = @"mypackage (2.0.0-1) stable; urgency=medium
* Security fixes for CVE-2024-1111 and CVE-2024-2222
* Additional fix for CVE-2024-3333
-- Author <author@example.com> Tue, 20 Feb 2024 14:00:00 +0000";
// Act
var result = ChangelogParser.ParseDebianChangelog(changelog);
// Assert
result.Entries.Should().HaveCount(1);
result.Entries[0].CveIds.Should().HaveCount(3);
result.Entries[0].CveIds.Should().Contain("CVE-2024-1111");
result.Entries[0].CveIds.Should().Contain("CVE-2024-2222");
result.Entries[0].CveIds.Should().Contain("CVE-2024-3333");
}
[Fact]
public void ParseDebianChangelog_MultipleEntries_ExtractsOnlyThoseWithCves()
{
// Arrange
var changelog = @"pkg (1.0.0-2) unstable; urgency=low
* Fix for CVE-2024-9999
-- Dev <dev@example.com> Mon, 01 Jan 2024 12:00:00 +0000
pkg (1.0.0-1) unstable; urgency=low
* Initial release (no CVE)
-- Dev <dev@example.com> Sun, 31 Dec 2023 12:00:00 +0000";
// Act
var result = ChangelogParser.ParseDebianChangelog(changelog);
// Assert
result.Entries.Should().HaveCount(1);
result.Entries[0].Version.Should().Be("1.0.0-2");
result.Entries[0].CveIds.Should().ContainSingle("CVE-2024-9999");
}
[Fact]
public void ParseDebianChangelog_NoCves_ReturnsEmptyList()
{
// Arrange
var changelog = @"pkg (1.0.0-1) unstable; urgency=low
* Regular update with no security fixes
-- Dev <dev@example.com> Mon, 01 Jan 2024 12:00:00 +0000";
// Act
var result = ChangelogParser.ParseDebianChangelog(changelog);
// Assert
result.Entries.Should().BeEmpty();
}
[Fact]
public void ParseRpmChangelog_SingleEntry_ExtractsCve()
{
// Arrange
var changelog = @"* Mon Jan 15 2024 Maintainer <maint@example.com> - 1.2.3-1
- Fix CVE-2024-5678 vulnerability
- Other changes";
// Act
var result = ChangelogParser.ParseRpmChangelog(changelog);
// Assert
result.Entries.Should().HaveCount(1);
var entry = result.Entries[0];
entry.Version.Should().Be("1.2.3-1");
entry.CveIds.Should().ContainSingle("CVE-2024-5678");
entry.Confidence.Should().Be(0.80);
}
[Fact]
public void ParseRpmChangelog_MultipleCves_ExtractsAll()
{
// Arrange
var changelog = @"* Tue Feb 20 2024 Dev <dev@example.com> - 2.0.0-1
- Security update for CVE-2024-1111
- Also fixes CVE-2024-2222 and CVE-2024-3333";
// Act
var result = ChangelogParser.ParseRpmChangelog(changelog);
// Assert
result.Entries.Should().HaveCount(1);
result.Entries[0].CveIds.Should().HaveCount(3);
}
[Fact]
public void ParseAlpineSecfixes_SingleVersion_ExtractsCves()
{
// Arrange
var secfixes = @"secfixes:
1.2.3-r0:
- CVE-2024-1234
- CVE-2024-5678";
// Act
var result = ChangelogParser.ParseAlpineSecfixes(secfixes);
// Assert
result.Entries.Should().HaveCount(1);
var entry = result.Entries[0];
entry.Version.Should().Be("1.2.3-r0");
entry.CveIds.Should().HaveCount(2);
entry.CveIds.Should().Contain("CVE-2024-1234");
entry.CveIds.Should().Contain("CVE-2024-5678");
entry.Confidence.Should().Be(0.85); // Alpine has higher confidence
}
[Fact]
public void ParseAlpineSecfixes_MultipleVersions_ExtractsAll()
{
// Arrange
var secfixes = @"secfixes:
2.0.0-r0:
- CVE-2024-9999
1.5.0-r1:
- CVE-2024-8888
- CVE-2024-7777";
// Act
var result = ChangelogParser.ParseAlpineSecfixes(secfixes);
// Assert
result.Entries.Should().HaveCount(2);
result.Entries.Should().Contain(e => e.Version == "2.0.0-r0" && e.CveIds.Contains("CVE-2024-9999"));
result.Entries.Should().Contain(e => e.Version == "1.5.0-r1" && e.CveIds.Count == 2);
}
[Fact]
public void ParseAlpineSecfixes_NoSecfixes_ReturnsEmpty()
{
// Arrange
var secfixes = @"secfixes:";
// Act
var result = ChangelogParser.ParseAlpineSecfixes(secfixes);
// Assert
result.Entries.Should().BeEmpty();
}
[Fact]
public void ParseDebianChangelog_ParsedAtTimestamp_IsRecorded()
{
// Arrange
var changelog = @"pkg (1.0.0-1) unstable; urgency=low
* Fix CVE-2024-0001
-- Dev <dev@example.com> Mon, 01 Jan 2024 12:00:00 +0000";
// Act
var before = DateTimeOffset.UtcNow.AddSeconds(-1);
var result = ChangelogParser.ParseDebianChangelog(changelog);
var after = DateTimeOffset.UtcNow.AddSeconds(1);
// Assert
result.ParsedAt.Should().BeAfter(before);
result.ParsedAt.Should().BeBefore(after);
}
[Fact]
public void ParseDebianChangelog_DuplicateCves_AreNotDuplicated()
{
// Arrange
var changelog = @"pkg (1.0.0-1) unstable; urgency=low
* Fix CVE-2024-1234
* Additional fix for CVE-2024-1234
-- Dev <dev@example.com> Mon, 01 Jan 2024 12:00:00 +0000";
// Act
var result = ChangelogParser.ParseDebianChangelog(changelog);
// Assert
result.Entries.Should().HaveCount(1);
result.Entries[0].CveIds.Should().HaveCount(1);
result.Entries[0].CveIds.Should().ContainSingle("CVE-2024-1234");
}
[Fact]
public void ParseRpmChangelog_MultipleEntries_ExtractsOnlyWithCves()
{
// Arrange
var changelog = @"* Mon Jan 15 2024 Dev <dev@example.com> - 1.2.0-1
- Fix CVE-2024-1111
* Sun Jan 14 2024 Dev <dev@example.com> - 1.1.0-1
- Regular update, no CVE";
// Act
var result = ChangelogParser.ParseRpmChangelog(changelog);
// Assert
result.Entries.Should().HaveCount(1);
result.Entries[0].Version.Should().Be("1.2.0-1");
}
[Fact]
public void ParseDebianChangelog_DescriptionContainsCveReference_IsCaptured()
{
// Arrange
var changelog = @"pkg (1.0.0-1) unstable; urgency=high
* Security update addressing CVE-2024-ABCD
* Fixes buffer overflow in parsing function
-- Dev <dev@example.com> Mon, 01 Jan 2024 12:00:00 +0000";
// Act
var result = ChangelogParser.ParseDebianChangelog(changelog);
// Assert
result.Entries.Should().HaveCount(1);
result.Entries[0].Description.Should().Contain("CVE-2024-ABCD");
result.Entries[0].Description.Should().Contain("buffer overflow");
}
}

View File

@@ -0,0 +1,282 @@
namespace StellaOps.Concelier.SourceIntel.Tests;
using FluentAssertions;
using StellaOps.Concelier.SourceIntel;
using Xunit;
public sealed class PatchHeaderParserTests
{
[Fact]
public void ParsePatchFile_Dep3FormatWithCve_ExtractsCveAndMetadata()
{
// Arrange
var patch = @"Description: Fix buffer overflow vulnerability
This patch addresses CVE-2024-1234 by validating input length
Origin: upstream, https://example.com/commit/abc123
Bug: https://bugs.example.com/12345
Bug-Debian: https://bugs.debian.org/67890
--- a/src/file.c
+++ b/src/file.c
@@ -10,3 +10,4 @@
context
+fixed line";
// Act
var result = PatchHeaderParser.ParsePatchFile(patch, "CVE-2024-1234.patch");
// Assert
result.CveIds.Should().ContainSingle("CVE-2024-1234");
result.Description.Should().Contain("buffer overflow");
result.Origin.Should().Contain("upstream");
result.BugReferences.Should().HaveCount(2);
result.Confidence.Should().BeGreaterThan(0.80);
}
[Fact]
public void ParsePatchFile_MultipleCves_ExtractsAll()
{
// Arrange
var patch = @"Description: Security fixes for CVE-2024-1111 and CVE-2024-2222
Origin: upstream
--- a/file.c
+++ b/file.c
@@ -1,1 +1,2 @@
+fix";
// Act
var result = PatchHeaderParser.ParsePatchFile(patch, "multi-cve.patch");
// Assert
result.CveIds.Should().HaveCount(2);
result.CveIds.Should().Contain("CVE-2024-1111");
result.CveIds.Should().Contain("CVE-2024-2222");
}
[Fact]
public void ParsePatchFile_CveInFilename_ExtractsFromFilename()
{
// Arrange
var patch = @"Description: Security fix
Origin: upstream
--- a/file.c
+++ b/file.c
@@ -1,1 +1,2 @@
+fix";
// Act
var result = PatchHeaderParser.ParsePatchFile(patch, "patches/CVE-2024-9999.patch");
// Assert
result.CveIds.Should().ContainSingle("CVE-2024-9999");
}
[Fact]
public void ParsePatchFile_CveInBothHeaderAndFilename_ExtractsBoth()
{
// Arrange
var patch = @"Description: Fix for CVE-2024-1111
Origin: upstream
--- a/file.c
+++ b/file.c
@@ -1,1 +1,2 @@
+fix";
// Act
var result = PatchHeaderParser.ParsePatchFile(patch, "CVE-2024-2222.patch");
// Assert
result.CveIds.Should().HaveCount(2);
result.CveIds.Should().Contain("CVE-2024-1111");
result.CveIds.Should().Contain("CVE-2024-2222");
}
[Fact]
public void ParsePatchFile_BugReferences_ExtractsFromMultipleSources()
{
// Arrange
var patch = @"Description: Security fix
Bug: https://example.com/bug1
Bug-Debian: https://bugs.debian.org/123
Bug-Ubuntu: https://launchpad.net/456
--- a/file.c
+++ b/file.c";
// Act
var result = PatchHeaderParser.ParsePatchFile(patch, "test.patch");
// Assert
result.BugReferences.Should().HaveCount(3);
result.BugReferences.Should().Contain(b => b.Contains("example.com"));
result.BugReferences.Should().Contain(b => b.Contains("debian.org"));
result.BugReferences.Should().Contain(b => b.Contains("launchpad.net"));
}
[Fact]
public void ParsePatchFile_ConfidenceCalculation_IncreasesWithMoreEvidence()
{
// Arrange
var patchMinimal = @"Description: Fix
--- a/file.c";
var patchDetailed = @"Description: Detailed security fix for memory corruption vulnerability
This patch addresses a critical buffer overflow that could lead to remote
code execution. The fix validates all input before processing.
Origin: upstream, https://github.com/example/repo/commit/abc123
Bug: https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2024-1234
--- a/file.c";
// Act
var resultMinimal = PatchHeaderParser.ParsePatchFile(patchMinimal, "CVE-2024-1234.patch");
var resultDetailed = PatchHeaderParser.ParsePatchFile(patchDetailed, "CVE-2024-1234.patch");
// Assert
resultDetailed.Confidence.Should().BeGreaterThan(resultMinimal.Confidence);
}
[Fact]
public void ParsePatchFile_MultipleCvesInHeader_IncreasesConfidence()
{
// Arrange
var patchSingle = @"Description: Fix CVE-2024-1111
Origin: upstream
--- a/file.c";
var patchMultiple = @"Description: Fix CVE-2024-1111 and CVE-2024-2222
Origin: upstream
--- a/file.c";
// Act
var resultSingle = PatchHeaderParser.ParsePatchFile(patchSingle, "test.patch");
var resultMultiple = PatchHeaderParser.ParsePatchFile(patchMultiple, "test.patch");
// Assert
resultMultiple.Confidence.Should().BeGreaterThan(resultSingle.Confidence);
}
[Fact]
public void ParsePatchFile_UpstreamOrigin_IncreasesConfidence()
{
// Arrange
var patchNoOrigin = @"Description: Fix CVE-2024-1234
--- a/file.c";
var patchUpstream = @"Description: Fix CVE-2024-1234
Origin: upstream
--- a/file.c";
// Act
var resultNoOrigin = PatchHeaderParser.ParsePatchFile(patchNoOrigin, "test.patch");
var resultUpstream = PatchHeaderParser.ParsePatchFile(patchUpstream, "test.patch");
// Assert
resultUpstream.Confidence.Should().BeGreaterThan(resultNoOrigin.Confidence);
}
[Fact]
public void ParsePatchFile_StopsAtDiffContent_DoesNotParseBody()
{
// Arrange
var patch = @"Description: Security fix
Origin: upstream
--- a/src/file.c
+++ b/src/file.c
@@ -10,3 +10,4 @@
context line
+// This mentions CVE-9999-9999 but should not be extracted
context line";
// Act
var result = PatchHeaderParser.ParsePatchFile(patch, "test.patch");
// Assert
result.CveIds.Should().NotContain("CVE-9999-9999");
}
[Fact]
public void ParsePatchFile_NoCves_ReturnsEmptyCveList()
{
// Arrange
var patch = @"Description: Regular update
Origin: vendor
--- a/file.c
+++ b/file.c";
// Act
var result = PatchHeaderParser.ParsePatchFile(patch, "regular.patch");
// Assert
result.CveIds.Should().BeEmpty();
result.Confidence.Should().Be(0.0);
}
[Fact]
public void ParsePatchFile_ConfidenceCappedAt95Percent()
{
// Arrange
var patch = @"Description: Extremely detailed security fix with multiple CVE references
CVE-2024-1111 CVE-2024-2222 CVE-2024-3333 CVE-2024-4444
Very long description to ensure confidence bonus
Origin: upstream, backported from mainline
Bug: https://example.com/1
Bug-Debian: https://debian.org/2
Bug-Ubuntu: https://ubuntu.com/3
--- a/file.c";
// Act
var result = PatchHeaderParser.ParsePatchFile(patch, "CVE-2024-5555.patch");
// Assert
result.Confidence.Should().BeLessOrEqualTo(0.95);
}
[Fact]
public void ParsePatchDirectory_MultiplePatches_FiltersOnlyWithCves()
{
// Arrange - This would need filesystem setup, skipping actual implementation
// Just documenting expected behavior:
// Given a directory with patches, only those containing CVE references should be returned
}
[Fact]
public void ParsePatchFile_ParsedAtTimestamp_IsRecorded()
{
// Arrange
var patch = @"Description: Fix CVE-2024-1234
--- a/file.c";
// Act
var before = DateTimeOffset.UtcNow.AddSeconds(-1);
var result = PatchHeaderParser.ParsePatchFile(patch, "test.patch");
var after = DateTimeOffset.UtcNow.AddSeconds(1);
// Assert
result.ParsedAt.Should().BeAfter(before);
result.ParsedAt.Should().BeBefore(after);
}
[Fact]
public void ParsePatchFile_DuplicateCves_AreNotDuplicated()
{
// Arrange
var patch = @"Description: Fix CVE-2024-1234 and CVE-2024-1234 again
Origin: upstream
--- a/file.c";
// Act
var result = PatchHeaderParser.ParsePatchFile(patch, "CVE-2024-1234.patch");
// Assert
result.CveIds.Should().HaveCount(1);
result.CveIds.Should().ContainSingle("CVE-2024-1234");
}
}

View File

@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.12.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\__Libraries\StellaOps.Concelier.SourceIntel\StellaOps.Concelier.SourceIntel.csproj" />
</ItemGroup>
</Project>