2020-01-07 14:56:07 +01:00
|
|
|
import validator from 'validator'
|
2019-04-11 14:26:41 +02:00
|
|
|
import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
|
2021-05-07 08:59:59 +02:00
|
|
|
import { exists, isArray, isDateValid } from '../misc'
|
2017-12-14 17:38:41 +01:00
|
|
|
import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
|
2018-08-16 15:25:20 +02:00
|
|
|
import { isHostValid } from '../servers'
|
2019-10-21 09:48:21 +02:00
|
|
|
import { peertubeTruncate } from '@server/helpers/core-utils'
|
2017-12-14 11:18:49 +01:00
|
|
|
|
|
|
|
function isActorEndpointsObjectValid (endpointObject: any) {
|
2020-01-31 16:56:52 +01:00
|
|
|
if (endpointObject?.sharedInbox) {
|
2019-10-23 11:33:53 +02:00
|
|
|
return isActivityPubUrlValid(endpointObject.sharedInbox)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shared inbox is optional
|
|
|
|
return true
|
2017-12-14 11:18:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function isActorPublicKeyObjectValid (publicKeyObject: any) {
|
|
|
|
return isActivityPubUrlValid(publicKeyObject.id) &&
|
|
|
|
isActivityPubUrlValid(publicKeyObject.owner) &&
|
|
|
|
isActorPublicKeyValid(publicKeyObject.publicKeyPem)
|
|
|
|
}
|
|
|
|
|
|
|
|
function isActorTypeValid (type: string) {
|
2019-10-23 11:33:53 +02:00
|
|
|
return type === 'Person' || type === 'Application' || type === 'Group' || type === 'Service' || type === 'Organization'
|
2017-12-14 11:18:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function isActorPublicKeyValid (publicKey: string) {
|
|
|
|
return exists(publicKey) &&
|
|
|
|
typeof publicKey === 'string' &&
|
|
|
|
publicKey.startsWith('-----BEGIN PUBLIC KEY-----') &&
|
2020-02-28 16:03:39 +01:00
|
|
|
publicKey.includes('-----END PUBLIC KEY-----') &&
|
2018-01-03 11:10:40 +01:00
|
|
|
validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY)
|
2017-12-14 11:18:49 +01:00
|
|
|
}
|
|
|
|
|
2019-08-30 09:40:21 +02:00
|
|
|
const actorNameAlphabet = '[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\\-_.:]'
|
2019-01-04 08:56:20 +01:00
|
|
|
const actorNameRegExp = new RegExp(`^${actorNameAlphabet}+$`)
|
2017-12-14 11:18:49 +01:00
|
|
|
function isActorPreferredUsernameValid (preferredUsername: string) {
|
2017-12-19 10:34:56 +01:00
|
|
|
return exists(preferredUsername) && validator.matches(preferredUsername, actorNameRegExp)
|
2017-12-14 17:38:41 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 11:18:49 +01:00
|
|
|
function isActorPrivateKeyValid (privateKey: string) {
|
|
|
|
return exists(privateKey) &&
|
|
|
|
typeof privateKey === 'string' &&
|
|
|
|
privateKey.startsWith('-----BEGIN RSA PRIVATE KEY-----') &&
|
2017-12-19 10:34:56 +01:00
|
|
|
// Sometimes there is a \n at the end, so just assert the string contains the end mark
|
2020-02-28 16:03:39 +01:00
|
|
|
privateKey.includes('-----END RSA PRIVATE KEY-----') &&
|
2018-01-03 11:10:40 +01:00
|
|
|
validator.isLength(privateKey, CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY)
|
2017-12-14 11:18:49 +01:00
|
|
|
}
|
|
|
|
|
2021-05-07 08:59:59 +02:00
|
|
|
function isActorFollowingCountValid (value: string) {
|
|
|
|
return exists(value) && validator.isInt('' + value, { min: 0 })
|
|
|
|
}
|
|
|
|
|
|
|
|
function isActorFollowersCountValid (value: string) {
|
|
|
|
return exists(value) && validator.isInt('' + value, { min: 0 })
|
|
|
|
}
|
|
|
|
|
|
|
|
function isActorDeleteActivityValid (activity: any) {
|
|
|
|
return isBaseActivityValid(activity, 'Delete')
|
|
|
|
}
|
|
|
|
|
|
|
|
function sanitizeAndCheckActorObject (actor: any) {
|
2021-11-16 10:05:12 +01:00
|
|
|
if (!isActorTypeValid(actor.type)) return false
|
|
|
|
|
2021-05-07 08:59:59 +02:00
|
|
|
normalizeActor(actor)
|
|
|
|
|
2018-01-03 16:38:50 +01:00
|
|
|
return exists(actor) &&
|
|
|
|
isActivityPubUrlValid(actor.id) &&
|
|
|
|
isActivityPubUrlValid(actor.inbox) &&
|
|
|
|
isActorPreferredUsernameValid(actor.preferredUsername) &&
|
|
|
|
isActivityPubUrlValid(actor.url) &&
|
|
|
|
isActorPublicKeyObjectValid(actor.publicKey) &&
|
|
|
|
isActorEndpointsObjectValid(actor.endpoints) &&
|
2018-03-19 10:23:42 +01:00
|
|
|
|
2019-08-30 09:40:21 +02:00
|
|
|
(!actor.outbox || isActivityPubUrlValid(actor.outbox)) &&
|
|
|
|
(!actor.following || isActivityPubUrlValid(actor.following)) &&
|
|
|
|
(!actor.followers || isActivityPubUrlValid(actor.followers)) &&
|
|
|
|
|
|
|
|
setValidAttributedTo(actor) &&
|
2021-01-11 11:06:11 +01:00
|
|
|
setValidDescription(actor) &&
|
2019-08-30 09:40:21 +02:00
|
|
|
// If this is a group (a channel), it should be attributed to an account
|
2017-12-14 17:38:41 +01:00
|
|
|
// In PeerTube we use this to attach a video channel to a specific account
|
2019-08-30 09:40:21 +02:00
|
|
|
(actor.type !== 'Group' || actor.attributedTo.length !== 0)
|
2017-12-14 11:18:49 +01:00
|
|
|
}
|
|
|
|
|
2018-05-11 15:55:39 +02:00
|
|
|
function normalizeActor (actor: any) {
|
2019-10-23 11:33:53 +02:00
|
|
|
if (!actor) return
|
2018-05-11 15:55:39 +02:00
|
|
|
|
2019-10-23 11:33:53 +02:00
|
|
|
if (!actor.url) {
|
|
|
|
actor.url = actor.id
|
|
|
|
} else if (typeof actor.url !== 'string') {
|
2018-05-11 15:55:39 +02:00
|
|
|
actor.url = actor.url.href || actor.url.url
|
|
|
|
}
|
|
|
|
|
2021-05-07 08:59:59 +02:00
|
|
|
if (!isDateValid(actor.published)) actor.published = undefined
|
|
|
|
|
2018-05-11 15:55:39 +02:00
|
|
|
if (actor.summary && typeof actor.summary === 'string') {
|
2019-10-21 09:48:21 +02:00
|
|
|
actor.summary = peertubeTruncate(actor.summary, { length: CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max })
|
2018-05-11 15:55:39 +02:00
|
|
|
|
|
|
|
if (actor.summary.length < CONSTRAINTS_FIELDS.USERS.DESCRIPTION.min) {
|
|
|
|
actor.summary = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 15:25:20 +02:00
|
|
|
function isValidActorHandle (handle: string) {
|
|
|
|
if (!exists(handle)) return false
|
|
|
|
|
|
|
|
const parts = handle.split('@')
|
|
|
|
if (parts.length !== 2) return false
|
|
|
|
|
|
|
|
return isHostValid(parts[1])
|
|
|
|
}
|
|
|
|
|
2018-08-23 17:58:39 +02:00
|
|
|
function areValidActorHandles (handles: string[]) {
|
|
|
|
return isArray(handles) && handles.every(h => isValidActorHandle(h))
|
|
|
|
}
|
|
|
|
|
2021-01-11 11:06:11 +01:00
|
|
|
function setValidDescription (obj: any) {
|
|
|
|
if (!obj.summary) obj.summary = null
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:18:49 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2018-05-11 15:55:39 +02:00
|
|
|
normalizeActor,
|
2019-01-04 08:56:20 +01:00
|
|
|
actorNameAlphabet,
|
2018-08-23 17:58:39 +02:00
|
|
|
areValidActorHandles,
|
2017-12-14 11:18:49 +01:00
|
|
|
isActorEndpointsObjectValid,
|
|
|
|
isActorPublicKeyObjectValid,
|
|
|
|
isActorTypeValid,
|
|
|
|
isActorPublicKeyValid,
|
|
|
|
isActorPreferredUsernameValid,
|
|
|
|
isActorPrivateKeyValid,
|
|
|
|
isActorFollowingCountValid,
|
|
|
|
isActorFollowersCountValid,
|
2017-12-14 17:38:41 +01:00
|
|
|
isActorDeleteActivityValid,
|
2019-01-15 11:14:12 +01:00
|
|
|
sanitizeAndCheckActorObject,
|
2018-08-16 15:25:20 +02:00
|
|
|
isValidActorHandle
|
2017-12-14 11:18:49 +01:00
|
|
|
}
|