diff options
author | Chocobozzz <me@florianbigard.com> | 2019-02-11 11:52:34 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-02-11 11:52:34 +0100 |
commit | 88108880bbdba473cfe36ecbebc1c3c4f972e102 (patch) | |
tree | b242efb3b4f0d7e49d88f2d1f2063b5b3b0489c0 /server/middlewares/validators/videos/videos.ts | |
parent | 53a94c7cfa8368da4cd248d65df8346905938f0c (diff) | |
parent | 9b712a2017e4ab3cf12cd6bd58278905520159d0 (diff) | |
download | PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.tar.gz PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.tar.zst PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.zip |
Merge branch 'develop' into pr/1217
Diffstat (limited to 'server/middlewares/validators/videos/videos.ts')
-rw-r--r-- | server/middlewares/validators/videos/videos.ts | 165 |
1 files changed, 101 insertions, 64 deletions
diff --git a/server/middlewares/validators/videos/videos.ts b/server/middlewares/validators/videos/videos.ts index 27e8a7449..d9626929c 100644 --- a/server/middlewares/validators/videos/videos.ts +++ b/server/middlewares/validators/videos/videos.ts | |||
@@ -1,6 +1,6 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import 'express-validator' | 2 | import 'express-validator' |
3 | import { body, param, ValidationChain } from 'express-validator/check' | 3 | import { body, param, query, ValidationChain } from 'express-validator/check' |
4 | import { UserRight, VideoChangeOwnershipStatus, VideoPrivacy } from '../../../../shared' | 4 | import { UserRight, VideoChangeOwnershipStatus, VideoPrivacy } from '../../../../shared' |
5 | import { | 5 | import { |
6 | isBooleanValid, | 6 | isBooleanValid, |
@@ -8,6 +8,7 @@ import { | |||
8 | isIdOrUUIDValid, | 8 | isIdOrUUIDValid, |
9 | isIdValid, | 9 | isIdValid, |
10 | isUUIDValid, | 10 | isUUIDValid, |
11 | toArray, | ||
11 | toIntOrNull, | 12 | toIntOrNull, |
12 | toValueOrNull | 13 | toValueOrNull |
13 | } from '../../../helpers/custom-validators/misc' | 14 | } from '../../../helpers/custom-validators/misc' |
@@ -19,20 +20,19 @@ import { | |||
19 | isVideoDescriptionValid, | 20 | isVideoDescriptionValid, |
20 | isVideoExist, | 21 | isVideoExist, |
21 | isVideoFile, | 22 | isVideoFile, |
23 | isVideoFilterValid, | ||
22 | isVideoImage, | 24 | isVideoImage, |
23 | isVideoLanguageValid, | 25 | isVideoLanguageValid, |
24 | isVideoLicenceValid, | 26 | isVideoLicenceValid, |
25 | isVideoNameValid, | 27 | isVideoNameValid, |
26 | isVideoPrivacyValid, | 28 | isVideoPrivacyValid, |
27 | isVideoRatingTypeValid, | ||
28 | isVideoSupportValid, | 29 | isVideoSupportValid, |
29 | isVideoTagsValid | 30 | isVideoTagsValid |
30 | } from '../../../helpers/custom-validators/videos' | 31 | } from '../../../helpers/custom-validators/videos' |
31 | import { getDurationFromVideoFile } from '../../../helpers/ffmpeg-utils' | 32 | import { getDurationFromVideoFile } from '../../../helpers/ffmpeg-utils' |
32 | import { logger } from '../../../helpers/logger' | 33 | import { logger } from '../../../helpers/logger' |
33 | import { CONSTRAINTS_FIELDS } from '../../../initializers' | 34 | import { CONFIG, CONSTRAINTS_FIELDS } from '../../../initializers' |
34 | import { VideoShareModel } from '../../../models/video/video-share' | 35 | import { authenticatePromiseIfNeeded } from '../../oauth' |
35 | import { authenticate } from '../../oauth' | ||
36 | import { areValidationErrors } from '../utils' | 36 | import { areValidationErrors } from '../utils' |
37 | import { cleanUpReqFiles } from '../../../helpers/express-utils' | 37 | import { cleanUpReqFiles } from '../../../helpers/express-utils' |
38 | import { VideoModel } from '../../../models/video/video' | 38 | import { VideoModel } from '../../../models/video/video' |
@@ -42,6 +42,8 @@ import { VideoChangeOwnershipAccept } from '../../../../shared/models/videos/vid | |||
42 | import { VideoChangeOwnershipModel } from '../../../models/video/video-change-ownership' | 42 | import { VideoChangeOwnershipModel } from '../../../models/video/video-change-ownership' |
43 | import { AccountModel } from '../../../models/account/account' | 43 | import { AccountModel } from '../../../models/account/account' |
44 | import { VideoFetchType } from '../../../helpers/video' | 44 | import { VideoFetchType } from '../../../helpers/video' |
45 | import { isNSFWQueryValid, isNumberArray, isStringArray } from '../../../helpers/custom-validators/search' | ||
46 | import { getServerActor } from '../../../helpers/utils' | ||
45 | 47 | ||
46 | const videosAddValidator = getCommonVideoAttributes().concat([ | 48 | const videosAddValidator = getCommonVideoAttributes().concat([ |
47 | body('videofile') | 49 | body('videofile') |
@@ -69,7 +71,6 @@ const videosAddValidator = getCommonVideoAttributes().concat([ | |||
69 | if (isAble === false) { | 71 | if (isAble === false) { |
70 | res.status(403) | 72 | res.status(403) |
71 | .json({ error: 'The user video quota is exceeded with this video.' }) | 73 | .json({ error: 'The user video quota is exceeded with this video.' }) |
72 | .end() | ||
73 | 74 | ||
74 | return cleanUpReqFiles(req) | 75 | return cleanUpReqFiles(req) |
75 | } | 76 | } |
@@ -82,7 +83,6 @@ const videosAddValidator = getCommonVideoAttributes().concat([ | |||
82 | logger.error('Invalid input file in videosAddValidator.', { err }) | 83 | logger.error('Invalid input file in videosAddValidator.', { err }) |
83 | res.status(400) | 84 | res.status(400) |
84 | .json({ error: 'Invalid input file.' }) | 85 | .json({ error: 'Invalid input file.' }) |
85 | .end() | ||
86 | 86 | ||
87 | return cleanUpReqFiles(req) | 87 | return cleanUpReqFiles(req) |
88 | } | 88 | } |
@@ -120,7 +120,6 @@ const videosUpdateValidator = getCommonVideoAttributes().concat([ | |||
120 | cleanUpReqFiles(req) | 120 | cleanUpReqFiles(req) |
121 | return res.status(409) | 121 | return res.status(409) |
122 | .json({ error: 'Cannot set "private" a video that was not private.' }) | 122 | .json({ error: 'Cannot set "private" a video that was not private.' }) |
123 | .end() | ||
124 | } | 123 | } |
125 | 124 | ||
126 | if (req.body.channelId && !await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) | 125 | if (req.body.channelId && !await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) |
@@ -129,6 +128,31 @@ const videosUpdateValidator = getCommonVideoAttributes().concat([ | |||
129 | } | 128 | } |
130 | ]) | 129 | ]) |
131 | 130 | ||
131 | async function checkVideoFollowConstraints (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
132 | const video: VideoModel = res.locals.video | ||
133 | |||
134 | // Anybody can watch local videos | ||
135 | if (video.isOwned() === true) return next() | ||
136 | |||
137 | // Logged user | ||
138 | if (res.locals.oauth) { | ||
139 | // Users can search or watch remote videos | ||
140 | if (CONFIG.SEARCH.REMOTE_URI.USERS === true) return next() | ||
141 | } | ||
142 | |||
143 | // Anybody can search or watch remote videos | ||
144 | if (CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true) return next() | ||
145 | |||
146 | // Check our instance follows an actor that shared this video | ||
147 | const serverActor = await getServerActor() | ||
148 | if (await VideoModel.checkVideoHasInstanceFollow(video.id, serverActor.id) === true) return next() | ||
149 | |||
150 | return res.status(403) | ||
151 | .json({ | ||
152 | error: 'Cannot get this video regarding follow constraints.' | ||
153 | }) | ||
154 | } | ||
155 | |||
132 | const videosCustomGetValidator = (fetchType: VideoFetchType) => { | 156 | const videosCustomGetValidator = (fetchType: VideoFetchType) => { |
133 | return [ | 157 | return [ |
134 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), | 158 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), |
@@ -143,18 +167,20 @@ const videosCustomGetValidator = (fetchType: VideoFetchType) => { | |||
143 | 167 | ||
144 | // Video private or blacklisted | 168 | // Video private or blacklisted |
145 | if (video.privacy === VideoPrivacy.PRIVATE || video.VideoBlacklist) { | 169 | if (video.privacy === VideoPrivacy.PRIVATE || video.VideoBlacklist) { |
146 | return authenticate(req, res, () => { | 170 | await authenticatePromiseIfNeeded(req, res) |
147 | const user: UserModel = res.locals.oauth.token.User | 171 | |
148 | 172 | const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : null | |
149 | // Only the owner or a user that have blacklist rights can see the video | 173 | |
150 | if (video.VideoChannel.Account.userId !== user.id && !user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST)) { | 174 | // Only the owner or a user that have blacklist rights can see the video |
151 | return res.status(403) | 175 | if ( |
152 | .json({ error: 'Cannot get this private or blacklisted video.' }) | 176 | !user || |
153 | .end() | 177 | (video.VideoChannel.Account.userId !== user.id && !user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST)) |
154 | } | 178 | ) { |
155 | 179 | return res.status(403) | |
156 | return next() | 180 | .json({ error: 'Cannot get this private or blacklisted video.' }) |
157 | }) | 181 | } |
182 | |||
183 | return next() | ||
158 | } | 184 | } |
159 | 185 | ||
160 | // Video is public, anyone can access it | 186 | // Video is public, anyone can access it |
@@ -189,41 +215,6 @@ const videosRemoveValidator = [ | |||
189 | } | 215 | } |
190 | ] | 216 | ] |
191 | 217 | ||
192 | const videoRateValidator = [ | ||
193 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), | ||
194 | body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'), | ||
195 | |||
196 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
197 | logger.debug('Checking videoRate parameters', { parameters: req.body }) | ||
198 | |||
199 | if (areValidationErrors(req, res)) return | ||
200 | if (!await isVideoExist(req.params.id, res)) return | ||
201 | |||
202 | return next() | ||
203 | } | ||
204 | ] | ||
205 | |||
206 | const videosShareValidator = [ | ||
207 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), | ||
208 | param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'), | ||
209 | |||
210 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
211 | logger.debug('Checking videoShare parameters', { parameters: req.params }) | ||
212 | |||
213 | if (areValidationErrors(req, res)) return | ||
214 | if (!await isVideoExist(req.params.id, res)) return | ||
215 | |||
216 | const share = await VideoShareModel.load(req.params.accountId, res.locals.video.id, undefined) | ||
217 | if (!share) { | ||
218 | return res.status(404) | ||
219 | .end() | ||
220 | } | ||
221 | |||
222 | res.locals.videoShare = share | ||
223 | return next() | ||
224 | } | ||
225 | ] | ||
226 | |||
227 | const videosChangeOwnershipValidator = [ | 218 | const videosChangeOwnershipValidator = [ |
228 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), | 219 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), |
229 | 220 | ||
@@ -239,8 +230,8 @@ const videosChangeOwnershipValidator = [ | |||
239 | const nextOwner = await AccountModel.loadLocalByName(req.body.username) | 230 | const nextOwner = await AccountModel.loadLocalByName(req.body.username) |
240 | if (!nextOwner) { | 231 | if (!nextOwner) { |
241 | res.status(400) | 232 | res.status(400) |
242 | .type('json') | 233 | .json({ error: 'Changing video ownership to a remote account is not supported yet' }) |
243 | .end() | 234 | |
244 | return | 235 | return |
245 | } | 236 | } |
246 | res.locals.nextOwner = nextOwner | 237 | res.locals.nextOwner = nextOwner |
@@ -271,7 +262,7 @@ const videosTerminateChangeOwnershipValidator = [ | |||
271 | } else { | 262 | } else { |
272 | res.status(403) | 263 | res.status(403) |
273 | .json({ error: 'Ownership already accepted or refused' }) | 264 | .json({ error: 'Ownership already accepted or refused' }) |
274 | .end() | 265 | |
275 | return | 266 | return |
276 | } | 267 | } |
277 | } | 268 | } |
@@ -288,7 +279,7 @@ const videosAcceptChangeOwnershipValidator = [ | |||
288 | if (isAble === false) { | 279 | if (isAble === false) { |
289 | res.status(403) | 280 | res.status(403) |
290 | .json({ error: 'The user video quota is exceeded with this video.' }) | 281 | .json({ error: 'The user video quota is exceeded with this video.' }) |
291 | .end() | 282 | |
292 | return | 283 | return |
293 | } | 284 | } |
294 | 285 | ||
@@ -367,23 +358,68 @@ function getCommonVideoAttributes () { | |||
367 | ] as (ValidationChain | express.Handler)[] | 358 | ] as (ValidationChain | express.Handler)[] |
368 | } | 359 | } |
369 | 360 | ||
361 | const commonVideosFiltersValidator = [ | ||
362 | query('categoryOneOf') | ||
363 | .optional() | ||
364 | .customSanitizer(toArray) | ||
365 | .custom(isNumberArray).withMessage('Should have a valid one of category array'), | ||
366 | query('licenceOneOf') | ||
367 | .optional() | ||
368 | .customSanitizer(toArray) | ||
369 | .custom(isNumberArray).withMessage('Should have a valid one of licence array'), | ||
370 | query('languageOneOf') | ||
371 | .optional() | ||
372 | .customSanitizer(toArray) | ||
373 | .custom(isStringArray).withMessage('Should have a valid one of language array'), | ||
374 | query('tagsOneOf') | ||
375 | .optional() | ||
376 | .customSanitizer(toArray) | ||
377 | .custom(isStringArray).withMessage('Should have a valid one of tags array'), | ||
378 | query('tagsAllOf') | ||
379 | .optional() | ||
380 | .customSanitizer(toArray) | ||
381 | .custom(isStringArray).withMessage('Should have a valid all of tags array'), | ||
382 | query('nsfw') | ||
383 | .optional() | ||
384 | .custom(isNSFWQueryValid).withMessage('Should have a valid NSFW attribute'), | ||
385 | query('filter') | ||
386 | .optional() | ||
387 | .custom(isVideoFilterValid).withMessage('Should have a valid filter attribute'), | ||
388 | |||
389 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
390 | logger.debug('Checking commons video filters query', { parameters: req.query }) | ||
391 | |||
392 | if (areValidationErrors(req, res)) return | ||
393 | |||
394 | const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : undefined | ||
395 | if (req.query.filter === 'all-local' && (!user || user.hasRight(UserRight.SEE_ALL_VIDEOS) === false)) { | ||
396 | res.status(401) | ||
397 | .json({ error: 'You are not allowed to see all local videos.' }) | ||
398 | |||
399 | return | ||
400 | } | ||
401 | |||
402 | return next() | ||
403 | } | ||
404 | ] | ||
405 | |||
370 | // --------------------------------------------------------------------------- | 406 | // --------------------------------------------------------------------------- |
371 | 407 | ||
372 | export { | 408 | export { |
373 | videosAddValidator, | 409 | videosAddValidator, |
374 | videosUpdateValidator, | 410 | videosUpdateValidator, |
375 | videosGetValidator, | 411 | videosGetValidator, |
412 | checkVideoFollowConstraints, | ||
376 | videosCustomGetValidator, | 413 | videosCustomGetValidator, |
377 | videosRemoveValidator, | 414 | videosRemoveValidator, |
378 | videosShareValidator, | ||
379 | |||
380 | videoRateValidator, | ||
381 | 415 | ||
382 | videosChangeOwnershipValidator, | 416 | videosChangeOwnershipValidator, |
383 | videosTerminateChangeOwnershipValidator, | 417 | videosTerminateChangeOwnershipValidator, |
384 | videosAcceptChangeOwnershipValidator, | 418 | videosAcceptChangeOwnershipValidator, |
385 | 419 | ||
386 | getCommonVideoAttributes | 420 | getCommonVideoAttributes, |
421 | |||
422 | commonVideosFiltersValidator | ||
387 | } | 423 | } |
388 | 424 | ||
389 | // --------------------------------------------------------------------------- | 425 | // --------------------------------------------------------------------------- |
@@ -391,9 +427,10 @@ export { | |||
391 | function areErrorsInScheduleUpdate (req: express.Request, res: express.Response) { | 427 | function areErrorsInScheduleUpdate (req: express.Request, res: express.Response) { |
392 | if (req.body.scheduleUpdate) { | 428 | if (req.body.scheduleUpdate) { |
393 | if (!req.body.scheduleUpdate.updateAt) { | 429 | if (!req.body.scheduleUpdate.updateAt) { |
430 | logger.warn('Invalid parameters: scheduleUpdate.updateAt is mandatory.') | ||
431 | |||
394 | res.status(400) | 432 | res.status(400) |
395 | .json({ error: 'Schedule update at is mandatory.' }) | 433 | .json({ error: 'Schedule update at is mandatory.' }) |
396 | .end() | ||
397 | 434 | ||
398 | return true | 435 | return true |
399 | } | 436 | } |