]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/validators/logs.ts
Translated using Weblate (Persian)
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / logs.ts
index 7380c6edde6a174fb7fdf1656e8b86a686f25d9a..2d828bb429a9eb603118fe13621205b6fb7e94b2 100644 (file)
@@ -1,23 +1,83 @@
-import * as express from 'express'
-import { logger } from '../../helpers/logger'
-import { areValidationErrors } from './utils'
+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 { arrayify } from '@shared/core-utils'
+import { HttpStatusCode } from '@shared/models'
+import {
+  isValidClientLogLevel,
+  isValidClientLogMessage,
+  isValidClientLogMeta,
+  isValidClientLogStackTrace,
+  isValidClientLogUserAgent,
+  isValidLogLevel
+} from '../../helpers/custom-validators/logs'
 import { isDateValid } from '../../helpers/custom-validators/misc'
-import { query } from 'express-validator/check'
-import { isValidLogLevel } from '../../helpers/custom-validators/logs'
+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) => {
+    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 valid start date'),
+    .custom(isDateValid).withMessage('Should have a start date that conforms to ISO 8601'),
   query('level')
     .optional()
-    .custom(isValidLogLevel).withMessage('Should have a valid level'),
+    .custom(isValidLogLevel),
+  query('tagsOneOf')
+    .optional()
+    .customSanitizer(arrayify)
+    .custom(isStringArray).withMessage('Should have a valid one of tags array'),
   query('endDate')
     .optional()
-    .custom(isDateValid).withMessage('Should have a valid end date'),
+    .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) => {
     if (areValidationErrors(req, res)) return
 
     return next()
@@ -27,5 +87,7 @@ const getLogsValidator = [
 // ---------------------------------------------------------------------------
 
 export {
-  getLogsValidator
+  getLogsValidator,
+  getAuditLogsValidator,
+  createClientLogValidator
 }