Improve AP validation for Notes

pull/559/head
Chocobozzz 2018-05-11 15:41:54 +02:00
parent 0f320037e6
commit 5cf1350011
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
3 changed files with 25 additions and 11 deletions

View File

@ -1,16 +1,19 @@
import * as validator from 'validator' import * as validator from 'validator'
import { ACTIVITY_PUB } from '../../../initializers' import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers'
import { exists, isArray, isDateValid } from '../misc' import { exists, isArray, isDateValid } from '../misc'
import { isActivityPubUrlValid, isBaseActivityValid } from './misc' import { isActivityPubUrlValid, isBaseActivityValid } from './misc'
function isVideoCommentCreateActivityValid (activity: any) { function isVideoCommentCreateActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Create') && return isBaseActivityValid(activity, 'Create') &&
isVideoCommentObjectValid(activity.object) sanitizeAndCheckVideoCommentObject(activity.object)
} }
function isVideoCommentObjectValid (comment: any) { function sanitizeAndCheckVideoCommentObject (comment: any) {
return comment.type === 'Note' && if (comment.type !== 'Note') return false
isActivityPubUrlValid(comment.id) &&
normalizeComment(comment)
return isActivityPubUrlValid(comment.id) &&
isCommentContentValid(comment.content) && isCommentContentValid(comment.content) &&
isActivityPubUrlValid(comment.inReplyTo) && isActivityPubUrlValid(comment.inReplyTo) &&
isDateValid(comment.published) && isDateValid(comment.published) &&
@ -31,7 +34,7 @@ function isVideoCommentDeleteActivityValid (activity: any) {
export { export {
isVideoCommentCreateActivityValid, isVideoCommentCreateActivityValid,
isVideoCommentDeleteActivityValid, isVideoCommentDeleteActivityValid,
isVideoCommentObjectValid sanitizeAndCheckVideoCommentObject
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -39,3 +42,13 @@ export {
function isCommentContentValid (content: any) { function isCommentContentValid (content: any) {
return exists(content) && validator.isLength('' + content, { min: 1 }) return exists(content) && validator.isLength('' + content, { min: 1 })
} }
function normalizeComment (comment: any) {
if (!comment) return
if (!comment.url || typeof comment.url !== 'string') {
comment.url = comment.url.href || comment.url.url
}
return
}

View File

@ -43,13 +43,14 @@ function isActivityPubVideoDurationValid (value: string) {
} }
function sanitizeAndCheckVideoTorrentObject (video: any) { function sanitizeAndCheckVideoTorrentObject (video: any) {
if (video.type !== 'Video') return false
if (!setValidRemoteTags(video)) return false if (!setValidRemoteTags(video)) return false
if (!setValidRemoteVideoUrls(video)) return false if (!setValidRemoteVideoUrls(video)) return false
if (!setRemoteVideoTruncatedContent(video)) return false if (!setRemoteVideoTruncatedContent(video)) return false
if (!setValidAttributedTo(video)) return false if (!setValidAttributedTo(video)) return false
return video.type === 'Video' && return isActivityPubUrlValid(video.id) &&
isActivityPubUrlValid(video.id) &&
isVideoNameValid(video.name) && isVideoNameValid(video.name) &&
isActivityPubVideoDurationValid(video.duration) && isActivityPubVideoDurationValid(video.duration) &&
isUUIDValid(video.uuid) && isUUIDValid(video.uuid) &&

View File

@ -1,5 +1,5 @@
import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object' import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
import { isVideoCommentObjectValid } from '../../helpers/custom-validators/activitypub/video-comments' import { sanitizeAndCheckVideoCommentObject } from '../../helpers/custom-validators/activitypub/video-comments'
import { logger } from '../../helpers/logger' import { logger } from '../../helpers/logger'
import { doRequest } from '../../helpers/requests' import { doRequest } from '../../helpers/requests'
import { ACTIVITY_PUB } from '../../initializers' import { ACTIVITY_PUB } from '../../initializers'
@ -52,7 +52,7 @@ async function addVideoComment (videoInstance: VideoModel, commentUrl: string) {
activityPub: true activityPub: true
}) })
if (isVideoCommentObjectValid(body) === false) { if (sanitizeAndCheckVideoCommentObject(body) === false) {
logger.debug('Remote video comment JSON is not valid.', { body }) logger.debug('Remote video comment JSON is not valid.', { body })
return undefined return undefined
} }
@ -123,7 +123,7 @@ async function resolveThread (url: string, comments: VideoCommentModel[] = []) {
activityPub: true activityPub: true
}) })
if (isVideoCommentObjectValid(body) === false) { if (sanitizeAndCheckVideoCommentObject(body) === false) {
throw new Error('Remote video comment JSON is not valid :' + JSON.stringify(body)) throw new Error('Remote video comment JSON is not valid :' + JSON.stringify(body))
} }