PeerTube/server/middlewares/validators/logs.ts

94 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-08-27 07:32:44 -05:00
import express from 'express'
import { body, query } from 'express-validator'
import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
2021-10-20 07:23:32 -05:00
import { isStringArray } from '@server/helpers/custom-validators/search'
import { CONFIG } from '@server/initializers/config'
2022-08-17 08:44:32 -05:00
import { arrayify } from '@shared/core-utils'
import { HttpStatusCode } from '@shared/models'
import {
isValidClientLogLevel,
isValidClientLogMessage,
isValidClientLogMeta,
isValidClientLogStackTrace,
isValidClientLogUserAgent,
isValidLogLevel
} from '../../helpers/custom-validators/logs'
2022-08-17 08:44:32 -05:00
import { isDateValid } from '../../helpers/custom-validators/misc'
import { areValidationErrors } from './shared'
2019-04-10 08:26:33 -05:00
const createClientLogValidator = [
body('message')
.custom(isValidClientLogMessage),
body('url')
.custom(isUrlValid),
body('level')
.custom(isValidClientLogLevel),
body('stackTrace')
.optional()
.custom(isValidClientLogStackTrace),
body('meta')
.optional()
.custom(isValidClientLogMeta),
body('userAgent')
.optional()
.custom(isValidClientLogUserAgent),
(req: express.Request, res: express.Response, next: express.NextFunction) => {
if (CONFIG.LOG.ACCEPT_CLIENT_LOG !== true) {
return res.sendStatus(HttpStatusCode.FORBIDDEN_403)
}
if (areValidationErrors(req, res)) return
return next()
}
]
2019-04-10 08:26:33 -05:00
const getLogsValidator = [
query('startDate')
2021-05-31 12:47:14 -05:00
.custom(isDateValid).withMessage('Should have a start date that conforms to ISO 8601'),
2019-04-10 08:26:33 -05:00
query('level')
.optional()
.custom(isValidLogLevel),
2021-10-20 07:23:32 -05:00
query('tagsOneOf')
.optional()
2022-08-17 08:44:32 -05:00
.customSanitizer(arrayify)
2021-10-20 07:23:32 -05:00
.custom(isStringArray).withMessage('Should have a valid one of tags array'),
2019-04-10 08:26:33 -05:00
query('endDate')
.optional()
2021-05-31 12:47:14 -05:00
.custom(isDateValid).withMessage('Should have an end date that conforms to ISO 8601'),
2019-04-10 08:26:33 -05:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
if (areValidationErrors(req, res)) return
return next()
}
]
2019-12-11 07:14:01 -06:00
const getAuditLogsValidator = [
query('startDate')
2021-05-31 12:47:14 -05:00
.custom(isDateValid).withMessage('Should have a start date that conforms to ISO 8601'),
2019-12-11 07:14:01 -06:00
query('endDate')
.optional()
2021-05-31 12:47:14 -05:00
.custom(isDateValid).withMessage('Should have a end date that conforms to ISO 8601'),
2019-12-11 07:14:01 -06:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
if (areValidationErrors(req, res)) return
return next()
}
]
2019-04-10 08:26:33 -05:00
// ---------------------------------------------------------------------------
export {
2019-12-11 07:14:01 -06:00
getLogsValidator,
getAuditLogsValidator,
createClientLogValidator
2019-04-10 08:26:33 -05:00
}