2020-01-07 14:56:07 +01:00
|
|
|
import validator from 'validator'
|
2022-08-11 10:43:31 +02:00
|
|
|
import { CONFIG } from '@server/initializers/config'
|
2019-04-11 14:26:41 +02:00
|
|
|
import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { exists } from '../misc'
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2018-09-11 16:27:07 +02:00
|
|
|
function isUrlValid (url: string) {
|
2017-11-09 17:51:58 +01:00
|
|
|
const isURLOptions = {
|
|
|
|
require_host: true,
|
|
|
|
require_tld: true,
|
|
|
|
require_protocol: true,
|
|
|
|
require_valid_protocol: true,
|
|
|
|
protocols: [ 'http', 'https' ]
|
|
|
|
}
|
|
|
|
|
2017-11-14 10:57:56 +01:00
|
|
|
// We validate 'localhost', so we don't have the top level domain
|
2022-08-11 10:43:31 +02:00
|
|
|
if (CONFIG.WEBSERVER.HOSTNAME === 'localhost') {
|
2017-11-14 10:57:56 +01:00
|
|
|
isURLOptions.require_tld = false
|
|
|
|
}
|
|
|
|
|
2018-09-11 16:27:07 +02:00
|
|
|
return exists(url) && validator.isURL('' + url, isURLOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
function isActivityPubUrlValid (url: string) {
|
|
|
|
return isUrlValid(url) && validator.isLength('' + url, CONSTRAINTS_FIELDS.ACTORS.URL)
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
|
|
|
|
2017-11-10 14:34:45 +01:00
|
|
|
function isBaseActivityValid (activity: any, type: string) {
|
2019-05-13 14:51:43 +02:00
|
|
|
return activity.type === type &&
|
2017-11-14 17:31:26 +01:00
|
|
|
isActivityPubUrlValid(activity.id) &&
|
2019-01-15 11:14:12 +01:00
|
|
|
isObjectValid(activity.actor) &&
|
|
|
|
isUrlCollectionValid(activity.to) &&
|
|
|
|
isUrlCollectionValid(activity.cc)
|
|
|
|
}
|
|
|
|
|
|
|
|
function isUrlCollectionValid (collection: any) {
|
|
|
|
return collection === undefined ||
|
|
|
|
(Array.isArray(collection) && collection.every(t => isActivityPubUrlValid(t)))
|
|
|
|
}
|
|
|
|
|
|
|
|
function isObjectValid (object: any) {
|
|
|
|
return exists(object) &&
|
2017-11-17 15:20:42 +01:00
|
|
|
(
|
2019-01-15 11:14:12 +01:00
|
|
|
isActivityPubUrlValid(object) || isActivityPubUrlValid(object.id)
|
2017-11-14 17:31:26 +01:00
|
|
|
)
|
2017-11-10 14:34:45 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 17:38:41 +01:00
|
|
|
function setValidAttributedTo (obj: any) {
|
|
|
|
if (Array.isArray(obj.attributedTo) === false) {
|
|
|
|
obj.attributedTo = []
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-08-28 18:29:29 +02:00
|
|
|
obj.attributedTo = obj.attributedTo.filter(a => {
|
2017-12-14 17:38:41 +01:00
|
|
|
return (a.type === 'Group' || a.type === 'Person') && isActivityPubUrlValid(a.id)
|
|
|
|
})
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-03-24 13:36:47 +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')
|
|
|
|
}
|
|
|
|
|
2017-11-09 17:51:58 +01:00
|
|
|
export {
|
2018-09-11 16:27:07 +02:00
|
|
|
isUrlValid,
|
2017-11-10 14:34:45 +01:00
|
|
|
isActivityPubUrlValid,
|
2017-12-14 17:38:41 +01:00
|
|
|
isBaseActivityValid,
|
2019-01-15 11:14:12 +01:00
|
|
|
setValidAttributedTo,
|
2022-03-24 13:36:47 +01:00
|
|
|
isObjectValid,
|
|
|
|
isActivityPubVideoDurationValid
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|