mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-05-22 09:12:26 +00:00
* Add migration to de-sanitize database strings * Remove SanitizationHelper functions related to XML sanitization * Remove sanitization usage from website * Implement suggested changes
51 lines
No EOL
1.7 KiB
C#
51 lines
No EOL
1.7 KiB
C#
#nullable enable
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using LBPUnion.ProjectLighthouse.Database;
|
|
using LBPUnion.ProjectLighthouse.Types.Maintenance;
|
|
|
|
namespace LBPUnion.ProjectLighthouse.Administration.Maintenance.MigrationTasks;
|
|
|
|
public class CleanupSanitizedStrings : IMigrationTask
|
|
{
|
|
public string Name() => "Cleanup Sanitized strings";
|
|
|
|
async Task<bool> IMigrationTask.Run(DatabaseContext database)
|
|
{
|
|
List<object> objsToBeSanitized = new();
|
|
|
|
// Store all the objects we need to sanitize in a list.
|
|
// The alternative here is to loop through every table, but thats a ton of code...
|
|
objsToBeSanitized.AddRange(database.Slots);
|
|
objsToBeSanitized.AddRange(database.Reviews);
|
|
objsToBeSanitized.AddRange(database.Comments);
|
|
objsToBeSanitized.AddRange(database.Scores);
|
|
objsToBeSanitized.AddRange(database.Users);
|
|
objsToBeSanitized.AddRange(database.Photos);
|
|
objsToBeSanitized.AddRange(database.Reports);
|
|
|
|
foreach (object obj in objsToBeSanitized)
|
|
{
|
|
PropertyInfo[] properties = obj.GetType().GetProperties();
|
|
foreach (PropertyInfo property in properties)
|
|
{
|
|
if (property.PropertyType != typeof(string)) continue;
|
|
|
|
string? before = (string?)property.GetValue(obj);
|
|
|
|
if (before == null) continue;
|
|
|
|
string after = HttpUtility.HtmlDecode(before);
|
|
if (before != after)
|
|
{
|
|
property.SetValue(obj, after);
|
|
}
|
|
}
|
|
}
|
|
|
|
await database.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
} |