X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fcustom-validators%2Factivitypub%2Fvideos.ts;h=cb385b07d2b18d4279562e809ff9abf2ded27920;hb=af4ae64f6faf38f8179f2e07d3cd4ad60006be92;hp=0f34aab213eb5be8f6057c549310a75d8509895d;hpb=73471b1a52f242e86364ffb077ea6cadb3b07ae2;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/custom-validators/activitypub/videos.ts b/server/helpers/custom-validators/activitypub/videos.ts index 0f34aab21..cb385b07d 100644 --- a/server/helpers/custom-validators/activitypub/videos.ts +++ b/server/helpers/custom-validators/activitypub/videos.ts @@ -1,7 +1,7 @@ -import * as validator from 'validator' -import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers' +import validator from 'validator' +import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers/constants' import { peertubeTruncate } from '../../core-utils' -import { exists, isBooleanValid, isDateValid, isUUIDValid } from '../misc' +import { exists, isArray, isBooleanValid, isDateValid, isUUIDValid } from '../misc' import { isVideoDurationValid, isVideoNameValid, @@ -12,7 +12,8 @@ import { } from '../videos' import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc' import { VideoState } from '../../../../shared/models/videos' -import { isVideoAbuseReasonValid } from '../video-abuses' +import { logger } from '@server/helpers/logger' +import { ActivityVideoFileMetadataObject } from '@shared/models' function sanitizeAndCheckVideoTorrentUpdateActivity (activity: any) { return isBaseActivityValid(activity, 'Update') && @@ -31,15 +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) && @@ -51,37 +75,46 @@ function sanitizeAndCheckVideoTorrentObject (video: any) { isVideoViewsValid(video.views) && 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) && - 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 }) - ) + ) || + ( + (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') } // --------------------------------------------------------------------------- @@ -90,7 +123,8 @@ export { sanitizeAndCheckVideoTorrentUpdateActivity, isRemoteStringIdentifierValid, sanitizeAndCheckVideoTorrentObject, - isRemoteVideoUrlValid + isRemoteVideoUrlValid, + isAPVideoFileMetadataObject } // --------------------------------------------------------------------------- @@ -112,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) }) @@ -130,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) { @@ -148,7 +191,7 @@ function setValidRemoteVideoUrls (video: any) { function setRemoteVideoTruncatedContent (video: any) { if (video.content) { - video.content = peertubeTruncate(video.content, CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max) + video.content = peertubeTruncate(video.content, { length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max }) } return true