From 7b6fa15893532182368ce926ddf2becf80aebf59 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Sun, 21 Feb 2021 22:16:05 -0500 Subject: [PATCH] Sanitize Chat --- mods/src/chat/chat.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/mods/src/chat/chat.cpp b/mods/src/chat/chat.cpp index b0ee998..adefb97 100644 --- a/mods/src/chat/chat.cpp +++ b/mods/src/chat/chat.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -10,6 +11,11 @@ #include "chat.h" +// Message Limitations +#define MAX_MESSAGE_LENGTH 512 +#define MINIMUM_MESSAGE_CHARACTER 32 +#define MAXIMUM_MESSAGE_CHARACTER 126 + // Send API Command static void send_api_command(unsigned char *minecraft, char *str) { struct ConnectedClient client; @@ -30,11 +36,29 @@ static void send_api_chat_command(unsigned char *minecraft, char *str) { free(command); } +// Sanitize Message +static void sanitize_message(char **message) { + // Store Message Length + int length = strlen(*message); + // Truncate Message + if (length > MAX_MESSAGE_LENGTH) { + (*message)[MAX_MESSAGE_LENGTH] = '\0'; + length = MAX_MESSAGE_LENGTH; + } + // Loop Through Message + for (int i = 0; i < length; i++) { + if ((*message)[i] < MINIMUM_MESSAGE_CHARACTER || (*message)[i] > MAXIMUM_MESSAGE_CHARACTER) { + // Replace Illegal Character + (*message)[i] = '?'; + } + } +} // Send Message To Players static void send_message(unsigned char *server_side_network_handler, char *username, char *message) { char *full_message = NULL; asprintf(&full_message, "<%s> %s", username, message); ALLOC_CHECK(full_message); + sanitize_message(&full_message); (*ServerSideNetworkHandler_displayGameMessage)(server_side_network_handler, std::string(full_message)); free(full_message); }