2021-08-27 08:32:44 -04:00
|
|
|
import express from 'express'
|
2022-07-15 09:30:14 -04:00
|
|
|
import { body, query } from 'express-validator'
|
|
|
|
import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
|
2021-10-20 08:23:32 -04:00
|
|
|
import { isStringArray } from '@server/helpers/custom-validators/search'
|
2022-07-15 09:30:14 -04:00
|
|
|
import { CONFIG } from '@server/initializers/config'
|
|
|
|
import { HttpStatusCode } from '@shared/models'
|
|
|
|
import {
|
|
|
|
isValidClientLogLevel,
|
|
|
|
isValidClientLogMessage,
|
|
|
|
isValidClientLogMeta,
|
|
|
|
isValidClientLogStackTrace,
|
|
|
|
isValidClientLogUserAgent,
|
|
|
|
isValidLogLevel
|
|
|
|
} from '../../helpers/custom-validators/logs'
|
2021-10-20 08:23:32 -04:00
|
|
|
import { isDateValid, toArray } from '../../helpers/custom-validators/misc'
|
2021-06-03 11:33:44 -04:00
|
|
|
import { logger } from '../../helpers/logger'
|
|
|
|
import { areValidationErrors } from './shared'
|
2019-04-10 09:26:33 -04:00
|
|
|
|
2022-07-15 09:30:14 -04:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2019-04-10 09:26:33 -04:00
|
|
|
const getLogsValidator = [
|
|
|
|
query('startDate')
|
2021-05-31 13:47:14 -04:00
|
|
|
.custom(isDateValid).withMessage('Should have a start date that conforms to ISO 8601'),
|
2019-04-10 09:26:33 -04:00
|
|
|
query('level')
|
|
|
|
.optional()
|
|
|
|
.custom(isValidLogLevel).withMessage('Should have a valid level'),
|
2021-10-20 08:23:32 -04:00
|
|
|
query('tagsOneOf')
|
|
|
|
.optional()
|
|
|
|
.customSanitizer(toArray)
|
|
|
|
.custom(isStringArray).withMessage('Should have a valid one of tags array'),
|
2019-04-10 09:26:33 -04:00
|
|
|
query('endDate')
|
|
|
|
.optional()
|
2021-05-31 13:47:14 -04:00
|
|
|
.custom(isDateValid).withMessage('Should have an end date that conforms to ISO 8601'),
|
2019-04-10 09:26:33 -04:00
|
|
|
|
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking getLogsValidator parameters.', { parameters: req.query })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2019-12-11 08:14:01 -05:00
|
|
|
const getAuditLogsValidator = [
|
|
|
|
query('startDate')
|
2021-05-31 13:47:14 -04:00
|
|
|
.custom(isDateValid).withMessage('Should have a start date that conforms to ISO 8601'),
|
2019-12-11 08:14:01 -05:00
|
|
|
query('endDate')
|
|
|
|
.optional()
|
2021-05-31 13:47:14 -04:00
|
|
|
.custom(isDateValid).withMessage('Should have a end date that conforms to ISO 8601'),
|
2019-12-11 08:14:01 -05:00
|
|
|
|
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking getAuditLogsValidator parameters.', { parameters: req.query })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2019-04-10 09:26:33 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2019-12-11 08:14:01 -05:00
|
|
|
getLogsValidator,
|
2022-07-15 09:30:14 -04:00
|
|
|
getAuditLogsValidator,
|
|
|
|
createClientLogValidator
|
2019-04-10 09:26:33 -04:00
|
|
|
}
|