From d613a8bd7d9bbe468c089f8a37968d4272c0996e Mon Sep 17 00:00:00 2001 From: Alan Date: Sun, 20 Mar 2022 11:00:45 -0700 Subject: [PATCH 1/2] :sparkles: Escape suggestion ids --- src/commands/Bot Owner/gsid.js | 5 +++-- src/commands/Staff/approve.js | 5 +++-- src/commands/Staff/note.js | 6 +++--- src/commands/Staff/reject.js | 5 +++-- src/commands/Suggestions/sid.js | 5 +++-- src/utils/functions.js | 11 +++++++++++ 6 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/commands/Bot Owner/gsid.js b/src/commands/Bot Owner/gsid.js index 952023d..0c9a9da 100644 --- a/src/commands/Bot Owner/gsid.js +++ b/src/commands/Bot Owner/gsid.js @@ -2,7 +2,7 @@ const { MessageEmbed, Util: { escapeMarkdown } } = require('discord.js-light'); const { stripIndent } = require('common-tags'); const Command = require('../../structures/Command'); const Logger = require('../../utils/logger'); -const { buildErrorEmbed } = require('../../utils/functions'); +const { buildErrorEmbed, escapeSuggestionId } = require('../../utils/functions'); module.exports = class GSIDCommand extends Command { constructor(client) { @@ -25,9 +25,10 @@ module.exports = class GSIDCommand extends Command { if (!args[0]) return this.client.errors.noUsage(message.channel, this, settings); + const escapedId = escapeSuggestionId(args[0]); let suggestion; try { - suggestion = await this.client.mongodb.helpers.suggestions.getGlobalSuggestion(args[0]); + suggestion = await this.client.mongodb.helpers.suggestions.getGlobalSuggestion(escapedId); } catch (err) { Logger.errorCmd(this, err.stack); return message.channel.send(buildErrorEmbed(err)); diff --git a/src/commands/Staff/approve.js b/src/commands/Staff/approve.js index ac44e62..4ccec1e 100644 --- a/src/commands/Staff/approve.js +++ b/src/commands/Staff/approve.js @@ -2,7 +2,7 @@ const { MessageEmbed, Util: { escapeMarkdown } } = require('discord.js-light'); const { stripIndent } = require('common-tags'); const Command = require('../../structures/Command'); const Logger = require('../../utils/logger'); -const { validateSnowflake, suggestionMessageReactionFilter, buildErrorEmbed, getLogsChannel } = require('../../utils/functions'); +const { validateSnowflake, suggestionMessageReactionFilter, buildErrorEmbed, getLogsChannel, escapeSuggestionId } = require('../../utils/functions'); module.exports = class ApproveCommand extends Command { constructor(client) { @@ -29,10 +29,11 @@ module.exports = class ApproveCommand extends Command { if (!id) return this.client.errors.noUsage(message.channel, this, settings); const reply = args.slice(1).join(' '); + const escapedId = escapeSuggestionId(args[0]); const errMessage = new Error(`\`${id}\` does not resolve to or return a valid suggestion!`); try { - if ([7, 8].includes(id.length)) document = await this.client.mongodb.helpers.suggestions.getGlobalSuggestion(id); + if ([7, 8].includes(escapedId.length)) document = await this.client.mongodb.helpers.suggestions.getGlobalSuggestion(escapedId); else if (validateSnowflake(id)) document = await this.client.mongodb.helpers.suggestions.getGuildSuggestionViaMessageID(message.guild, id); else return message.channel.send(buildErrorEmbed(errMessage, false)); } catch (error) { diff --git a/src/commands/Staff/note.js b/src/commands/Staff/note.js index bd1be42..207594b 100644 --- a/src/commands/Staff/note.js +++ b/src/commands/Staff/note.js @@ -1,7 +1,7 @@ const { MessageEmbed } = require('discord.js-light'); const { oneLine } = require('common-tags'); const Command = require('../../structures/Command'); -const { validateSnowflake, buildErrorEmbed } = require('../../utils/functions'); +const { validateSnowflake, buildErrorEmbed, escapeSuggestionId } = require('../../utils/functions'); const Logger = require('../../utils/logger'); module.exports = class NoteCommand extends Command { @@ -25,11 +25,11 @@ module.exports = class NoteCommand extends Command { const id = args[0]; const note = args.slice(1).join(' '); if (!note) return this.client.errors.noUsage(message.channel, this, settings); - + const escapedId = escapeSuggestionId(args[0]); let document; const errMessage = new Error(`\`${id}\` does not resolve to or return a valid suggestion!`); try { - if ([7, 8].includes(id.length)) document = await this.client.mongodb.helpers.suggestions.getGlobalSuggestion(id); + if ([7, 8].includes(escapedId.length)) document = await this.client.mongodb.helpers.suggestions.getGlobalSuggestion(escapedId); else if (validateSnowflake(id)) document = await this.client.mongodb.helpers.suggestions.getGuildSuggestionViaMessageID(message.guild, id); else return message.channel.send(buildErrorEmbed(errMessage, false)); } catch (err) { diff --git a/src/commands/Staff/reject.js b/src/commands/Staff/reject.js index a5aa899..7fcd1cd 100644 --- a/src/commands/Staff/reject.js +++ b/src/commands/Staff/reject.js @@ -2,7 +2,7 @@ const { MessageEmbed, Util: { escapeMarkdown } } = require('discord.js-light'); const { stripIndent } = require('common-tags'); const Command = require('../../structures/Command'); const Logger = require('../../utils/logger'); -const { validateSnowflake, suggestionMessageReactionFilter, buildErrorEmbed, getLogsChannel } = require('../../utils/functions'); +const { validateSnowflake, suggestionMessageReactionFilter, buildErrorEmbed, getLogsChannel, escapeSuggestionId } = require('../../utils/functions'); module.exports = class RejectCommand extends Command { constructor(client) { @@ -33,9 +33,10 @@ module.exports = class RejectCommand extends Command { const reply = args.slice(1).join(' '); if (!reply && settings.responseRequired) return this.client.errors.noRejectedResponse(message.channel); + const escapedId = escapeSuggestionId(args[0]); const errMessage = new Error(`\`${id}\` does not resolve to or return a valid suggestion!`); try { - if ([7, 8].includes(id.length)) document = await this.client.mongodb.helpers.suggestions.getGlobalSuggestion(id); + if ([7, 8].includes(escapedId.length)) document = await this.client.mongodb.helpers.suggestions.getGlobalSuggestion(escapedId); else if (validateSnowflake(id)) document = await this.client.mongodb.helpers.suggestions.getGuildSuggestionViaMessageID(message.guild, id); else return message.channel.send(buildErrorEmbed(errMessage, false)); } catch (error) { diff --git a/src/commands/Suggestions/sid.js b/src/commands/Suggestions/sid.js index ea8b26c..6df9b3e 100644 --- a/src/commands/Suggestions/sid.js +++ b/src/commands/Suggestions/sid.js @@ -2,7 +2,7 @@ const { MessageEmbed, Util: { escapeMarkdown } } = require('discord.js-light'); const { stripIndent } = require('common-tags'); const Command = require('../../structures/Command'); const Logger = require('../../utils/logger'); -const { buildErrorEmbed } = require('../../utils/functions'); +const { buildErrorEmbed, escapeSuggestionId } = require('../../utils/functions'); module.exports = class SIDCommand extends Command { constructor(client) { @@ -23,9 +23,10 @@ module.exports = class SIDCommand extends Command { if (!args[0]) return this.client.errors.noUsage(message.channel, this, settings); + const escapedId = escapeSuggestionId(args[0]); let suggestion; try { - suggestion = await this.client.mongodb.helpers.suggestions.getGuildSuggestion(message.guild, args[0]); + suggestion = await this.client.mongodb.helpers.suggestions.getGuildSuggestion(message.guild, escapedId); } catch (err) { Logger.errorCmd(this, err.stack); return message.channel.send(buildErrorEmbed(err)); diff --git a/src/utils/functions.js b/src/utils/functions.js index d648cc7..9680ef5 100644 --- a/src/utils/functions.js +++ b/src/utils/functions.js @@ -123,6 +123,16 @@ const getDefaultSuggestionsChannel = (guild) => { return guild.channels.fetch({ cache: false }).then(res => res.filter(c => c.type === 'text').find(c => c.name === config.suggestionsChannel)) ?? null; }; +/** + * Escape all non-alphanumeric characters from a suggestion id. + * @param {id} The raw suggestion id. + * @return {String} The escaped suggestion id. + */ +const escapeSuggestionId = (id) => { + const idRegex = new RegExp(/[^0-9a-z]/g); + return id.replace(idRegex, ''); +}; + /** * Return a new array of parsed array of arguments removing brackets. * @param {Array} args The command arguments. @@ -299,6 +309,7 @@ module.exports = { displayUptime, getRandomGiphyImage, getDefaultSuggestionsChannel, + escapeSuggestionId, parseCommandArguments, postStatsCronJob, suggestionMessageReactionFilter, From f1b24ddffaf4d44dc59376ac3bd75eec7261b128 Mon Sep 17 00:00:00 2001 From: Alan <40917647+alanlichen@users.noreply.github.com> Date: Sat, 2 Apr 2022 20:12:07 -0700 Subject: [PATCH 2/2] :memo: Fix escapeSuggestionId documentation --- src/utils/functions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/functions.js b/src/utils/functions.js index 9680ef5..7da2fb6 100644 --- a/src/utils/functions.js +++ b/src/utils/functions.js @@ -125,7 +125,7 @@ const getDefaultSuggestionsChannel = (guild) => { /** * Escape all non-alphanumeric characters from a suggestion id. - * @param {id} The raw suggestion id. + * @param {String} The raw suggestion id. * @return {String} The escaped suggestion id. */ const escapeSuggestionId = (id) => {