Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Commit

Permalink
Security fix and code lookup changes (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
lundylizard authored Jul 31, 2023
1 parent a5b85ce commit 2f34b98
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
47 changes: 45 additions & 2 deletions src/main/java/xyz/rc24/bot/commands/botadm/BashCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.rc24.bot.RiiConnect24Bot;
import xyz.rc24.bot.commands.Command;

import java.io.BufferedReader;
Expand All @@ -49,11 +50,53 @@ public class BashCommand implements Command {

@Override
public void onCommand(SlashCommandInteractionEvent event) {

// Allow usage only on config-defined root server
if (event.getGuild().getIdLong() != RiiConnect24Bot.getInstance().config.getRootServer()) {
event.reply("This command is private.").setEphemeral(true).queue();
return;
}

String bashCommand = event.getOption("command").getAsString();

if (bashCommand.isEmpty()) {
event.reply("Command cannot be empty!").setEphemeral(true).queue();
return;
}

StringBuilder output = new StringBuilder();
String finalOutput;

try {

ProcessBuilder builder = new ProcessBuilder(bashCommand.split(" "));
Process process = builder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String runningLineOutput;

while (!((runningLineOutput = reader.readLine()) == null)) {
output.append(runningLineOutput).append("\n");
}

if (output.toString().isEmpty()) {
event.reply("Executed command without output!").queue();
return;
}

// Remove linebreak
finalOutput = output.substring(0, output.length() - 1);
reader.close();

} catch (IOException e) {
event.reply("I wasn't able to find the command `" + bashCommand + "`!").setEphemeral(true).queue();
return;
} catch (Exception e) {
logger.error("An error occurred", e);
event.replyFormat("An error occurred: %s - Check the bot console.", e.getMessage()).setEphemeral(true).queue();
return;
}


return;
event.replyFormat("Input:\n```%s```\nOutput:\n```%s```", bashCommand, finalOutput).queue();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,20 @@
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import xyz.rc24.bot.RiiConnect24Bot;
import xyz.rc24.bot.commands.Command;

public class ShutdownCommand implements Command {

@Override
public void onCommand(SlashCommandInteractionEvent event) {

// Allow usage only on config-defined root server
if (event.getGuild().getIdLong() != RiiConnect24Bot.getInstance().config.getRootServer()) {
event.reply("This command is private.").setEphemeral(true).queue();
return;
}

event.reply("Shutting down bot...").queue();
event.getJDA().shutdown();
System.exit(0);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/xyz/rc24/bot/commands/wii/CodeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void onCommand(SlashCommandInteractionEvent event) {
return;
}

event.replyEmbeds(embed.build()).setEphemeral(true).queue();
event.replyEmbeds(embed.build()).queue();

}
return;
Expand Down

0 comments on commit 2f34b98

Please sign in to comment.