using Microsoft.Extensions.Options;
namespace StellaOps.Scanner.Analyzers.Native;
///
/// Options for ELF section hash extraction.
///
public sealed class ElfSectionHashOptions
{
///
/// Whether section hashing is enabled.
///
public bool Enabled { get; set; } = true;
///
/// Section names to include (e.g., .text, .rodata, .data).
///
public IList Sections { get; } = new List
{
".text",
".rodata",
".data",
".symtab",
".dynsym"
};
///
/// Hash algorithms to compute (sha256 required, blake3 optional).
///
public IList Algorithms { get; } = new List
{
"sha256"
};
///
/// Maximum section size to hash (bytes).
///
public long MaxSectionSizeBytes { get; set; } = 100 * 1024 * 1024;
}
///
/// Validates .
///
public sealed class ElfSectionHashOptionsValidator : IValidateOptions
{
private static readonly HashSet AllowedAlgorithms = new(StringComparer.OrdinalIgnoreCase)
{
"sha256",
"blake3"
};
public ValidateOptionsResult Validate(string? name, ElfSectionHashOptions options)
{
ArgumentNullException.ThrowIfNull(options);
if (!options.Enabled)
{
return ValidateOptionsResult.Success;
}
if (options.MaxSectionSizeBytes <= 0)
{
return ValidateOptionsResult.Fail("MaxSectionSizeBytes must be greater than zero.");
}
var sections = options.Sections
.Where(section => !string.IsNullOrWhiteSpace(section))
.Select(section => section.Trim())
.ToArray();
if (sections.Length == 0)
{
return ValidateOptionsResult.Fail("At least one section name must be configured.");
}
if (options.Algorithms.Count == 0 ||
!options.Algorithms.Any(alg => string.Equals(alg?.Trim(), "sha256", StringComparison.OrdinalIgnoreCase)))
{
return ValidateOptionsResult.Fail("Algorithms must include sha256.");
}
var invalid = options.Algorithms
.Where(alg => !string.IsNullOrWhiteSpace(alg))
.Select(alg => alg.Trim())
.Where(alg => !AllowedAlgorithms.Contains(alg))
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToArray();
if (invalid.Length > 0)
{
return ValidateOptionsResult.Fail($"Unsupported algorithms: {string.Join(", ", invalid)}");
}
return ValidateOptionsResult.Success;
}
}