X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Fvalidators%2Fserver.ts;h=fc7239b25203fb411a70a25597f1e0c2f4a32e50;hb=d4a8e7a65f97bb3257facc13e1ae8ffdbad61ddb;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..fc7239b25 100644 --- a/server/middlewares/validators/server.ts +++ b/server/middlewares/validators/server.ts @@ -1,9 +1,13 @@ import * as express from 'express' +import { body } from 'express-validator' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' +import { isHostValid, isValidContactBody } from '../../helpers/custom-validators/servers' +import { isUserDisplayNameValid } from '../../helpers/custom-validators/users' import { logger } from '../../helpers/logger' -import { areValidationErrors } from './utils' -import { isHostValid } from '../../helpers/custom-validators/servers' +import { CONFIG, isEmailEnabled } from '../../initializers/config' +import { Redis } from '../../lib/redis' import { ServerModel } from '../../models/server/server' -import { body } from 'express-validator/check' +import { areValidationErrors } from './shared' const serverGetValidator = [ body('host').custom(isHostValid).withMessage('Should have a valid host'), @@ -15,9 +19,10 @@ const serverGetValidator = [ const server = await ServerModel.loadByHost(req.body.host) if (!server) { - return res.status(404) - .send({ error: 'Server host not found.' }) - .end() + return res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Server host not found.' + }) } res.locals.server = server @@ -26,8 +31,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.fail({ + status: HttpStatusCode.CONFLICT_409, + message: 'Contact form is not enabled on this instance.' + }) + } + + if (isEmailEnabled() === false) { + return res.fail({ + status: HttpStatusCode.CONFLICT_409, + message: 'Emailer is not enabled on this instance.' + }) + } + + if (await Redis.Instance.doesContactFormIpExist(req.ip)) { + logger.info('Refusing a contact form by %s: already sent one recently.', req.ip) + + return res.fail({ + status: HttpStatusCode.FORBIDDEN_403, + message: 'You already sent a contact form recently.' + }) + } + + return next() + } +] + // --------------------------------------------------------------------------- export { - serverGetValidator + serverGetValidator, + contactAdministratorValidator }