// SPDX-License-Identifier: BUSL-1.1 // Copyright (c) StellaOps using StellaOps.Scanner.Reachability.Stack; using System.Collections.Immutable; namespace StellaOps.Scanner.Reachability.Layer3; /// /// Layer 3 analyzer: Runtime gating detection. /// Determines if any feature flag, configuration, or environment condition /// blocks execution of the vulnerable code path. /// public interface ILayer3Analyzer { /// /// Analyzes whether runtime conditions gate (block) execution of a call path. /// /// The call path to analyze for gating conditions /// Runtime context (config, env vars, etc.) /// Cancellation token /// Layer 3 gating analysis result Task AnalyzeAsync( CallPath path, RuntimeContext context, CancellationToken ct = default); /// /// Analyzes gating for multiple paths and aggregates results. /// /// Call paths to analyze /// Runtime context /// Cancellation token /// Aggregated Layer 3 result Task AnalyzeMultipleAsync( ImmutableArray paths, RuntimeContext context, CancellationToken ct = default); } /// /// Runtime context - configuration and environment affecting execution. /// public sealed record RuntimeContext { /// Environment variables public ImmutableDictionary EnvironmentVariables { get; init; } = ImmutableDictionary.Empty; /// Configuration values from files/services public ImmutableDictionary Configuration { get; init; } = ImmutableDictionary.Empty; /// Feature flags and their states public ImmutableDictionary FeatureFlags { get; init; } = ImmutableDictionary.Empty; /// Build/compile-time configuration public BuildConfiguration? BuildConfig { get; init; } /// Platform information public PlatformInfo? Platform { get; init; } /// Process capabilities/privileges public ImmutableArray Capabilities { get; init; } = []; } /// /// A configuration value. /// public sealed record ConfigValue( string Key, string? Value, ConfigValueSource Source, bool IsSecret ); /// /// Source of a configuration value. /// public enum ConfigValueSource { EnvironmentVariable, ConfigFile, CommandLine, RemoteService, Default, Unknown } /// /// A feature flag. /// public sealed record FeatureFlag( string Name, bool IsEnabled, FeatureFlagSource Source, string? Description ); /// /// Source of a feature flag. /// public enum FeatureFlagSource { CompileTime, ConfigFile, RemoteService, EnvironmentVariable, Default, Unknown } /// /// Build/compile-time configuration. /// public sealed record BuildConfiguration { /// Whether this is a debug build public bool IsDebugBuild { get; init; } /// Defined preprocessor symbols public ImmutableArray DefineConstants { get; init; } = []; /// Target framework public string? TargetFramework { get; init; } /// Build mode (Debug, Release, etc.) public string? BuildMode { get; init; } } /// /// Platform information. /// public sealed record PlatformInfo { /// Operating system public required string OS { get; init; } /// OS version public string? OSVersion { get; init; } /// Architecture (x64, arm64, etc.) public required string Architecture { get; init; } /// Whether running in container public bool IsContainer { get; init; } /// Container runtime if applicable public string? ContainerRuntime { get; init; } } /// /// Input for Layer 3 analysis. /// public sealed record Layer3AnalysisInput { public required CallPath Path { get; init; } public required RuntimeContext Context { get; init; } public Layer3AnalysisOptions? Options { get; init; } } /// /// Options for Layer 3 analysis. /// public sealed record Layer3AnalysisOptions { /// Detect feature flag patterns in code public bool DetectFeatureFlags { get; init; } = true; /// Detect environment variable checks public bool DetectEnvVarChecks { get; init; } = true; /// Detect configuration value checks public bool DetectConfigChecks { get; init; } = true; /// Detect platform checks public bool DetectPlatformChecks { get; init; } = true; /// Detect capability/privilege checks public bool DetectCapabilityChecks { get; init; } = true; /// Feature flag patterns to detect (regex) public ImmutableArray FeatureFlagPatterns { get; init; } = [ @"FeatureFlags?\.", @"IsFeatureEnabled", @"Feature\.IsEnabled", @"LaunchDarkly", @"Unleash", @"ConfigCat" ]; /// Known blocking conditions public ImmutableArray KnownPatterns { get; init; } = []; } /// /// A known gating pattern to detect. /// public sealed record KnownGatingPattern( string Pattern, GatingType Type, string Description, bool IsBlockingByDefault );