From 6ba5968861c4d38f699f8589f99b791cc1470e43 Mon Sep 17 00:00:00 2001 From: MeiMei <30769358+mei23@users.noreply.github.com> Date: Tue, 1 Oct 2019 01:46:31 +0900 Subject: [PATCH] Apply DB limit to the maximum note text length (#5465) --- src/misc/hard-limits.ts | 8 ++++++++ src/server/api/endpoints/admin/update-meta.ts | 3 ++- src/server/api/endpoints/meta.ts | 3 ++- src/server/api/endpoints/notes/create.ts | 5 ++++- 4 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 src/misc/hard-limits.ts diff --git a/src/misc/hard-limits.ts b/src/misc/hard-limits.ts new file mode 100644 index 0000000000..2a61cb321b --- /dev/null +++ b/src/misc/hard-limits.ts @@ -0,0 +1,8 @@ + +// If you change DB_* values, you must also change the DB schema. + +/** + * Maximum note text length that can be stored in DB. + * Surrogate pairs count as one + */ +export const DB_MAX_NOTE_TEXT_LENGTH = 8192; diff --git a/src/server/api/endpoints/admin/update-meta.ts b/src/server/api/endpoints/admin/update-meta.ts index 1d6973e7ac..05a1e25c01 100644 --- a/src/server/api/endpoints/admin/update-meta.ts +++ b/src/server/api/endpoints/admin/update-meta.ts @@ -3,6 +3,7 @@ import define from '../../define'; import { getConnection } from 'typeorm'; import { Meta } from '../../../../models/entities/meta'; import { insertModerationLog } from '../../../../services/insert-moderation-log'; +import { DB_MAX_NOTE_TEXT_LENGTH } from '../../../../misc/hard-limits'; export const meta = { desc: { @@ -121,7 +122,7 @@ export const meta = { }, maxNoteTextLength: { - validator: $.optional.num.min(0), + validator: $.optional.num.min(0).max(DB_MAX_NOTE_TEXT_LENGTH), desc: { 'ja-JP': '投稿の最大文字数' } diff --git a/src/server/api/endpoints/meta.ts b/src/server/api/endpoints/meta.ts index c07a1c07bb..0b56a9d4ef 100644 --- a/src/server/api/endpoints/meta.ts +++ b/src/server/api/endpoints/meta.ts @@ -7,6 +7,7 @@ import * as pkg from '../../../../package.json'; import { Emojis } from '../../../models'; import { getConnection } from 'typeorm'; import redis from '../../../db/redis'; +import { DB_MAX_NOTE_TEXT_LENGTH } from '../../../misc/hard-limits'; export const meta = { stability: 'stable', @@ -138,7 +139,7 @@ export default define(meta, async (ps, me) => { bannerUrl: instance.bannerUrl, errorImageUrl: instance.errorImageUrl, iconUrl: instance.iconUrl, - maxNoteTextLength: instance.maxNoteTextLength, + maxNoteTextLength: Math.min(instance.maxNoteTextLength, DB_MAX_NOTE_TEXT_LENGTH), emojis: emojis.map(e => ({ id: e.id, aliases: e.aliases, diff --git a/src/server/api/endpoints/notes/create.ts b/src/server/api/endpoints/notes/create.ts index 1650de9071..6e392c03f1 100644 --- a/src/server/api/endpoints/notes/create.ts +++ b/src/server/api/endpoints/notes/create.ts @@ -10,6 +10,7 @@ import { User } from '../../../../models/entities/user'; import { Users, DriveFiles, Notes } from '../../../../models'; import { DriveFile } from '../../../../models/entities/drive-file'; import { Note } from '../../../../models/entities/note'; +import { DB_MAX_NOTE_TEXT_LENGTH } from '../../../../misc/hard-limits'; let maxNoteTextLength = 1000; @@ -55,7 +56,9 @@ export const meta = { text: { validator: $.optional.nullable.str.pipe(text => - length(text.trim()) <= maxNoteTextLength && text.trim() != '' + text.trim() != '' + && length(text.trim()) <= maxNoteTextLength + && Array.from(text.trim()).length <= DB_MAX_NOTE_TEXT_LENGTH // DB limit ), default: null as any, desc: {