diff options
Diffstat (limited to 'server/helpers/custom-validators/activitypub/actor.ts')
-rw-r--r-- | server/helpers/custom-validators/activitypub/actor.ts | 61 |
1 files changed, 57 insertions, 4 deletions
diff --git a/server/helpers/custom-validators/activitypub/actor.ts b/server/helpers/custom-validators/activitypub/actor.ts index 28551c96c..bf42757c5 100644 --- a/server/helpers/custom-validators/activitypub/actor.ts +++ b/server/helpers/custom-validators/activitypub/actor.ts | |||
@@ -1,8 +1,12 @@ | |||
1 | import * as Bluebird from 'bluebird' | ||
2 | import { Response } from 'express' | ||
1 | import * as validator from 'validator' | 3 | import * as validator from 'validator' |
2 | import { CONSTRAINTS_FIELDS } from '../../../initializers' | 4 | import { CONSTRAINTS_FIELDS } from '../../../initializers' |
5 | import { ActorModel } from '../../../models/activitypub/actor' | ||
3 | import { isAccountNameValid } from '../accounts' | 6 | import { isAccountNameValid } from '../accounts' |
4 | import { exists, isUUIDValid } from '../misc' | 7 | import { exists, isUUIDValid } from '../misc' |
5 | import { isActivityPubUrlValid, isBaseActivityValid } from './misc' | 8 | import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../video-channels' |
9 | import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc' | ||
6 | 10 | ||
7 | function isActorEndpointsObjectValid (endpointObject: any) { | 11 | function isActorEndpointsObjectValid (endpointObject: any) { |
8 | return isActivityPubUrlValid(endpointObject.sharedInbox) | 12 | return isActivityPubUrlValid(endpointObject.sharedInbox) |
@@ -27,7 +31,12 @@ function isActorPublicKeyValid (publicKey: string) { | |||
27 | } | 31 | } |
28 | 32 | ||
29 | function isActorPreferredUsernameValid (preferredUsername: string) { | 33 | function isActorPreferredUsernameValid (preferredUsername: string) { |
30 | return isAccountNameValid(preferredUsername) | 34 | return isAccountNameValid(preferredUsername) || isVideoChannelNameValid(preferredUsername) |
35 | } | ||
36 | |||
37 | const actorNameRegExp = new RegExp('[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_]+') | ||
38 | function isActorNameValid (name: string) { | ||
39 | return exists(name) && validator.matches(name, actorNameRegExp) | ||
31 | } | 40 | } |
32 | 41 | ||
33 | function isActorPrivateKeyValid (privateKey: string) { | 42 | function isActorPrivateKeyValid (privateKey: string) { |
@@ -46,10 +55,16 @@ function isRemoteActorValid (remoteActor: any) { | |||
46 | isActivityPubUrlValid(remoteActor.followers) && | 55 | isActivityPubUrlValid(remoteActor.followers) && |
47 | isActivityPubUrlValid(remoteActor.inbox) && | 56 | isActivityPubUrlValid(remoteActor.inbox) && |
48 | isActivityPubUrlValid(remoteActor.outbox) && | 57 | isActivityPubUrlValid(remoteActor.outbox) && |
58 | isActorNameValid(remoteActor.name) && | ||
49 | isActorPreferredUsernameValid(remoteActor.preferredUsername) && | 59 | isActorPreferredUsernameValid(remoteActor.preferredUsername) && |
50 | isActivityPubUrlValid(remoteActor.url) && | 60 | isActivityPubUrlValid(remoteActor.url) && |
51 | isActorPublicKeyObjectValid(remoteActor.publicKey) && | 61 | isActorPublicKeyObjectValid(remoteActor.publicKey) && |
52 | isActorEndpointsObjectValid(remoteActor.endpoints) | 62 | isActorEndpointsObjectValid(remoteActor.endpoints) && |
63 | (!remoteActor.summary || isVideoChannelDescriptionValid(remoteActor.summary)) && | ||
64 | setValidAttributedTo(remoteActor) && | ||
65 | // If this is not an account, it should be attributed to an account | ||
66 | // In PeerTube we use this to attach a video channel to a specific account | ||
67 | (remoteActor.type === 'Person' || remoteActor.attributedTo.length !== 0) | ||
53 | } | 68 | } |
54 | 69 | ||
55 | function isActorFollowingCountValid (value: string) { | 70 | function isActorFollowingCountValid (value: string) { |
@@ -73,6 +88,40 @@ function isActorAcceptActivityValid (activity: any) { | |||
73 | return isBaseActivityValid(activity, 'Accept') | 88 | return isBaseActivityValid(activity, 'Accept') |
74 | } | 89 | } |
75 | 90 | ||
91 | function isActorIdExist (id: number | string, res: Response) { | ||
92 | let promise: Bluebird<ActorModel> | ||
93 | |||
94 | if (validator.isInt('' + id)) { | ||
95 | promise = ActorModel.load(+id) | ||
96 | } else { // UUID | ||
97 | promise = ActorModel.loadByUUID('' + id) | ||
98 | } | ||
99 | |||
100 | return isActorExist(promise, res) | ||
101 | } | ||
102 | |||
103 | function isLocalActorNameExist (name: string, res: Response) { | ||
104 | const promise = ActorModel.loadLocalByName(name) | ||
105 | |||
106 | return isActorExist(promise, res) | ||
107 | } | ||
108 | |||
109 | async function isActorExist (p: Bluebird<ActorModel>, res: Response) { | ||
110 | const actor = await p | ||
111 | |||
112 | if (!actor) { | ||
113 | res.status(404) | ||
114 | .send({ error: 'Actor not found' }) | ||
115 | .end() | ||
116 | |||
117 | return false | ||
118 | } | ||
119 | |||
120 | res.locals.actor = actor | ||
121 | |||
122 | return true | ||
123 | } | ||
124 | |||
76 | // --------------------------------------------------------------------------- | 125 | // --------------------------------------------------------------------------- |
77 | 126 | ||
78 | export { | 127 | export { |
@@ -87,5 +136,9 @@ export { | |||
87 | isActorFollowersCountValid, | 136 | isActorFollowersCountValid, |
88 | isActorFollowActivityValid, | 137 | isActorFollowActivityValid, |
89 | isActorAcceptActivityValid, | 138 | isActorAcceptActivityValid, |
90 | isActorDeleteActivityValid | 139 | isActorDeleteActivityValid, |
140 | isActorIdExist, | ||
141 | isLocalActorNameExist, | ||
142 | isActorNameValid, | ||
143 | isActorExist | ||
91 | } | 144 | } |