]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/activitypub/videos.ts
Add new abuses tests
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / activitypub / videos.ts
1 import validator from 'validator'
2 import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers/constants'
3 import { peertubeTruncate } from '../../core-utils'
4 import { exists, isArray, isBooleanValid, isDateValid, isUUIDValid } from '../misc'
5 import {
6 isVideoDurationValid,
7 isVideoNameValid,
8 isVideoStateValid,
9 isVideoTagValid,
10 isVideoTruncatedDescriptionValid,
11 isVideoViewsValid
12 } from '../videos'
13 import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
14 import { VideoState } from '../../../../shared/models/videos'
15 import { logger } from '@server/helpers/logger'
16 import { ActivityVideoFileMetadataObject } from '@shared/models'
17
18 function sanitizeAndCheckVideoTorrentUpdateActivity (activity: any) {
19 return isBaseActivityValid(activity, 'Update') &&
20 sanitizeAndCheckVideoTorrentObject(activity.object)
21 }
22
23 function isActivityPubVideoDurationValid (value: string) {
24 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
25 return exists(value) &&
26 typeof value === 'string' &&
27 value.startsWith('PT') &&
28 value.endsWith('S') &&
29 isVideoDurationValid(value.replace(/[^0-9]+/g, ''))
30 }
31
32 function sanitizeAndCheckVideoTorrentObject (video: any) {
33 if (!video || video.type !== 'Video') return false
34
35 if (!setValidRemoteTags(video)) {
36 logger.debug('Video has invalid tags', { video })
37 return false
38 }
39 if (!setValidRemoteVideoUrls(video)) {
40 logger.debug('Video has invalid urls', { video })
41 return false
42 }
43 if (!setRemoteVideoTruncatedContent(video)) {
44 logger.debug('Video has invalid content', { video })
45 return false
46 }
47 if (!setValidAttributedTo(video)) {
48 logger.debug('Video has invalid attributedTo', { video })
49 return false
50 }
51 if (!setValidRemoteCaptions(video)) {
52 logger.debug('Video has invalid captions', { video })
53 return false
54 }
55 if (!setValidRemoteIcon(video)) {
56 logger.debug('Video has invalid icons', { video })
57 return false
58 }
59
60 // Default attributes
61 if (!isVideoStateValid(video.state)) video.state = VideoState.PUBLISHED
62 if (!isBooleanValid(video.waitTranscoding)) video.waitTranscoding = false
63 if (!isBooleanValid(video.downloadEnabled)) video.downloadEnabled = true
64 if (!isBooleanValid(video.commentsEnabled)) video.commentsEnabled = false
65
66 return isActivityPubUrlValid(video.id) &&
67 isVideoNameValid(video.name) &&
68 isActivityPubVideoDurationValid(video.duration) &&
69 isUUIDValid(video.uuid) &&
70 (!video.category || isRemoteNumberIdentifierValid(video.category)) &&
71 (!video.licence || isRemoteNumberIdentifierValid(video.licence)) &&
72 (!video.language || isRemoteStringIdentifierValid(video.language)) &&
73 isVideoViewsValid(video.views) &&
74 isBooleanValid(video.sensitive) &&
75 isBooleanValid(video.commentsEnabled) &&
76 isBooleanValid(video.downloadEnabled) &&
77 isDateValid(video.published) &&
78 isDateValid(video.updated) &&
79 (!video.originallyPublishedAt || isDateValid(video.originallyPublishedAt)) &&
80 (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) &&
81 video.url.length !== 0 &&
82 video.attributedTo.length !== 0
83 }
84
85 function isRemoteVideoUrlValid (url: any) {
86 return url.type === 'Link' &&
87 (
88 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.includes(url.mediaType) &&
89 isActivityPubUrlValid(url.href) &&
90 validator.isInt(url.height + '', { min: 0 }) &&
91 validator.isInt(url.size + '', { min: 0 }) &&
92 (!url.fps || validator.isInt(url.fps + '', { min: -1 }))
93 ) ||
94 (
95 ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.includes(url.mediaType) &&
96 isActivityPubUrlValid(url.href) &&
97 validator.isInt(url.height + '', { min: 0 })
98 ) ||
99 (
100 ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.includes(url.mediaType) &&
101 validator.isLength(url.href, { min: 5 }) &&
102 validator.isInt(url.height + '', { min: 0 })
103 ) ||
104 (
105 (url.mediaType || url.mimeType) === 'application/x-mpegURL' &&
106 isActivityPubUrlValid(url.href) &&
107 isArray(url.tag)
108 ) ||
109 isAPVideoFileMetadataObject(url)
110 }
111
112 function isAPVideoFileMetadataObject (url: any): url is ActivityVideoFileMetadataObject {
113 return url &&
114 url.type === 'Link' &&
115 url.mediaType === 'application/json' &&
116 isArray(url.rel) && url.rel.includes('metadata')
117 }
118
119 // ---------------------------------------------------------------------------
120
121 export {
122 sanitizeAndCheckVideoTorrentUpdateActivity,
123 isRemoteStringIdentifierValid,
124 sanitizeAndCheckVideoTorrentObject,
125 isRemoteVideoUrlValid,
126 isAPVideoFileMetadataObject
127 }
128
129 // ---------------------------------------------------------------------------
130
131 function setValidRemoteTags (video: any) {
132 if (Array.isArray(video.tag) === false) return false
133
134 video.tag = video.tag.filter(t => {
135 return t.type === 'Hashtag' &&
136 isVideoTagValid(t.name)
137 })
138
139 return true
140 }
141
142 function setValidRemoteCaptions (video: any) {
143 if (!video.subtitleLanguage) video.subtitleLanguage = []
144
145 if (Array.isArray(video.subtitleLanguage) === false) return false
146
147 video.subtitleLanguage = video.subtitleLanguage.filter(caption => {
148 if (!isActivityPubUrlValid(caption.url)) caption.url = null
149
150 return isRemoteStringIdentifierValid(caption)
151 })
152
153 return true
154 }
155
156 function isRemoteNumberIdentifierValid (data: any) {
157 return validator.isInt(data.identifier, { min: 0 })
158 }
159
160 function isRemoteStringIdentifierValid (data: any) {
161 return typeof data.identifier === 'string'
162 }
163
164 function isRemoteVideoContentValid (mediaType: string, content: string) {
165 return mediaType === 'text/markdown' && isVideoTruncatedDescriptionValid(content)
166 }
167
168 function setValidRemoteIcon (video: any) {
169 if (video.icon && !isArray(video.icon)) video.icon = [ video.icon ]
170 if (!video.icon) video.icon = []
171
172 video.icon = video.icon.filter(icon => {
173 return icon.type === 'Image' &&
174 isActivityPubUrlValid(icon.url) &&
175 icon.mediaType === 'image/jpeg' &&
176 validator.isInt(icon.width + '', { min: 0 }) &&
177 validator.isInt(icon.height + '', { min: 0 })
178 })
179
180 return video.icon.length !== 0
181 }
182
183 function setValidRemoteVideoUrls (video: any) {
184 if (Array.isArray(video.url) === false) return false
185
186 video.url = video.url.filter(u => isRemoteVideoUrlValid(u))
187
188 return true
189 }
190
191 function setRemoteVideoTruncatedContent (video: any) {
192 if (video.content) {
193 video.content = peertubeTruncate(video.content, { length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max })
194 }
195
196 return true
197 }