diff --git a/lib/api/mailboxes.js b/lib/api/mailboxes.js index 30210164..327c37c5 100644 --- a/lib/api/mailboxes.js +++ b/lib/api/mailboxes.js @@ -6,7 +6,7 @@ const imapTools = require('../../imap-core/lib/imap-tools'); const tools = require('../tools'); const roles = require('../roles'); const util = require('util'); -const { sessSchema, sessIPSchema, booleanSchema } = require('../schemas'); +const { sessSchema, sessIPSchema, booleanSchema, mailboxPathValidator } = require('../schemas'); const { userId, mailboxId } = require('../schemas/request/general-schemas'); const { successRes } = require('../schemas/response/general-schemas'); const { GetMailboxesResult } = require('../schemas/response/mailboxes-schemas'); @@ -283,23 +283,7 @@ module.exports = (db, server, mailboxHandler) => { path: Joi.string() .regex(/\/{2,}|\/$/, { invert: true }) .max(MAX_MAILBOX_NAME_LENGTH * MAX_SUB_MAILBOXES + 127) - .custom((value, helpers) => { - const splittedPathArr = value.split('/'); - - if (splittedPathArr.length > MAX_SUB_MAILBOXES) { - // too many paths - return helpers.message(`The mailbox path cannot be more than ${MAX_SUB_MAILBOXES} levels deep`); - } - - for (const pathPart of splittedPathArr) { - if (pathPart.length > MAX_MAILBOX_NAME_LENGTH) { - // part too long error - return helpers.message(`Any part of the mailbox path cannot be longer than ${MAX_MAILBOX_NAME_LENGTH} chars long`); - } - } - - return value; - }, 'Mailbox path validation') + .custom(mailboxPathValidator, 'Mailbox path validation') .required() .description('Full path of the mailbox, folders are separated by slashes, ends with the mailbox name (unicode string)'), hidden: booleanSchema.default(false).description('Is the folder hidden or not. Hidden folders can not be opened in IMAP.'), @@ -559,23 +543,7 @@ module.exports = (db, server, mailboxHandler) => { path: Joi.string() .regex(/\/{2,}|\/$/, { invert: true }) .max(MAX_MAILBOX_NAME_LENGTH * MAX_SUB_MAILBOXES + 127) - .custom((value, helpers) => { - const splittedPathArr = value.split('/'); - - if (splittedPathArr.length > MAX_SUB_MAILBOXES) { - // too many paths - return helpers.message(`The mailbox path cannot be more than ${MAX_SUB_MAILBOXES} levels deep`); - } - - for (const pathPart of splittedPathArr) { - if (pathPart.length > MAX_MAILBOX_NAME_LENGTH) { - // part too long error - return helpers.message(`Any part of the mailbox path cannot be longer than ${MAX_MAILBOX_NAME_LENGTH} chars long`); - } - } - - return value; - }, 'Mailbox path validation') + .custom(mailboxPathValidator, 'Mailbox path validation') .description('Full path of the mailbox, use this to rename an existing Mailbox'), retention: Joi.number() .empty('') diff --git a/lib/schemas.js b/lib/schemas.js index 9da57058..41c0e80a 100644 --- a/lib/schemas.js +++ b/lib/schemas.js @@ -2,6 +2,7 @@ const EJSON = require('mongodb-extended-json'); const Joi = require('joi'); +const { MAX_SUB_MAILBOXES, MAX_MAILBOX_NAME_LENGTH } = require('./consts'); const sessSchema = Joi.string().max(255).label('Session identifier').description('Session identifier for the logs'); const sessIPSchema = Joi.string() @@ -82,6 +83,24 @@ const metaDataValidator = () => (value, helpers) => { return strValue; }; +const mailboxPathValidator = (value, helpers) => { + const splittedPathArr = value.split('/'); + + if (splittedPathArr.length > MAX_SUB_MAILBOXES) { + // too many paths + return helpers.message(`The mailbox path cannot be more than ${MAX_SUB_MAILBOXES} levels deep`); + } + + for (const pathPart of splittedPathArr) { + if (pathPart.length > MAX_MAILBOX_NAME_LENGTH) { + // part too long error + return helpers.message(`Any part of the mailbox path cannot be longer than ${MAX_MAILBOX_NAME_LENGTH} chars long`); + } + } + + return value; +}; + const mongoCursorSchema = Joi.string().trim().empty('').custom(mongoCursorValidator({}), 'Cursor validation').max(1024); const pageLimitSchema = Joi.number().default(20).min(1).max(250).label('Page size'); const pageNrSchema = Joi.number().default(1).label('Page number').description('Current page number. Informational only, page numbers start from 1'); @@ -111,5 +130,6 @@ module.exports = { pageLimitSchema, booleanSchema, metaDataSchema, - usernameSchema + usernameSchema, + mailboxPathValidator };