From 795212f7acc690c88c86d0fab8772f6564d59cb8 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 6 Jan 2022 13:27:29 +0100 Subject: Prevent caption listing of private videos --- server/middlewares/validators/shared/videos.ts | 33 +++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'server/middlewares/validators/shared/videos.ts') diff --git a/server/middlewares/validators/shared/videos.ts b/server/middlewares/validators/shared/videos.ts index 71b81654f..fc978b63a 100644 --- a/server/middlewares/validators/shared/videos.ts +++ b/server/middlewares/validators/shared/videos.ts @@ -1,16 +1,20 @@ -import { Response } from 'express' +import { Request, Response } from 'express' import { loadVideo, VideoLoadType } from '@server/lib/model-loaders' +import { authenticatePromiseIfNeeded } from '@server/middlewares/auth' +import { VideoModel } from '@server/models/video/video' import { VideoChannelModel } from '@server/models/video/video-channel' import { VideoFileModel } from '@server/models/video/video-file' import { MUser, MUserAccountId, + MVideo, MVideoAccountLight, MVideoFormattableDetails, MVideoFullLight, MVideoId, MVideoImmutable, - MVideoThumbnail + MVideoThumbnail, + MVideoWithRights } from '@server/types/models' import { HttpStatusCode, UserRight } from '@shared/models' @@ -89,6 +93,27 @@ async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAcc return true } +async function checkCanSeeVideoIfPrivate (req: Request, res: Response, video: MVideo, authenticateInQuery = false) { + if (!video.requiresAuth()) return true + + const videoWithRights = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.id) + + return checkCanSeePrivateVideo(req, res, videoWithRights, authenticateInQuery) +} + +async function checkCanSeePrivateVideo (req: Request, res: Response, video: MVideoWithRights, authenticateInQuery = false) { + await authenticatePromiseIfNeeded(req, res, authenticateInQuery) + + const user = res.locals.oauth ? res.locals.oauth.token.User : null + + // Only the owner or a user that have blocklist rights can see the video + if (!user || !user.canGetVideo(video)) { + return false + } + + return true +} + function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response, onlyOwned = true) { // Retrieve the user who did the request if (onlyOwned && video.isOwned() === false) { @@ -120,5 +145,7 @@ export { doesVideoChannelOfAccountExist, doesVideoExist, doesVideoFileOfVideoExist, - checkUserCanManageVideo + checkUserCanManageVideo, + checkCanSeeVideoIfPrivate, + checkCanSeePrivateVideo } -- cgit v1.2.3