]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/activitypub/actor.ts
Add avatar max size limit
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / activitypub / actor.ts
1 import * as validator from 'validator'
2 import { CONSTRAINTS_FIELDS } from '../../../initializers'
3 import { isAccountNameValid } from '../accounts'
4 import { exists } from '../misc'
5 import { isVideoChannelNameValid } from '../video-channels'
6 import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
7
8 function isActorEndpointsObjectValid (endpointObject: any) {
9 return isActivityPubUrlValid(endpointObject.sharedInbox)
10 }
11
12 function isActorPublicKeyObjectValid (publicKeyObject: any) {
13 return isActivityPubUrlValid(publicKeyObject.id) &&
14 isActivityPubUrlValid(publicKeyObject.owner) &&
15 isActorPublicKeyValid(publicKeyObject.publicKeyPem)
16 }
17
18 function isActorTypeValid (type: string) {
19 return type === 'Person' || type === 'Application' || type === 'Group'
20 }
21
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)
28 }
29
30 const actorNameRegExp = new RegExp('[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_]+')
31 function isActorPreferredUsernameValid (preferredUsername: string) {
32 return exists(preferredUsername) && validator.matches(preferredUsername, actorNameRegExp)
33 }
34
35 function isActorNameValid (name: string) {
36 return isAccountNameValid(name) || isVideoChannelNameValid(name)
37 }
38
39 function isActorPrivateKeyValid (privateKey: string) {
40 return exists(privateKey) &&
41 typeof privateKey === 'string' &&
42 privateKey.startsWith('-----BEGIN RSA PRIVATE KEY-----') &&
43 // Sometimes there is a \n at the end, so just assert the string contains the end mark
44 privateKey.indexOf('-----END RSA PRIVATE KEY-----') !== -1 &&
45 validator.isLength(privateKey, CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY)
46 }
47
48 function isRemoteActorValid (remoteActor: any) {
49 return exists(remoteActor) &&
50 isActivityPubUrlValid(remoteActor.id) &&
51 isActorTypeValid(remoteActor.type) &&
52 isActivityPubUrlValid(remoteActor.following) &&
53 isActivityPubUrlValid(remoteActor.followers) &&
54 isActivityPubUrlValid(remoteActor.inbox) &&
55 isActivityPubUrlValid(remoteActor.outbox) &&
56 isActorPreferredUsernameValid(remoteActor.preferredUsername) &&
57 isActivityPubUrlValid(remoteActor.url) &&
58 isActorPublicKeyObjectValid(remoteActor.publicKey) &&
59 isActorEndpointsObjectValid(remoteActor.endpoints) &&
60 setValidAttributedTo(remoteActor) &&
61 // If this is not an account, it should be attributed to an account
62 // In PeerTube we use this to attach a video channel to a specific account
63 (remoteActor.type === 'Person' || remoteActor.attributedTo.length !== 0)
64 }
65
66 function isActorFollowingCountValid (value: string) {
67 return exists(value) && validator.isInt('' + value, { min: 0 })
68 }
69
70 function isActorFollowersCountValid (value: string) {
71 return exists(value) && validator.isInt('' + value, { min: 0 })
72 }
73
74 function isActorDeleteActivityValid (activity: any) {
75 return isBaseActivityValid(activity, 'Delete')
76 }
77
78 function isActorFollowActivityValid (activity: any) {
79 return isBaseActivityValid(activity, 'Follow') &&
80 isActivityPubUrlValid(activity.object)
81 }
82
83 function isActorAcceptActivityValid (activity: any) {
84 return isBaseActivityValid(activity, 'Accept')
85 }
86
87 // ---------------------------------------------------------------------------
88
89 export {
90 isActorEndpointsObjectValid,
91 isActorPublicKeyObjectValid,
92 isActorTypeValid,
93 isActorPublicKeyValid,
94 isActorPreferredUsernameValid,
95 isActorPrivateKeyValid,
96 isRemoteActorValid,
97 isActorFollowingCountValid,
98 isActorFollowersCountValid,
99 isActorFollowActivityValid,
100 isActorAcceptActivityValid,
101 isActorDeleteActivityValid,
102 isActorNameValid
103 }