diff options
author | Chocobozzz <me@florianbigard.com> | 2017-12-14 11:18:49 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2017-12-19 10:51:09 +0100 |
commit | fadf619ad61a016c1c7fc53de5a8f398a4f77519 (patch) | |
tree | bd449b9fe2353d812f4cf57f6dd03c2221b25607 /server/helpers/custom-validators | |
parent | 7efe153b0bc23e596d5019b9fb3e3e32b6cfeccd (diff) | |
download | PeerTube-fadf619ad61a016c1c7fc53de5a8f398a4f77519.tar.gz PeerTube-fadf619ad61a016c1c7fc53de5a8f398a4f77519.tar.zst PeerTube-fadf619ad61a016c1c7fc53de5a8f398a4f77519.zip |
Save
Diffstat (limited to 'server/helpers/custom-validators')
5 files changed, 94 insertions, 95 deletions
diff --git a/server/helpers/custom-validators/activitypub/account.ts b/server/helpers/custom-validators/activitypub/account.ts deleted file mode 100644 index 10bf81e8a..000000000 --- a/server/helpers/custom-validators/activitypub/account.ts +++ /dev/null | |||
@@ -1,92 +0,0 @@ | |||
1 | import * as validator from 'validator' | ||
2 | import { CONSTRAINTS_FIELDS } from '../../../initializers' | ||
3 | import { isAccountNameValid } from '../accounts' | ||
4 | import { exists, isUUIDValid } from '../misc' | ||
5 | import { isActivityPubUrlValid, isBaseActivityValid } from './misc' | ||
6 | |||
7 | function isAccountEndpointsObjectValid (endpointObject: any) { | ||
8 | return isActivityPubUrlValid(endpointObject.sharedInbox) | ||
9 | } | ||
10 | |||
11 | function isAccountPublicKeyObjectValid (publicKeyObject: any) { | ||
12 | return isActivityPubUrlValid(publicKeyObject.id) && | ||
13 | isActivityPubUrlValid(publicKeyObject.owner) && | ||
14 | isAccountPublicKeyValid(publicKeyObject.publicKeyPem) | ||
15 | } | ||
16 | |||
17 | function isAccountTypeValid (type: string) { | ||
18 | return type === 'Person' || type === 'Application' | ||
19 | } | ||
20 | |||
21 | function isAccountPublicKeyValid (publicKey: string) { | ||
22 | return exists(publicKey) && | ||
23 | typeof publicKey === 'string' && | ||
24 | publicKey.startsWith('-----BEGIN PUBLIC KEY-----') && | ||
25 | publicKey.endsWith('-----END PUBLIC KEY-----') && | ||
26 | validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY) | ||
27 | } | ||
28 | |||
29 | function isAccountPreferredUsernameValid (preferredUsername: string) { | ||
30 | return isAccountNameValid(preferredUsername) | ||
31 | } | ||
32 | |||
33 | function isAccountPrivateKeyValid (privateKey: string) { | ||
34 | return exists(privateKey) && | ||
35 | typeof privateKey === 'string' && | ||
36 | privateKey.startsWith('-----BEGIN RSA PRIVATE KEY-----') && | ||
37 | privateKey.endsWith('-----END RSA PRIVATE KEY-----') && | ||
38 | validator.isLength(privateKey, CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY) | ||
39 | } | ||
40 | |||
41 | function isRemoteAccountValid (remoteAccount: any) { | ||
42 | return isActivityPubUrlValid(remoteAccount.id) && | ||
43 | isUUIDValid(remoteAccount.uuid) && | ||
44 | isAccountTypeValid(remoteAccount.type) && | ||
45 | isActivityPubUrlValid(remoteAccount.following) && | ||
46 | isActivityPubUrlValid(remoteAccount.followers) && | ||
47 | isActivityPubUrlValid(remoteAccount.inbox) && | ||
48 | isActivityPubUrlValid(remoteAccount.outbox) && | ||
49 | isAccountPreferredUsernameValid(remoteAccount.preferredUsername) && | ||
50 | isActivityPubUrlValid(remoteAccount.url) && | ||
51 | isAccountPublicKeyObjectValid(remoteAccount.publicKey) && | ||
52 | isAccountEndpointsObjectValid(remoteAccount.endpoints) | ||
53 | } | ||
54 | |||
55 | function isAccountFollowingCountValid (value: string) { | ||
56 | return exists(value) && validator.isInt('' + value, { min: 0 }) | ||
57 | } | ||
58 | |||
59 | function isAccountFollowersCountValid (value: string) { | ||
60 | return exists(value) && validator.isInt('' + value, { min: 0 }) | ||
61 | } | ||
62 | |||
63 | function isAccountDeleteActivityValid (activity: any) { | ||
64 | return isBaseActivityValid(activity, 'Delete') | ||
65 | } | ||
66 | |||
67 | function isAccountFollowActivityValid (activity: any) { | ||
68 | return isBaseActivityValid(activity, 'Follow') && | ||
69 | isActivityPubUrlValid(activity.object) | ||
70 | } | ||
71 | |||
72 | function isAccountAcceptActivityValid (activity: any) { | ||
73 | return isBaseActivityValid(activity, 'Accept') | ||
74 | } | ||
75 | |||
76 | // --------------------------------------------------------------------------- | ||
77 | |||
78 | export { | ||
79 | isAccountEndpointsObjectValid, | ||
80 | isAccountPublicKeyObjectValid, | ||
81 | isAccountTypeValid, | ||
82 | isAccountPublicKeyValid, | ||
83 | isAccountPreferredUsernameValid, | ||
84 | isAccountPrivateKeyValid, | ||
85 | isRemoteAccountValid, | ||
86 | isAccountFollowingCountValid, | ||
87 | isAccountFollowersCountValid, | ||
88 | isAccountNameValid, | ||
89 | isAccountFollowActivityValid, | ||
90 | isAccountAcceptActivityValid, | ||
91 | isAccountDeleteActivityValid | ||
92 | } | ||
diff --git a/server/helpers/custom-validators/activitypub/activity.ts b/server/helpers/custom-validators/activitypub/activity.ts index 043e3c55e..ae7732194 100644 --- a/server/helpers/custom-validators/activitypub/activity.ts +++ b/server/helpers/custom-validators/activitypub/activity.ts | |||
@@ -1,6 +1,6 @@ | |||
1 | import * as validator from 'validator' | 1 | import * as validator from 'validator' |
2 | import { Activity, ActivityType } from '../../../../shared/models/activitypub' | 2 | import { Activity, ActivityType } from '../../../../shared/models/activitypub' |
3 | import { isAccountAcceptActivityValid, isAccountDeleteActivityValid, isAccountFollowActivityValid } from './account' | 3 | import { isAccountAcceptActivityValid, isAccountDeleteActivityValid, isAccountFollowActivityValid } from './actor' |
4 | import { isAnnounceActivityValid } from './announce' | 4 | import { isAnnounceActivityValid } from './announce' |
5 | import { isActivityPubUrlValid } from './misc' | 5 | import { isActivityPubUrlValid } from './misc' |
6 | import { isDislikeActivityValid, isLikeActivityValid } from './rate' | 6 | import { isDislikeActivityValid, isLikeActivityValid } from './rate' |
diff --git a/server/helpers/custom-validators/activitypub/actor.ts b/server/helpers/custom-validators/activitypub/actor.ts new file mode 100644 index 000000000..28551c96c --- /dev/null +++ b/server/helpers/custom-validators/activitypub/actor.ts | |||
@@ -0,0 +1,91 @@ | |||
1 | import * as validator from 'validator' | ||
2 | import { CONSTRAINTS_FIELDS } from '../../../initializers' | ||
3 | import { isAccountNameValid } from '../accounts' | ||
4 | import { exists, isUUIDValid } from '../misc' | ||
5 | import { isActivityPubUrlValid, isBaseActivityValid } from './misc' | ||
6 | |||
7 | function isActorEndpointsObjectValid (endpointObject: any) { | ||
8 | return isActivityPubUrlValid(endpointObject.sharedInbox) | ||
9 | } | ||
10 | |||
11 | function isActorPublicKeyObjectValid (publicKeyObject: any) { | ||
12 | return isActivityPubUrlValid(publicKeyObject.id) && | ||
13 | isActivityPubUrlValid(publicKeyObject.owner) && | ||
14 | isActorPublicKeyValid(publicKeyObject.publicKeyPem) | ||
15 | } | ||
16 | |||
17 | function isActorTypeValid (type: string) { | ||
18 | return type === 'Person' || type === 'Application' || type === 'Group' | ||
19 | } | ||
20 | |||
21 | function isActorPublicKeyValid (publicKey: string) { | ||
22 | return exists(publicKey) && | ||
23 | typeof publicKey === 'string' && | ||
24 | publicKey.startsWith('-----BEGIN PUBLIC KEY-----') && | ||
25 | publicKey.endsWith('-----END PUBLIC KEY-----') && | ||
26 | validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACTOR.PUBLIC_KEY) | ||
27 | } | ||
28 | |||
29 | function isActorPreferredUsernameValid (preferredUsername: string) { | ||
30 | return isAccountNameValid(preferredUsername) | ||
31 | } | ||
32 | |||
33 | function isActorPrivateKeyValid (privateKey: string) { | ||
34 | return exists(privateKey) && | ||
35 | typeof privateKey === 'string' && | ||
36 | privateKey.startsWith('-----BEGIN RSA PRIVATE KEY-----') && | ||
37 | privateKey.endsWith('-----END RSA PRIVATE KEY-----') && | ||
38 | validator.isLength(privateKey, CONSTRAINTS_FIELDS.ACTOR.PRIVATE_KEY) | ||
39 | } | ||
40 | |||
41 | function isRemoteActorValid (remoteActor: any) { | ||
42 | return isActivityPubUrlValid(remoteActor.id) && | ||
43 | isUUIDValid(remoteActor.uuid) && | ||
44 | isActorTypeValid(remoteActor.type) && | ||
45 | isActivityPubUrlValid(remoteActor.following) && | ||
46 | isActivityPubUrlValid(remoteActor.followers) && | ||
47 | isActivityPubUrlValid(remoteActor.inbox) && | ||
48 | isActivityPubUrlValid(remoteActor.outbox) && | ||
49 | isActorPreferredUsernameValid(remoteActor.preferredUsername) && | ||
50 | isActivityPubUrlValid(remoteActor.url) && | ||
51 | isActorPublicKeyObjectValid(remoteActor.publicKey) && | ||
52 | isActorEndpointsObjectValid(remoteActor.endpoints) | ||
53 | } | ||
54 | |||
55 | function isActorFollowingCountValid (value: string) { | ||
56 | return exists(value) && validator.isInt('' + value, { min: 0 }) | ||
57 | } | ||
58 | |||
59 | function isActorFollowersCountValid (value: string) { | ||
60 | return exists(value) && validator.isInt('' + value, { min: 0 }) | ||
61 | } | ||
62 | |||
63 | function isActorDeleteActivityValid (activity: any) { | ||
64 | return isBaseActivityValid(activity, 'Delete') | ||
65 | } | ||
66 | |||
67 | function isActorFollowActivityValid (activity: any) { | ||
68 | return isBaseActivityValid(activity, 'Follow') && | ||
69 | isActivityPubUrlValid(activity.object) | ||
70 | } | ||
71 | |||
72 | function isActorAcceptActivityValid (activity: any) { | ||
73 | return isBaseActivityValid(activity, 'Accept') | ||
74 | } | ||
75 | |||
76 | // --------------------------------------------------------------------------- | ||
77 | |||
78 | export { | ||
79 | isActorEndpointsObjectValid, | ||
80 | isActorPublicKeyObjectValid, | ||
81 | isActorTypeValid, | ||
82 | isActorPublicKeyValid, | ||
83 | isActorPreferredUsernameValid, | ||
84 | isActorPrivateKeyValid, | ||
85 | isRemoteActorValid, | ||
86 | isActorFollowingCountValid, | ||
87 | isActorFollowersCountValid, | ||
88 | isActorFollowActivityValid, | ||
89 | isActorAcceptActivityValid, | ||
90 | isActorDeleteActivityValid | ||
91 | } | ||
diff --git a/server/helpers/custom-validators/activitypub/index.ts b/server/helpers/custom-validators/activitypub/index.ts index f8dfae4ff..ba411f1c6 100644 --- a/server/helpers/custom-validators/activitypub/index.ts +++ b/server/helpers/custom-validators/activitypub/index.ts | |||
@@ -1,4 +1,4 @@ | |||
1 | export * from './account' | 1 | export * from './actor' |
2 | export * from './activity' | 2 | export * from './activity' |
3 | export * from './misc' | 3 | export * from './misc' |
4 | export * from './signature' | 4 | export * from './signature' |
diff --git a/server/helpers/custom-validators/activitypub/undo.ts b/server/helpers/custom-validators/activitypub/undo.ts index 58043f8a1..d07bbf6b7 100644 --- a/server/helpers/custom-validators/activitypub/undo.ts +++ b/server/helpers/custom-validators/activitypub/undo.ts | |||
@@ -1,4 +1,4 @@ | |||
1 | import { isAccountFollowActivityValid } from './account' | 1 | import { isAccountFollowActivityValid } from './actor' |
2 | import { isBaseActivityValid } from './misc' | 2 | import { isBaseActivityValid } from './misc' |
3 | import { isDislikeActivityValid, isLikeActivityValid } from './rate' | 3 | import { isDislikeActivityValid, isLikeActivityValid } from './rate' |
4 | 4 | ||