Skip to content

Commit

Permalink
fix(autocomplete): fix errors and db vulnerability where users were a…
Browse files Browse the repository at this point in the history
…ble to query regex through searches
  • Loading branch information
dev-737 committed Jan 25, 2024
1 parent 978ded0 commit 1e0d44c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/commands/slash/Main/blacklist/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Collection,
RESTPostAPIApplicationCommandsJSONBody,
} from 'discord.js';
import { handleError } from '../../../../utils/Utils.js';
import { escapeRegexChars, handleError } from '../../../../utils/Utils.js';
import BaseCommand from '../../../BaseCommand.js';
import db from '../../../../utils/Db.js';

Expand Down Expand Up @@ -181,7 +181,7 @@ export default class BlacklistCommand extends BaseCommand {
if (focusedHub.focused) {
const hub = await db.hubs.findMany({
where: {
name: { mode: 'insensitive', contains: focusedHub.value },
name: { mode: 'insensitive', contains: escapeRegexChars(focusedHub.value) },
OR: [
{ ownerId: interaction.user.id },
{ moderators: { some: { userId: interaction.user.id } } },
Expand Down
3 changes: 2 additions & 1 deletion src/commands/slash/Main/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
simpleEmbed,
getOrCreateWebhook,
setComponentExpiry,
escapeRegexChars,
} from '../../../utils/Utils.js';
import { t } from '../../../utils/Locale.js';

Expand Down Expand Up @@ -141,7 +142,7 @@ export default class Connection extends BaseCommand {
}

async autocomplete(interaction: AutocompleteInteraction): Promise<void> {
const focusedValue = interaction.options.getFocused();
const focusedValue = escapeRegexChars(interaction.options.getFocused());

const isInDb = await db.connectedList.findMany({
where: {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/slash/Main/hub/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from 'discord.js';
import BaseCommand from '../../../BaseCommand.js';
import db from '../../../../utils/Db.js';
import { handleError } from '../../../../utils/Utils.js';
import { escapeRegexChars, handleError } from '../../../../utils/Utils.js';

const hubOption: APIApplicationCommandBasicOption = {
type: ApplicationCommandOptionType.String,
Expand Down Expand Up @@ -291,7 +291,7 @@ export default class Hub extends BaseCommand {

const subcommand = interaction.options.getSubcommand();
const subcommandGroup = interaction.options.getSubcommandGroup();
const focusedValue = interaction.options.getFocused();
const focusedValue = escapeRegexChars(interaction.options.getFocused());
let hubChoices;

if (subcommand === 'browse' || subcommand === 'join') {
Expand Down
12 changes: 5 additions & 7 deletions src/commands/slash/Staff/find/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,17 @@ export default class Find extends BaseCommand {
case 'server': {
const guilds = interaction.client.guilds.cache;
const focusedValue = interaction.options.getFocused().toLowerCase();
const choices: { name: string; id: string }[] = [];
const choices = guilds.map((guild) => ({ name: guild.name, value: guild.id }));

guilds.map((guild) => choices.push({ name: guild.name, id: guild.id }));
const filtered = choices
.filter(
(choice) =>
choice.name.toLowerCase().includes(focusedValue) ||
choice.id.toLowerCase().includes(focusedValue),
choice.value.toLowerCase().includes(focusedValue),
)
.slice(0, 25)
.map((choice) => ({ name: choice.name, value: choice.id }));
.slice(0, 25);

interaction.respond(filtered);
await interaction.respond(filtered);
break;
}

Expand All @@ -101,7 +99,7 @@ export default class Find extends BaseCommand {
.slice(0, 25)
.map((choice) => ({ name: choice.username, value: choice.id }));

interaction.respond(filtered);
await interaction.respond(filtered);
break;
}
default:
Expand Down
22 changes: 16 additions & 6 deletions src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,17 +272,23 @@ export const parseTimestampFromId = (id: Snowflake) => {
return timestamp + 1420070400000; // Discord epoch time
};

export const deleteMsgsFromDb = async (ids: string[]) => {
export const deleteMsgsFromDb = async (broadcastMsgs: string[]) => {
// delete all relations first and then delete the hub
const msgsToDelete = await db.broadcastedMessages.findMany({ where: { messageId: { in: ids } } });
const msgsToDelete = await db.broadcastedMessages.findMany({
where: { messageId: { in: broadcastMsgs } },
});
if (!msgsToDelete) return;

await db.broadcastedMessages.deleteMany({
where: { messageId: { in: msgsToDelete.map(({ messageId }) => messageId) } },
const originalMsgIds = msgsToDelete.map(({ originalMsgId }) => originalMsgId);

const childrenBatch = db.broadcastedMessages.deleteMany({
where: { originalMsgId: { in: originalMsgIds } },
});
await db.originalMessages.deleteMany({
where: { messageId: { in: msgsToDelete.map(({ originalMsgId }) => originalMsgId) } },
const originalBatch = db.originalMessages.deleteMany({
where: { messageId: { in: originalMsgIds } },
});

return await db.$transaction([childrenBatch, originalBatch]);
};

export const channelMention = (channelId: Snowflake | null | undefined) => {
Expand Down Expand Up @@ -318,3 +324,7 @@ export const handleError = (e: Error, interaction?: Interaction) => {
export const isDev = (userId: Snowflake) => {
return DeveloperIds.includes(userId);
};

export const escapeRegexChars = (input: string): string => {
return input.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
};

0 comments on commit 1e0d44c

Please sign in to comment.