aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/activitypub/actor.ts
blob: bf42757c59005d95bce30a7934f2d1e8bdd30c06 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import * as Bluebird from 'bluebird'
import { Response } from 'express'
import * as validator from 'validator'
import { CONSTRAINTS_FIELDS } from '../../../initializers'
import { ActorModel } from '../../../models/activitypub/actor'
import { isAccountNameValid } from '../accounts'
import { exists, isUUIDValid } from '../misc'
import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../video-channels'
import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'

function isActorEndpointsObjectValid (endpointObject: any) {
  return isActivityPubUrlValid(endpointObject.sharedInbox)
}

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'
}

function isActorPublicKeyValid (publicKey: string) {
  return exists(publicKey) &&
    typeof publicKey === 'string' &&
    publicKey.startsWith('-----BEGIN PUBLIC KEY-----') &&
    publicKey.endsWith('-----END PUBLIC KEY-----') &&
    validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACTOR.PUBLIC_KEY)
}

function isActorPreferredUsernameValid (preferredUsername: string) {
  return isAccountNameValid(preferredUsername) || isVideoChannelNameValid(preferredUsername)
}

const actorNameRegExp = new RegExp('[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_]+')
function isActorNameValid (name: string) {
  return exists(name) && validator.matches(name, actorNameRegExp)
}

function isActorPrivateKeyValid (privateKey: string) {
  return exists(privateKey) &&
    typeof privateKey === 'string' &&
    privateKey.startsWith('-----BEGIN RSA PRIVATE KEY-----') &&
    privateKey.endsWith('-----END RSA PRIVATE KEY-----') &&
    validator.isLength(privateKey, CONSTRAINTS_FIELDS.ACTOR.PRIVATE_KEY)
}

function isRemoteActorValid (remoteActor: any) {
  return isActivityPubUrlValid(remoteActor.id) &&
    isUUIDValid(remoteActor.uuid) &&
    isActorTypeValid(remoteActor.type) &&
    isActivityPubUrlValid(remoteActor.following) &&
    isActivityPubUrlValid(remoteActor.followers) &&
    isActivityPubUrlValid(remoteActor.inbox) &&
    isActivityPubUrlValid(remoteActor.outbox) &&
    isActorNameValid(remoteActor.name) &&
    isActorPreferredUsernameValid(remoteActor.preferredUsername) &&
    isActivityPubUrlValid(remoteActor.url) &&
    isActorPublicKeyObjectValid(remoteActor.publicKey) &&
    isActorEndpointsObjectValid(remoteActor.endpoints) &&
    (!remoteActor.summary || isVideoChannelDescriptionValid(remoteActor.summary)) &&
    setValidAttributedTo(remoteActor) &&
    // If this is not an account, it should be attributed to an account
    // In PeerTube we use this to attach a video channel to a specific account
    (remoteActor.type === 'Person' || remoteActor.attributedTo.length !== 0)
}

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 isActorFollowActivityValid (activity: any) {
  return isBaseActivityValid(activity, 'Follow') &&
    isActivityPubUrlValid(activity.object)
}

function isActorAcceptActivityValid (activity: any) {
  return isBaseActivityValid(activity, 'Accept')
}

function isActorIdExist (id: number | string, res: Response) {
  let promise: Bluebird<ActorModel>

  if (validator.isInt('' + id)) {
    promise = ActorModel.load(+id)
  } else { // UUID
    promise = ActorModel.loadByUUID('' + id)
  }

  return isActorExist(promise, res)
}

function isLocalActorNameExist (name: string, res: Response) {
  const promise = ActorModel.loadLocalByName(name)

  return isActorExist(promise, res)
}

async function isActorExist (p: Bluebird<ActorModel>, res: Response) {
  const actor = await p

  if (!actor) {
    res.status(404)
      .send({ error: 'Actor not found' })
      .end()

    return false
  }

  res.locals.actor = actor

  return true
}

// ---------------------------------------------------------------------------

export {
  isActorEndpointsObjectValid,
  isActorPublicKeyObjectValid,
  isActorTypeValid,
  isActorPublicKeyValid,
  isActorPreferredUsernameValid,
  isActorPrivateKeyValid,
  isRemoteActorValid,
  isActorFollowingCountValid,
  isActorFollowersCountValid,
  isActorFollowActivityValid,
  isActorAcceptActivityValid,
  isActorDeleteActivityValid,
  isActorIdExist,
  isLocalActorNameExist,
  isActorNameValid,
  isActorExist
}