X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fcustom-validators%2Factivitypub%2Fvideos.ts;h=fe94bd58a72a10adf599f018a9b5c235bb0ee2c1;hb=7cde3b9c2e84ea20bb0aae4544598483cde9e22c;hp=2ed2988f55e04fd393a0b123f4a34ed2e604c222;hpb=fada8d75550dc7365f7e18ee1569b9406251d660;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/custom-validators/activitypub/videos.ts b/server/helpers/custom-validators/activitypub/videos.ts index 2ed2988f5..fe94bd58a 100644 --- a/server/helpers/custom-validators/activitypub/videos.ts +++ b/server/helpers/custom-validators/activitypub/videos.ts @@ -1,36 +1,22 @@ -import * as validator from 'validator' -import { ACTIVITY_PUB } from '../../../initializers' -import { exists, isDateValid, isUUIDValid } from '../misc' +import validator from 'validator' +import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers/constants' +import { peertubeTruncate } from '../../core-utils' +import { exists, isArray, isBooleanValid, isDateValid, isUUIDValid } from '../misc' import { - isVideoAbuseReasonValid, isVideoDurationValid, isVideoNameValid, - isVideoNSFWValid, + isVideoStateValid, isVideoTagValid, isVideoTruncatedDescriptionValid, isVideoViewsValid } from '../videos' -import { isActivityPubUrlValid, isBaseActivityValid } from './misc' +import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc' +import { VideoState } from '../../../../shared/models/videos' +import { logger } from '@server/helpers/logger' -function isVideoTorrentAddActivityValid (activity: any) { - return isBaseActivityValid(activity, 'Add') && - isVideoTorrentObjectValid(activity.object) -} - -function isVideoTorrentUpdateActivityValid (activity: any) { +function sanitizeAndCheckVideoTorrentUpdateActivity (activity: any) { return isBaseActivityValid(activity, 'Update') && - isVideoTorrentObjectValid(activity.object) -} - -function isVideoTorrentDeleteActivityValid (activity: any) { - return isBaseActivityValid(activity, 'Delete') -} - -function isVideoFlagValid (activity: any) { - return isBaseActivityValid(activity, 'Create') && - activity.object.type === 'Flag' && - isVideoAbuseReasonValid(activity.object.content) && - isActivityPubUrlValid(activity.object.object) + sanitizeAndCheckVideoTorrentObject(activity.object) } function isActivityPubVideoDurationValid (value: string) { @@ -42,33 +28,88 @@ function isActivityPubVideoDurationValid (value: string) { isVideoDurationValid(value.replace(/[^0-9]+/g, '')) } -function isVideoTorrentObjectValid (video: any) { - return video.type === 'Video' && - isActivityPubUrlValid(video.id) && +function sanitizeAndCheckVideoTorrentObject (video: any) { + if (!video || video.type !== 'Video') return false + + if (!setValidRemoteTags(video)) { + logger.debug('Video has invalid tags', { video }) + return false + } + if (!setValidRemoteVideoUrls(video)) { + logger.debug('Video has invalid urls', { video }) + return false + } + if (!setRemoteVideoTruncatedContent(video)) { + logger.debug('Video has invalid content', { video }) + return false + } + if (!setValidAttributedTo(video)) { + logger.debug('Video has invalid attributedTo', { video }) + return false + } + if (!setValidRemoteCaptions(video)) { + logger.debug('Video has invalid captions', { video }) + return false + } + + // Default attributes + if (!isVideoStateValid(video.state)) video.state = VideoState.PUBLISHED + if (!isBooleanValid(video.waitTranscoding)) video.waitTranscoding = false + if (!isBooleanValid(video.downloadEnabled)) video.downloadEnabled = true + + return isActivityPubUrlValid(video.id) && isVideoNameValid(video.name) && isActivityPubVideoDurationValid(video.duration) && isUUIDValid(video.uuid) && - setValidRemoteTags(video) && - (!video.category || isRemoteIdentifierValid(video.category)) && - (!video.licence || isRemoteIdentifierValid(video.licence)) && - (!video.language || isRemoteIdentifierValid(video.language)) && + (!video.category || isRemoteNumberIdentifierValid(video.category)) && + (!video.licence || isRemoteNumberIdentifierValid(video.licence)) && + (!video.language || isRemoteStringIdentifierValid(video.language)) && isVideoViewsValid(video.views) && - isVideoNSFWValid(video.nsfw) && + isBooleanValid(video.sensitive) && + isBooleanValid(video.commentsEnabled) && + isBooleanValid(video.downloadEnabled) && isDateValid(video.published) && isDateValid(video.updated) && + (!video.originallyPublishedAt || isDateValid(video.originallyPublishedAt)) && (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) && isRemoteVideoIconValid(video.icon) && - setValidRemoteVideoUrls(video) && - video.url.length !== 0 + video.url.length !== 0 && + video.attributedTo.length !== 0 +} + +function isRemoteVideoUrlValid (url: any) { + return url.type === 'Link' && + ( + ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mediaType) !== -1 && + isActivityPubUrlValid(url.href) && + validator.isInt(url.height + '', { min: 0 }) && + validator.isInt(url.size + '', { min: 0 }) && + (!url.fps || validator.isInt(url.fps + '', { min: -1 })) + ) || + ( + ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mediaType) !== -1 && + isActivityPubUrlValid(url.href) && + validator.isInt(url.height + '', { min: 0 }) + ) || + ( + ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mediaType) !== -1 && + validator.isLength(url.href, { min: 5 }) && + validator.isInt(url.height + '', { min: 0 }) + ) || + ( + (url.mediaType || url.mimeType) === 'application/x-mpegURL' && + isActivityPubUrlValid(url.href) && + isArray(url.tag) + ) } // --------------------------------------------------------------------------- export { - isVideoTorrentAddActivityValid, - isVideoTorrentUpdateActivityValid, - isVideoTorrentDeleteActivityValid, - isVideoFlagValid + sanitizeAndCheckVideoTorrentUpdateActivity, + isRemoteStringIdentifierValid, + sanitizeAndCheckVideoTorrentObject, + isRemoteVideoUrlValid } // --------------------------------------------------------------------------- @@ -84,10 +125,26 @@ function setValidRemoteTags (video: any) { return true } -function isRemoteIdentifierValid (data: any) { +function setValidRemoteCaptions (video: any) { + if (!video.subtitleLanguage) video.subtitleLanguage = [] + + if (Array.isArray(video.subtitleLanguage) === false) return false + + video.subtitleLanguage = video.subtitleLanguage.filter(caption => { + return isRemoteStringIdentifierValid(caption) + }) + + return true +} + +function isRemoteNumberIdentifierValid (data: any) { return validator.isInt(data.identifier, { min: 0 }) } +function isRemoteStringIdentifierValid (data: any) { + return typeof data.identifier === 'string' +} + function isRemoteVideoContentValid (mediaType: string, content: string) { return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content) } @@ -108,22 +165,10 @@ function setValidRemoteVideoUrls (video: any) { return true } -function isRemoteVideoUrlValid (url: any) { - return url.type === 'Link' && - ( - ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mimeType) !== -1 && - isActivityPubUrlValid(url.url) && - validator.isInt(url.width + '', { min: 0 }) && - validator.isInt(url.size + '', { min: 0 }) - ) || - ( - ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mimeType) !== -1 && - isActivityPubUrlValid(url.url) && - validator.isInt(url.width + '', { min: 0 }) - ) || - ( - ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mimeType) !== -1 && - validator.isLength(url.url, { min: 5 }) && - validator.isInt(url.width + '', { min: 0 }) - ) +function setRemoteVideoTruncatedContent (video: any) { + if (video.content) { + video.content = peertubeTruncate(video.content, { length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max }) + } + + return true }