]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/logs.ts
Remove unnecessary logs
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / logs.ts
1 import express from 'express'
2 import { body, query } from 'express-validator'
3 import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
4 import { isStringArray } from '@server/helpers/custom-validators/search'
5 import { CONFIG } from '@server/initializers/config'
6 import { HttpStatusCode } from '@shared/models'
7 import {
8 isValidClientLogLevel,
9 isValidClientLogMessage,
10 isValidClientLogMeta,
11 isValidClientLogStackTrace,
12 isValidClientLogUserAgent,
13 isValidLogLevel
14 } from '../../helpers/custom-validators/logs'
15 import { isDateValid, toArray } from '../../helpers/custom-validators/misc'
16 import { areValidationErrors } from './shared'
17
18 const createClientLogValidator = [
19 body('message')
20 .custom(isValidClientLogMessage),
21
22 body('url')
23 .custom(isUrlValid),
24
25 body('level')
26 .custom(isValidClientLogLevel),
27
28 body('stackTrace')
29 .optional()
30 .custom(isValidClientLogStackTrace),
31
32 body('meta')
33 .optional()
34 .custom(isValidClientLogMeta),
35
36 body('userAgent')
37 .optional()
38 .custom(isValidClientLogUserAgent),
39
40 (req: express.Request, res: express.Response, next: express.NextFunction) => {
41 if (CONFIG.LOG.ACCEPT_CLIENT_LOG !== true) {
42 return res.sendStatus(HttpStatusCode.FORBIDDEN_403)
43 }
44
45 if (areValidationErrors(req, res)) return
46
47 return next()
48 }
49 ]
50
51 const getLogsValidator = [
52 query('startDate')
53 .custom(isDateValid).withMessage('Should have a start date that conforms to ISO 8601'),
54 query('level')
55 .optional()
56 .custom(isValidLogLevel),
57 query('tagsOneOf')
58 .optional()
59 .customSanitizer(toArray)
60 .custom(isStringArray).withMessage('Should have a valid one of tags array'),
61 query('endDate')
62 .optional()
63 .custom(isDateValid).withMessage('Should have an end date that conforms to ISO 8601'),
64
65 (req: express.Request, res: express.Response, next: express.NextFunction) => {
66 if (areValidationErrors(req, res)) return
67
68 return next()
69 }
70 ]
71
72 const getAuditLogsValidator = [
73 query('startDate')
74 .custom(isDateValid).withMessage('Should have a start date that conforms to ISO 8601'),
75 query('endDate')
76 .optional()
77 .custom(isDateValid).withMessage('Should have a end date that conforms to ISO 8601'),
78
79 (req: express.Request, res: express.Response, next: express.NextFunction) => {
80 if (areValidationErrors(req, res)) return
81
82 return next()
83 }
84 ]
85
86 // ---------------------------------------------------------------------------
87
88 export {
89 getLogsValidator,
90 getAuditLogsValidator,
91 createClientLogValidator
92 }