| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | |
| | | 3 | | namespace Chronicis.Shared.Utilities; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides aggressive sanitization of user input before logging to prevent log injection attacks. |
| | | 7 | | /// </summary> |
| | | 8 | | public 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. |
| | 0 | 15 | | private static readonly Regex ControlCharPattern = new(@"[\x00-\x1F\x7F-\x9F]", RegexOptions.Compiled); |
| | | 16 | | |
| | | 17 | | // Matches potentially dangerous sequences |
| | 0 | 18 | | 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 | | { |
| | 0 | 27 | | if (string.IsNullOrEmpty(value)) |
| | | 28 | | { |
| | 0 | 29 | | return value; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | // First pass: Remove control characters |
| | 0 | 33 | | var sanitized = ControlCharPattern.Replace(value, ""); |
| | | 34 | | |
| | | 35 | | // Second pass: Remove URL-encoded dangerous characters |
| | 0 | 36 | | sanitized = DangerousPattern.Replace(sanitized, ""); |
| | | 37 | | |
| | | 38 | | // Remove any remaining whitespace sequences longer than 1 space |
| | 0 | 39 | | sanitized = Regex.Replace(sanitized, @"\s{2,}", " "); |
| | | 40 | | |
| | | 41 | | // Trim the result |
| | 0 | 42 | | sanitized = sanitized.Trim(); |
| | | 43 | | |
| | | 44 | | // Check if we sanitized anything |
| | 0 | 45 | | var wasSanitized = sanitized != value; |
| | | 46 | | |
| | | 47 | | // Truncate if too long |
| | 0 | 48 | | if (sanitized.Length > MaxLogLength) |
| | | 49 | | { |
| | 0 | 50 | | sanitized = sanitized.Substring(0, MaxLogLength) + TruncationMarker; |
| | 0 | 51 | | wasSanitized = true; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | // Add marker if content was modified |
| | 0 | 55 | | if (wasSanitized && !string.IsNullOrWhiteSpace(sanitized)) |
| | | 56 | | { |
| | 0 | 57 | | sanitized = $"{sanitized} {SanitizedMarker}"; |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | 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 | | { |
| | 0 | 71 | | if (value == null) |
| | | 72 | | { |
| | 0 | 73 | | return null; |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | 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 | | { |
| | 0 | 86 | | return values.Select(Sanitize).ToArray(); |
| | | 87 | | } |
| | | 88 | | } |