Refactor a little bit AP validators

pull/3888/head
Chocobozzz 2021-03-05 13:49:58 +01:00
parent 452b3bea08
commit 67f87b66fe
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
6 changed files with 72 additions and 113 deletions

View File

@ -1,16 +1,13 @@
import validator from 'validator'
import { Activity, ActivityType } from '../../../../shared/models/activitypub'
import { isAbuseReasonValid } from '../abuses'
import { exists } from '../misc'
import { sanitizeAndCheckActorObject } from './actor'
import { isCacheFileObjectValid } from './cache-file'
import { isFlagActivityValid } from './flag'
import { isActivityPubUrlValid, isBaseActivityValid, isObjectValid } from './misc'
import { isPlaylistObjectValid } from './playlist'
import { isDislikeActivityValid, isLikeActivityValid } from './rate'
import { isShareActivityValid } from './share'
import { sanitizeAndCheckVideoCommentObject } from './video-comments'
import { sanitizeAndCheckVideoTorrentObject } from './videos'
import { isViewActivityValid } from './view'
function isRootActivityValid (activity: any) {
return isCollection(activity) || isActivity(activity)
@ -29,56 +26,56 @@ function isActivity (activity: any) {
}
const activityCheckers: { [ P in ActivityType ]: (activity: Activity) => boolean } = {
Create: checkCreateActivity,
Update: checkUpdateActivity,
Delete: checkDeleteActivity,
Follow: checkFollowActivity,
Accept: checkAcceptActivity,
Reject: checkRejectActivity,
Announce: checkAnnounceActivity,
Undo: checkUndoActivity,
Like: checkLikeActivity,
View: checkViewActivity,
Flag: checkFlagActivity,
Dislike: checkDislikeActivity
Create: isCreateActivityValid,
Update: isUpdateActivityValid,
Delete: isDeleteActivityValid,
Follow: isFollowActivityValid,
Accept: isAcceptActivityValid,
Reject: isRejectActivityValid,
Announce: isAnnounceActivityValid,
Undo: isUndoActivityValid,
Like: isLikeActivityValid,
View: isViewActivityValid,
Flag: isFlagActivityValid,
Dislike: isDislikeActivityValid
}
function isActivityValid (activity: any) {
const checker = activityCheckers[activity.type]
const checker = activityCheckers[activity.tswype]
// Unknown activity type
if (!checker) return false
return checker(activity)
}
// ---------------------------------------------------------------------------
export {
isRootActivityValid,
isActivityValid
function isFlagActivityValid (activity: any) {
return isBaseActivityValid(activity.type, 'Flag') &&
isAbuseReasonValid(activity.content) &&
isActivityPubUrlValid(activity.object)
}
// ---------------------------------------------------------------------------
function isLikeActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Like') &&
isObjectValid(activity.object)
}
function checkViewActivity (activity: any) {
function isDislikeActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Dislike') &&
isObjectValid(activity.object)
}
function isAnnounceActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Announce') &&
isObjectValid(activity.object)
}
function isViewActivityValid (activity: any) {
return isBaseActivityValid(activity, 'View') &&
isViewActivityValid(activity)
isActivityPubUrlValid(activity.actor) &&
isActivityPubUrlValid(activity.object)
}
function checkFlagActivity (activity: any) {
return isBaseActivityValid(activity, 'Flag') &&
isFlagActivityValid(activity)
}
function checkDislikeActivity (activity: any) {
return isDislikeActivityValid(activity)
}
function checkLikeActivity (activity: any) {
return isLikeActivityValid(activity)
}
function checkCreateActivity (activity: any) {
function isCreateActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Create') &&
(
isViewActivityValid(activity.object) ||
@ -92,7 +89,7 @@ function checkCreateActivity (activity: any) {
)
}
function checkUpdateActivity (activity: any) {
function isUpdateActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Update') &&
(
isCacheFileObjectValid(activity.object) ||
@ -102,36 +99,51 @@ function checkUpdateActivity (activity: any) {
)
}
function checkDeleteActivity (activity: any) {
function isDeleteActivityValid (activity: any) {
// We don't really check objects
return isBaseActivityValid(activity, 'Delete') &&
isObjectValid(activity.object)
}
function checkFollowActivity (activity: any) {
function isFollowActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Follow') &&
isObjectValid(activity.object)
}
function checkAcceptActivity (activity: any) {
function isAcceptActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Accept')
}
function checkRejectActivity (activity: any) {
function isRejectActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Reject')
}
function checkAnnounceActivity (activity: any) {
return isShareActivityValid(activity)
}
function checkUndoActivity (activity: any) {
function isUndoActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Undo') &&
(
checkFollowActivity(activity.object) ||
checkLikeActivity(activity.object) ||
checkDislikeActivity(activity.object) ||
checkAnnounceActivity(activity.object) ||
checkCreateActivity(activity.object)
isFollowActivityValid(activity.object) ||
isLikeActivityValid(activity.object) ||
isDislikeActivityValid(activity.object) ||
isAnnounceActivityValid(activity.object) ||
isCreateActivityValid(activity.object)
)
}
// ---------------------------------------------------------------------------
export {
isRootActivityValid,
isActivityValid,
isFlagActivityValid,
isLikeActivityValid,
isDislikeActivityValid,
isAnnounceActivityValid,
isViewActivityValid,
isCreateActivityValid,
isUpdateActivityValid,
isDeleteActivityValid,
isFollowActivityValid,
isAcceptActivityValid,
isRejectActivityValid,
isUndoActivityValid
}

View File

@ -1,14 +0,0 @@
import { isActivityPubUrlValid } from './misc'
import { isAbuseReasonValid } from '../abuses'
function isFlagActivityValid (activity: any) {
return activity.type === 'Flag' &&
isAbuseReasonValid(activity.content) &&
isActivityPubUrlValid(activity.object)
}
// ---------------------------------------------------------------------------
export {
isFlagActivityValid
}

View File

@ -1,18 +0,0 @@
import { isBaseActivityValid, isObjectValid } from './misc'
function isLikeActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Like') &&
isObjectValid(activity.object)
}
function isDislikeActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Dislike') &&
isObjectValid(activity.object)
}
// ---------------------------------------------------------------------------
export {
isDislikeActivityValid,
isLikeActivityValid
}

View File

@ -1,11 +0,0 @@
import { isBaseActivityValid, isObjectValid } from './misc'
function isShareActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Announce') &&
isObjectValid(activity.object)
}
// ---------------------------------------------------------------------------
export {
isShareActivityValid
}

