X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Fvalidators%2Fserver.ts;h=d85afc2ffee8875356422d7ad5ae5ec8b0469b60;hb=b427febb4d5cebf03b815bca2c59af6e82491569;hp=a491dfeb33e62e50d746467d9291a6e0afda984b;hpb=7ad9b9846c44d198a736183fb186c2039f5236b5;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/validators/server.ts b/server/middlewares/validators/server.ts index a491dfeb3..d85afc2ff 100644 --- a/server/middlewares/validators/server.ts +++ b/server/middlewares/validators/server.ts @@ -1,9 +1,13 @@ import * as express from 'express' import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' -import { isHostValid } from '../../helpers/custom-validators/servers' +import { isHostValid, isValidContactBody } from '../../helpers/custom-validators/servers' import { ServerModel } from '../../models/server/server' import { body } from 'express-validator/check' +import { isUserDisplayNameValid } from '../../helpers/custom-validators/users' +import { Emailer } from '../../lib/emailer' +import { Redis } from '../../lib/redis' +import { CONFIG } from '../../initializers/constants' const serverGetValidator = [ body('host').custom(isHostValid).withMessage('Should have a valid host'), @@ -26,8 +30,49 @@ const serverGetValidator = [ } ] +const contactAdministratorValidator = [ + body('fromName') + .custom(isUserDisplayNameValid).withMessage('Should have a valid name'), + body('fromEmail') + .isEmail().withMessage('Should have a valid email'), + body('body') + .custom(isValidContactBody).withMessage('Should have a valid body'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking contactAdministratorValidator parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + + if (CONFIG.CONTACT_FORM.ENABLED === false) { + return res + .status(409) + .send({ error: 'Contact form is not enabled on this instance.' }) + .end() + } + + if (Emailer.isEnabled() === false) { + return res + .status(409) + .send({ error: 'Emailer is not enabled on this instance.' }) + .end() + } + + if (await Redis.Instance.isContactFormIpExists(req.ip)) { + logger.info('Refusing a contact form by %s: already sent one recently.', req.ip) + + return res + .status(403) + .send({ error: 'You already sent a contact form recently.' }) + .end() + } + + return next() + } +] + // --------------------------------------------------------------------------- export { - serverGetValidator + serverGetValidator, + contactAdministratorValidator }