From e34c85e527100c0b5c44567bd951e95be41b8d7e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 14 Nov 2017 10:57:56 +0100 Subject: Fix issues on server start --- server/helpers/custom-validators/activitypub/account.ts | 7 +++++-- server/helpers/custom-validators/activitypub/misc.ts | 10 +++++++++- server/helpers/custom-validators/activitypub/videos.ts | 7 ++++--- server/helpers/custom-validators/video-channels.ts | 8 +++++++- server/helpers/custom-validators/videos.ts | 6 ++++++ 5 files changed, 31 insertions(+), 7 deletions(-) (limited to 'server/helpers/custom-validators') diff --git a/server/helpers/custom-validators/activitypub/account.ts b/server/helpers/custom-validators/activitypub/account.ts index 8a7d1b7fe..acd2b8058 100644 --- a/server/helpers/custom-validators/activitypub/account.ts +++ b/server/helpers/custom-validators/activitypub/account.ts @@ -3,6 +3,7 @@ import * as validator from 'validator' import { exists, isUUIDValid } from '../misc' import { isActivityPubUrlValid } from './misc' import { isUserUsernameValid } from '../users' +import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' function isAccountEndpointsObjectValid (endpointObject: any) { return isAccountSharedInboxValid(endpointObject.sharedInbox) @@ -34,7 +35,8 @@ function isAccountPublicKeyValid (publicKey: string) { return exists(publicKey) && typeof publicKey === 'string' && publicKey.startsWith('-----BEGIN PUBLIC KEY-----') && - publicKey.endsWith('-----END PUBLIC KEY-----') + publicKey.endsWith('-----END PUBLIC KEY-----') && + validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY) } function isAccountIdValid (id: string) { @@ -73,7 +75,8 @@ function isAccountPrivateKeyValid (privateKey: string) { return exists(privateKey) && typeof privateKey === 'string' && privateKey.startsWith('-----BEGIN RSA PRIVATE KEY-----') && - privateKey.endsWith('-----END RSA PRIVATE KEY-----') + privateKey.endsWith('-----END RSA PRIVATE KEY-----') && + validator.isLength(privateKey, CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY) } function isRemoteAccountValid (remoteAccount: any) { diff --git a/server/helpers/custom-validators/activitypub/misc.ts b/server/helpers/custom-validators/activitypub/misc.ts index f049f5a8c..a94c36b51 100644 --- a/server/helpers/custom-validators/activitypub/misc.ts +++ b/server/helpers/custom-validators/activitypub/misc.ts @@ -1,4 +1,7 @@ +import * as validator from 'validator' import { exists } from '../misc' +import { isTestInstance } from '../../core-utils' +import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' function isActivityPubUrlValid (url: string) { const isURLOptions = { @@ -9,7 +12,12 @@ function isActivityPubUrlValid (url: string) { protocols: [ 'http', 'https' ] } - return exists(url) && validator.isURL(url, isURLOptions) + // We validate 'localhost', so we don't have the top level domain + if (isTestInstance()) { + isURLOptions.require_tld = false + } + + return exists(url) && validator.isURL(url, isURLOptions) && validator.isLength(url, CONSTRAINTS_FIELDS.ACCOUNTS.URL) } function isBaseActivityValid (activity: any, type: string) { diff --git a/server/helpers/custom-validators/activitypub/videos.ts b/server/helpers/custom-validators/activitypub/videos.ts index 9233a1359..8f6d50f50 100644 --- a/server/helpers/custom-validators/activitypub/videos.ts +++ b/server/helpers/custom-validators/activitypub/videos.ts @@ -10,7 +10,8 @@ import { isVideoTruncatedDescriptionValid, isVideoDurationValid, isVideoNameValid, - isVideoTagValid + isVideoTagValid, + isVideoUrlValid } from '../videos' import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../video-channels' import { isBaseActivityValid } from './misc' @@ -93,7 +94,7 @@ function isRemoteVideoContentValid (mediaType: string, content: string) { function isRemoteVideoIconValid (icon: any) { return icon.type === 'Image' && - validator.isURL(icon.url) && + isVideoUrlValid(icon.url) && icon.mediaType === 'image/jpeg' && validator.isInt(icon.width, { min: 0 }) && validator.isInt(icon.height, { min: 0 }) @@ -111,7 +112,7 @@ function setValidRemoteVideoUrls (video: any) { function isRemoteVideoUrlValid (url: any) { return url.type === 'Link' && ACTIVITY_PUB.VIDEO_URL_MIME_TYPES.indexOf(url.mimeType) !== -1 && - validator.isURL(url.url) && + isVideoUrlValid(url.url) && validator.isInt(url.width, { min: 0 }) && validator.isInt(url.size, { min: 0 }) } diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts index acc42f4a4..5787c3850 100644 --- a/server/helpers/custom-validators/video-channels.ts +++ b/server/helpers/custom-validators/video-channels.ts @@ -8,9 +8,14 @@ import { database as db, CONSTRAINTS_FIELDS } from '../../initializers' import { VideoChannelInstance } from '../../models' import { logger } from '../logger' import { exists } from './misc' +import { isActivityPubUrlValid } from './index' const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS +function isVideoChannelUrlValid (value: string) { + return isActivityPubUrlValid(value) +} + function isVideoChannelDescriptionValid (value: string) { return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION) } @@ -53,5 +58,6 @@ export { isVideoChannelDescriptionValid, isVideoChannelNameValid, isVideoChannelUUIDValid, - checkVideoChannelExists + checkVideoChannelExists, + isVideoChannelUrlValid } diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index 487b3d646..715119cf6 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts @@ -19,6 +19,7 @@ import { isArray, exists } from './misc' import { VideoInstance } from '../../models' import { logger } from '../../helpers' import { VideoRateType } from '../../../shared' +import { isActivityPubUrlValid } from './activitypub/misc' const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES @@ -33,6 +34,10 @@ function isRemoteVideoCategoryValid (value: string) { return validator.isInt('' + value) } +function isVideoUrlValid (value: string) { + return isActivityPubUrlValid(value) +} + function isVideoLicenceValid (value: number) { return VIDEO_LICENCES[value] !== undefined } @@ -219,5 +224,6 @@ export { isVideoTagValid, isRemoteVideoCategoryValid, isRemoteVideoLicenceValid, + isVideoUrlValid, isRemoteVideoLanguageValid } -- cgit v1.2.3