Skip to content

Commit

Permalink
fix(network): disallow sending of gifs
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed May 23, 2024
1 parent dbe3f8d commit 761cde6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/commands/context-menu/messageInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ export default class MessageInfo extends BaseCommand {
const reason = interaction.fields.getTextInputValue('reason');
const message = await interaction.channel?.messages.fetch(messageId).catch(() => null);
const content = message?.content || message?.embeds[0].description || undefined;
const attachmentUrl = content?.match(REGEX.IMAGE_URL)?.at(0) ?? message?.embeds[0]?.image?.url;
const attachmentUrl = content?.match(REGEX.STATIC_IMAGE_URL)?.at(0) ?? message?.embeds[0]?.image?.url;

await sendHubReport(messageInDb.originalMsg.hub.id, interaction.client, {
userId: authorId,
Expand Down
7 changes: 4 additions & 3 deletions src/scripts/network/runChecks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,14 @@ export const containsNSFW = async (message: Message, imgUrl: string | null | und
export const containsLinks = (message: Message, settings: HubSettingsBitField) => {
return (
settings.has('HideLinks') &&
!REGEX.IMAGE_URL.test(message.content) &&
!REGEX.STATIC_IMAGE_URL.test(message.content) &&
REGEX.LINKS.test(message.content)
);
};
export const unsupportedAttachment = (message: Message) => {
const attachment = message.attachments.first();
const allowedTypes = ['image/gif', 'image/png', 'image/jpeg', 'image/jpg'];
// NOTE: Even 'image/gif' was allowed
const allowedTypes = ['image/png', 'image/jpeg', 'image/jpg'];

return (attachment?.contentType && !allowedTypes.includes(attachment.contentType)) === true;
};
Expand Down Expand Up @@ -157,7 +158,7 @@ export const runChecks = async (
return false;
}
if (unsupportedAttachment(message)) {
await replyToMsg(message, 'Only images and gifs are allowed to be sent within the network.');
await replyToMsg(message, 'Only images and tenor gifs are allowed to be sent within the network.');
return false;
}

Expand Down
32 changes: 17 additions & 15 deletions src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import 'dotenv/config';
import { captureException } from '@sentry/node';
import { CustomID } from './CustomID.js';
import { ClusterManager } from 'discord-hybrid-sharding';
import { deleteConnections } from './ConnectedList.js';
import { deleteConnection, deleteConnections } from './ConnectedList.js';

/** Convert milliseconds to a human readable time (eg: 1d 2h 3m 4s) */
export const msToReadable = (milliseconds: number) => {
Expand Down Expand Up @@ -322,20 +322,19 @@ export const channelMention = (channelId: Snowflake | null | undefined) => {
export const handleError = (e: Error, interaction?: Interaction) => {
// log the error to the system
Logger.error(e);
let extra;

if (interaction) {
extra = {
const extra = interaction
? {
user: { id: interaction.user.id, username: interaction.user.username },
extra: {
type: interaction.type,
identifier:
interaction.isCommand() || interaction.isAutocomplete()
? interaction.commandName
: CustomID.parseCustomId(interaction.customId),
interaction.isCommand() || interaction.isAutocomplete()
? interaction.commandName
: CustomID.parseCustomId(interaction.customId),
},
};
}
}
: undefined;

// capture the error to Sentry.io with additional information
captureException(e, extra);
Expand Down Expand Up @@ -432,17 +431,20 @@ export const sendToHub = async (hubId: string, message: string | WebhookMessageC

const res = connections
.filter((c) => c.connected === true)
.map(async (connection) => {
const threadId = connection.parentId ? connection.channelId : undefined;
.map(async ({ channelId, webhookURL, parentId }) => {
const threadId = parentId ? channelId : undefined;
const payload =
typeof message === 'string' ? { content: message, threadId } : { ...message, threadId };

try {
const webhook = new WebhookClient({ url: connection.webhookURL });
const webhook = new WebhookClient({ url: webhookURL });
return await webhook.send(payload);
}
catch (e) {
e.message = `For Connection: ${connection.channelId} ${e.message}`;
// if the webhook is unknown, delete the connection
if (e.message.includes('Unknown Webhook')) await deleteConnection({ channelId });

e.message = `For Connection: ${channelId} ${e.message}`;
Logger.error(e);
return null;
}
Expand All @@ -458,7 +460,7 @@ export const sendToHub = async (hubId: string, message: string | WebhookMessageC
*/
export const getAttachmentURL = async (string: string) => {
// Tenor Gifs / Image URLs
const URLMatch = string.match(REGEX.IMAGE_URL);
const URLMatch = string.match(REGEX.STATIC_IMAGE_URL);
if (URLMatch) return URLMatch[0];

const gifMatch = string.match(REGEX.TENOR_LINKS);
Expand Down Expand Up @@ -489,4 +491,4 @@ export const getUserLocale = async (userId: Snowflake) => {
export const containsInviteLinks = (str: string) => {
const inviteLinks = ['discord.gg', 'discord.com/invite', 'dsc.gg'];
return inviteLinks.some((link) => str.includes(link));
};
};

0 comments on commit 761cde6

Please sign in to comment.