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'
|
2022-08-17 09:44:32 -04:00
|
|
|
import { arrayify } from '@shared/core-utils'
|
2022-07-15 09:30:14 -04:00
|
|
|
import { HttpStatusCode } from '@shared/models'
|
|
|
|
import {
|
|
|
|
isValidClientLogLevel,
|
|
|
|
isValidClientLogMessage,
|
|
|
|
isValidClientLogMeta,
|
|
|
|
isValidClientLogStackTrace,
|
|
|
|
isValidClientLogUserAgent,
|
|
|
|
isValidLogLevel
|
|
|
|
} from '../../helpers/custom-validators/logs'
|
2022-08-17 09:44:32 -04:00
|
|
|
import { isDateValid } from '../../helpers/custom-validators/misc'
|
2021-06-03 11:33:44 -04:00
|
|
|
import { areValidationErrors } from './shared'
|
2019-04-10 09:26:33 -04:00
|
|
|
|
2022-07-15 09:30:14 -04:00
|
|
|
const createClientLogValidator = [
|
|
|
|
body('message')
|
2022-08-17 08:27:04 -04:00
|
|
|
.custom(isValidClientLogMessage),
|
2022-07-15 09:30:14 -04:00
|
|
|
|
|
|
|
body('url')
|
2022-08-17 08:27:04 -04:00
|
|
|
.custom(isUrlValid),
|
2022-07-15 09:30:14 -04:00
|
|
|
|
|
|
|
body('level')
|
2022-08-17 08:27:04 -04:00
|
|
|
.custom(isValidClientLogLevel),
|
2022-07-15 09:30:14 -04:00
|
|
|
|
|
|
|
body('stackTrace')
|
|
|
|
.optional()
|
2022-08-17 08:27:04 -04:00
|
|
|
.custom(isValidClientLogStackTrace),
|
2022-07-15 09:30:14 -04:00
|
|
|
|
|
|
|
body('meta')
|
|
|
|
.optional()
|
2022-08-17 08:27:04 -04:00
|
|
|
.custom(isValidClientLogMeta),
|
2022-07-15 09:30:14 -04:00
|
|
|
|
|
|
|
body('userAgent')
|
|
|
|
.optional()
|
2022-08-17 08:27:04 -04:00
|
|
|
.custom(isValidClientLogUserAgent),
|
2022-07-15 09:30:14 -04:00
|
|
|
|
|
|
|
(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 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()
|
2022-08-17 08:27:04 -04:00
|
|
|
.custom(isValidLogLevel),
|
2021-10-20 08:23:32 -04:00
|
|
|
query('tagsOneOf')
|
|
|
|
.optional()
|
2022-08-17 09:44:32 -04:00
|
|
|
.customSanitizer(arrayify)
|
2021-10-20 08:23:32 -04:00
|
|
|
.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) => {
|
|
|
|
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) => {
|
|
|
|
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
|
|
|
}
|