Skip to content

Commit

Permalink
Fix missing filtering, filter inconsistencies, and filter logging (#1082
Browse files Browse the repository at this point in the history
)

* Fix missing filtering, filter inconsistencies, and filter logging

* Remove unused import and replace removed logger

* Make filter log arguments optional

* Update ProjectLighthouse.Servers.GameServer/Controllers/MessageController.cs

Co-authored-by: sudokoko <[email protected]>

* Update ProjectLighthouse.Servers.Website/Pages/Debug/FilterTestPage.cshtml.cs

Co-authored-by: sudokoko <[email protected]>

* Replace filter location strings with enum

* Rename enum to FilterLocation for readability

---------

Co-authored-by: sudokoko <[email protected]>
  • Loading branch information
FeTetra and sudokoko authored Dec 11, 2024
1 parent a3022ff commit f059b20
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 38 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
using LBPUnion.ProjectLighthouse.Types.Filter;
using LBPUnion.ProjectLighthouse.Types.Levels;
using LBPUnion.ProjectLighthouse.Types.Logging;
using LBPUnion.ProjectLighthouse.Types.Serialization;
using LBPUnion.ProjectLighthouse.Types.Users;
using Microsoft.AspNetCore.Authorization;
Expand Down Expand Up @@ -144,11 +142,7 @@ public async Task<IActionResult> PostComment(string? username, string? slotType,
targetId = await this.database.UserIdFromUsername(username!);
}

string filteredText = CensorHelper.FilterMessage(comment.Message);

if (ServerConfiguration.Instance.LogChatFiltering && filteredText != comment.Message)
Logger.Info($"Censored profane word(s) from in-game comment sent by {username}: \"{comment.Message}\" => \"{filteredText}\"",
LogArea.Filter);
string filteredText = CensorHelper.FilterMessage(comment.Message, FilterLocation.ChatMessage, username);

bool success = await this.database.PostComment(token.UserId, targetId, type, filteredText);
if (success) return this.Ok();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using LBPUnion.ProjectLighthouse.Types.Entities.Notifications;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
using LBPUnion.ProjectLighthouse.Types.Filter;
using LBPUnion.ProjectLighthouse.Types.Logging;
using LBPUnion.ProjectLighthouse.Types.Mail;
using LBPUnion.ProjectLighthouse.Types.Serialization;
Expand Down Expand Up @@ -143,15 +144,10 @@ public async Task<IActionResult> Filter(IMailService mailService)

string username = await this.database.UsernameFromGameToken(token);

string filteredText = CensorHelper.FilterMessage(message);

if (ServerConfiguration.Instance.LogChatMessages) Logger.Info($"{username}: \"{message}\"", LogArea.Filter);

if (ServerConfiguration.Instance.LogChatFiltering && filteredText != message)
Logger.Info(
$"Censored profane word(s) from in-game text sent by {username}: \"{message}\" => \"{filteredText}\"",
LogArea.Filter);
message = CensorHelper.FilterMessage(message, FilterLocation.ChatMessage, username);

