1 import * as validator from 'validator'
2 import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
3 import { exists, isArray } from '../misc'
4 import { truncate } from 'lodash'
5 import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
6 import { isHostValid } from '../servers'
8 function isActorEndpointsObjectValid (endpointObject: any) {
9 return isActivityPubUrlValid(endpointObject.sharedInbox)
12 function isActorPublicKeyObjectValid (publicKeyObject: any) {
13 return isActivityPubUrlValid(publicKeyObject.id) &&
14 isActivityPubUrlValid(publicKeyObject.owner) &&
15 isActorPublicKeyValid(publicKeyObject.publicKeyPem)
18 function isActorTypeValid (type: string) {
19 return type === 'Person' || type === 'Application' || type === 'Group'
22 function isActorPublicKeyValid (publicKey: string) {
23 return exists(publicKey) &&
24 typeof publicKey === 'string' &&
25 publicKey.startsWith('-----BEGIN PUBLIC KEY-----') &&
26 publicKey.indexOf('-----END PUBLIC KEY-----') !== -1 &&
27 validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY)
30 const actorNameAlphabet = '[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\\-_.:]'
31 const actorNameRegExp = new RegExp(`^${actorNameAlphabet}+$`)
32 function isActorPreferredUsernameValid (preferredUsername: string) {
33 return exists(preferredUsername) && validator.matches(preferredUsername, actorNameRegExp)
36 function isActorPrivateKeyValid (privateKey: string) {
37 return exists(privateKey) &&
38 typeof privateKey === 'string' &&
39 privateKey.startsWith('-----BEGIN RSA PRIVATE KEY-----') &&
40 // Sometimes there is a \n at the end, so just assert the string contains the end mark
41 privateKey.indexOf('-----END RSA PRIVATE KEY-----') !== -1 &&
42 validator.isLength(privateKey, CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY)
45 function isActorObjectValid (actor: any) {
46 return exists(actor) &&
47 isActivityPubUrlValid(actor.id) &&
48 isActorTypeValid(actor.type) &&
49 isActivityPubUrlValid(actor.inbox) &&
50 isActorPreferredUsernameValid(actor.preferredUsername) &&
51 isActivityPubUrlValid(actor.url) &&
52 isActorPublicKeyObjectValid(actor.publicKey) &&
53 isActorEndpointsObjectValid(actor.endpoints) &&
55 (!actor.outbox || isActivityPubUrlValid(actor.outbox)) &&
56 (!actor.following || isActivityPubUrlValid(actor.following)) &&
57 (!actor.followers || isActivityPubUrlValid(actor.followers)) &&
59 setValidAttributedTo(actor) &&
60 // If this is a group (a channel), it should be attributed to an account
61 // In PeerTube we use this to attach a video channel to a specific account
62 (actor.type !== 'Group' || actor.attributedTo.length !== 0)
65 function isActorFollowingCountValid (value: string) {
66 return exists(value) && validator.isInt('' + value, { min: 0 })
69 function isActorFollowersCountValid (value: string) {
70 return exists(value) && validator.isInt('' + value, { min: 0 })
73 function isActorDeleteActivityValid (activity: any) {
74 return isBaseActivityValid(activity, 'Delete')
77 function sanitizeAndCheckActorObject (object: any) {
78 normalizeActor(object)
80 return isActorObjectValid(object)
83 function normalizeActor (actor: any) {
84 if (!actor || !actor.url) return
86 if (typeof actor.url !== 'string') {
87 actor.url = actor.url.href || actor.url.url
90 if (actor.summary && typeof actor.summary === 'string') {
91 actor.summary = truncate(actor.summary, { length: CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max })
93 if (actor.summary.length < CONSTRAINTS_FIELDS.USERS.DESCRIPTION.min) {
101 function isValidActorHandle (handle: string) {
102 if (!exists(handle)) return false
104 const parts = handle.split('@')
105 if (parts.length !== 2) return false
107 return isHostValid(parts[1])
110 function areValidActorHandles (handles: string[]) {
111 return isArray(handles) && handles.every(h => isValidActorHandle(h))
114 // ---------------------------------------------------------------------------
119 areValidActorHandles,
120 isActorEndpointsObjectValid,
121 isActorPublicKeyObjectValid,
123 isActorPublicKeyValid,
124 isActorPreferredUsernameValid,
125 isActorPrivateKeyValid,
127 isActorFollowingCountValid,
128 isActorFollowersCountValid,
129 isActorDeleteActivityValid,
130 sanitizeAndCheckActorObject,