Fixed critical bug with server-side commandPreprocessor

This commit is contained in:
rainbow napkin 2025-01-11 23:26:04 -05:00
parent f8a6e00345
commit 4c1d3c9db5

View file

@ -32,84 +32,87 @@ module.exports = class commandPreprocessor{
} }
async preprocess(socket, data){ async preprocess(socket, data){
//Set socket, data, and sendFlag //Set command object
this.socket = socket; let commandObj = {
this.sendFlag = true; socket,
this.rawData = data; sendFlag: true,
this.chatType = 'chat'; rawData: data,
chatType: 'chat'
}
//If we don't pass sanatization/validation turn this car around //If we don't pass sanatization/validation turn this car around
if(!this.sanatizeCommand()){ if(!this.sanatizeCommand(commandObj)){
return; return;
} }
//split the command //split the command
this.splitCommand(); this.splitCommand(commandObj);
//Process the command //Process the command
await this.processServerCommand(); await this.processServerCommand(commandObj);
//If we're going to relay this command as a message, continue on to chat processing //If we're going to relay this command as a message, continue on to chat processing
if(this.sendFlag){ if(commandObj.sendFlag){
//Prep the message //Prep the message
await this.prepMessage(); await this.prepMessage(commandObj);
//Send the chat //Send the chat
this.sendChat(); this.sendChat(commandObj);
} }
} }
sanatizeCommand(){ sanatizeCommand(commandObj){
//Trim and Sanatize for XSS //Trim and Sanatize for XSS
this.command = validator.trim(validator.escape(this.rawData.msg)); commandObj.command = validator.trim(validator.escape(commandObj.rawData.msg));
//nuke the message if its empty or huge //Return whether or not the shit was long enough
return (validator.isLength(this.command, {min: 1, max: 255})); return (validator.isLength(commandObj.rawData.msg, {min: 1, max: 255}));
} }
splitCommand(){ splitCommand(commandObj){
//Split string by words //Split string by words
this.commandArray = this.command.split(/\b/g);//Split by word-borders commandObj.commandArray = commandObj.command.split(/\b/g);//Split by word-borders
this.argumentArray = this.command.match(/\b\w+\b/g);//Match by words surrounded by borders commandObj.argumentArray = commandObj.command.match(/\b\w+\b/g);//Match by words surrounded by borders
} }
async processServerCommand(){ async processServerCommand(commandObj){
//If the raw message starts with '!' (skip commands that start with whitespace so people can send example commands in chat) //If the raw message starts with '!' (skip commands that start with whitespace so people can send example commands in chat)
if(this.rawData.msg[0] == '!'){ if(commandObj.rawData.msg[0] == '!'){
//if it isn't just an exclimation point, and we have a real command //if it isn't just an exclimation point, and we have a real command
if(this.argumentArray != null){ if(commandObj.argumentArray != null){
if(this.commandProcessor[this.argumentArray[0].toLowerCase()] != null){ if(this.commandProcessor[commandObj.argumentArray[0].toLowerCase()] != null){
//Process the command and use the return value to set the sendflag (true if command valid) //Process the command and use the return value to set the sendflag (true if command valid)
this.sendFlag = await this.commandProcessor[this.argumentArray[0].toLowerCase()](this); commandObj.sendFlag = await this.commandProcessor[commandObj.argumentArray[0].toLowerCase()](commandObj);
}else{ }else{
//Process as toke command if we didnt get a match from the standard server-side command processor //Process as toke command if we didnt get a match from the standard server-side command processor
this.sendFlag = await this.tokebot.tokeProcessor(this); commandObj.sendFlag = await this.tokebot.tokeProcessor(commandObj);
} }
} }
} }
} }
async markLinks(){ async markLinks(commandObj){
//Empty out the links array //Setup the links array
this.links = []; commandObj.links = [];
//For each link sent from the client //For each link sent from the client
//this.rawData.links.forEach((link) => { //this.rawData.links.forEach((link) => {
for (const link of this.rawData.links){ for (const link of commandObj.rawData.links){
//Add a marked link object to our links array //Add a marked link object to our links array
this.links.push(await linkUtils.markLink(link)); commandObj.links.push(await linkUtils.markLink(link));
} }
} }
async prepMessage(){ async prepMessage(commandObj){
//Create message from commandArray //Create message from commandArray
this.message = this.commandArray.join('').trimStart(); commandObj.message = commandObj.commandArray.join('').trimStart();
//Validate links and mark them by embed type //Validate links and mark them by embed type
await this.markLinks(); await this.markLinks(commandObj);
} }
sendChat(){ sendChat(commandObj){
//FUCKIN' SEND IT! //FUCKIN' SEND IT!
this.chatHandler.relayUserChat(this.socket, this.message, this.chatType, this.links); this.chatHandler.relayUserChat(commandObj.socket, commandObj.message, commandObj.chatType, commandObj.links);
} }
} }