return this.Ok(filteredText);
return this.Ok(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
using LBPUnion.ProjectLighthouse.Types.Logging;
using LBPUnion.ProjectLighthouse.Types.Filter;
using LBPUnion.ProjectLighthouse.Types.Resources;
using LBPUnion.ProjectLighthouse.Types.Serialization;
using LBPUnion.ProjectLighthouse.Types.Users;
Expand Down Expand Up @@ -142,7 +143,7 @@ await this.database.SendNotification(user.UserId,
// Yes Rider, this isn't null
Debug.Assert(slot.Resources != null, "slot.ResourceList != null");

slot.Name = CensorHelper.FilterMessage(slot.Name);
slot.Name = CensorHelper.FilterMessage(slot.Name, FilterLocation.SlotName, user.Username);

if (slot.Name.Length > 64)
{
Expand All @@ -153,7 +154,7 @@ await this.database.SendNotification(user.UserId,
return this.BadRequest();
}

slot.Description = CensorHelper.FilterMessage(slot.Description);
slot.Description = CensorHelper.FilterMessage(slot.Description, FilterLocation.SlotDescription, user.Username);

if (slot.Description.Length > 512)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ public async Task<IActionResult> PostReview(int slotId)
GameReview? newReview = await this.DeserializeBody<GameReview>();
if (newReview == null) return this.BadRequest();

newReview.Text = CensorHelper.FilterMessage(newReview.Text);
// Temporary fix until this can be refactored to use a UserEntity properly
string username = await this.database.UsernameFromGameToken(token);
newReview.Text = CensorHelper.FilterMessage(newReview.Text, FilterLocation.SlotReview, username);

if (newReview.Text.Length > 512) return this.BadRequest();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Files;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Servers.GameServer.Helpers;
using LBPUnion.ProjectLighthouse.Servers.GameServer.Types.Users;
Expand All @@ -11,6 +12,7 @@
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
using LBPUnion.ProjectLighthouse.Types.Levels;
using LBPUnion.ProjectLighthouse.Types.Logging;
using LBPUnion.ProjectLighthouse.Types.Filter;
using LBPUnion.ProjectLighthouse.Types.Serialization;
using LBPUnion.ProjectLighthouse.Types.Users;
using Microsoft.AspNetCore.Authorization;
Expand Down Expand Up @@ -79,7 +81,9 @@ public async Task<IActionResult> UpdateUser()

if (update.Biography.Length > 512) return this.BadRequest();

user.Biography = update.Biography;
string filteredBio = CensorHelper.FilterMessage(update.Biography, FilterLocation.UserBiography, user.Username);

user.Biography = filteredBio;
}

if (update.Location != null) user.Location = update.Location;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
using LBPUnion.ProjectLighthouse.Types.Filter;
using LBPUnion.ProjectLighthouse.Types.Logging;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -73,11 +74,7 @@ public async Task<IActionResult> PostComment([FromRoute] int id, [FromForm] stri
}

string username = await this.database.UsernameFromWebToken(token);
string filteredText = CensorHelper.FilterMessage(msg);

if (ServerConfiguration.Instance.LogChatFiltering && filteredText != msg)
Logger.Info($"Censored profane word(s) from slot comment sent by {username}: \"{msg}\" => \"{filteredText}\"",
LogArea.Filter);
string filteredText = CensorHelper.FilterMessage(msg, FilterLocation.SlotReview, username);

bool success = await this.database.PostComment(token.UserId, id, CommentType.Level, filteredText);
if (success)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
using LBPUnion.ProjectLighthouse.Types.Filter;
using LBPUnion.ProjectLighthouse.Types.Logging;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -49,11 +50,7 @@ public async Task<IActionResult> PostComment([FromRoute] int id, [FromForm] stri
}

string username = await this.database.UsernameFromWebToken(token);
string filteredText = CensorHelper.FilterMessage(msg);

if (ServerConfiguration.Instance.LogChatFiltering && filteredText != msg)
Logger.Info($"Censored profane word(s) from user comment sent by {username}: \"{msg}\" => \"{filteredText}\"",
LogArea.Filter);
string filteredText = CensorHelper.FilterMessage(msg, FilterLocation.UserComment, username);

bool success = await this.database.PostComment(token.UserId, id, CommentType.Profile, filteredText);
if (success)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using LBPUnion.ProjectLighthouse.Helpers;
#endif
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Types.Filter;
using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
using Microsoft.AspNetCore.Mvc;

Expand All @@ -19,7 +20,7 @@ public FilterTestPage(DatabaseContext database) : base(database)
public IActionResult OnGet(string? text = null)
{
#if DEBUG
if (text != null) this.FilteredText = CensorHelper.FilterMessage(text);
if (text != null) this.FilteredText = CensorHelper.FilterMessage(text, FilterLocation.Test);
this.Text = text;

return this.Page();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Filter;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

Expand Down Expand Up @@ -36,15 +37,15 @@ public async Task<IActionResult> OnPost([FromRoute] int slotId, [FromForm] strin

if (name != null)
{
name = CensorHelper.FilterMessage(name);
if (this.Slot.Name != name && name.Length <= 64)
name = CensorHelper.FilterMessage(name, FilterLocation.SlotName, this.User.Username);
if (this.Slot.Name != name && name.Length <= 64)
this.Slot.Name = name;
}

if (description != null)
{
description = CensorHelper.FilterMessage(description);
if (this.Slot.Description != description && description?.Length <= 512)
description = CensorHelper.FilterMessage(description, FilterLocation.SlotDescription, this.User.Username);
if (this.Slot.Description != description && description.Length <= 512)
this.Slot.Description = description;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using LBPUnion.ProjectLighthouse.Localization;
using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Filter;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

Expand Down Expand Up @@ -55,9 +56,12 @@ [FromForm] string? language
if (ServerConfiguration.Instance.UserGeneratedContentLimits.ReadOnlyMode)
return this.Redirect($"~/user/{userId}");

biography = CensorHelper.FilterMessage(biography);
if (this.ProfileUser.Biography != biography && biography.Length <= 512)
this.ProfileUser.Biography = biography;
{
string filteredBio = CensorHelper.FilterMessage(biography, FilterLocation.UserBiography, this.ProfileUser.Username);

this.ProfileUser.Biography = filteredBio;
}
}

if (ServerConfiguration.Instance.Mail.MailEnabled &&
Expand Down
14 changes: 12 additions & 2 deletions ProjectLighthouse/Helpers/CensorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Collections.Generic;
using System.Text;
using LBPUnion.ProjectLighthouse.Configuration;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Types.Filter;
using LBPUnion.ProjectLighthouse.Types.Logging;

namespace LBPUnion.ProjectLighthouse.Helpers;

Expand All @@ -17,7 +20,7 @@ public static class CensorHelper
"UwU", "OwO", "uwu", "owo", "o3o", ">.>", "*pounces on you*", "*boops*", "*baps*", ":P", "x3", "O_O", "xD", ":3", ";3", "^w^",
};

public static string FilterMessage(string message)
public static string FilterMessage(string message, FilterLocation filterLocation = FilterLocation.None, string username = null)
{
if (CensorConfiguration.Instance.UserInputFilterMode == FilterMode.None) return message;
StringBuilder stringBuilder = new(message);
Expand All @@ -44,7 +47,14 @@ public static string FilterMessage(string message)
}
}

return stringBuilder.ToString();
string filteredMessage = stringBuilder.ToString();

if (ServerConfiguration.Instance.LogChatFiltering && filteredMessage != message)
Logger.Info(
$"Comment sent {(username != null ? $"by {username} " : "")}" + $"from {filterLocation}" +
$"\"{message}\" => \"{filteredMessage}\"", LogArea.Filter);

return filteredMessage;
}

private static void Censor(int profanityIndex, int profanityLength, StringBuilder message)
Expand Down
13 changes: 13 additions & 0 deletions ProjectLighthouse/Types/Filter/FilterLocation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace LBPUnion.ProjectLighthouse.Types.Filter;

public enum FilterLocation
{
SlotName = 0,
SlotDescription = 1,
SlotReview = 2,
UserBiography = 3,
UserComment = 4,
ChatMessage = 5,
Test = 6,
None = 7,
}

0 comments on commit f059b20

Please sign in to comment.