X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Fvalidators%2Fvideos%2Fvideo-imports.ts;h=72442aeb67e0b5ceb26fb1db5988847fa50c22b8;hb=cb0eda5602a21d1626a7face32de6153ed07b5f9;hp=d0643ff26e8935648913643ef5c0b5a9330a25f7;hpb=32985a0a779e0e2100a3990b1f1188645e58dfb1;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/validators/videos/video-imports.ts b/server/middlewares/validators/videos/video-imports.ts index d0643ff26..72442aeb6 100644 --- a/server/middlewares/validators/videos/video-imports.ts +++ b/server/middlewares/validators/videos/video-imports.ts @@ -1,30 +1,32 @@ -import * as express from 'express' -import { body } from 'express-validator' +import express from 'express' +import { body, param, query } from 'express-validator' +import { isResolvingToUnicastOnly } from '@server/helpers/dns' import { isPreImportVideoAccepted } from '@server/lib/moderation' import { Hooks } from '@server/lib/plugins/hooks' +import { MUserAccountId, MVideoImport } from '@server/types/models' +import { forceNumber } from '@shared/core-utils' +import { HttpStatusCode, UserRight, VideoImportState } from '@shared/models' import { VideoImportCreate } from '@shared/models/videos/import/video-import-create.model' import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc' import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports' import { isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos' import { cleanUpReqFiles } from '../../../helpers/express-utils' import { logger } from '../../../helpers/logger' -import { doesVideoChannelOfAccountExist } from '../../../helpers/middlewares' import { CONFIG } from '../../../initializers/config' import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' -import { areValidationErrors } from '../utils' +import { areValidationErrors, doesVideoChannelOfAccountExist, doesVideoImportExist } from '../shared' import { getCommonVideoEditAttributes } from './videos' -import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' const videoImportAddValidator = getCommonVideoEditAttributes().concat([ body('channelId') .customSanitizer(toIntOrNull) - .custom(isIdValid).withMessage('Should have correct video channel id'), + .custom(isIdValid), body('targetUrl') .optional() - .custom(isVideoImportTargetUrlValid).withMessage('Should have a valid video import target URL'), + .custom(isVideoImportTargetUrlValid), body('magnetUri') .optional() - .custom(isVideoMagnetUriValid).withMessage('Should have a valid video magnet URI'), + .custom(isVideoMagnetUriValid), body('torrentfile') .custom((value, { req }) => isVideoImportTorrentFile(req.files)) .withMessage( @@ -33,11 +35,11 @@ const videoImportAddValidator = getCommonVideoEditAttributes().concat([ ), body('name') .optional() - .custom(isVideoNameValid).withMessage('Should have a valid name'), + .custom(isVideoNameValid).withMessage( + `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long` + ), async (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking videoImportAddValidator parameters', { parameters: req.body }) - const user = res.locals.oauth.token.User const torrentFile = req.files?.['torrentfile'] ? req.files['torrentfile'][0] : undefined @@ -45,14 +47,20 @@ const videoImportAddValidator = getCommonVideoEditAttributes().concat([ if (CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true && req.body.targetUrl) { cleanUpReqFiles(req) - return res.status(HttpStatusCode.CONFLICT_409) - .json({ error: 'HTTP import is not enabled on this instance.' }) + + return res.fail({ + status: HttpStatusCode.CONFLICT_409, + message: 'HTTP import is not enabled on this instance.' + }) } if (CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED !== true && (req.body.magnetUri || torrentFile)) { cleanUpReqFiles(req) - return res.status(HttpStatusCode.CONFLICT_409) - .json({ error: 'Torrent/magnet URI import is not enabled on this instance.' }) + + return res.fail({ + status: HttpStatusCode.CONFLICT_409, + message: 'Torrent/magnet URI import is not enabled on this instance.' + }) } if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) @@ -61,8 +69,20 @@ const videoImportAddValidator = getCommonVideoEditAttributes().concat([ if (!req.body.targetUrl && !req.body.magnetUri && !torrentFile) { cleanUpReqFiles(req) - return res.status(HttpStatusCode.BAD_REQUEST_400) - .json({ error: 'Should have a magnetUri or a targetUrl or a torrent file.' }) + return res.fail({ message: 'Should have a magnetUri or a targetUrl or a torrent file.' }) + } + + if (req.body.targetUrl) { + const hostname = new URL(req.body.targetUrl).hostname + + if (await isResolvingToUnicastOnly(hostname) !== true) { + cleanUpReqFiles(req) + + return res.fail({ + status: HttpStatusCode.FORBIDDEN_403, + message: 'Cannot use non unicast IP as targetUrl.' + }) + } } if (!await isImportAccepted(req, res)) return cleanUpReqFiles(req) @@ -71,10 +91,67 @@ const videoImportAddValidator = getCommonVideoEditAttributes().concat([ } ]) +const getMyVideoImportsValidator = [ + query('videoChannelSyncId') + .optional() + .custom(isIdValid), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + if (areValidationErrors(req, res)) return + + return next() + } +] + +const videoImportDeleteValidator = [ + param('id') + .custom(isIdValid), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + if (areValidationErrors(req, res)) return + + if (!await doesVideoImportExist(parseInt(req.params.id), res)) return + if (!checkUserCanManageImport(res.locals.oauth.token.user, res.locals.videoImport, res)) return + + if (res.locals.videoImport.state === VideoImportState.PENDING) { + return res.fail({ + status: HttpStatusCode.CONFLICT_409, + message: 'Cannot delete a pending video import. Cancel it or wait for the end of the import first.' + }) + } + + return next() + } +] + +const videoImportCancelValidator = [ + param('id') + .custom(isIdValid), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + if (areValidationErrors(req, res)) return + + if (!await doesVideoImportExist(forceNumber(req.params.id), res)) return + if (!checkUserCanManageImport(res.locals.oauth.token.user, res.locals.videoImport, res)) return + + if (res.locals.videoImport.state !== VideoImportState.PENDING) { + return res.fail({ + status: HttpStatusCode.CONFLICT_409, + message: 'Cannot cancel a non pending video import.' + }) + } + + return next() + } +] + // --------------------------------------------------------------------------- export { - videoImportAddValidator + videoImportAddValidator, + videoImportCancelValidator, + videoImportDeleteValidator, + getMyVideoImportsValidator } // --------------------------------------------------------------------------- @@ -98,9 +175,23 @@ async function isImportAccepted (req: express.Request, res: express.Response) { if (!acceptedResult || acceptedResult.accepted !== true) { logger.info('Refused to import video.', { acceptedResult, acceptParameters }) - res.status(HttpStatusCode.FORBIDDEN_403) - .json({ error: acceptedResult.errorMessage || 'Refused to import video' }) + res.fail({ + status: HttpStatusCode.FORBIDDEN_403, + message: acceptedResult.errorMessage || 'Refused to import video' + }) + return false + } + + return true +} + +function checkUserCanManageImport (user: MUserAccountId, videoImport: MVideoImport, res: express.Response) { + if (user.hasRight(UserRight.MANAGE_VIDEO_IMPORTS) === false && videoImport.userId !== user.id) { + res.fail({ + status: HttpStatusCode.FORBIDDEN_403, + message: 'Cannot manage video import of another user' + }) return false }