From 67f87b66fe99a5982580b1f555245abde1c03380 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 5 Mar 2021 13:49:58 +0100 Subject: Refactor a little bit AP validators --- .../custom-validators/activitypub/activity.ts | 112 ++++++++++++--------- .../helpers/custom-validators/activitypub/flag.ts | 14 --- .../helpers/custom-validators/activitypub/rate.ts | 18 ---- .../helpers/custom-validators/activitypub/share.ts | 11 -- .../helpers/custom-validators/activitypub/view.ts | 13 --- .../lib/job-queue/handlers/activitypub-cleaner.ts | 9 +- 6 files changed, 68 insertions(+), 109 deletions(-) delete mode 100644 server/helpers/custom-validators/activitypub/flag.ts delete mode 100644 server/helpers/custom-validators/activitypub/rate.ts delete mode 100644 server/helpers/custom-validators/activitypub/share.ts delete mode 100644 server/helpers/custom-validators/activitypub/view.ts (limited to 'server') diff --git a/server/helpers/custom-validators/activitypub/activity.ts b/server/helpers/custom-validators/activitypub/activity.ts index da79b2782..46126da57 100644 --- a/server/helpers/custom-validators/activitypub/activity.ts +++ b/server/helpers/custom-validators/activitypub/activity.ts @@ -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 checkViewActivity (activity: any) { - return isBaseActivityValid(activity, 'View') && - isViewActivityValid(activity) +function isLikeActivityValid (activity: any) { + return isBaseActivityValid(activity, 'Like') && + isObjectValid(activity.object) } -function checkFlagActivity (activity: any) { - return isBaseActivityValid(activity, 'Flag') && - isFlagActivityValid(activity) +function isDislikeActivityValid (activity: any) { + return isBaseActivityValid(activity, 'Dislike') && + isObjectValid(activity.object) } -function checkDislikeActivity (activity: any) { - return isDislikeActivityValid(activity) +function isAnnounceActivityValid (activity: any) { + return isBaseActivityValid(activity, 'Announce') && + isObjectValid(activity.object) } -function checkLikeActivity (activity: any) { - return isLikeActivityValid(activity) +function isViewActivityValid (activity: any) { + return isBaseActivityValid(activity, 'View') && + isActivityPubUrlValid(activity.actor) && + isActivityPubUrlValid(activity.object) } -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 +} diff --git a/server/helpers/custom-validators/activitypub/flag.ts b/server/helpers/custom-validators/activitypub/flag.ts deleted file mode 100644 index dc90b3667..000000000 --- a/server/helpers/custom-validators/activitypub/flag.ts +++ /dev/null @@ -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 -} diff --git a/server/helpers/custom-validators/activitypub/rate.ts b/server/helpers/custom-validators/activitypub/rate.ts deleted file mode 100644 index aafdda443..000000000 --- a/server/helpers/custom-validators/activitypub/rate.ts +++ /dev/null @@ -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 -} diff --git a/server/helpers/custom-validators/activitypub/share.ts b/server/helpers/custom-validators/activitypub/share.ts deleted file mode 100644 index fb5e4c05e..000000000 --- a/server/helpers/custom-validators/activitypub/share.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { isBaseActivityValid, isObjectValid } from './misc' - -function isShareActivityValid (activity: any) { - return isBaseActivityValid(activity, 'Announce') && - isObjectValid(activity.object) -} -// --------------------------------------------------------------------------- - -export { - isShareActivityValid -} diff --git a/server/helpers/custom-validators/activitypub/view.ts b/server/helpers/custom-validators/activitypub/view.ts deleted file mode 100644 index 41d16469f..000000000 --- a/server/helpers/custom-validators/activitypub/view.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { isActivityPubUrlValid } from './misc' - -function isViewActivityValid (activity: any) { - return activity.type === 'View' && - isActivityPubUrlValid(activity.actor) && - isActivityPubUrlValid(activity.object) -} - -// --------------------------------------------------------------------------- - -export { - isViewActivityValid -} diff --git a/server/lib/job-queue/handlers/activitypub-cleaner.ts b/server/lib/job-queue/handlers/activitypub-cleaner.ts index b58bbc983..0e75b0a6e 100644 --- a/server/lib/job-queue/handlers/activitypub-cleaner.ts +++ b/server/lib/job-queue/handlers/activitypub-cleaner.ts @@ -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) -- cgit v1.2.3