2020-01-07 07:56:07 -06:00
|
|
|
import validator from 'validator'
|
2019-04-11 07:26:41 -05:00
|
|
|
import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers/constants'
|
2018-03-28 04:00:02 -05:00
|
|
|
import { peertubeTruncate } from '../../core-utils'
|
2019-01-29 01:37:25 -06:00
|
|
|
import { exists, isArray, isBooleanValid, isDateValid, isUUIDValid } from '../misc'
|
2017-05-15 15:22:03 -05:00
|
|
|
import {
|
|
|
|
isVideoDurationValid,
|
|
|
|
isVideoNameValid,
|
2018-06-12 13:04:58 -05:00
|
|
|
isVideoStateValid,
|
2017-11-14 03:57:56 -06:00
|
|
|
isVideoTagValid,
|
2017-11-15 09:28:35 -06:00
|
|
|
isVideoTruncatedDescriptionValid,
|
|
|
|
isVideoViewsValid
|
2017-05-15 15:22:03 -05:00
|
|
|
} from '../videos'
|
2017-12-14 10:38:41 -06:00
|
|
|
import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
|
2018-06-12 13:04:58 -05:00
|
|
|
import { VideoState } from '../../../../shared/models/videos'
|
2019-11-15 08:06:03 -06:00
|
|
|
import { logger } from '@server/helpers/logger'
|
2020-03-10 08:49:02 -05:00
|
|
|
import { ActivityVideoFileMetadataObject } from '@shared/models'
|
2017-11-10 07:34:45 -06:00
|
|
|
|
2018-05-09 09:16:22 -05:00
|
|
|
function sanitizeAndCheckVideoTorrentUpdateActivity (activity: any) {
|
2017-11-10 07:34:45 -06:00
|
|
|
return isBaseActivityValid(activity, 'Update') &&
|
2018-05-09 09:16:22 -05:00
|
|
|
sanitizeAndCheckVideoTorrentObject(activity.object)
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|
|
|
|
|
2017-11-15 09:28:35 -06:00
|
|
|
function isActivityPubVideoDurationValid (value: string) {
|
|
|
|
// https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
|
|
|
|
return exists(value) &&
|
|
|
|
typeof value === 'string' &&
|
|
|
|
value.startsWith('PT') &&
|
|
|
|
value.endsWith('S') &&
|
2017-11-16 04:08:25 -06:00
|
|
|
isVideoDurationValid(value.replace(/[^0-9]+/g, ''))
|
2017-11-15 09:28:35 -06:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:16:22 -05:00
|
|
|
function sanitizeAndCheckVideoTorrentObject (video: any) {
|
2018-08-02 08:34:09 -05:00
|
|
|
if (!video || video.type !== 'Video') return false
|
2018-05-11 08:41:54 -05:00
|
|
|
|
2019-11-15 08:06:03 -06:00
|
|
|
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
|
|
|
|
}
|
2020-01-30 04:53:38 -06:00
|
|
|
if (!setValidRemoteIcon(video)) {
|
|
|
|
logger.debug('Video has invalid icons', { video })
|
|
|
|
return false
|
|
|
|
}
|
2018-05-09 09:16:22 -05:00
|
|
|
|
2018-06-12 13:04:58 -05:00
|
|
|
// Default attributes
|
|
|
|
if (!isVideoStateValid(video.state)) video.state = VideoState.PUBLISHED
|
|
|
|
if (!isBooleanValid(video.waitTranscoding)) video.waitTranscoding = false
|
2018-10-08 07:45:22 -05:00
|
|
|
if (!isBooleanValid(video.downloadEnabled)) video.downloadEnabled = true
|
2020-01-29 07:56:07 -06:00
|
|
|
if (!isBooleanValid(video.commentsEnabled)) video.commentsEnabled = false
|
2020-09-17 06:59:02 -05:00
|
|
|
if (!isBooleanValid(video.isLiveBroadcast)) video.isLiveBroadcast = false
|
2020-11-02 08:43:44 -06:00
|
|
|
if (!isBooleanValid(video.liveSaveReplay)) video.liveSaveReplay = false
|
2018-06-12 13:04:58 -05:00
|
|
|
|
2018-05-11 08:41:54 -05:00
|
|
|
return isActivityPubUrlValid(video.id) &&
|
2017-11-10 07:34:45 -06:00
|
|
|
isVideoNameValid(video.name) &&
|
2017-11-15 09:28:35 -06:00
|
|
|
isActivityPubVideoDurationValid(video.duration) &&
|
2017-11-10 07:34:45 -06:00
|
|
|
isUUIDValid(video.uuid) &&
|
2018-04-23 07:39:52 -05:00
|
|
|
(!video.category || isRemoteNumberIdentifierValid(video.category)) &&
|
|
|
|
(!video.licence || isRemoteNumberIdentifierValid(video.licence)) &&
|
|
|
|
(!video.language || isRemoteStringIdentifierValid(video.language)) &&
|
2017-11-16 04:08:25 -06:00
|
|
|
isVideoViewsValid(video.views) &&
|
2018-01-26 10:25:35 -06:00
|
|
|
isBooleanValid(video.sensitive) &&
|
2018-01-03 03:12:36 -06:00
|
|
|
isBooleanValid(video.commentsEnabled) &&
|
2018-10-08 07:45:22 -05:00
|
|
|
isBooleanValid(video.downloadEnabled) &&
|
2017-11-10 07:34:45 -06:00
|
|
|
isDateValid(video.published) &&
|
|
|
|
isDateValid(video.updated) &&
|
2019-02-11 07:41:55 -06:00
|
|
|
(!video.originallyPublishedAt || isDateValid(video.originallyPublishedAt)) &&
|
2017-12-08 10:31:21 -06:00
|
|
|
(!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) &&
|
2017-12-14 10:38:41 -06:00
|
|
|
video.attributedTo.length !== 0
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|
|
|
|
|
2018-09-11 09:27:07 -05:00
|
|
|
function isRemoteVideoUrlValid (url: any) {
|
|
|
|
return url.type === 'Link' &&
|
|
|
|
(
|
2020-02-28 09:03:39 -06:00
|
|
|
ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.includes(url.mediaType) &&
|
2018-09-11 09:27:07 -05:00
|
|
|
isActivityPubUrlValid(url.href) &&
|
|
|
|
validator.isInt(url.height + '', { min: 0 }) &&
|
|
|
|
validator.isInt(url.size + '', { min: 0 }) &&
|
2018-10-01 09:27:47 -05:00
|
|
|
(!url.fps || validator.isInt(url.fps + '', { min: -1 }))
|
2018-09-11 09:27:07 -05:00
|
|
|
) ||
|
|
|
|
(
|
2020-02-28 09:03:39 -06:00
|
|
|
ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.includes(url.mediaType) &&
|
2018-09-11 09:27:07 -05:00
|
|
|
isActivityPubUrlValid(url.href) &&
|
|
|
|
validator.isInt(url.height + '', { min: 0 })
|
|
|
|
) ||
|
|
|
|
(
|
2020-02-28 09:03:39 -06:00
|
|
|
ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.includes(url.mediaType) &&
|
2018-09-11 09:27:07 -05:00
|
|
|
validator.isLength(url.href, { min: 5 }) &&
|
|
|
|
validator.isInt(url.height + '', { min: 0 })
|
2019-01-29 01:37:25 -06:00
|
|
|
) ||
|
|
|
|
(
|
|
|
|
(url.mediaType || url.mimeType) === 'application/x-mpegURL' &&
|
|
|
|
isActivityPubUrlValid(url.href) &&
|
|
|
|
isArray(url.tag)
|
2020-03-10 08:49:02 -05:00
|
|
|
) ||
|
|
|
|
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')
|
2018-09-11 09:27:07 -05:00
|
|
|
}
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2018-05-09 09:16:22 -05:00
|
|
|
sanitizeAndCheckVideoTorrentUpdateActivity,
|
2018-04-23 07:39:52 -05:00
|
|
|
isRemoteStringIdentifierValid,
|
2018-09-11 09:27:07 -05:00
|
|
|
sanitizeAndCheckVideoTorrentObject,
|
2020-03-10 08:49:02 -05:00
|
|
|
isRemoteVideoUrlValid,
|
|
|
|
isAPVideoFileMetadataObject
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-11-10 07:34:45 -06:00
|
|
|
function setValidRemoteTags (video: any) {
|
|
|
|
if (Array.isArray(video.tag) === false) return false
|
2017-05-15 15:22:03 -05:00
|
|
|
|
2017-11-27 10:30:46 -06:00
|
|
|
video.tag = video.tag.filter(t => {
|
2017-11-10 07:34:45 -06:00
|
|
|
return t.type === 'Hashtag' &&
|
|
|
|
isVideoTagValid(t.name)
|
|
|
|
})
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-11-10 07:34:45 -06:00
|
|
|
return true
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2018-07-12 12:02:00 -05:00
|
|
|
function setValidRemoteCaptions (video: any) {
|
|
|
|
if (!video.subtitleLanguage) video.subtitleLanguage = []
|
|
|
|
|
|
|
|
if (Array.isArray(video.subtitleLanguage) === false) return false
|
|
|
|
|
|
|
|
video.subtitleLanguage = video.subtitleLanguage.filter(caption => {
|
2020-01-30 04:53:38 -06:00
|
|
|
if (!isActivityPubUrlValid(caption.url)) caption.url = null
|
|
|
|
|
2018-07-12 12:02:00 -05:00
|
|
|
return isRemoteStringIdentifierValid(caption)
|
|
|
|
})
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-04-23 07:39:52 -05:00
|
|
|
function isRemoteNumberIdentifierValid (data: any) {
|
2017-11-10 07:34:45 -06:00
|
|
|
return validator.isInt(data.identifier, { min: 0 })
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2018-04-23 07:39:52 -05:00
|
|
|
function isRemoteStringIdentifierValid (data: any) {
|
|
|
|
return typeof data.identifier === 'string'
|
|
|
|
}
|
|
|
|
|
2017-11-10 07:34:45 -06:00
|
|
|
function isRemoteVideoContentValid (mediaType: string, content: string) {
|
|
|
|
return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content)
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2020-01-30 04:53:38 -06:00
|
|
|
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
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2017-11-10 07:34:45 -06:00
|
|
|
function setValidRemoteVideoUrls (video: any) {
|
|
|
|
if (Array.isArray(video.url) === false) return false
|
2017-05-15 15:22:03 -05:00
|
|
|
|
2017-11-27 10:30:46 -06:00
|
|
|
video.url = video.url.filter(u => isRemoteVideoUrlValid(u))
|
2017-05-15 15:22:03 -05:00
|
|
|
|
2017-11-10 07:34:45 -06:00
|
|
|
return true
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|
|
|
|
|
2018-03-28 06:45:24 -05:00
|
|
|
function setRemoteVideoTruncatedContent (video: any) {
|
2018-03-28 04:00:02 -05:00
|
|
|
if (video.content) {
|
2019-10-21 02:48:21 -05:00
|
|
|
video.content = peertubeTruncate(video.content, { length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max })
|
2018-03-28 04:00:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|