]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/plugins.ts
Add peertube plugin index website models
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / plugins.ts
1 import { exists, isArray, isSafePath } from './misc'
2 import * as validator from 'validator'
3 import { PluginType } from '../../../shared/models/plugins/plugin.type'
4 import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
5 import { PluginPackageJson } from '../../../shared/models/plugins/plugin-package-json.model'
6 import { isUrlValid } from './activitypub/misc'
7
8 const PLUGINS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.PLUGINS
9
10 function isPluginTypeValid (value: any) {
11 return exists(value) && validator.isInt('' + value) && PluginType[value] !== undefined
12 }
13
14 function isPluginNameValid (value: string) {
15 return exists(value) &&
16 validator.isLength(value, PLUGINS_CONSTRAINTS_FIELDS.NAME) &&
17 validator.matches(value, /^[a-z\-]+$/)
18 }
19
20 function isNpmPluginNameValid (value: string) {
21 return exists(value) &&
22 validator.isLength(value, PLUGINS_CONSTRAINTS_FIELDS.NAME) &&
23 validator.matches(value, /^[a-z\-]+$/) &&
24 (value.startsWith('peertube-plugin-') || value.startsWith('peertube-theme-'))
25 }
26
27 function isPluginDescriptionValid (value: string) {
28 return exists(value) && validator.isLength(value, PLUGINS_CONSTRAINTS_FIELDS.DESCRIPTION)
29 }
30
31 function isPluginVersionValid (value: string) {
32 if (!exists(value)) return false
33
34 const parts = (value + '').split('.')
35
36 return parts.length === 3 && parts.every(p => validator.isInt(p))
37 }
38
39 function isPluginEngineValid (engine: any) {
40 return exists(engine) && exists(engine.peertube)
41 }
42
43 function isPluginHomepage (value: string) {
44 return isUrlValid(value)
45 }
46
47 function isStaticDirectoriesValid (staticDirs: any) {
48 if (!exists(staticDirs) || typeof staticDirs !== 'object') return false
49
50 for (const key of Object.keys(staticDirs)) {
51 if (!isSafePath(staticDirs[key])) return false
52 }
53
54 return true
55 }
56
57 function isClientScriptsValid (clientScripts: any[]) {
58 return isArray(clientScripts) &&
59 clientScripts.every(c => {
60 return isSafePath(c.script) && isArray(c.scopes)
61 })
62 }
63
64 function isCSSPathsValid (css: any[]) {
65 return isArray(css) && css.every(c => isSafePath(c))
66 }
67
68 function isThemeNameValid (name: string) {
69 return isPluginNameValid(name)
70 }
71
72 function isPackageJSONValid (packageJSON: PluginPackageJson, pluginType: PluginType) {
73 return isNpmPluginNameValid(packageJSON.name) &&
74 isPluginDescriptionValid(packageJSON.description) &&
75 isPluginEngineValid(packageJSON.engine) &&
76 isPluginHomepage(packageJSON.homepage) &&
77 exists(packageJSON.author) &&
78 isUrlValid(packageJSON.bugs) &&
79 (pluginType === PluginType.THEME || isSafePath(packageJSON.library)) &&
80 isStaticDirectoriesValid(packageJSON.staticDirs) &&
81 isCSSPathsValid(packageJSON.css) &&
82 isClientScriptsValid(packageJSON.clientScripts)
83 }
84
85 function isLibraryCodeValid (library: any) {
86 return typeof library.register === 'function'
87 && typeof library.unregister === 'function'
88 }
89
90 export {
91 isPluginTypeValid,
92 isPackageJSONValid,
93 isThemeNameValid,
94 isPluginHomepage,
95 isPluginVersionValid,
96 isPluginNameValid,
97 isPluginDescriptionValid,
98 isLibraryCodeValid,
99 isNpmPluginNameValid
100 }