]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
42b40636 C |
2 | import { body, query } from 'express-validator' |
3 | import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc' | |
64553e88 | 4 | import { isStringArray } from '@server/helpers/custom-validators/search' |
42b40636 C |
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' | |
64553e88 | 15 | import { isDateValid, toArray } from '../../helpers/custom-validators/misc' |
10363c74 C |
16 | import { logger } from '../../helpers/logger' |
17 | import { areValidationErrors } from './shared' | |
fd8710b8 | 18 | |
42b40636 C |
19 | const createClientLogValidator = [ |
20 | body('message') | |
21 | .custom(isValidClientLogMessage).withMessage('Should have a valid log message'), | |
22 | ||
23 | body('url') | |
24 | .custom(isUrlValid).withMessage('Should have a valid log url'), | |
25 | ||
26 | body('level') | |
27 | .custom(isValidClientLogLevel).withMessage('Should have a valid log message'), | |
28 | ||
29 | body('stackTrace') | |
30 | .optional() | |
31 | .custom(isValidClientLogStackTrace).withMessage('Should have a valid log stack trace'), | |
32 | ||
33 | body('meta') | |
34 | .optional() | |
35 | .custom(isValidClientLogMeta).withMessage('Should have a valid log meta'), | |
36 | ||
37 | body('userAgent') | |
38 | .optional() | |
39 | .custom(isValidClientLogUserAgent).withMessage('Should have a valid log user agent'), | |
40 | ||
41 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
42 | logger.debug('Checking createClientLogValidator parameters.', { parameters: req.query }) | |
43 | ||
44 | if (CONFIG.LOG.ACCEPT_CLIENT_LOG !== true) { | |
45 | return res.sendStatus(HttpStatusCode.FORBIDDEN_403) | |
46 | } | |
47 | ||
48 | if (areValidationErrors(req, res)) return | |
49 | ||
50 | return next() | |
51 | } | |
52 | ] | |
53 | ||
fd8710b8 C |
54 | const getLogsValidator = [ |
55 | query('startDate') | |
70330f63 | 56 | .custom(isDateValid).withMessage('Should have a start date that conforms to ISO 8601'), |
fd8710b8 C |
57 | query('level') |
58 | .optional() | |
59 | .custom(isValidLogLevel).withMessage('Should have a valid level'), | |
64553e88 C |
60 | query('tagsOneOf') |
61 | .optional() | |
62 | .customSanitizer(toArray) | |
63 | .custom(isStringArray).withMessage('Should have a valid one of tags array'), | |
fd8710b8 C |
64 | query('endDate') |
65 | .optional() | |
70330f63 | 66 | .custom(isDateValid).withMessage('Should have an end date that conforms to ISO 8601'), |
fd8710b8 C |
67 | |
68 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
69 | logger.debug('Checking getLogsValidator parameters.', { parameters: req.query }) | |
70 | ||
71 | if (areValidationErrors(req, res)) return | |
72 | ||
73 | return next() | |
74 | } | |
75 | ] | |
76 | ||
566c125d C |
77 | const getAuditLogsValidator = [ |
78 | query('startDate') | |
70330f63 | 79 | .custom(isDateValid).withMessage('Should have a start date that conforms to ISO 8601'), |
566c125d C |
80 | query('endDate') |
81 | .optional() | |
70330f63 | 82 | .custom(isDateValid).withMessage('Should have a end date that conforms to ISO 8601'), |
566c125d C |
83 | |
84 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
85 | logger.debug('Checking getAuditLogsValidator parameters.', { parameters: req.query }) | |
86 | ||
87 | if (areValidationErrors(req, res)) return | |
88 | ||
89 | return next() | |
90 | } | |
91 | ] | |
92 | ||
fd8710b8 C |
93 | // --------------------------------------------------------------------------- |
94 | ||
95 | export { | |
566c125d | 96 | getLogsValidator, |
42b40636 C |
97 | getAuditLogsValidator, |
98 | createClientLogValidator | |
fd8710b8 | 99 | } |