From 42b40636991b97fe818007fab19091764fc5db73 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 15 Jul 2022 15:30:14 +0200 Subject: Add ability for client to create server logs --- server/middlewares/validators/logs.ts | 52 +++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) (limited to 'server/middlewares') diff --git a/server/middlewares/validators/logs.ts b/server/middlewares/validators/logs.ts index 901d8ca64..324ba6915 100644 --- a/server/middlewares/validators/logs.ts +++ b/server/middlewares/validators/logs.ts @@ -1,11 +1,56 @@ import express from 'express' -import { query } from 'express-validator' +import { body, query } from 'express-validator' +import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc' import { isStringArray } from '@server/helpers/custom-validators/search' -import { isValidLogLevel } from '../../helpers/custom-validators/logs' +import { CONFIG } from '@server/initializers/config' +import { HttpStatusCode } from '@shared/models' +import { + isValidClientLogLevel, + isValidClientLogMessage, + isValidClientLogMeta, + isValidClientLogStackTrace, + isValidClientLogUserAgent, + isValidLogLevel +} from '../../helpers/custom-validators/logs' import { isDateValid, toArray } from '../../helpers/custom-validators/misc' import { logger } from '../../helpers/logger' import { areValidationErrors } from './shared' +const createClientLogValidator = [ + body('message') + .custom(isValidClientLogMessage).withMessage('Should have a valid log message'), + + body('url') + .custom(isUrlValid).withMessage('Should have a valid log url'), + + body('level') + .custom(isValidClientLogLevel).withMessage('Should have a valid log message'), + + body('stackTrace') + .optional() + .custom(isValidClientLogStackTrace).withMessage('Should have a valid log stack trace'), + + body('meta') + .optional() + .custom(isValidClientLogMeta).withMessage('Should have a valid log meta'), + + body('userAgent') + .optional() + .custom(isValidClientLogUserAgent).withMessage('Should have a valid log user agent'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking createClientLogValidator parameters.', { parameters: req.query }) + + if (CONFIG.LOG.ACCEPT_CLIENT_LOG !== true) { + return res.sendStatus(HttpStatusCode.FORBIDDEN_403) + } + + if (areValidationErrors(req, res)) return + + return next() + } +] + const getLogsValidator = [ query('startDate') .custom(isDateValid).withMessage('Should have a start date that conforms to ISO 8601'), @@ -49,5 +94,6 @@ const getAuditLogsValidator = [ export { getLogsValidator, - getAuditLogsValidator + getAuditLogsValidator, + createClientLogValidator } -- cgit v1.2.3