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

140 lines
4.5 KiB
TypeScript
Raw Normal View History

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