From 848f499def54db2dd36437ef0dfb74dd5041c23b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 15 Jan 2019 11:14:12 +0100 Subject: Prepare Dislike/Flag/View fixes For now we Create these activities, but we should just send them directly. This fix handles correctly direct Dislikes/Flags/Views, we'll implement the sending correctly these activities in the next peertube version --- .../custom-validators/activitypub/activity.ts | 99 +++++++++++++--------- .../helpers/custom-validators/activitypub/actor.ts | 25 +----- .../custom-validators/activitypub/announce.ts | 13 --- .../custom-validators/activitypub/cache-file.ts | 16 +--- .../helpers/custom-validators/activitypub/flag.ts | 14 +++ .../helpers/custom-validators/activitypub/misc.ts | 24 ++++-- .../helpers/custom-validators/activitypub/rate.ts | 15 +--- .../helpers/custom-validators/activitypub/undo.ts | 20 ----- .../activitypub/video-comments.ts | 11 --- .../custom-validators/activitypub/videos.ts | 19 ----- .../helpers/custom-validators/activitypub/view.ts | 10 +-- 11 files changed, 105 insertions(+), 161 deletions(-) delete mode 100644 server/helpers/custom-validators/activitypub/announce.ts create mode 100644 server/helpers/custom-validators/activitypub/flag.ts delete mode 100644 server/helpers/custom-validators/activitypub/undo.ts (limited to 'server/helpers/custom-validators') diff --git a/server/helpers/custom-validators/activitypub/activity.ts b/server/helpers/custom-validators/activitypub/activity.ts index 2562ead9b..b24590d9d 100644 --- a/server/helpers/custom-validators/activitypub/activity.ts +++ b/server/helpers/custom-validators/activitypub/activity.ts @@ -1,26 +1,14 @@ import * as validator from 'validator' import { Activity, ActivityType } from '../../../../shared/models/activitypub' -import { - isActorAcceptActivityValid, - isActorDeleteActivityValid, - isActorFollowActivityValid, - isActorRejectActivityValid, - isActorUpdateActivityValid -} from './actor' -import { isAnnounceActivityValid } from './announce' -import { isActivityPubUrlValid } from './misc' -import { isDislikeActivityValid, isLikeActivityValid } from './rate' -import { isUndoActivityValid } from './undo' -import { isVideoCommentCreateActivityValid, isVideoCommentDeleteActivityValid } from './video-comments' -import { - isVideoFlagValid, - isVideoTorrentDeleteActivityValid, - sanitizeAndCheckVideoTorrentCreateActivity, - sanitizeAndCheckVideoTorrentUpdateActivity -} from './videos' +import { sanitizeAndCheckActorObject } from './actor' +import { isActivityPubUrlValid, isBaseActivityValid, isObjectValid } from './misc' +import { isDislikeActivityValid } from './rate' +import { sanitizeAndCheckVideoCommentObject } from './video-comments' +import { sanitizeAndCheckVideoTorrentObject } from './videos' import { isViewActivityValid } from './view' import { exists } from '../misc' -import { isCacheFileCreateActivityValid, isCacheFileUpdateActivityValid } from './cache-file' +import { isCacheFileObjectValid } from './cache-file' +import { isFlagActivityValid } from './flag' function isRootActivityValid (activity: any) { return Array.isArray(activity['@context']) && ( @@ -46,7 +34,10 @@ const activityCheckers: { [ P in ActivityType ]: (activity: Activity) => boolean Reject: checkRejectActivity, Announce: checkAnnounceActivity, Undo: checkUndoActivity, - Like: checkLikeActivity + Like: checkLikeActivity, + View: checkViewActivity, + Flag: checkFlagActivity, + Dislike: checkDislikeActivity } function isActivityValid (activity: any) { @@ -66,47 +57,79 @@ export { // --------------------------------------------------------------------------- +function checkViewActivity (activity: any) { + return isBaseActivityValid(activity, 'View') && + isViewActivityValid(activity) +} + +function checkFlagActivity (activity: any) { + return isBaseActivityValid(activity, 'Flag') && + isFlagActivityValid(activity) +} + +function checkDislikeActivity (activity: any) { + return isBaseActivityValid(activity, 'Dislike') && + isDislikeActivityValid(activity) +} + function checkCreateActivity (activity: any) { - return isViewActivityValid(activity) || - isDislikeActivityValid(activity) || - sanitizeAndCheckVideoTorrentCreateActivity(activity) || - isVideoFlagValid(activity) || - isVideoCommentCreateActivityValid(activity) || - isCacheFileCreateActivityValid(activity) + return isBaseActivityValid(activity, 'Create') && + ( + isViewActivityValid(activity.object) || + isDislikeActivityValid(activity.object) || + isFlagActivityValid(activity.object) || + + isCacheFileObjectValid(activity.object) || + sanitizeAndCheckVideoCommentObject(activity.object) || + sanitizeAndCheckVideoTorrentObject(activity.object) + ) } function checkUpdateActivity (activity: any) { - return isCacheFileUpdateActivityValid(activity) || - sanitizeAndCheckVideoTorrentUpdateActivity(activity) || - isActorUpdateActivityValid(activity) + return isBaseActivityValid(activity, 'Update') && + ( + isCacheFileObjectValid(activity.object) || + sanitizeAndCheckVideoTorrentObject(activity.object) || + sanitizeAndCheckActorObject(activity.object) + ) } function checkDeleteActivity (activity: any) { - return isVideoTorrentDeleteActivityValid(activity) || - isActorDeleteActivityValid(activity) || - isVideoCommentDeleteActivityValid(activity) + // We don't really check objects + return isBaseActivityValid(activity, 'Delete') && + isObjectValid(activity.object) } function checkFollowActivity (activity: any) { - return isActorFollowActivityValid(activity) + return isBaseActivityValid(activity, 'Follow') && + isObjectValid(activity.object) } function checkAcceptActivity (activity: any) { - return isActorAcceptActivityValid(activity) + return isBaseActivityValid(activity, 'Accept') } function checkRejectActivity (activity: any) { - return isActorRejectActivityValid(activity) + return isBaseActivityValid(activity, 'Reject') } function checkAnnounceActivity (activity: any) { - return isAnnounceActivityValid(activity) + return isBaseActivityValid(activity, 'Announce') && + isObjectValid(activity.object) } function checkUndoActivity (activity: any) { - return isUndoActivityValid(activity) + return isBaseActivityValid(activity, 'Undo') && + ( + checkFollowActivity(activity.object) || + checkLikeActivity(activity.object) || + checkDislikeActivity(activity.object) || + checkAnnounceActivity(activity.object) || + checkCreateActivity(activity.object) + ) } function checkLikeActivity (activity: any) { - return isLikeActivityValid(activity) + return isBaseActivityValid(activity, 'Like') && + isObjectValid(activity.object) } diff --git a/server/helpers/custom-validators/activitypub/actor.ts b/server/helpers/custom-validators/activitypub/actor.ts index 070632a20..c05f60f14 100644 --- a/server/helpers/custom-validators/activitypub/actor.ts +++ b/server/helpers/custom-validators/activitypub/actor.ts @@ -73,24 +73,10 @@ function isActorDeleteActivityValid (activity: any) { return isBaseActivityValid(activity, 'Delete') } -function isActorFollowActivityValid (activity: any) { - return isBaseActivityValid(activity, 'Follow') && - isActivityPubUrlValid(activity.object) -} - -function isActorAcceptActivityValid (activity: any) { - return isBaseActivityValid(activity, 'Accept') -} - -function isActorRejectActivityValid (activity: any) { - return isBaseActivityValid(activity, 'Reject') -} - -function isActorUpdateActivityValid (activity: any) { - normalizeActor(activity.object) +function sanitizeAndCheckActorObject (object: any) { + normalizeActor(object) - return isBaseActivityValid(activity, 'Update') && - isActorObjectValid(activity.object) + return isActorObjectValid(object) } function normalizeActor (actor: any) { @@ -139,10 +125,7 @@ export { isActorObjectValid, isActorFollowingCountValid, isActorFollowersCountValid, - isActorFollowActivityValid, - isActorAcceptActivityValid, - isActorRejectActivityValid, isActorDeleteActivityValid, - isActorUpdateActivityValid, + sanitizeAndCheckActorObject, isValidActorHandle } diff --git a/server/helpers/custom-validators/activitypub/announce.ts b/server/helpers/custom-validators/activitypub/announce.ts deleted file mode 100644 index 0519c6026..000000000 --- a/server/helpers/custom-validators/activitypub/announce.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { isActivityPubUrlValid, isBaseActivityValid } from './misc' - -function isAnnounceActivityValid (activity: any) { - return isBaseActivityValid(activity, 'Announce') && - ( - isActivityPubUrlValid(activity.object) || - (activity.object && isActivityPubUrlValid(activity.object.id)) - ) -} - -export { - isAnnounceActivityValid -} diff --git a/server/helpers/custom-validators/activitypub/cache-file.ts b/server/helpers/custom-validators/activitypub/cache-file.ts index bd70934c8..e2bd0c55e 100644 --- a/server/helpers/custom-validators/activitypub/cache-file.ts +++ b/server/helpers/custom-validators/activitypub/cache-file.ts @@ -1,18 +1,8 @@ -import { isActivityPubUrlValid, isBaseActivityValid } from './misc' +import { isActivityPubUrlValid } from './misc' import { isRemoteVideoUrlValid } from './videos' -import { isDateValid, exists } from '../misc' +import { exists, isDateValid } from '../misc' import { CacheFileObject } from '../../../../shared/models/activitypub/objects' -function isCacheFileCreateActivityValid (activity: any) { - return isBaseActivityValid(activity, 'Create') && - isCacheFileObjectValid(activity.object) -} - -function isCacheFileUpdateActivityValid (activity: any) { - return isBaseActivityValid(activity, 'Update') && - isCacheFileObjectValid(activity.object) -} - function isCacheFileObjectValid (object: CacheFileObject) { return exists(object) && object.type === 'CacheFile' && @@ -22,7 +12,5 @@ function isCacheFileObjectValid (object: CacheFileObject) { } export { - isCacheFileUpdateActivityValid, - isCacheFileCreateActivityValid, isCacheFileObjectValid } diff --git a/server/helpers/custom-validators/activitypub/flag.ts b/server/helpers/custom-validators/activitypub/flag.ts new file mode 100644 index 000000000..6452e297c --- /dev/null +++ b/server/helpers/custom-validators/activitypub/flag.ts @@ -0,0 +1,14 @@ +import { isActivityPubUrlValid } from './misc' +import { isVideoAbuseReasonValid } from '../video-abuses' + +function isFlagActivityValid (activity: any) { + return activity.type === 'Flag' && + isVideoAbuseReasonValid(activity.content) && + isActivityPubUrlValid(activity.object) +} + +// --------------------------------------------------------------------------- + +export { + isFlagActivityValid +} diff --git a/server/helpers/custom-validators/activitypub/misc.ts b/server/helpers/custom-validators/activitypub/misc.ts index 4e2c57f04..f1762d11c 100644 --- a/server/helpers/custom-validators/activitypub/misc.ts +++ b/server/helpers/custom-validators/activitypub/misc.ts @@ -28,15 +28,20 @@ function isBaseActivityValid (activity: any, type: string) { return (activity['@context'] === undefined || Array.isArray(activity['@context'])) && activity.type === type && isActivityPubUrlValid(activity.id) && - exists(activity.actor) && - (isActivityPubUrlValid(activity.actor) || isActivityPubUrlValid(activity.actor.id)) && - ( - activity.to === undefined || - (Array.isArray(activity.to) && activity.to.every(t => isActivityPubUrlValid(t))) - ) && + isObjectValid(activity.actor) && + isUrlCollectionValid(activity.to) && + isUrlCollectionValid(activity.cc) +} + +function isUrlCollectionValid (collection: any) { + return collection === undefined || + (Array.isArray(collection) && collection.every(t => isActivityPubUrlValid(t))) +} + +function isObjectValid (object: any) { + return exists(object) && ( - activity.cc === undefined || - (Array.isArray(activity.cc) && activity.cc.every(t => isActivityPubUrlValid(t))) + isActivityPubUrlValid(object) || isActivityPubUrlValid(object.id) ) } @@ -57,5 +62,6 @@ export { isUrlValid, isActivityPubUrlValid, isBaseActivityValid, - setValidAttributedTo + setValidAttributedTo, + isObjectValid } diff --git a/server/helpers/custom-validators/activitypub/rate.ts b/server/helpers/custom-validators/activitypub/rate.ts index e70bd94b8..ba68e8074 100644 --- a/server/helpers/custom-validators/activitypub/rate.ts +++ b/server/helpers/custom-validators/activitypub/rate.ts @@ -1,20 +1,13 @@ -import { isActivityPubUrlValid, isBaseActivityValid } from './misc' - -function isLikeActivityValid (activity: any) { - return isBaseActivityValid(activity, 'Like') && - isActivityPubUrlValid(activity.object) -} +import { isActivityPubUrlValid, isObjectValid } from './misc' function isDislikeActivityValid (activity: any) { - return isBaseActivityValid(activity, 'Create') && - activity.object.type === 'Dislike' && - isActivityPubUrlValid(activity.object.actor) && - isActivityPubUrlValid(activity.object.object) + return activity.type === 'Dislike' && + isActivityPubUrlValid(activity.actor) && + isObjectValid(activity.object) } // --------------------------------------------------------------------------- export { - isLikeActivityValid, isDislikeActivityValid } diff --git a/server/helpers/custom-validators/activitypub/undo.ts b/server/helpers/custom-validators/activitypub/undo.ts deleted file mode 100644 index 578035893..000000000 --- a/server/helpers/custom-validators/activitypub/undo.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { isActorFollowActivityValid } from './actor' -import { isBaseActivityValid } from './misc' -import { isDislikeActivityValid, isLikeActivityValid } from './rate' -import { isAnnounceActivityValid } from './announce' -import { isCacheFileCreateActivityValid } from './cache-file' - -function isUndoActivityValid (activity: any) { - return isBaseActivityValid(activity, 'Undo') && - ( - isActorFollowActivityValid(activity.object) || - isLikeActivityValid(activity.object) || - isDislikeActivityValid(activity.object) || - isAnnounceActivityValid(activity.object) || - isCacheFileCreateActivityValid(activity.object) - ) -} - -export { - isUndoActivityValid -} diff --git a/server/helpers/custom-validators/activitypub/video-comments.ts b/server/helpers/custom-validators/activitypub/video-comments.ts index 051c4565a..0415db21c 100644 --- a/server/helpers/custom-validators/activitypub/video-comments.ts +++ b/server/helpers/custom-validators/activitypub/video-comments.ts @@ -3,11 +3,6 @@ import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers' import { exists, isArray, isDateValid } from '../misc' import { isActivityPubUrlValid, isBaseActivityValid } from './misc' -function isVideoCommentCreateActivityValid (activity: any) { - return isBaseActivityValid(activity, 'Create') && - sanitizeAndCheckVideoCommentObject(activity.object) -} - function sanitizeAndCheckVideoCommentObject (comment: any) { if (!comment || comment.type !== 'Note') return false @@ -25,15 +20,9 @@ function sanitizeAndCheckVideoCommentObject (comment: any) { ) // Only accept public comments } -function isVideoCommentDeleteActivityValid (activity: any) { - return isBaseActivityValid(activity, 'Delete') -} - // --------------------------------------------------------------------------- export { - isVideoCommentCreateActivityValid, - isVideoCommentDeleteActivityValid, sanitizeAndCheckVideoCommentObject } diff --git a/server/helpers/custom-validators/activitypub/videos.ts b/server/helpers/custom-validators/activitypub/videos.ts index 95fe824b9..0f34aab21 100644 --- a/server/helpers/custom-validators/activitypub/videos.ts +++ b/server/helpers/custom-validators/activitypub/videos.ts @@ -14,27 +14,11 @@ import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from import { VideoState } from '../../../../shared/models/videos' import { isVideoAbuseReasonValid } from '../video-abuses' -function sanitizeAndCheckVideoTorrentCreateActivity (activity: any) { - return isBaseActivityValid(activity, 'Create') && - sanitizeAndCheckVideoTorrentObject(activity.object) -} - function sanitizeAndCheckVideoTorrentUpdateActivity (activity: any) { return isBaseActivityValid(activity, 'Update') && sanitizeAndCheckVideoTorrentObject(activity.object) } -function isVideoTorrentDeleteActivityValid (activity: any) { - return isBaseActivityValid(activity, 'Delete') -} - -function isVideoFlagValid (activity: any) { - return isBaseActivityValid(activity, 'Create') && - activity.object.type === 'Flag' && - isVideoAbuseReasonValid(activity.object.content) && - isActivityPubUrlValid(activity.object.object) -} - function isActivityPubVideoDurationValid (value: string) { // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration return exists(value) && @@ -103,11 +87,8 @@ function isRemoteVideoUrlValid (url: any) { // --------------------------------------------------------------------------- export { - sanitizeAndCheckVideoTorrentCreateActivity, sanitizeAndCheckVideoTorrentUpdateActivity, - isVideoTorrentDeleteActivityValid, isRemoteStringIdentifierValid, - isVideoFlagValid, sanitizeAndCheckVideoTorrentObject, isRemoteVideoUrlValid } diff --git a/server/helpers/custom-validators/activitypub/view.ts b/server/helpers/custom-validators/activitypub/view.ts index 7a3aca6f5..41d16469f 100644 --- a/server/helpers/custom-validators/activitypub/view.ts +++ b/server/helpers/custom-validators/activitypub/view.ts @@ -1,11 +1,11 @@ -import { isActivityPubUrlValid, isBaseActivityValid } from './misc' +import { isActivityPubUrlValid } from './misc' function isViewActivityValid (activity: any) { - return isBaseActivityValid(activity, 'Create') && - activity.object.type === 'View' && - isActivityPubUrlValid(activity.object.actor) && - isActivityPubUrlValid(activity.object.object) + return activity.type === 'View' && + isActivityPubUrlValid(activity.actor) && + isActivityPubUrlValid(activity.object) } + // --------------------------------------------------------------------------- export { -- cgit v1.2.3