aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/custom-validators')
-rw-r--r--server/helpers/custom-validators/activitypub/activity.ts73
-rw-r--r--server/helpers/custom-validators/activitypub/announce.ts15
-rw-r--r--server/helpers/custom-validators/activitypub/index.ts4
-rw-r--r--server/helpers/custom-validators/activitypub/undo.ts13
-rw-r--r--server/helpers/custom-validators/activitypub/video-channels.ts36
-rw-r--r--server/helpers/custom-validators/activitypub/videos.ts54
6 files changed, 133 insertions, 62 deletions
diff --git a/server/helpers/custom-validators/activitypub/activity.ts b/server/helpers/custom-validators/activitypub/activity.ts
index 8084cf7b0..9305e092c 100644
--- a/server/helpers/custom-validators/activitypub/activity.ts
+++ b/server/helpers/custom-validators/activitypub/activity.ts
@@ -1,11 +1,11 @@
1import * as validator from 'validator' 1import * as validator from 'validator'
2import { Activity, ActivityType } from '../../../../shared/models/activitypub/activity'
2import { isAccountAcceptActivityValid, isAccountDeleteActivityValid, isAccountFollowActivityValid } from './account' 3import { isAccountAcceptActivityValid, isAccountDeleteActivityValid, isAccountFollowActivityValid } from './account'
4import { isAnnounceValid } from './announce'
3import { isActivityPubUrlValid } from './misc' 5import { isActivityPubUrlValid } from './misc'
6import { isUndoValid } from './undo'
7import { isVideoChannelCreateActivityValid, isVideoChannelDeleteActivityValid, isVideoChannelUpdateActivityValid } from './video-channels'
4import { 8import {
5 isAnnounceValid,
6 isVideoChannelCreateActivityValid,
7 isVideoChannelDeleteActivityValid,
8 isVideoChannelUpdateActivityValid,
9 isVideoFlagValid, 9 isVideoFlagValid,
10 isVideoTorrentAddActivityValid, 10 isVideoTorrentAddActivityValid,
11 isVideoTorrentDeleteActivityValid, 11 isVideoTorrentDeleteActivityValid,
@@ -25,18 +25,23 @@ function isRootActivityValid (activity: any) {
25 ) 25 )
26} 26}
27 27
28const activityCheckers: { [ P in ActivityType ]: (activity: Activity) => boolean } = {
29 Create: checkCreateActivity,
30 Add: checkAddActivity,
31 Update: checkUpdateActivity,
32 Delete: checkDeleteActivity,
33 Follow: checkFollowActivity,
34 Accept: checkAcceptActivity,
35 Announce: checkAnnounceActivity,
36 Undo: checkUndoActivity
37}
38
28function isActivityValid (activity: any) { 39function isActivityValid (activity: any) {
29 return isVideoTorrentAddActivityValid(activity) || 40 const checker = activityCheckers[activity.type]
30 isVideoChannelCreateActivityValid(activity) || 41 // Unknown activity type
31 isVideoTorrentUpdateActivityValid(activity) || 42 if (!checker) return false
32 isVideoChannelUpdateActivityValid(activity) || 43
33 isVideoTorrentDeleteActivityValid(activity) || 44 return checker(activity)
34 isVideoChannelDeleteActivityValid(activity) ||
35 isAccountDeleteActivityValid(activity) ||
36 isAccountFollowActivityValid(activity) ||
37 isAccountAcceptActivityValid(activity) ||
38 isVideoFlagValid(activity) ||
39 isAnnounceValid(activity)
40} 45}
41 46
42// --------------------------------------------------------------------------- 47// ---------------------------------------------------------------------------
@@ -45,3 +50,41 @@ export {
45 isRootActivityValid, 50 isRootActivityValid,
46 isActivityValid 51 isActivityValid
47} 52}
53
54// ---------------------------------------------------------------------------
55
56function checkCreateActivity (activity: any) {
57 return isVideoChannelCreateActivityValid(activity) ||
58 isVideoFlagValid(activity)
59}
60
61function checkAddActivity (activity: any) {
62 return isVideoTorrentAddActivityValid(activity)
63}
64
65function checkUpdateActivity (activity: any) {
66 return isVideoTorrentUpdateActivityValid(activity) ||
67 isVideoChannelUpdateActivityValid(activity)
68}
69
70function checkDeleteActivity (activity: any) {
71 return isVideoTorrentDeleteActivityValid(activity) ||
72 isVideoChannelDeleteActivityValid(activity) ||
73 isAccountDeleteActivityValid(activity)
74}
75
76function checkFollowActivity (activity: any) {
77 return isAccountFollowActivityValid(activity)
78}
79
80function checkAcceptActivity (activity: any) {
81 return isAccountAcceptActivityValid(activity)
82}
83
84function checkAnnounceActivity (activity: any) {
85 return isAnnounceValid(activity)
86}
87
88function checkUndoActivity (activity: any) {
89 return isUndoValid(activity)
90}
diff --git a/server/helpers/custom-validators/activitypub/announce.ts b/server/helpers/custom-validators/activitypub/announce.ts
new file mode 100644
index 000000000..4ba99d1ea
--- /dev/null
+++ b/server/helpers/custom-validators/activitypub/announce.ts
@@ -0,0 +1,15 @@
1import { isBaseActivityValid } from './misc'
2import { isVideoTorrentAddActivityValid } from './videos'
3import { isVideoChannelCreateActivityValid } from './video-channels'
4
5function isAnnounceValid (activity: any) {
6 return isBaseActivityValid(activity, 'Announce') &&
7 (
8 isVideoChannelCreateActivityValid(activity.object) ||
9 isVideoTorrentAddActivityValid(activity.object)
10 )
11}
12
13export {
14 isAnnounceValid
15}
diff --git a/server/helpers/custom-validators/activitypub/index.ts b/server/helpers/custom-validators/activitypub/index.ts
index 0eba06a7b..6685b269f 100644
--- a/server/helpers/custom-validators/activitypub/index.ts
+++ b/server/helpers/custom-validators/activitypub/index.ts
@@ -1,5 +1,7 @@
1export * from './account' 1export * from './account'
2export * from './activity' 2export * from './activity'
3export * from './signature'
4export * from './misc' 3export * from './misc'
4export * from './signature'
5export * from './undo'
6export * from './video-channels'
5export * from './videos' 7export * from './videos'
diff --git a/server/helpers/custom-validators/activitypub/undo.ts b/server/helpers/custom-validators/activitypub/undo.ts
new file mode 100644
index 000000000..a9a2a3a41
--- /dev/null
+++ b/server/helpers/custom-validators/activitypub/undo.ts
@@ -0,0 +1,13 @@
1import { isAccountFollowActivityValid } from './account'
2import { isBaseActivityValid } from './misc'
3
4function isUndoValid (activity: any) {
5 return isBaseActivityValid(activity, 'Undo') &&
6 (
7 isAccountFollowActivityValid(activity.object)
8 )
9}
10
11export {
12 isUndoValid
13}
diff --git a/server/helpers/custom-validators/activitypub/video-channels.ts b/server/helpers/custom-validators/activitypub/video-channels.ts
new file mode 100644
index 000000000..9fd3bb149
--- /dev/null
+++ b/server/helpers/custom-validators/activitypub/video-channels.ts
@@ -0,0 +1,36 @@
1import { isDateValid, isUUIDValid } from '../misc'
2import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../video-channels'
3import { isActivityPubUrlValid, isBaseActivityValid } from './misc'
4
5function isVideoChannelCreateActivityValid (activity: any) {
6 return isBaseActivityValid(activity, 'Create') &&
7 isVideoChannelObjectValid(activity.object)
8}
9
10function isVideoChannelUpdateActivityValid (activity: any) {
11 return isBaseActivityValid(activity, 'Update') &&
12 isVideoChannelObjectValid(activity.object)
13}
14
15function isVideoChannelDeleteActivityValid (activity: any) {
16 return isBaseActivityValid(activity, 'Delete')
17}
18
19function isVideoChannelObjectValid (videoChannel: any) {
20 return videoChannel.type === 'VideoChannel' &&
21 isActivityPubUrlValid(videoChannel.id) &&
22 isVideoChannelNameValid(videoChannel.name) &&
23 isVideoChannelDescriptionValid(videoChannel.content) &&
24 isDateValid(videoChannel.published) &&
25 isDateValid(videoChannel.updated) &&
26 isUUIDValid(videoChannel.uuid)
27}
28
29// ---------------------------------------------------------------------------
30
31export {
32 isVideoChannelCreateActivityValid,
33 isVideoChannelUpdateActivityValid,
34 isVideoChannelDeleteActivityValid,
35 isVideoChannelObjectValid
36}
diff --git a/server/helpers/custom-validators/activitypub/videos.ts b/server/helpers/custom-validators/activitypub/videos.ts
index 728511e3d..faeedd3df 100644
--- a/server/helpers/custom-validators/activitypub/videos.ts
+++ b/server/helpers/custom-validators/activitypub/videos.ts
@@ -1,7 +1,6 @@
1import * as validator from 'validator' 1import * as validator from 'validator'
2import { ACTIVITY_PUB } from '../../../initializers' 2import { ACTIVITY_PUB } from '../../../initializers'
3import { exists, isDateValid, isUUIDValid } from '../misc' 3import { exists, isDateValid, isUUIDValid } from '../misc'
4import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../video-channels'
5import { 4import {
6 isVideoAbuseReasonValid, 5 isVideoAbuseReasonValid,
7 isVideoDurationValid, 6 isVideoDurationValid,
@@ -28,6 +27,13 @@ function isVideoTorrentDeleteActivityValid (activity: any) {
28 return isBaseActivityValid(activity, 'Delete') 27 return isBaseActivityValid(activity, 'Delete')
29} 28}
30 29
30function isVideoFlagValid (activity: any) {
31 return isBaseActivityValid(activity, 'Create') &&
32 activity.object.type === 'Flag' &&
33 isVideoAbuseReasonValid(activity.object.content) &&
34 isActivityPubUrlValid(activity.object.object)
35}
36
31function isActivityPubVideoDurationValid (value: string) { 37function isActivityPubVideoDurationValid (value: string) {
32 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration 38 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
33 return exists(value) && 39 return exists(value) &&
@@ -57,57 +63,13 @@ function isVideoTorrentObjectValid (video: any) {
57 video.url.length !== 0 63 video.url.length !== 0
58} 64}
59 65
60function isVideoFlagValid (activity: any) {
61 return isBaseActivityValid(activity, 'Create') &&
62 activity.object.type === 'Flag' &&
63 isVideoAbuseReasonValid(activity.object.content) &&
64 isActivityPubUrlValid(activity.object.object)
65}
66
67function isAnnounceValid (activity: any) {
68 return isBaseActivityValid(activity, 'Announce') &&
69 (
70 isVideoChannelCreateActivityValid(activity.object) ||
71 isVideoTorrentAddActivityValid(activity.object)
72 )
73}
74
75function isVideoChannelCreateActivityValid (activity: any) {
76 return isBaseActivityValid(activity, 'Create') &&
77 isVideoChannelObjectValid(activity.object)
78}
79
80function isVideoChannelUpdateActivityValid (activity: any) {
81 return isBaseActivityValid(activity, 'Update') &&
82 isVideoChannelObjectValid(activity.object)
83}
84
85function isVideoChannelDeleteActivityValid (activity: any) {
86 return isBaseActivityValid(activity, 'Delete')
87}
88
89function isVideoChannelObjectValid (videoChannel: any) {
90 return videoChannel.type === 'VideoChannel' &&
91 isActivityPubUrlValid(videoChannel.id) &&
92 isVideoChannelNameValid(videoChannel.name) &&
93 isVideoChannelDescriptionValid(videoChannel.content) &&
94 isDateValid(videoChannel.published) &&
95 isDateValid(videoChannel.updated) &&
96 isUUIDValid(videoChannel.uuid)
97}
98
99// --------------------------------------------------------------------------- 66// ---------------------------------------------------------------------------
100 67
101export { 68export {
102 isVideoTorrentAddActivityValid, 69 isVideoTorrentAddActivityValid,
103 isVideoChannelCreateActivityValid,
104 isVideoTorrentUpdateActivityValid, 70 isVideoTorrentUpdateActivityValid,
105 isVideoChannelUpdateActivityValid,
106 isVideoChannelDeleteActivityValid,
107 isVideoTorrentDeleteActivityValid, 71 isVideoTorrentDeleteActivityValid,
108 isVideoFlagValid, 72 isVideoFlagValid
109 isAnnounceValid,
110 isVideoChannelObjectValid
111} 73}
112 74
113// --------------------------------------------------------------------------- 75// ---------------------------------------------------------------------------