aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/logs.ts
blob: ce36dc40b45fd8cebba1da1edcf3d79fa5f9f270 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import express from 'express'
import { body, query } from 'express-validator'
import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
import { isStringArray } from '@server/helpers/custom-validators/search'
import { CONFIG } from '@server/initializers/config'
import { HttpStatusCode } from '@shared/models'
import {
  isValidClientLogLevel,
  isValidClientLogMessage,
  isValidClientLogMeta,
  isValidClientLogStackTrace,
  isValidClientLogUserAgent,
  isValidLogLevel
} from '../../helpers/custom-validators/logs'
import { isDateValid, toArray } from '../../helpers/custom-validators/misc'
import { logger } from '../../helpers/logger'
import { areValidationErrors } from './shared'

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) => {
    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()
  }
]

const getLogsValidator = [
  query('startDate')
    .custom(isDateValid).withMessage('Should have a start date that conforms to ISO 8601'),
  query('level')
    .optional()
    .custom(isValidLogLevel),
  query('tagsOneOf')
    .optional()
    .customSanitizer(toArray)
    .custom(isStringArray).withMessage('Should have a valid one of tags array'),
  query('endDate')
    .optional()
    .custom(isDateValid).withMessage('Should have an end date that conforms to ISO 8601'),

  (req: express.Request, res: express.Response, next: express.NextFunction) => {
    logger.debug('Checking getLogsValidator parameters.', { parameters: req.query })

    if (areValidationErrors(req, res)) return

    return next()
  }
]

const getAuditLogsValidator = [
  query('startDate')
    .custom(isDateValid).withMessage('Should have a start date that conforms to ISO 8601'),
  query('endDate')
    .optional()
    .custom(isDateValid).withMessage('Should have a end date that conforms to ISO 8601'),

  (req: express.Request, res: express.Response, next: express.NextFunction) => {
    logger.debug('Checking getAuditLogsValidator parameters.', { parameters: req.query })

    if (areValidationErrors(req, res)) return

    return next()
  }
]

// ---------------------------------------------------------------------------

export {
  getLogsValidator,
  getAuditLogsValidator,
  createClientLogValidator
}