Skip to content

Commit 405e958

Browse files
fix: check message id validity on actions and add-message slash commands
1 parent cf501f5 commit 405e958

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "message-manager-backend",
3-
"version": "2.3.0",
3+
"version": "2.3.1",
44
"description": "Backend for the message manager site",
55
"main": "./dist/index.js",
66
"exports": "./dist/index.js",

src/errors.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ enum InteractionOrRequestFinalStatus {
3636
MESSAGE_GENERATION_CACHE_NOT_FOUND,
3737
MIGRATION_ATTEMPTED_ON_MESSAGE_WITH_MULTIPLE_EMBEDS,
3838
MESSAGE_NOT_FOUND_DISCORD_DELETED_OR_NOT_EXIST,
39+
APPLICATION_COMMAND_SNOWFLAKE_OPTION_NOT_VALID,
3940
GENERIC_EXPECTED_PERMISSIONS_FAILURE = 3000,
4041
USER_MISSING_DISCORD_PERMISSION,
4142
BOT_MISSING_DISCORD_PERMISSION,

src/interactions/commands/chatInput/actions.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default async function handleActionsCommand(
2525
): Promise<InteractionReturnData> {
2626
const interaction = internalInteraction.interaction;
2727
// First option: Message Id
28-
const messageId: string | undefined = (
28+
let messageId: string | undefined = (
2929
interaction.data.options?.find(
3030
(option) =>
3131
option.name === "message-id" &&
@@ -38,6 +38,19 @@ export default async function handleActionsCommand(
3838
"No message id option on actions command"
3939
);
4040
}
41+
42+
// Check if messageId is a valid snowflake
43+
if (!/^[0-9]{16,20}$/.test(messageId)) {
44+
// Check if messageId is in the format of ${channelId}-${messageId}
45+
if (/^[0-9]{16,20}-[0-9]{16,20}$/.test(messageId)) {
46+
messageId = messageId.split("-")[1];
47+
} else {
48+
throw new ExpectedFailure(
49+
InteractionOrRequestFinalStatus.APPLICATION_COMMAND_SNOWFLAKE_OPTION_NOT_VALID,
50+
"Invalid message id option on actions command - make sure this is a valid message id"
51+
);
52+
}
53+
}
4154
// Fetch message from api
4255
try {
4356
const message = (await instance.restClient.get(
@@ -64,6 +77,11 @@ export default async function handleActionsCommand(
6477
InteractionOrRequestFinalStatus.MESSAGE_NOT_FOUND_DISCORD_DELETED_OR_NOT_EXIST,
6578
"That message could not be found, make sure you are using the command in the same channel as the message"
6679
);
80+
} else if (error.code === 50035) {
81+
throw new ExpectedFailure(
82+
InteractionOrRequestFinalStatus.APPLICATION_COMMAND_SNOWFLAKE_OPTION_NOT_VALID,
83+
"Invalid message id option on actions command - make sure this is a valid message id"
84+
);
6785
}
6886
throw error;
6987
}

src/interactions/commands/chatInput/add-message.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default async function handleAddMessageCommand(
2525
): Promise<InteractionReturnData> {
2626
const interaction = internalInteraction.interaction;
2727
// First option: Message Id
28-
const messageId: string | undefined = (
28+
let messageId: string | undefined = (
2929
interaction.data.options?.find(
3030
(option) =>
3131
option.name === "message-id" &&
@@ -38,6 +38,18 @@ export default async function handleAddMessageCommand(
3838
"No message id option on add message command"
3939
);
4040
}
41+
// Check if messageId is a valid snowflake
42+
if (!/^[0-9]{16,20}$/.test(messageId)) {
43+
// Check if messageId is in the format of ${channelId}-${messageId}
44+
if (/^[0-9]{16,20}-[0-9]{16,20}$/.test(messageId)) {
45+
messageId = messageId.split("-")[1];
46+
} else {
47+
throw new ExpectedFailure(
48+
InteractionOrRequestFinalStatus.APPLICATION_COMMAND_SNOWFLAKE_OPTION_NOT_VALID,
49+
"Invalid message id option on actions command - make sure this is a valid message id"
50+
);
51+
}
52+
}
4153
// Fetch message from api
4254
try {
4355
const message = (await instance.restClient.get(
@@ -64,6 +76,11 @@ export default async function handleAddMessageCommand(
6476
InteractionOrRequestFinalStatus.MESSAGE_NOT_FOUND_DISCORD_DELETED_OR_NOT_EXIST,
6577
"That message could not be found, make sure you are using the command in the same channel as the message"
6678
);
79+
} else if (error.code === 50035) {
80+
throw new ExpectedFailure(
81+
InteractionOrRequestFinalStatus.APPLICATION_COMMAND_SNOWFLAKE_OPTION_NOT_VALID,
82+
"Invalid message id option on actions command - make sure this is a valid message id"
83+
);
6784
}
6885
throw error;
6986
}

0 commit comments

Comments
 (0)