< Summary

Information
Class: Chronicis.Api.Utilities.LogSanitizer
Assembly: Chronicis.Api
File(s): /home/runner/work/chronicis/chronicis/src/Chronicis.Api/Utilities/LogSanitizer.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 19
Coverable lines: 19
Total lines: 88
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%210%
Sanitize(...)0%7280%
SanitizeObject(...)0%620%
SanitizeMultiple(...)100%210%

File(s)

/home/runner/work/chronicis/chronicis/src/Chronicis.Api/Utilities/LogSanitizer.cs

#LineLine coverage
 1using System.Text.RegularExpressions;
 2
 3namespace Chronicis.Api.Utilities;
 4
 5/// <summary>
 6/// Provides aggressive sanitization of user input before logging to prevent log injection attacks.
 7/// </summary>
 8public static class LogSanitizer
 9{
 10    private const int MaxLogLength = 1000;
 11    private const string TruncationMarker = "...[TRUNCATED]";
 12    private const string SanitizedMarker = "[SANITIZED]";
 13
 14    // Matches all control characters including newlines, carriage returns, tabs, etc.
 015    private static readonly Regex ControlCharPattern = new(@"[\x00-\x1F\x7F-\x9F]", RegexOptions.Compiled);
 16
 17    // Matches potentially dangerous sequences
 018    private static readonly Regex DangerousPattern = new(@"(\r\n|\r|\n|\t|%0[aAdD]|%0[aA]|%0[dD])", RegexOptions.Compile
 19
 20    /// <summary>
 21    /// Sanitizes a string value for safe logging by removing control characters and truncating length.
 22    /// </summary>
 23    /// <param name="value">The value to sanitize. Can be null.</param>
 24    /// <returns>A sanitized version of the input safe for logging.</returns>
 25    public static string? Sanitize(string? value)
 26    {
 027        if (string.IsNullOrEmpty(value))
 28        {
 029            return value;
 30        }
 31
 32        // First pass: Remove control characters
 033        var sanitized = ControlCharPattern.Replace(value, "");
 34
 35        // Second pass: Remove URL-encoded dangerous characters
 036        sanitized = DangerousPattern.Replace(sanitized, "");
 37
 38        // Remove any remaining whitespace sequences longer than 1 space
 039        sanitized = Regex.Replace(sanitized, @"\s{2,}", " ");
 40
 41        // Trim the result
 042        sanitized = sanitized.Trim();
 43
 44        // Check if we sanitized anything
 045        var wasSanitized = sanitized != value;
 46
 47        // Truncate if too long
 048        if (sanitized.Length > MaxLogLength)
 49        {
 050            sanitized = sanitized.Substring(0, MaxLogLength) + TruncationMarker;
 051            wasSanitized = true;
 52        }
 53
 54        // Add marker if content was modified
 055        if (wasSanitized && !string.IsNullOrWhiteSpace(sanitized))
 56        {
 057            sanitized = $"{sanitized} {SanitizedMarker}";
 58        }
 59
 060        return sanitized;
 61    }
 62
 63    /// <summary>
 64    /// Sanitizes an object by converting it to string and then sanitizing.
 65    /// Handles null values gracefully.
 66    /// </summary>
 67    /// <param name="value">The object to sanitize. Can be null.</param>
 68    /// <returns>A sanitized string representation safe for logging.</returns>
 69    public static string? SanitizeObject(object? value)
 70    {
 071        if (value == null)
 72        {
 073            return null;
 74        }
 75
 076        return Sanitize(value.ToString());
 77    }
 78
 79    /// <summary>
 80    /// Sanitizes multiple values for logging.
 81    /// </summary>
 82    /// <param name="values">Array of values to sanitize.</param>
 83    /// <returns>Array of sanitized values in the same order.</returns>
 84    public static string?[] SanitizeMultiple(params string?[] values)
 85    {
 086        return values.Select(Sanitize).ToArray();
 87    }
 88}