1
0
Fork 0
peertube/server/helpers/custom-validators/activitypub/misc.ts

67 lines
1.7 KiB
TypeScript
Raw Normal View History

2020-01-07 08:56:07 -05:00
import validator from 'validator'
import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
2017-11-14 04:57:56 -05:00
import { isTestInstance } from '../../core-utils'
2017-12-12 11:53:50 -05:00
import { exists } from '../misc'
2017-11-09 11:51:58 -05:00
2018-09-11 10:27:07 -04:00
function isUrlValid (url: string) {
2017-11-09 11:51:58 -05:00
const isURLOptions = {
require_host: true,
require_tld: true,
require_protocol: true,
require_valid_protocol: true,
protocols: [ 'http', 'https' ]
}
2017-11-14 04:57:56 -05:00
// We validate 'localhost', so we don't have the top level domain
if (isTestInstance()) {
isURLOptions.require_tld = false
}
2018-09-11 10:27:07 -04: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 11:51:58 -05:00
}
2017-11-10 08:34:45 -05:00
function isBaseActivityValid (activity: any, type: string) {
2019-05-13 08:51:43 -04:00
return activity.type === type &&
2017-11-14 11:31:26 -05:00
isActivityPubUrlValid(activity.id) &&
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 09:20:42 -05:00
(
isActivityPubUrlValid(object) || isActivityPubUrlValid(object.id)
2017-11-14 11:31:26 -05:00
)
2017-11-10 08:34:45 -05:00
}
2017-12-14 11:38:41 -05:00
function setValidAttributedTo (obj: any) {
if (Array.isArray(obj.attributedTo) === false) {
obj.attributedTo = []
return true
}
2018-08-28 12:29:29 -04:00
obj.attributedTo = obj.attributedTo.filter(a => {
2017-12-14 11:38:41 -05:00
return (a.type === 'Group' || a.type === 'Person') && isActivityPubUrlValid(a.id)
})
return true
}
2017-11-09 11:51:58 -05:00
export {
2018-09-11 10:27:07 -04:00
isUrlValid,
2017-11-10 08:34:45 -05:00
isActivityPubUrlValid,
2017-12-14 11:38:41 -05:00
isBaseActivityValid,
setValidAttributedTo,
isObjectValid
2017-11-09 11:51:58 -05:00
}