View File

@ -1,13 +0,0 @@
import { isActivityPubUrlValid } from './misc'
function isViewActivityValid (activity: any) {
return activity.type === 'View' &&
isActivityPubUrlValid(activity.actor) &&
isActivityPubUrlValid(activity.object)
}
// ---------------------------------------------------------------------------
export {
isViewActivityValid
}

View File

@ -1,8 +1,11 @@
import * as Bluebird from 'bluebird'
import * as Bull from 'bull'
import { checkUrlsSameHost } from '@server/helpers/activitypub'
import { isDislikeActivityValid, isLikeActivityValid } from '@server/helpers/custom-validators/activitypub/rate'
import { isShareActivityValid } from '@server/helpers/custom-validators/activitypub/share'
import {
isAnnounceActivityValid,
isDislikeActivityValid,
isLikeActivityValid
} from '@server/helpers/custom-validators/activitypub/activity'
import { sanitizeAndCheckVideoCommentObject } from '@server/helpers/custom-validators/activitypub/video-comments'
import { doRequest } from '@server/helpers/requests'
import { AP_CLEANER_CONCURRENCY } from '@server/initializers/constants'
@ -149,7 +152,7 @@ function rateOptionsFactory () {
function shareOptionsFactory () {
return {
bodyValidator: (body: any) => isShareActivityValid(body),
bodyValidator: (body: any) => isAnnounceActivityValid(body),
updater: async (url: string, newUrl: string) => {
const share = await VideoShareModel.loadByUrl(url, undefined)