Merge pull request #736 from MaddyUnderStars/backfilling

Backfilling sanitation etc
This commit is contained in:
Erkin Alp Güney 2022-04-27 23:09:39 +03:00 committed by GitHub
commit 1a45a36910

View File

@ -2,13 +2,16 @@ import {
Attachment, Attachment,
Channel, Channel,
Embed, Embed,
DiscordApiErrors,
emitEvent, emitEvent,
FosscordApiErrors,
getPermission, getPermission,
getRights, getRights,
Message, Message,
MessageCreateEvent, MessageCreateEvent,
MessageDeleteEvent, MessageDeleteEvent,
MessageUpdateEvent, MessageUpdateEvent,
Snowflake,
uploadFile uploadFile
} from "@fosscord/util"; } from "@fosscord/util";
import { Router, Response, Request } from "express"; import { Router, Response, Request } from "express";
@ -16,6 +19,7 @@ import multer from "multer";
import { route } from "@fosscord/api"; import { route } from "@fosscord/api";
import { handleMessage, postHandleMessage } from "@fosscord/api"; import { handleMessage, postHandleMessage } from "@fosscord/api";
import { MessageCreateSchema } from "../index"; import { MessageCreateSchema } from "../index";
import { HTTPError } from "lambert-server";
const router = Router(); const router = Router();
// TODO: message content/embed string length limit // TODO: message content/embed string length limit
@ -91,6 +95,25 @@ router.put(
var body = req.body as MessageCreateSchema; var body = req.body as MessageCreateSchema;
const attachments: Attachment[] = []; const attachments: Attachment[] = [];
const rights = getRights(req.user_id);
rights.hasThrow("SEND_MESSAGES");
// regex to check if message contains anything other than numerals ( also no decimals )
if (!message_id.match(/^\+?\d+$/)) {
throw new HTTPError("Message IDs must be positive integers", 400);
}
const snowflake = Snowflake.deconstruct(message_id)
if (Date.now() < snowflake.timestamp) {
// message is in the future
throw FosscordApiErrors.CANNOT_BACKFILL_TO_THE_FUTURE;
}
const exists = await Message.findOne({ where: { id: message_id, channel_id: channel_id }});
if (exists) {
throw FosscordApiErrors.CANNOT_REPLACE_BY_BACKFILL;
}
if (req.file) { if (req.file) {
try { try {
const file = await uploadFile(`/attachments/${req.params.channel_id}`, req.file); const file = await uploadFile(`/attachments/${req.params.channel_id}`, req.file);
@ -101,8 +124,6 @@ router.put(
} }
const channel = await Channel.findOneOrFail({ where: { id: channel_id }, relations: ["recipients", "recipients.user"] }); const channel = await Channel.findOneOrFail({ where: { id: channel_id }, relations: ["recipients", "recipients.user"] });
// TODO: check the ID is not from the future, to prevent future-faking of channel histories
const embeds = body.embeds || []; const embeds = body.embeds || [];
if (body.embed) embeds.push(body.embed); if (body.embed) embeds.push(body.embed);
let message = await handleMessage({ let message = await handleMessage({
@ -115,11 +136,9 @@ router.put(
channel_id, channel_id,
attachments, attachments,
edited_timestamp: undefined, edited_timestamp: undefined,
timestamp: undefined, // FIXME: calculate timestamp from snowflake timestamp: new Date(snowflake.timestamp),
}); });
channel.last_message_id = message.id;
//Fix for the client bug //Fix for the client bug
delete message.member delete message.member