| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | |
| | | 3 | | namespace Chronicis.Api.Utilities; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides aggressive sanitization of user input before logging to prevent log injection attacks. |
| | | 7 | | /// </summary> |
| | | 8 | | public static partial class LogSanitizer |
| | | 9 | | { |
| | | 10 | | private const int MaxLogLength = 1000; |
| | | 11 | | private const string TruncationMarker = "...[TRUNCATED]"; |
| | | 12 | | private const string SanitizedMarker = "[SANITIZED]"; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Sanitizes a string value for safe logging by removing control characters and truncating length. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="value">The value to sanitize. Can be null.</param> |
| | | 18 | | /// <returns>A sanitized version of the input safe for logging.</returns> |
| | | 19 | | public static string? Sanitize(string? value) |
| | | 20 | | { |
| | 15 | 21 | | if (string.IsNullOrEmpty(value)) |
| | | 22 | | { |
| | 3 | 23 | | return value; |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | // First pass: Remove control characters |
| | 12 | 27 | | var sanitized = ControlCharRegex().Replace(value, ""); |
| | | 28 | | |
| | | 29 | | // Second pass: Remove URL-encoded dangerous characters |
| | 12 | 30 | | sanitized = DangerousRegex().Replace(sanitized, ""); |
| | | 31 | | |
| | | 32 | | // Remove any remaining whitespace sequences longer than 1 space |
| | 12 | 33 | | sanitized = MultiSpaceRegex().Replace(sanitized, " "); |
| | | 34 | | |
| | | 35 | | // Trim the result |
| | 12 | 36 | | sanitized = sanitized.Trim(); |
| | | 37 | | |
| | | 38 | | // Check if we sanitized anything |
| | 12 | 39 | | var wasSanitized = sanitized != value; |
| | | 40 | | |
| | | 41 | | // Truncate if too long |
| | 12 | 42 | | if (sanitized.Length > MaxLogLength) |
| | | 43 | | { |
| | 1 | 44 | | sanitized = sanitized[..MaxLogLength] + TruncationMarker; |
| | 1 | 45 | | wasSanitized = true; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | // Add marker if content was modified |
| | 12 | 49 | | if (wasSanitized && !string.IsNullOrWhiteSpace(sanitized)) |
| | | 50 | | { |
| | 8 | 51 | | sanitized = $"{sanitized} {SanitizedMarker}"; |
| | | 52 | | } |
| | | 53 | | |
| | 12 | 54 | | return sanitized; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Sanitizes an object by converting it to string and then sanitizing. |
| | | 59 | | /// Handles null values gracefully. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="value">The object to sanitize. Can be null.</param> |
| | | 62 | | /// <returns>A sanitized string representation safe for logging.</returns> |
| | | 63 | | public static string? SanitizeObject(object? value) |
| | | 64 | | { |
| | 3 | 65 | | if (value == null) |
| | | 66 | | { |
| | 1 | 67 | | return null; |
| | | 68 | | } |
| | | 69 | | |
| | 2 | 70 | | return Sanitize(value.ToString()); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Sanitizes multiple values for logging. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="values">Array of values to sanitize.</param> |
| | | 77 | | /// <returns>Array of sanitized values in the same order.</returns> |
| | | 78 | | public static string?[] SanitizeMultiple(params string?[] values) |
| | | 79 | | { |
| | 2 | 80 | | return values.Select(Sanitize).ToArray(); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | [GeneratedRegex(@"[\x00-\x1F\x7F-\x9F]")] |
| | | 84 | | private static partial Regex ControlCharRegex(); |
| | | 85 | | |
| | | 86 | | [GeneratedRegex(@"(\r\n|\r|\n|\t|%0[aAdD]|%0[aA]|%0[dD])")] |
| | | 87 | | private static partial Regex DangerousRegex(); |
| | | 88 | | |
| | | 89 | | [GeneratedRegex(@"\s{2,}")] |
| | | 90 | | private static partial Regex MultiSpaceRegex(); |
| | | 91 | | } |