X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fcustom-validators%2Factivitypub%2Fvideos.ts;h=cb385b07d2b18d4279562e809ff9abf2ded27920;hb=af4ae64f6faf38f8179f2e07d3cd4ad60006be92;hp=02f91432699dda2c53e08eecad74c2e69594ff66;hpb=687c6180bc5f48b9159bbb229ec5404cc205919e;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/custom-validators/activitypub/videos.ts b/server/helpers/custom-validators/activitypub/videos.ts index 02f914326..cb385b07d 100644 --- a/server/helpers/custom-validators/activitypub/videos.ts +++ b/server/helpers/custom-validators/activitypub/videos.ts @@ -1,4 +1,4 @@ -import * as validator from 'validator' +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' @@ -12,6 +12,8 @@ import { } from '../videos' import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc' import { VideoState } from '../../../../shared/models/videos' +import { logger } from '@server/helpers/logger' +import { ActivityVideoFileMetadataObject } from '@shared/models' function sanitizeAndCheckVideoTorrentUpdateActivity (activity: any) { return isBaseActivityValid(activity, 'Update') && @@ -30,16 +32,38 @@ function isActivityPubVideoDurationValid (value: string) { function sanitizeAndCheckVideoTorrentObject (video: any) { if (!video || video.type !== 'Video') return false - if (!setValidRemoteTags(video)) return false - if (!setValidRemoteVideoUrls(video)) return false - if (!setRemoteVideoTruncatedContent(video)) return false - if (!setValidAttributedTo(video)) return false - if (!setValidRemoteCaptions(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 + } + if (!setValidRemoteIcon(video)) { + logger.debug('Video has invalid icons', { 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 + if (!isBooleanValid(video.commentsEnabled)) video.commentsEnabled = false + if (!isBooleanValid(video.isLiveBroadcast)) video.isLiveBroadcast = false + if (!isBooleanValid(video.liveSaveReplay)) video.liveSaveReplay = false return isActivityPubUrlValid(video.id) && isVideoNameValid(video.name) && @@ -56,31 +80,25 @@ function sanitizeAndCheckVideoTorrentObject (video: any) { isDateValid(video.updated) && (!video.originallyPublishedAt || isDateValid(video.originallyPublishedAt)) && (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) && - isRemoteVideoIconValid(video.icon) && - video.url.length !== 0 && video.attributedTo.length !== 0 } function isRemoteVideoUrlValid (url: any) { - // FIXME: Old bug, we used the width to represent the resolution. Remove it in a few release (currently beta.11) - if (url.width && !url.height) url.height = url.width - return url.type === 'Link' && ( - // TODO: remove mimeType (backward compatibility, introduced in v1.1.0) - ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mediaType || url.mimeType) !== -1 && + ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.includes(url.mediaType) && 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 || url.mimeType) !== -1 && + ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.includes(url.mediaType) && isActivityPubUrlValid(url.href) && validator.isInt(url.height + '', { min: 0 }) ) || ( - ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mediaType || url.mimeType) !== -1 && + ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.includes(url.mediaType) && validator.isLength(url.href, { min: 5 }) && validator.isInt(url.height + '', { min: 0 }) ) || @@ -88,7 +106,15 @@ function isRemoteVideoUrlValid (url: any) { (url.mediaType || url.mimeType) === 'application/x-mpegURL' && isActivityPubUrlValid(url.href) && isArray(url.tag) - ) + ) || + isAPVideoFileMetadataObject(url) +} + +function isAPVideoFileMetadataObject (url: any): url is ActivityVideoFileMetadataObject { + return url && + url.type === 'Link' && + url.mediaType === 'application/json' && + isArray(url.rel) && url.rel.includes('metadata') } // --------------------------------------------------------------------------- @@ -97,7 +123,8 @@ export { sanitizeAndCheckVideoTorrentUpdateActivity, isRemoteStringIdentifierValid, sanitizeAndCheckVideoTorrentObject, - isRemoteVideoUrlValid + isRemoteVideoUrlValid, + isAPVideoFileMetadataObject } // --------------------------------------------------------------------------- @@ -119,6 +146,8 @@ function setValidRemoteCaptions (video: any) { if (Array.isArray(video.subtitleLanguage) === false) return false video.subtitleLanguage = video.subtitleLanguage.filter(caption => { + if (!isActivityPubUrlValid(caption.url)) caption.url = null + return isRemoteStringIdentifierValid(caption) }) @@ -137,12 +166,19 @@ function isRemoteVideoContentValid (mediaType: string, content: string) { return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content) } -function isRemoteVideoIconValid (icon: any) { - return icon.type === 'Image' && - isActivityPubUrlValid(icon.url) && - icon.mediaType === 'image/jpeg' && - validator.isInt(icon.width + '', { min: 0 }) && - validator.isInt(icon.height + '', { min: 0 }) +function setValidRemoteIcon (video: any) { + if (video.icon && !isArray(video.icon)) video.icon = [ video.icon ] + if (!video.icon) video.icon = [] + + video.icon = video.icon.filter(icon => { + return icon.type === 'Image' && + isActivityPubUrlValid(icon.url) && + icon.mediaType === 'image/jpeg' && + validator.isInt(icon.width + '', { min: 0 }) && + validator.isInt(icon.height + '', { min: 0 }) + }) + + return video.icon.length !== 0 } function setValidRemoteVideoUrls (video: any) {