144 lines
3.8 KiB
C#
144 lines
3.8 KiB
C#
using System;
|
|
using System.Text;
|
|
|
|
namespace GsDChat.Server.Services;
|
|
|
|
public static class InputSanitizer
|
|
{
|
|
private static readonly string[] DangerousChars = { "\0", "\r", "\n", "\t", "<", ">", "&", "\"", "'", "\\" };
|
|
|
|
public static string SanitizeForLogging(string input)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(input))
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
var sanitized = new StringBuilder(input.Length);
|
|
foreach (char c in input)
|
|
{
|
|
if (char.IsControl(c) && !char.IsWhiteSpace(c))
|
|
{
|
|
sanitized.Append($"\\x{(int)c:X2}");
|
|
}
|
|
else
|
|
{
|
|
sanitized.Append(c);
|
|
}
|
|
}
|
|
return sanitized.ToString();
|
|
}
|
|
|
|
public static string SanitizeForDisplay(string input, int maxLength = 1000)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(input))
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
// Truncate if too long
|
|
if (input.Length > maxLength)
|
|
{
|
|
input = input.Substring(0, maxLength) + "...";
|
|
}
|
|
|
|
// Remove null bytes and other control characters except newlines and tabs
|
|
var sanitized = new StringBuilder(input.Length);
|
|
foreach (char c in input)
|
|
{
|
|
if (c == '\0')
|
|
{
|
|
continue;
|
|
}
|
|
if (char.IsControl(c) && c != '\n' && c != '\r' && c != '\t')
|
|
{
|
|
continue;
|
|
}
|
|
sanitized.Append(c);
|
|
}
|
|
return sanitized.ToString();
|
|
}
|
|
|
|
public static string SanitizeUsername(string username)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(username))
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
// Allow alphanumeric, underscore, hyphen, and common special characters
|
|
var sanitized = new StringBuilder(username.Length);
|
|
foreach (char c in username)
|
|
{
|
|
if (char.IsLetterOrDigit(c) || c == '_' || c == '-' || c == '.' || c == '@')
|
|
{
|
|
sanitized.Append(c);
|
|
}
|
|
}
|
|
return sanitized.ToString();
|
|
}
|
|
|
|
public static string SanitizeChannelName(string channelName)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(channelName))
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
// Allow alphanumeric, spaces, hyphens, and underscores
|
|
var sanitized = new StringBuilder(channelName.Length);
|
|
foreach (char c in channelName)
|
|
{
|
|
if (char.IsLetterOrDigit(c) || char.IsWhiteSpace(c) || c == '-' || c == '_')
|
|
{
|
|
sanitized.Append(c);
|
|
}
|
|
}
|
|
return sanitized.ToString().Trim();
|
|
}
|
|
|
|
public static string SanitizeSystemMessage(string message)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(message))
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
// Remove control characters that could cause log injection
|
|
var sanitized = new StringBuilder(message.Length);
|
|
foreach (char c in message)
|
|
{
|
|
if (c == '\0' || c == '\r' || c == '\n')
|
|
{
|
|
sanitized.Append(' ');
|
|
}
|
|
else
|
|
{
|
|
sanitized.Append(c);
|
|
}
|
|
}
|
|
return sanitized.ToString();
|
|
}
|
|
|
|
public static bool IsValidUserId(string userId)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(userId) || userId.Length > 50)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Should be a valid GUID
|
|
return Guid.TryParse(userId, out _);
|
|
}
|
|
|
|
public static bool IsValidChannelId(string channelId)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(channelId) || channelId.Length > 50)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Should be a valid GUID
|
|
return Guid.TryParse(channelId, out _);
|
|
}
|
|
}
|