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'
8 const PLUGINS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.PLUGINS
10 function isPluginTypeValid (value: any) {
11 return exists(value) && validator.isInt('' + value) && PluginType[value] !== undefined
14 function isPluginNameValid (value: string) {
15 return exists(value) &&
16 validator.isLength(value, PLUGINS_CONSTRAINTS_FIELDS.NAME) &&
17 validator.matches(value, /^[a-z\-]+$/)
20 function isPluginDescriptionValid (value: string) {
21 return exists(value) && validator.isLength(value, PLUGINS_CONSTRAINTS_FIELDS.DESCRIPTION)
24 function isPluginVersionValid (value: string) {
25 if (!exists(value)) return false
27 const parts = (value + '').split('.')
29 return parts.length === 3 && parts.every(p => validator.isInt(p))
32 function isPluginEngineValid (engine: any) {
33 return exists(engine) && exists(engine.peertube)
36 function isStaticDirectoriesValid (staticDirs: any) {
37 if (!exists(staticDirs) || typeof staticDirs !== 'object') return false
39 for (const key of Object.keys(staticDirs)) {
40 if (!isSafePath(staticDirs[key])) return false
46 function isClientScriptsValid (clientScripts: any[]) {
47 return isArray(clientScripts) &&
48 clientScripts.every(c => {
49 return isSafePath(c.script) && isArray(c.scopes)
53 function isCSSPathsValid (css: any[]) {
54 return isArray(css) && css.every(c => isSafePath(c))
57 function isPackageJSONValid (packageJSON: PluginPackageJson, pluginType: PluginType) {
58 return isPluginNameValid(packageJSON.name) &&
59 isPluginDescriptionValid(packageJSON.description) &&
60 isPluginEngineValid(packageJSON.engine) &&
61 isUrlValid(packageJSON.homepage) &&
62 exists(packageJSON.author) &&
63 isUrlValid(packageJSON.bugs) &&
64 (pluginType === PluginType.THEME || isSafePath(packageJSON.library)) &&
65 isStaticDirectoriesValid(packageJSON.staticDirs) &&
66 isCSSPathsValid(packageJSON.css) &&
67 isClientScriptsValid(packageJSON.clientScripts)
70 function isLibraryCodeValid (library: any) {
71 return typeof library.register === 'function'
72 && typeof library.unregister === 'function'
80 isPluginDescriptionValid,