PeerTube/server/helpers/custom-validators/activitypub/videos.ts

210 lines
6.8 KiB
TypeScript
Raw Normal View History

2020-01-07 14:56:07 +01:00
import validator from 'validator'
2021-02-18 10:15:11 +01:00
import { logger } from '@server/helpers/logger'
import { ActivityTrackerUrlObject, ActivityVideoFileMetadataUrlObject } from '@shared/models'
import { VideoState } from '../../../../shared/models/videos'
import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers/constants'
2018-03-28 11:00:02 +02:00
import { peertubeTruncate } from '../../core-utils'
2019-01-29 08:37:25 +01:00
import { exists, isArray, isBooleanValid, isDateValid, isUUIDValid } from '../misc'
2017-05-15 22:22:03 +02:00
import {
isVideoDurationValid,
isVideoNameValid,
isVideoStateValid,
2017-11-14 10:57:56 +01:00
isVideoTagValid,
2017-11-15 16:28:35 +01:00
isVideoTruncatedDescriptionValid,
isVideoViewsValid
2017-05-15 22:22:03 +02:00
} from '../videos'
2017-12-14 17:38:41 +01:00
import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
2017-11-10 14:34:45 +01:00
function sanitizeAndCheckVideoTorrentUpdateActivity (activity: any) {
2017-11-10 14:34:45 +01:00
return isBaseActivityValid(activity, 'Update') &&
sanitizeAndCheckVideoTorrentObject(activity.object)
2017-05-15 22:22:03 +02:00
}
2017-11-15 16:28:35 +01: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') &&
isVideoDurationValid(value.replace(/[^0-9]+/g, ''))
2017-11-15 16:28:35 +01:00
}
function sanitizeAndCheckVideoTorrentObject (video: any) {
if (!video || video.type !== 'Video') return false
2018-05-11 15:41:54 +02: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
}
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
2020-09-17 13:59:02 +02:00
if (!isBooleanValid(video.isLiveBroadcast)) video.isLiveBroadcast = false
2020-11-02 15:43:44 +01:00
if (!isBooleanValid(video.liveSaveReplay)) video.liveSaveReplay = false
2020-12-03 14:10:54 +01:00
if (!isBooleanValid(video.permanentLive)) video.permanentLive = false
2018-05-11 15:41:54 +02:00
return isActivityPubUrlValid(video.id) &&
2017-11-10 14:34:45 +01:00
isVideoNameValid(video.name) &&
2017-11-15 16:28:35 +01:00
isActivityPubVideoDurationValid(video.duration) &&
2017-11-10 14:34:45 +01:00
isUUIDValid(video.uuid) &&
2018-04-23 14:39:52 +02:00
(!video.category || isRemoteNumberIdentifierValid(video.category)) &&
(!video.licence || isRemoteNumberIdentifierValid(video.licence)) &&
(!video.language || isRemoteStringIdentifierValid(video.language)) &&
isVideoViewsValid(video.views) &&
isBooleanValid(video.sensitive) &&
2017-11-10 14:34:45 +01:00
isDateValid(video.published) &&
isDateValid(video.updated) &&
2019-02-11 14:41:55 +01:00
(!video.originallyPublishedAt || isDateValid(video.originallyPublishedAt)) &&
2017-12-08 17:31:21 +01:00
(!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) &&
2017-12-14 17:38:41 +01:00
video.attributedTo.length !== 0
2017-05-15 22:22:03 +02:00
}
2018-09-11 16:27:07 +02:00
function isRemoteVideoUrlValid (url: any) {
return url.type === 'Link' &&
2021-02-18 10:15:11 +01:00
// Video file link
2018-09-11 16:27:07 +02:00
(
2020-02-28 16:03:39 +01:00
ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.includes(url.mediaType) &&
2018-09-11 16:27:07 +02:00
isActivityPubUrlValid(url.href) &&
validator.isInt(url.height + '', { min: 0 }) &&
validator.isInt(url.size + '', { min: 0 }) &&
2018-10-01 16:27:47 +02:00
(!url.fps || validator.isInt(url.fps + '', { min: -1 }))
2018-09-11 16:27:07 +02:00
) ||
2021-02-18 10:15:11 +01:00
// Torrent link
2018-09-11 16:27:07 +02:00
(
2020-02-28 16:03:39 +01:00
ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.includes(url.mediaType) &&
2018-09-11 16:27:07 +02:00
isActivityPubUrlValid(url.href) &&
validator.isInt(url.height + '', { min: 0 })
) ||
2021-02-18 10:15:11 +01:00
// Magnet link
2018-09-11 16:27:07 +02:00
(
2020-02-28 16:03:39 +01:00
ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.includes(url.mediaType) &&
2018-09-11 16:27:07 +02:00
validator.isLength(url.href, { min: 5 }) &&
validator.isInt(url.height + '', { min: 0 })
2019-01-29 08:37:25 +01:00
) ||
2021-02-18 10:15:11 +01:00
// HLS playlist link
2019-01-29 08:37:25 +01:00
(
(url.mediaType || url.mimeType) === 'application/x-mpegURL' &&
isActivityPubUrlValid(url.href) &&
isArray(url.tag)
2020-03-10 14:49:02 +01:00
) ||
2021-02-18 10:15:11 +01:00
isAPVideoTrackerUrlObject(url) ||
isAPVideoFileUrlMetadataObject(url)
2020-03-10 14:49:02 +01:00
}
2021-02-18 10:15:11 +01:00
function isAPVideoFileUrlMetadataObject (url: any): url is ActivityVideoFileMetadataUrlObject {
2020-03-10 14:49:02 +01:00
return url &&
url.type === 'Link' &&
url.mediaType === 'application/json' &&
isArray(url.rel) && url.rel.includes('metadata')
2018-09-11 16:27:07 +02:00
}
2021-02-18 10:15:11 +01:00
function isAPVideoTrackerUrlObject (url: any): url is ActivityTrackerUrlObject {
return isArray(url.rel) &&
url.rel.includes('tracker') &&
isActivityPubUrlValid(url.href)
}
2017-05-15 22:22:03 +02:00
// ---------------------------------------------------------------------------
export {
sanitizeAndCheckVideoTorrentUpdateActivity,
2018-04-23 14:39:52 +02:00
isRemoteStringIdentifierValid,
2018-09-11 16:27:07 +02:00
sanitizeAndCheckVideoTorrentObject,
2020-03-10 14:49:02 +01:00
isRemoteVideoUrlValid,
2021-02-18 10:15:11 +01:00
isAPVideoFileUrlMetadataObject,
isAPVideoTrackerUrlObject
2017-05-15 22:22:03 +02:00
}
// ---------------------------------------------------------------------------
2017-11-10 14:34:45 +01:00
function setValidRemoteTags (video: any) {
if (Array.isArray(video.tag) === false) return false
2017-05-15 22:22:03 +02:00
2017-11-27 17:30:46 +01:00
video.tag = video.tag.filter(t => {
2017-11-10 14:34:45 +01:00
return t.type === 'Hashtag' &&
isVideoTagValid(t.name)
})
2017-10-24 19:41:09 +02:00
2017-11-10 14:34:45 +01:00
return true
2017-10-24 19:41:09 +02:00
}
2018-07-12 19:02:00 +02:00
function setValidRemoteCaptions (video: any) {
if (!video.subtitleLanguage) video.subtitleLanguage = []
if (Array.isArray(video.subtitleLanguage) === false) return false
video.subtitleLanguage = video.subtitleLanguage.filter(caption => {
if (!isActivityPubUrlValid(caption.url)) caption.url = null
2018-07-12 19:02:00 +02:00
return isRemoteStringIdentifierValid(caption)
})
return true
}
2018-04-23 14:39:52 +02:00
function isRemoteNumberIdentifierValid (data: any) {
2017-11-10 14:34:45 +01:00
return validator.isInt(data.identifier, { min: 0 })
2017-10-24 19:41:09 +02:00
}
2018-04-23 14:39:52 +02:00
function isRemoteStringIdentifierValid (data: any) {
return typeof data.identifier === 'string'
}
2017-11-10 14:34:45 +01:00
function isRemoteVideoContentValid (mediaType: string, content: string) {
return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content)
2017-10-24 19:41:09 +02: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 19:41:09 +02:00
}
2017-11-10 14:34:45 +01:00
function setValidRemoteVideoUrls (video: any) {
if (Array.isArray(video.url) === false) return false
2017-05-15 22:22:03 +02:00
2017-11-27 17:30:46 +01:00
video.url = video.url.filter(u => isRemoteVideoUrlValid(u))
2017-05-15 22:22:03 +02:00
2017-11-10 14:34:45 +01:00
return true
2017-05-15 22:22:03 +02:00
}
2018-03-28 13:45:24 +02:00
function setRemoteVideoTruncatedContent (video: any) {
2018-03-28 11:00:02 +02:00
if (video.content) {
video.content = peertubeTruncate(video.content, { length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max })
2018-03-28 11:00:02 +02:00
}
return true
}