From 26de1467e2ae147b7f46dfce6fd18cd14511fe4c Mon Sep 17 00:00:00 2001 From: kontrollanten <6680299+kontrollanten@users.noreply.github.com> Date: Tue, 26 Mar 2024 17:08:15 +0100 Subject: [PATCH] fix(server/video-view): log invalid currentTime req (#6288) * fix(server/video-view): log invalid currentTime req relates to #6285 * Styling --------- Co-authored-by: Chocobozzz --- server/core/middlewares/error.ts | 8 ++++---- .../middlewares/validators/videos/video-view.ts | 14 ++++++++------ server/core/types/express.d.ts | 3 ++- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/server/core/middlewares/error.ts b/server/core/middlewares/error.ts index 84732f0f6..020791ae0 100644 --- a/server/core/middlewares/error.ts +++ b/server/core/middlewares/error.ts @@ -1,11 +1,11 @@ +import { HttpStatusCode } from '@peertube/peertube-models' +import { logger } from '@server/helpers/logger.js' import express from 'express' import { ProblemDocument, ProblemDocumentExtension } from 'http-problem-details' -import { logger } from '@server/helpers/logger.js' -import { HttpStatusCode } from '@peertube/peertube-models' function apiFailMiddleware (req: express.Request, res: express.Response, next: express.NextFunction) { res.fail = options => { - const { status = HttpStatusCode.BAD_REQUEST_400, message, title, type, data, instance, tags } = options + const { status = HttpStatusCode.BAD_REQUEST_400, message, title, type, data, instance, tags, logLevel = 'debug' } = options const extension = new ProblemDocumentExtension({ ...data, @@ -29,7 +29,7 @@ function apiFailMiddleware (req: express.Request, res: express.Response, next: e : undefined }, extension) - logger.debug('Bad HTTP request.', { json, tags }) + logger.log(logLevel, 'Bad HTTP request.', { json, tags }) res.status(status) diff --git a/server/core/middlewares/validators/videos/video-view.ts b/server/core/middlewares/validators/videos/video-view.ts index 3ccb9ba1f..c4796b474 100644 --- a/server/core/middlewares/validators/videos/video-view.ts +++ b/server/core/middlewares/validators/videos/video-view.ts @@ -1,9 +1,9 @@ -import express from 'express' -import { body, param } from 'express-validator' import { HttpStatusCode } from '@peertube/peertube-models' import { isVideoTimeValid } from '@server/helpers/custom-validators/video-view.js' import { getCachedVideoDuration } from '@server/lib/video.js' import { LocalVideoViewerModel } from '@server/models/view/local-video-viewer.js' +import express from 'express' +import { body, param } from 'express-validator' import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc.js' import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared/index.js' @@ -42,10 +42,12 @@ const videoViewValidator = [ const video = res.locals.onlyImmutableVideo const { duration } = await getCachedVideoDuration(video.id) - if (!isVideoTimeValid(req.body.currentTime, duration)) { + const currentTime = req.body.currentTime + if (!isVideoTimeValid(currentTime, duration)) { return res.fail({ status: HttpStatusCode.BAD_REQUEST_400, - message: 'Current time is invalid' + message: `Current time ${currentTime} is invalid (video ${video.uuid} duration: ${duration})`, + logLevel: 'warn' }) } @@ -56,6 +58,6 @@ const videoViewValidator = [ // --------------------------------------------------------------------------- export { - videoViewValidator, - getVideoLocalViewerValidator + getVideoLocalViewerValidator, videoViewValidator } + diff --git a/server/core/types/express.d.ts b/server/core/types/express.d.ts index 9f106cc11..4f669ffee 100644 --- a/server/core/types/express.d.ts +++ b/server/core/types/express.d.ts @@ -1,4 +1,4 @@ -import { HttpMethodType, PeerTubeProblemDocumentData, VideoCreate } from '@peertube/peertube-models' +import { HttpMethodType, PeerTubeProblemDocumentData, ServerLogLevel, VideoCreate } from '@peertube/peertube-models' import { RegisterServerAuthExternalOptions } from '@server/types/index.js' import { MAbuseMessage, @@ -109,6 +109,7 @@ declare module 'express' { data?: PeerTubeProblemDocumentData + logLevel?: ServerLogLevel // Default debug tags?: string[] }) => void