Be tolerant with remote requests

Just remove videos we don't want
This commit is contained in:
Chocobozzz 2017-10-26 10:40:37 +02:00
parent faab3a8453
commit 51c443dbe0
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
4 changed files with 90 additions and 51 deletions

View File

@ -15,9 +15,9 @@ import {
isVideoLikesValid, isVideoLikesValid,
isVideoDislikesValid, isVideoDislikesValid,
isVideoEventCountValid, isVideoEventCountValid,
isVideoCategoryValid, isRemoteVideoCategoryValid,
isVideoLicenceValid, isRemoteVideoLicenceValid,
isVideoLanguageValid, isRemoteVideoLanguageValid,
isVideoNSFWValid, isVideoNSFWValid,
isVideoDescriptionValid, isVideoDescriptionValid,
isVideoDurationValid, isVideoDurationValid,
@ -43,58 +43,64 @@ checkers[ENDPOINT_ACTIONS.REMOVE_CHANNEL] = checkRemoveVideoChannel
checkers[ENDPOINT_ACTIONS.ADD_AUTHOR] = checkAddAuthor checkers[ENDPOINT_ACTIONS.ADD_AUTHOR] = checkAddAuthor
checkers[ENDPOINT_ACTIONS.REMOVE_AUTHOR] = checkRemoveAuthor checkers[ENDPOINT_ACTIONS.REMOVE_AUTHOR] = checkRemoveAuthor
function isEachRemoteRequestVideosValid (requests: any[]) { function removeBadRequestVideos (requests: any[]) {
return isArray(requests) && for (let i = requests.length - 1; i >= 0 ; i--) {
requests.every(request => { const request = requests[i]
const video = request.data const video = request.data
if (!video) return false if (
!video ||
const checker = checkers[request.type] checkers[request.type] === undefined ||
// We don't know the request type checkers[request.type](video) === false
if (checker === undefined) return false ) {
requests.splice(i, 1)
return checker(video) }
}) }
} }
function isEachRemoteRequestVideosQaduValid (requests: any[]) { function removeBadRequestVideosQadu (requests: any[]) {
return isArray(requests) && for (let i = requests.length - 1; i >= 0 ; i--) {
requests.every(request => { const request = requests[i]
const video = request.data const video = request.data
if (!video) return false if (
!video ||
return ( (
isUUIDValid(video.uuid) && isUUIDValid(video.uuid) &&
(has(video, 'views') === false || isVideoViewsValid(video.views)) && (has(video, 'views') === false || isVideoViewsValid(video.views)) &&
(has(video, 'likes') === false || isVideoLikesValid(video.likes)) && (has(video, 'likes') === false || isVideoLikesValid(video.likes)) &&
(has(video, 'dislikes') === false || isVideoDislikesValid(video.dislikes)) (has(video, 'dislikes') === false || isVideoDislikesValid(video.dislikes))
) ) === false
}) ) {
requests.splice(i, 1)
}
}
} }
function isEachRemoteRequestVideosEventsValid (requests: any[]) { function removeBadRequestVideosEvents (requests: any[]) {
return isArray(requests) && for (let i = requests.length - 1; i >= 0 ; i--) {
requests.every(request => { const request = requests[i]
const eventData = request.data const eventData = request.data
if (!eventData) return false if (
!eventData ||
return ( (
isUUIDValid(eventData.uuid) && isUUIDValid(eventData.uuid) &&
values(REQUEST_VIDEO_EVENT_TYPES).indexOf(eventData.eventType) !== -1 && values(REQUEST_VIDEO_EVENT_TYPES).indexOf(eventData.eventType) !== -1 &&
isVideoEventCountValid(eventData.count) isVideoEventCountValid(eventData.count)
) ) === false
}) ) {
requests.splice(i, 1)
}
}
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
export { export {
isEachRemoteRequestVideosValid, removeBadRequestVideos,
isEachRemoteRequestVideosQaduValid, removeBadRequestVideosQadu,
isEachRemoteRequestVideosEventsValid removeBadRequestVideosEvents
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -102,9 +108,9 @@ export {
function isCommonVideoAttributesValid (video: any) { function isCommonVideoAttributesValid (video: any) {
return isDateValid(video.createdAt) && return isDateValid(video.createdAt) &&
isDateValid(video.updatedAt) && isDateValid(video.updatedAt) &&
isVideoCategoryValid(video.category) && isRemoteVideoCategoryValid(video.category) &&
isVideoLicenceValid(video.licence) && isRemoteVideoLicenceValid(video.licence) &&
isVideoLanguageValid(video.language) && isRemoteVideoLanguageValid(video.language) &&
isVideoNSFWValid(video.nsfw) && isVideoNSFWValid(video.nsfw) &&
isVideoDescriptionValid(video.description) && isVideoDescriptionValid(video.description) &&
isVideoDurationValid(video.duration) && isVideoDurationValid(video.duration) &&

View File

@ -27,14 +27,29 @@ function isVideoCategoryValid (value: number) {
return VIDEO_CATEGORIES[value] !== undefined return VIDEO_CATEGORIES[value] !== undefined
} }
// Maybe we don't know the remote category, but that doesn't matter
function isRemoteVideoCategoryValid (value: string) {
return validator.isInt('' + value)
}
function isVideoLicenceValid (value: number) { function isVideoLicenceValid (value: number) {
return VIDEO_LICENCES[value] !== undefined return VIDEO_LICENCES[value] !== undefined
} }
// Maybe we don't know the remote licence, but that doesn't matter
function isRemoteVideoLicenceValid (value: string) {
return validator.isInt('' + value)
}
function isVideoLanguageValid (value: number) { function isVideoLanguageValid (value: number) {
return value === null || VIDEO_LANGUAGES[value] !== undefined return value === null || VIDEO_LANGUAGES[value] !== undefined
} }
// Maybe we don't know the remote language, but that doesn't matter
function isRemoteVideoLanguageValid (value: string) {
return validator.isInt('' + value)
}
function isVideoNSFWValid (value: any) { function isVideoNSFWValid (value: any) {
return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value)) return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
} }
@ -176,5 +191,8 @@ export {
isVideoEventCountValid, isVideoEventCountValid,
isVideoFileSizeValid, isVideoFileSizeValid,
isVideoFileResolutionValid, isVideoFileResolutionValid,
checkVideoExists checkVideoExists,
isRemoteVideoCategoryValid,
isRemoteVideoLicenceValid,
isRemoteVideoLanguageValid
} }

View File

@ -88,8 +88,10 @@ abstract class AbstractRequestScheduler <T> {
// The function fire some useful callbacks // The function fire some useful callbacks
try { try {
const { response } = await makeSecureRequest(params) const { response } = await makeSecureRequest(params)
if (response.statusCode !== 200 && response.statusCode !== 201 && response.statusCode !== 204) {
throw new Error('Status code not 20x : ' + response.statusCode) // 400 because if the other pod is not up to date, it may not understand our request
if ([ 200, 201, 204, 400 ].indexOf(response.statusCode) === -1) {
throw new Error('Status code not 20x or 400 : ' + response.statusCode)
} }
} catch (err) { } catch (err) {
logger.error('Error sending secure request to %s pod.', toPod.host, err) logger.error('Error sending secure request to %s pod.', toPod.host, err)

View File

@ -3,39 +3,52 @@ import * as express from 'express'
import { import {
logger, logger,
isEachRemoteRequestVideosValid, isArray,
isEachRemoteRequestVideosQaduValid, removeBadRequestVideos,
isEachRemoteRequestVideosEventsValid removeBadRequestVideosQadu,
removeBadRequestVideosEvents
} from '../../../helpers' } from '../../../helpers'
import { checkErrors } from '../utils' import { checkErrors } from '../utils'
const remoteVideosValidator = [ const remoteVideosValidator = [
body('data').custom(isEachRemoteRequestVideosValid), body('data').custom(isArray),
(req: express.Request, res: express.Response, next: express.NextFunction) => { (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking remoteVideos parameters', { parameters: req.body }) logger.debug('Checking remoteVideos parameters', { parameters: req.body })
checkErrors(req, res, next) checkErrors(req, res, () => {
removeBadRequestVideos(req.body.data)
return next()
})
} }
] ]
const remoteQaduVideosValidator = [ const remoteQaduVideosValidator = [
body('data').custom(isEachRemoteRequestVideosQaduValid), body('data').custom(isArray),
(req: express.Request, res: express.Response, next: express.NextFunction) => { (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking remoteQaduVideos parameters', { parameters: req.body }) logger.debug('Checking remoteQaduVideos parameters', { parameters: req.body })
checkErrors(req, res, next) checkErrors(req, res, () => {
removeBadRequestVideosQadu(req.body.data)
return next()
})
} }
] ]
const remoteEventsVideosValidator = [ const remoteEventsVideosValidator = [
body('data').custom(isEachRemoteRequestVideosEventsValid), body('data').custom(isArray),
(req: express.Request, res: express.Response, next: express.NextFunction) => { (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking remoteEventsVideos parameters', { parameters: req.body }) logger.debug('Checking remoteEventsVideos parameters', { parameters: req.body })
checkErrors(req, res, next) checkErrors(req, res, () => {
removeBadRequestVideosEvents(req.body.data)
return next()
})
} }
] ]