using System; using System.Text.RegularExpressions; namespace Jellyfin.DbMigrator; /// /// Validates database table names to prevent SQL injection when names are /// interpolated into raw SQL strings. /// internal static partial class TableNameValidator { /// /// Gets the compiled regular expression that matches safe table names. /// A safe name consists only of ASCII letters, decimal digits, and underscores. /// [GeneratedRegex(@"^[A-Za-z0-9_]+$", RegexOptions.CultureInvariant)] private static partial Regex SafeNameRegex(); /// /// Throws an when /// contains characters that are not safe to embed inside a quoted SQL identifier. /// /// The candidate table name. /// /// Thrown when contains characters outside /// [A-Za-z0-9_]. /// public static void EnsureSafe(string tableName) { ArgumentException.ThrowIfNullOrWhiteSpace(tableName); if (!SafeNameRegex().IsMatch(tableName)) { throw new ArgumentException( $"Table name '{tableName}' contains characters that are not allowed in a SQL identifier.", nameof(tableName)); } } }