]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/custom-validators/misc.ts
Feature/filter already watched videos (#5739)
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / misc.ts
index c80c861932716e395574c3d76736bbd6915c85fd..ebab4c6b244ba1277a0424c089e535fd9faac131 100644 (file)
@@ -15,6 +15,10 @@ function isSafePath (p: string) {
     })
 }
 
+function isSafePeerTubeFilenameWithoutExtension (filename: string) {
+  return filename.match(/^[a-z0-9-]+$/)
+}
+
 function isArray (value: any): value is any[] {
   return Array.isArray(value)
 }
@@ -86,7 +90,7 @@ function isFileValid (options: {
 
   // The file exists
   const file = fileArray[0]
-  if (!file || !file.originalname) return false
+  if (!file?.originalname) return false
 
   // Check size
   if ((maxSize !== null) && file.size > maxSize) return false
@@ -103,7 +107,13 @@ function checkMimetypeRegex (fileMimeType: string, mimeTypeRegex: string) {
 // ---------------------------------------------------------------------------
 
 function toCompleteUUID (value: string) {
-  if (isShortUUID(value)) return shortToUUID(value)
+  if (isShortUUID(value)) {
+    try {
+      return shortToUUID(value)
+    } catch {
+      return null
+    }
+  }
 
   return value
 }
@@ -136,12 +146,6 @@ function toValueOrNull (value: string) {
   return value
 }
 
-function toArray (value: any) {
-  if (value && isArray(value) === false) return [ value ]
-
-  return value
-}
-
 function toIntArray (value: any) {
   if (!value) return []
   if (isArray(value) === false) return [ validator.toInt(value) ]
@@ -170,8 +174,8 @@ export {
   isBooleanValid,
   toIntOrNull,
   areUUIDsValid,
-  toArray,
   toIntArray,
   isFileValid,
+  isSafePeerTubeFilenameWithoutExtension,
   checkMimetypeRegex
 }