diff --git a/src/models/post.ts b/src/models/post.ts index 56df64b..f221f1a 100644 --- a/src/models/post.ts +++ b/src/models/post.ts @@ -1,6 +1,7 @@ import crypto from 'node:crypto'; import moment from 'moment'; import { Schema, model } from 'mongoose'; +import { INVALID_POST_BODY_REGEX } from '@/util'; import { HydratedPostDocument, IPost, IPostMethods, PostModel } from '@/types/mongoose/post'; import { HydratedCommunityDocument } from '@/types/mongoose/community'; import { PostToJSONOptions } from '@/types/mongoose/post-to-json-options'; @@ -107,7 +108,7 @@ PostSchema.method('generatePostUID', async function genera }); PostSchema.method('cleanedBody', function cleanedBody(): string { - return this.body ? this.body.replace(/[^\p{L}\p{P}\d\n\r~$^¨←→↑↓√¦⇒⇔¤¢€£¥™©®+×÷=±∞˘˙¸˛˜°¹²³♭♪¬¯¼½¾♡♥●◆■▲▼☆★♀♂<>]/gu, '').replace(/[\n\r]+/gm, '') : ''; + return this.body ? this.body.replace(INVALID_POST_BODY_REGEX, '').replace(/[\n\r]+/gm, '') : ''; }); PostSchema.method('cleanedMiiData', function cleanedMiiData(): string { diff --git a/src/services/api/routes/friend_messages.ts b/src/services/api/routes/friend_messages.ts index 4260651..75ed710 100644 --- a/src/services/api/routes/friend_messages.ts +++ b/src/services/api/routes/friend_messages.ts @@ -5,7 +5,14 @@ import moment from 'moment'; import xmlbuilder from 'xmlbuilder'; import { z } from 'zod'; import { GetUserDataResponse } from '@pretendonetwork/grpc/account/get_user_data_rpc'; -import { getUserFriendPIDs, getUserAccountData, processPainting, uploadCDNAsset, getValueFromQueryString } from '@/util'; +import { + getUserFriendPIDs, + getUserAccountData, + processPainting, + uploadCDNAsset, + getValueFromQueryString, + INVALID_POST_BODY_REGEX +} from '@/util'; import { getConversationByUsers, getUserSettings, getFriendMessages } from '@/database'; import { LOG_WARN } from '@/logger'; import { Post } from '@/models/post'; @@ -39,7 +46,7 @@ router.post('/', upload.none(), async function (request: express.Request, respon } const recipientPID = bodyCheck.data.message_to_pid; - let messageBody = bodyCheck.data.body || ''; + const messageBody = bodyCheck.data.body?.trim() || ''; const painting = bodyCheck.data.painting?.replace(/\0/g, '').trim() || ''; const screenshot = bodyCheck.data.screenshot?.trim().replace(/\0/g, '').trim() || ''; const appData = bodyCheck.data.app_data?.replace(/[^A-Za-z0-9+/=\s]/g, '').trim() || ''; @@ -150,12 +157,16 @@ router.post('/', upload.none(), async function (request: express.Request, respon break; } - if (messageBody) { - messageBody = messageBody.replace(/[^\p{L}\p{P}\d\n\r~$^¨←→↑↓√¦⇒⇔¤¢€£¥™©®+×÷=±∞˘˙¸˛˜°¹²³♭♪¬¯¼½¾♡♥●◆■▲▼☆★♀♂<>]/gu, ''); + if (messageBody && INVALID_POST_BODY_REGEX.test(messageBody)) { + // TODO - Log this error + response.sendStatus(400); + return; } - if (messageBody.length > 280) { - messageBody = messageBody.substring(0, 280); + if (messageBody && messageBody.length > 280) { + // TODO - Log this error + response.sendStatus(400); + return; } if (messageBody === '' && painting === '' && screenshot === '') { diff --git a/src/services/api/routes/posts.ts b/src/services/api/routes/posts.ts index 9f86267..258ba79 100644 --- a/src/services/api/routes/posts.ts +++ b/src/services/api/routes/posts.ts @@ -3,7 +3,13 @@ import multer from 'multer'; import xmlbuilder from 'xmlbuilder'; import { z } from 'zod'; import { GetUserDataResponse } from '@pretendonetwork/grpc/account/get_user_data_rpc'; -import { getUserAccountData, processPainting, uploadCDNAsset, getValueFromQueryString } from '@/util'; +import { + getUserAccountData, + processPainting, + uploadCDNAsset, + getValueFromQueryString, + INVALID_POST_BODY_REGEX +} from '@/util'; import { getPostByID, getUserContent, @@ -235,7 +241,7 @@ async function newPost(request: express.Request, response: express.Response): Pr } const communityID = bodyCheck.data.community_id || ''; - let messageBody = bodyCheck.data.body; + const messageBody = bodyCheck.data.body?.trim(); const painting = bodyCheck.data.painting?.replace(/\0/g, '').trim() || ''; const screenshot = bodyCheck.data.screenshot?.replace(/\0/g, '').trim() || ''; const appData = bodyCheck.data.app_data?.replace(/[^A-Za-z0-9+/=\s]/g, '').trim() || ''; @@ -317,12 +323,16 @@ async function newPost(request: express.Request, response: express.Response): Pr break; } - if (messageBody) { - messageBody = messageBody.replace(/[^\p{L}\p{P}\d\n\r~$^¨←→↑↓√¦⇒⇔¤¢€£¥™©®+×÷=±∞˘˙¸˛˜°¹²³♭♪¬¯¼½¾♡♥●◆■▲▼☆★♀♂<>]/gu, ''); + if (messageBody && INVALID_POST_BODY_REGEX.test(messageBody)) { + // TODO - Log this error + response.sendStatus(400); + return; } if (messageBody && messageBody.length > 280) { - messageBody = messageBody.substring(0, 280); + // TODO - Log this error + response.sendStatus(400); + return; } if ((!messageBody || messageBody === '') && painting === '' && screenshot === '') { diff --git a/src/util.ts b/src/util.ts index 1b1e016..432419e 100644 --- a/src/util.ts +++ b/src/util.ts @@ -31,6 +31,9 @@ const s3 = new aws.S3({ secretAccessKey: config.s3.secret }); +// TODO - This doesn't really belong here +export const INVALID_POST_BODY_REGEX = /[^\p{L}\p{P}\d\n\r$^¨←→↑↓√¦⇒⇔¤¢€£¥™©®+×÷=±∞˘˙¸˛˜°¹²³♭♪¬¯¼½¾♡♥●◆■▲▼☆★♀♂<> ]/gu; + export function decodeParamPack(paramPack: string): ParamPack { const values = Buffer.from(paramPack, 'base64').toString().split('\\'); const entries = values.filter(value => value).reduce((entries: string[][], value: string, index: number) => {