Apply DB limit to the maximum note text length (#5465)

This commit is contained in:
MeiMei 2019-10-01 01:46:31 +09:00 committed by syuilo
parent cea2d621f2
commit 6ba5968861
4 changed files with 16 additions and 3 deletions

8
src/misc/hard-limits.ts Normal file
View File

@ -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;

View File

@ -3,6 +3,7 @@ import define from '../../define';
import { getConnection } from 'typeorm'; import { getConnection } from 'typeorm';
import { Meta } from '../../../../models/entities/meta'; import { Meta } from '../../../../models/entities/meta';
import { insertModerationLog } from '../../../../services/insert-moderation-log'; import { insertModerationLog } from '../../../../services/insert-moderation-log';
import { DB_MAX_NOTE_TEXT_LENGTH } from '../../../../misc/hard-limits';
export const meta = { export const meta = {
desc: { desc: {
@ -121,7 +122,7 @@ export const meta = {
}, },
maxNoteTextLength: { maxNoteTextLength: {
validator: $.optional.num.min(0), validator: $.optional.num.min(0).max(DB_MAX_NOTE_TEXT_LENGTH),
desc: { desc: {
'ja-JP': '投稿の最大文字数' 'ja-JP': '投稿の最大文字数'
} }

View File

@ -7,6 +7,7 @@ import * as pkg from '../../../../package.json';
import { Emojis } from '../../../models'; import { Emojis } from '../../../models';
import { getConnection } from 'typeorm'; import { getConnection } from 'typeorm';
import redis from '../../../db/redis'; import redis from '../../../db/redis';
import { DB_MAX_NOTE_TEXT_LENGTH } from '../../../misc/hard-limits';
export const meta = { export const meta = {
stability: 'stable', stability: 'stable',
@ -138,7 +139,7 @@ export default define(meta, async (ps, me) => {
bannerUrl: instance.bannerUrl, bannerUrl: instance.bannerUrl,
errorImageUrl: instance.errorImageUrl, errorImageUrl: instance.errorImageUrl,
iconUrl: instance.iconUrl, iconUrl: instance.iconUrl,
maxNoteTextLength: instance.maxNoteTextLength, maxNoteTextLength: Math.min(instance.maxNoteTextLength, DB_MAX_NOTE_TEXT_LENGTH),
emojis: emojis.map(e => ({ emojis: emojis.map(e => ({
id: e.id, id: e.id,
aliases: e.aliases, aliases: e.aliases,

View File

@ -10,6 +10,7 @@ import { User } from '../../../../models/entities/user';
import { Users, DriveFiles, Notes } from '../../../../models'; import { Users, DriveFiles, Notes } from '../../../../models';
import { DriveFile } from '../../../../models/entities/drive-file'; import { DriveFile } from '../../../../models/entities/drive-file';
import { Note } from '../../../../models/entities/note'; import { Note } from '../../../../models/entities/note';
import { DB_MAX_NOTE_TEXT_LENGTH } from '../../../../misc/hard-limits';
let maxNoteTextLength = 1000; let maxNoteTextLength = 1000;
@ -55,7 +56,9 @@ export const meta = {
text: { text: {
validator: $.optional.nullable.str.pipe(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, default: null as any,
desc: { desc: {