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