| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | using AsiBackbone.Core.Decisions; |
| | | 3 | | using AsiBackbone.Core.Results; |
| | | 4 | | |
| | | 5 | | namespace AsiBackbone.Core.ThreatModeling; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Represents a structured threat-aware assessment contributed during policy evaluation. |
| | | 9 | | /// </summary> |
| | | 10 | | public sealed class ThreatAssessment |
| | | 11 | | { |
| | | 12 | | private const string ReservedMetadataPrefix = "threat."; |
| | | 13 | | |
| | 2 | 14 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 2 | 15 | | new ReadOnlyDictionary<string, string>( |
| | 2 | 16 | | new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Defines the minimum confidence value accepted by a threat assessment. |
| | | 20 | | /// </summary> |
| | | 21 | | public const double MinimumConfidence = 0.0D; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Defines the maximum confidence value accepted by a threat assessment. |
| | | 25 | | /// </summary> |
| | | 26 | | public const double MaximumConfidence = 1.0D; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Initializes a new instance of the <see cref="ThreatAssessment" /> class. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="severity">The severity reported by the contributor.</param> |
| | | 32 | | /// <param name="category">The threat category reported by the contributor.</param> |
| | | 33 | | /// <param name="reasonCode">The machine-readable reason code.</param> |
| | | 34 | | /// <param name="description">The human-readable threat description.</param> |
| | | 35 | | /// <param name="recommendedOutcome">The governance outcome recommended by the contributor.</param> |
| | | 36 | | /// <param name="confidence">The contributor confidence from 0.0 to 1.0.</param> |
| | | 37 | | /// <param name="metadata"> |
| | | 38 | | /// Optional contributor-supplied metadata retained on generated operation reasons. |
| | | 39 | | /// Keys beginning with <c>threat.</c> are reserved for framework-generated provenance. |
| | | 40 | | /// </param> |
| | | 41 | | /// <exception cref="ArgumentException"> |
| | | 42 | | /// Thrown when <paramref name="category" />, <paramref name="reasonCode" />, or |
| | | 43 | | /// <paramref name="description" /> is null, empty, or whitespace, or when |
| | | 44 | | /// <paramref name="metadata" /> contains a key in the reserved <c>threat.</c> namespace. |
| | | 45 | | /// </exception> |
| | | 46 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 47 | | /// Thrown when <paramref name="severity" /> or <paramref name="recommendedOutcome" /> is undefined, |
| | | 48 | | /// or when <paramref name="confidence" /> is outside the supported range. |
| | | 49 | | /// </exception> |
| | 172 | 50 | | public ThreatAssessment( |
| | 172 | 51 | | ThreatSeverity severity, |
| | 172 | 52 | | string category, |
| | 172 | 53 | | string reasonCode, |
| | 172 | 54 | | string description, |
| | 172 | 55 | | GovernanceDecisionOutcome recommendedOutcome, |
| | 172 | 56 | | double confidence = MaximumConfidence, |
| | 172 | 57 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 58 | | { |
| | 172 | 59 | | if (!Enum.IsDefined(severity)) |
| | | 60 | | { |
| | 2 | 61 | | throw new ArgumentOutOfRangeException(nameof(severity), severity, "Threat severity must be defined."); |
| | | 62 | | } |
| | | 63 | | |
| | 170 | 64 | | if (!Enum.IsDefined(recommendedOutcome)) |
| | | 65 | | { |
| | 4 | 66 | | throw new ArgumentOutOfRangeException( |
| | 4 | 67 | | nameof(recommendedOutcome), |
| | 4 | 68 | | recommendedOutcome, |
| | 4 | 69 | | "Recommended governance outcome must be defined."); |
| | | 70 | | } |
| | | 71 | | |
| | 166 | 72 | | ArgumentException.ThrowIfNullOrWhiteSpace(category); |
| | 164 | 73 | | ArgumentException.ThrowIfNullOrWhiteSpace(reasonCode); |
| | 162 | 74 | | ArgumentException.ThrowIfNullOrWhiteSpace(description); |
| | | 75 | | |
| | 160 | 76 | | if (confidence is < MinimumConfidence or > MaximumConfidence) |
| | | 77 | | { |
| | 4 | 78 | | throw new ArgumentOutOfRangeException( |
| | 4 | 79 | | nameof(confidence), |
| | 4 | 80 | | confidence, |
| | 4 | 81 | | $"{nameof(confidence)} must be between {MinimumConfidence} and {MaximumConfidence}."); |
| | | 82 | | } |
| | | 83 | | |
| | 156 | 84 | | Severity = severity; |
| | 156 | 85 | | Category = category.Trim(); |
| | 156 | 86 | | ReasonCode = reasonCode.Trim(); |
| | 156 | 87 | | Description = description.Trim(); |
| | 156 | 88 | | RecommendedOutcome = recommendedOutcome; |
| | 156 | 89 | | Confidence = confidence; |
| | 156 | 90 | | Metadata = NormalizeMetadata(metadata); |
| | 138 | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Gets the severity reported by the contributor. |
| | | 95 | | /// </summary> |
| | 248 | 96 | | public ThreatSeverity Severity { get; } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Gets the threat category reported by the contributor. |
| | | 100 | | /// </summary> |
| | 124 | 101 | | public string Category { get; } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Gets the machine-readable reason code. |
| | | 105 | | /// </summary> |
| | 122 | 106 | | public string ReasonCode { get; } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Gets the human-readable threat description. |
| | | 110 | | /// </summary> |
| | 122 | 111 | | public string Description { get; } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Gets the governance outcome recommended by the contributor. |
| | | 115 | | /// </summary> |
| | 368 | 116 | | public GovernanceDecisionOutcome RecommendedOutcome { get; } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Gets the contributor confidence from 0.0 to 1.0. |
| | | 120 | | /// </summary> |
| | 122 | 121 | | public double Confidence { get; } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Gets optional contributor-supplied metadata retained on generated operation reasons. |
| | | 125 | | /// </summary> |
| | 126 | 126 | | public IReadOnlyDictionary<string, string> Metadata { get; } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Gets a value indicating whether the assessment should influence the composed decision. |
| | | 130 | | /// </summary> |
| | 124 | 131 | | public bool IsActionable => Severity is not ThreatSeverity.None || RecommendedOutcome is not GovernanceDecisionOutco |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Creates a no-threat assessment. |
| | | 135 | | /// </summary> |
| | | 136 | | /// <returns>A no-threat assessment that does not influence decision composition.</returns> |
| | | 137 | | public static ThreatAssessment NoThreat() |
| | | 138 | | { |
| | 8 | 139 | | return new ThreatAssessment( |
| | 8 | 140 | | ThreatSeverity.None, |
| | 8 | 141 | | ThreatCategories.None, |
| | 8 | 142 | | "asibackbone.threat.none", |
| | 8 | 143 | | "No threat indicators were reported.", |
| | 8 | 144 | | GovernanceDecisionOutcome.Allowed, |
| | 8 | 145 | | MinimumConfidence); |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// Creates a threat assessment. |
| | | 150 | | /// </summary> |
| | | 151 | | /// <param name="severity">The severity reported by the contributor.</param> |
| | | 152 | | /// <param name="category">The threat category reported by the contributor.</param> |
| | | 153 | | /// <param name="reasonCode">The machine-readable reason code.</param> |
| | | 154 | | /// <param name="description">The human-readable threat description.</param> |
| | | 155 | | /// <param name="recommendedOutcome">The governance outcome recommended by the contributor.</param> |
| | | 156 | | /// <param name="confidence">The contributor confidence from 0.0 to 1.0.</param> |
| | | 157 | | /// <param name="metadata"> |
| | | 158 | | /// Optional contributor-supplied metadata retained on generated operation reasons. |
| | | 159 | | /// Keys beginning with <c>threat.</c> are reserved for framework-generated provenance. |
| | | 160 | | /// </param> |
| | | 161 | | /// <returns>A threat assessment.</returns> |
| | | 162 | | /// <exception cref="ArgumentException"> |
| | | 163 | | /// Thrown when <paramref name="category" />, <paramref name="reasonCode" />, or |
| | | 164 | | /// <paramref name="description" /> is null, empty, or whitespace, or when |
| | | 165 | | /// <paramref name="metadata" /> contains a key in the reserved <c>threat.</c> namespace. |
| | | 166 | | /// </exception> |
| | | 167 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 168 | | /// Thrown when <paramref name="severity" /> or <paramref name="recommendedOutcome" /> is undefined, |
| | | 169 | | /// or when <paramref name="confidence" /> is outside the supported range. |
| | | 170 | | /// </exception> |
| | | 171 | | public static ThreatAssessment Create( |
| | | 172 | | ThreatSeverity severity, |
| | | 173 | | string category, |
| | | 174 | | string reasonCode, |
| | | 175 | | string description, |
| | | 176 | | GovernanceDecisionOutcome recommendedOutcome, |
| | | 177 | | double confidence = MaximumConfidence, |
| | | 178 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 179 | | { |
| | 164 | 180 | | return new ThreatAssessment( |
| | 164 | 181 | | severity, |
| | 164 | 182 | | category, |
| | 164 | 183 | | reasonCode, |
| | 164 | 184 | | description, |
| | 164 | 185 | | recommendedOutcome, |
| | 164 | 186 | | confidence, |
| | 164 | 187 | | metadata); |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | /// <summary> |
| | | 191 | | /// Converts the assessment into an operation reason with threat metadata. |
| | | 192 | | /// </summary> |
| | | 193 | | /// <param name="contributorName">Optional contributor name to include in metadata.</param> |
| | | 194 | | /// <param name="effectiveOutcome">Optional effective outcome selected by the evaluator after safety promotion.</par |
| | | 195 | | /// <returns>An operation reason representing the assessment.</returns> |
| | | 196 | | public OperationReason ToOperationReason( |
| | | 197 | | string? contributorName = null, |
| | | 198 | | GovernanceDecisionOutcome? effectiveOutcome = null) |
| | | 199 | | { |
| | 122 | 200 | | Dictionary<string, string> metadata = new(StringComparer.Ordinal) |
| | 122 | 201 | | { |
| | 122 | 202 | | ["threat.category"] = Category, |
| | 122 | 203 | | ["threat.severity"] = Severity.ToString(), |
| | 122 | 204 | | ["threat.recommended_outcome"] = RecommendedOutcome.ToString(), |
| | 122 | 205 | | ["threat.effective_outcome"] = (effectiveOutcome ?? RecommendedOutcome).ToString(), |
| | 122 | 206 | | ["threat.confidence"] = Confidence.ToString("G", System.Globalization.CultureInfo.InvariantCulture) |
| | 122 | 207 | | }; |
| | | 208 | | |
| | 122 | 209 | | if (!string.IsNullOrWhiteSpace(contributorName)) |
| | | 210 | | { |
| | 118 | 211 | | metadata["threat.contributor"] = contributorName.Trim(); |
| | | 212 | | } |
| | | 213 | | |
| | 416 | 214 | | foreach (KeyValuePair<string, string> item in Metadata) |
| | | 215 | | { |
| | 86 | 216 | | metadata[item.Key] = item.Value; |
| | | 217 | | } |
| | | 218 | | |
| | 122 | 219 | | return OperationReason.Create(ReasonCode, Description, metadata); |
| | | 220 | | } |
| | | 221 | | |
| | | 222 | | private static IReadOnlyDictionary<string, string> NormalizeMetadata( |
| | | 223 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 224 | | { |
| | 156 | 225 | | if (metadata is null || metadata.Count == 0) |
| | | 226 | | { |
| | 90 | 227 | | return EmptyMetadata; |
| | | 228 | | } |
| | | 229 | | |
| | 66 | 230 | | Dictionary<string, string> normalizedMetadata = new(StringComparer.Ordinal); |
| | | 231 | | |
| | 326 | 232 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 233 | | { |
| | 106 | 234 | | if (string.IsNullOrWhiteSpace(item.Key)) |
| | | 235 | | { |
| | | 236 | | continue; |
| | | 237 | | } |
| | | 238 | | |
| | 104 | 239 | | string normalizedKey = item.Key.Trim(); |
| | | 240 | | |
| | 104 | 241 | | if (normalizedKey.StartsWith(ReservedMetadataPrefix, StringComparison.OrdinalIgnoreCase)) |
| | | 242 | | { |
| | 18 | 243 | | throw new ArgumentException( |
| | 18 | 244 | | $"Contributor metadata keys beginning with '{ReservedMetadataPrefix}' are reserved for framework-gen |
| | 18 | 245 | | nameof(metadata)); |
| | | 246 | | } |
| | | 247 | | |
| | 86 | 248 | | normalizedMetadata[normalizedKey] = item.Value?.Trim() ?? string.Empty; |
| | | 249 | | } |
| | | 250 | | |
| | 48 | 251 | | return normalizedMetadata.Count == 0 |
| | 48 | 252 | | ? EmptyMetadata |
| | 48 | 253 | | : new ReadOnlyDictionary<string, string>(normalizedMetadata); |
| | | 254 | | } |
| | | 255 | | } |