]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/custom-validators/activitypub/actor.ts
Feature/filter already watched videos (#5739)
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / activitypub / actor.ts
index fa58e163f7b7d19880dfc37fbeba4775e4c90705..f43c35b239d22031f5b53d8756eaba89edda04de 100644 (file)
@@ -1,12 +1,12 @@
 import validator from 'validator'
 import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
-import { exists, isArray } from '../misc'
+import { exists, isArray, isDateValid } from '../misc'
 import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
 import { isHostValid } from '../servers'
 import { peertubeTruncate } from '@server/helpers/core-utils'
 
 function isActorEndpointsObjectValid (endpointObject: any) {
-  if (endpointObject && endpointObject.sharedInbox) {
+  if (endpointObject?.sharedInbox) {
     return isActivityPubUrlValid(endpointObject.sharedInbox)
   }
 
@@ -28,7 +28,7 @@ function isActorPublicKeyValid (publicKey: string) {
   return exists(publicKey) &&
     typeof publicKey === 'string' &&
     publicKey.startsWith('-----BEGIN PUBLIC KEY-----') &&
-    publicKey.indexOf('-----END PUBLIC KEY-----') !== -1 &&
+    publicKey.includes('-----END PUBLIC KEY-----') &&
     validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY)
 }
 
@@ -41,16 +41,31 @@ function isActorPreferredUsernameValid (preferredUsername: string) {
 function isActorPrivateKeyValid (privateKey: string) {
   return exists(privateKey) &&
     typeof privateKey === 'string' &&
-    privateKey.startsWith('-----BEGIN RSA PRIVATE KEY-----') &&
+    (privateKey.startsWith('-----BEGIN RSA PRIVATE KEY-----') || privateKey.startsWith('-----BEGIN PRIVATE KEY-----')) &&
     // Sometimes there is a \n at the end, so just assert the string contains the end mark
-    privateKey.indexOf('-----END RSA PRIVATE KEY-----') !== -1 &&
+    (privateKey.includes('-----END RSA PRIVATE KEY-----') || privateKey.includes('-----END PRIVATE KEY-----')) &&
     validator.isLength(privateKey, CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY)
 }
 
-function isActorObjectValid (actor: any) {
+function isActorFollowingCountValid (value: string) {
+  return exists(value) && validator.isInt('' + value, { min: 0 })
+}
+
+function isActorFollowersCountValid (value: string) {
+  return exists(value) && validator.isInt('' + value, { min: 0 })
+}
+
+function isActorDeleteActivityValid (activity: any) {
+  return isBaseActivityValid(activity, 'Delete')
+}
+
+function sanitizeAndCheckActorObject (actor: any) {
+  if (!isActorTypeValid(actor.type)) return false
+
+  normalizeActor(actor)
+
   return exists(actor) &&
     isActivityPubUrlValid(actor.id) &&
-    isActorTypeValid(actor.type) &&
     isActivityPubUrlValid(actor.inbox) &&
     isActorPreferredUsernameValid(actor.preferredUsername) &&
     isActivityPubUrlValid(actor.url) &&
@@ -62,29 +77,12 @@ function isActorObjectValid (actor: any) {
     (!actor.followers || isActivityPubUrlValid(actor.followers)) &&
 
     setValidAttributedTo(actor) &&
+    setValidDescription(actor) &&
     // If this is a group (a channel), it should be attributed to an account
     // In PeerTube we use this to attach a video channel to a specific account
     (actor.type !== 'Group' || actor.attributedTo.length !== 0)
 }
 
-function isActorFollowingCountValid (value: string) {
-  return exists(value) && validator.isInt('' + value, { min: 0 })
-}
-
-function isActorFollowersCountValid (value: string) {
-  return exists(value) && validator.isInt('' + value, { min: 0 })
-}
-
-function isActorDeleteActivityValid (activity: any) {
-  return isBaseActivityValid(activity, 'Delete')
-}
-
-function sanitizeAndCheckActorObject (object: any) {
-  normalizeActor(object)
-
-  return isActorObjectValid(object)
-}
-
 function normalizeActor (actor: any) {
   if (!actor) return
 
@@ -94,6 +92,8 @@ function normalizeActor (actor: any) {
     actor.url = actor.url.href || actor.url.url
   }
 
+  if (!isDateValid(actor.published)) actor.published = undefined
+
   if (actor.summary && typeof actor.summary === 'string') {
     actor.summary = peertubeTruncate(actor.summary, { length: CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max })
 
@@ -101,8 +101,6 @@ function normalizeActor (actor: any) {
       actor.summary = null
     }
   }
-
-  return
 }
 
 function isValidActorHandle (handle: string) {
@@ -118,6 +116,12 @@ function areValidActorHandles (handles: string[]) {
   return isArray(handles) && handles.every(h => isValidActorHandle(h))
 }
 
+function setValidDescription (obj: any) {
+  if (!obj.summary) obj.summary = null
+
+  return true
+}
+
 // ---------------------------------------------------------------------------
 
 export {
@@ -130,7 +134,6 @@ export {
   isActorPublicKeyValid,
   isActorPreferredUsernameValid,
   isActorPrivateKeyValid,
-  isActorObjectValid,
   isActorFollowingCountValid,
   isActorFollowersCountValid,
   isActorDeleteActivityValid,