From 3842a1ee8c32d136b97b2a0b2b7c0f8f1e733fe7 Mon Sep 17 00:00:00 2001 From: Hazel K Date: Thu, 3 Oct 2024 20:05:15 -0400 Subject: [PATCH] fix length validation --- packages/backend/src/core/NoteCreateService.ts | 12 ++++++++++++ packages/backend/src/core/NoteEditService.ts | 12 ++++++++++++ .../backend/src/server/api/endpoints/notes/create.ts | 3 ++- .../backend/src/server/api/endpoints/notes/edit.ts | 4 +++- 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/core/NoteCreateService.ts b/packages/backend/src/core/NoteCreateService.ts index c252336f99..beaa75e737 100644 --- a/packages/backend/src/core/NoteCreateService.ts +++ b/packages/backend/src/core/NoteCreateService.ts @@ -346,6 +346,18 @@ export class NoteCreateService implements OnApplicationShutdown { data.text = null; } + if (data.cw) { + if (data.cw.length > DB_MAX_NOTE_TEXT_LENGTH) { + data.cw = data.cw.slice(0, DB_MAX_NOTE_TEXT_LENGTH); + } + data.cw = data.cw.trim(); + if (data.cw === '') { + data.cw = null; + } + } else { + data.cw = null; + } + let tags = data.apHashtags; let emojis = data.apEmojis; let mentionedUsers = data.apMentions; diff --git a/packages/backend/src/core/NoteEditService.ts b/packages/backend/src/core/NoteEditService.ts index f42d14b466..aecd37aeb9 100644 --- a/packages/backend/src/core/NoteEditService.ts +++ b/packages/backend/src/core/NoteEditService.ts @@ -371,6 +371,18 @@ export class NoteEditService implements OnApplicationShutdown { data.text = null; } + if (data.cw) { + if (data.cw.length > DB_MAX_NOTE_TEXT_LENGTH) { + data.cw = data.cw.slice(0, DB_MAX_NOTE_TEXT_LENGTH); + } + data.cw = data.cw.trim(); + if (data.cw === '') { + data.cw = null; + } + } else { + data.cw = null; + } + let tags = data.apHashtags; let emojis = data.apEmojis; let mentionedUsers = data.apMentions; diff --git a/packages/backend/src/server/api/endpoints/notes/create.ts b/packages/backend/src/server/api/endpoints/notes/create.ts index 626f03b758..a4c2e28129 100644 --- a/packages/backend/src/server/api/endpoints/notes/create.ts +++ b/packages/backend/src/server/api/endpoints/notes/create.ts @@ -252,7 +252,8 @@ export default class extends Endpoint { // eslint- private noteCreateService: NoteCreateService, ) { super(meta, paramDef, async (ps, me) => { - if (ps.text && (ps.text.length > this.config.maxNoteLength)) { + const contentLength = (ps.text?.length ?? 0) + (ps.cw?.length ?? 0); + if (contentLength > this.config.maxNoteLength) { throw new ApiError(meta.errors.maxLength); } diff --git a/packages/backend/src/server/api/endpoints/notes/edit.ts b/packages/backend/src/server/api/endpoints/notes/edit.ts index 835cbc14fa..b9be145caf 100644 --- a/packages/backend/src/server/api/endpoints/notes/edit.ts +++ b/packages/backend/src/server/api/endpoints/notes/edit.ts @@ -297,9 +297,11 @@ export default class extends Endpoint { // eslint- private noteEditService: NoteEditService, ) { super(meta, paramDef, async (ps, me) => { - if (ps.text && (ps.text.length > this.config.maxNoteLength)) { + const contentLength = (ps.text?.length ?? 0) + (ps.cw?.length ?? 0); + if (contentLength > this.config.maxNoteLength) { throw new ApiError(meta.errors.maxLength); } + let visibleUsers: MiUser[] = []; if (ps.visibleUserIds) { visibleUsers = await this.usersRepository.findBy({