]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/activitypub/actor.ts
Update server dependencies
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / activitypub / actor.ts
CommitLineData
7cde3b9c 1import validator from 'validator'
74dc3bca 2import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
a66c2e32 3import { exists, isArray, isDateValid } from '../misc'
50d6de9c 4import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
06a05d5f 5import { isHostValid } from '../servers'
687c6180 6import { peertubeTruncate } from '@server/helpers/core-utils'
fadf619a
C
7
8function isActorEndpointsObjectValid (endpointObject: any) {
a1587156 9 if (endpointObject?.sharedInbox) {
47581df0
C
10 return isActivityPubUrlValid(endpointObject.sharedInbox)
11 }
12
13 // Shared inbox is optional
14 return true
fadf619a
C
15}
16
17function isActorPublicKeyObjectValid (publicKeyObject: any) {
18 return isActivityPubUrlValid(publicKeyObject.id) &&
19 isActivityPubUrlValid(publicKeyObject.owner) &&
20 isActorPublicKeyValid(publicKeyObject.publicKeyPem)
21}
22
23function isActorTypeValid (type: string) {
47581df0 24 return type === 'Person' || type === 'Application' || type === 'Group' || type === 'Service' || type === 'Organization'
fadf619a
C
25}
26
27function isActorPublicKeyValid (publicKey: string) {
28 return exists(publicKey) &&
29 typeof publicKey === 'string' &&
30 publicKey.startsWith('-----BEGIN PUBLIC KEY-----') &&
bdd428a6 31 publicKey.includes('-----END PUBLIC KEY-----') &&
01de67b9 32 validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY)
fadf619a
C
33}
34
0b5c385b 35const actorNameAlphabet = '[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\\-_.:]'
f7cc67b4 36const actorNameRegExp = new RegExp(`^${actorNameAlphabet}+$`)
fadf619a 37function isActorPreferredUsernameValid (preferredUsername: string) {
e12a0092 38 return exists(preferredUsername) && validator.matches(preferredUsername, actorNameRegExp)
50d6de9c
C
39}
40
fadf619a
C
41function isActorPrivateKeyValid (privateKey: string) {
42 return exists(privateKey) &&
43 typeof privateKey === 'string' &&
44 privateKey.startsWith('-----BEGIN RSA PRIVATE KEY-----') &&
e12a0092 45 // Sometimes there is a \n at the end, so just assert the string contains the end mark
bdd428a6 46 privateKey.includes('-----END RSA PRIVATE KEY-----') &&
01de67b9 47 validator.isLength(privateKey, CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY)
fadf619a
C
48}
49
a66c2e32
C
50function isActorFollowingCountValid (value: string) {
51 return exists(value) && validator.isInt('' + value, { min: 0 })
52}
53
54function isActorFollowersCountValid (value: string) {
55 return exists(value) && validator.isInt('' + value, { min: 0 })
56}
57
58function isActorDeleteActivityValid (activity: any) {
59 return isBaseActivityValid(activity, 'Delete')
60}
61
62function sanitizeAndCheckActorObject (actor: any) {
63 normalizeActor(actor)
64
265ba139
C
65 return exists(actor) &&
66 isActivityPubUrlValid(actor.id) &&
67 isActorTypeValid(actor.type) &&
265ba139 68 isActivityPubUrlValid(actor.inbox) &&
265ba139
C
69 isActorPreferredUsernameValid(actor.preferredUsername) &&
70 isActivityPubUrlValid(actor.url) &&
71 isActorPublicKeyObjectValid(actor.publicKey) &&
72 isActorEndpointsObjectValid(actor.endpoints) &&
f47776e2 73
0b5c385b
C
74 (!actor.outbox || isActivityPubUrlValid(actor.outbox)) &&
75 (!actor.following || isActivityPubUrlValid(actor.following)) &&
76 (!actor.followers || isActivityPubUrlValid(actor.followers)) &&
77
78 setValidAttributedTo(actor) &&
b3fa96ee 79 setValidDescription(actor) &&
0b5c385b 80 // If this is a group (a channel), it should be attributed to an account
50d6de9c 81 // In PeerTube we use this to attach a video channel to a specific account
0b5c385b 82 (actor.type !== 'Group' || actor.attributedTo.length !== 0)
fadf619a
C
83}
84
938d3fa0 85function normalizeActor (actor: any) {
47581df0 86 if (!actor) return
938d3fa0 87
47581df0
C
88 if (!actor.url) {
89 actor.url = actor.id
90 } else if (typeof actor.url !== 'string') {
938d3fa0
C
91 actor.url = actor.url.href || actor.url.url
92 }
93
a66c2e32
C
94 if (!isDateValid(actor.published)) actor.published = undefined
95
938d3fa0 96 if (actor.summary && typeof actor.summary === 'string') {
687c6180 97 actor.summary = peertubeTruncate(actor.summary, { length: CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max })
938d3fa0
C
98
99 if (actor.summary.length < CONSTRAINTS_FIELDS.USERS.DESCRIPTION.min) {
100 actor.summary = null
101 }
102 }
938d3fa0
C
103}
104
06a05d5f
C
105function isValidActorHandle (handle: string) {
106 if (!exists(handle)) return false
107
108 const parts = handle.split('@')
109 if (parts.length !== 2) return false
110
111 return isHostValid(parts[1])
112}
113
f37dc0dd
C
114function areValidActorHandles (handles: string[]) {
115 return isArray(handles) && handles.every(h => isValidActorHandle(h))
116}
117
b3fa96ee
C
118function setValidDescription (obj: any) {
119 if (!obj.summary) obj.summary = null
120
121 return true
122}
123
fadf619a
C
124// ---------------------------------------------------------------------------
125
126export {
938d3fa0 127 normalizeActor,
f7cc67b4 128 actorNameAlphabet,
f37dc0dd 129 areValidActorHandles,
fadf619a
C
130 isActorEndpointsObjectValid,
131 isActorPublicKeyObjectValid,
132 isActorTypeValid,
133 isActorPublicKeyValid,
134 isActorPreferredUsernameValid,
135 isActorPrivateKeyValid,
fadf619a
C
136 isActorFollowingCountValid,
137 isActorFollowersCountValid,
50d6de9c 138 isActorDeleteActivityValid,
848f499d 139 sanitizeAndCheckActorObject,
06a05d5f 140 isValidActorHandle
fadf619a 141}