1 import { exists, isArray, isSafePath } from './misc'
2 import 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 isNpmPluginNameValid (value: string) {
21 return exists(value) &&
22 validator.isLength(value, PLUGINS_CONSTRAINTS_FIELDS.NAME) &&
23 validator.matches(value, /^[a-z\-._0-9]+$/) &&
24 (value.startsWith('peertube-plugin-') || value.startsWith('peertube-theme-'))
27 function isPluginDescriptionValid (value: string) {
28 return exists(value) && validator.isLength(value, PLUGINS_CONSTRAINTS_FIELDS.DESCRIPTION)
31 function isPluginVersionValid (value: string) {
32 if (!exists(value)) return false
34 const parts = (value + '').split('.')
36 return parts.length === 3 && parts.every(p => validator.isInt(p))
39 function isPluginEngineValid (engine: any) {
40 return exists(engine) && exists(engine.peertube)
43 function isPluginHomepage (value: string) {
44 return exists(value) && (!value || isUrlValid(value))
47 function isPluginBugs (value: string) {
48 return exists(value) && (!value || isUrlValid(value))
51 function areStaticDirectoriesValid (staticDirs: any) {
52 if (!exists(staticDirs) || typeof staticDirs !== 'object') return false
54 for (const key of Object.keys(staticDirs)) {
55 if (!isSafePath(staticDirs[key])) return false
61 function areClientScriptsValid (clientScripts: any[]) {
62 return isArray(clientScripts) &&
63 clientScripts.every(c => {
64 return isSafePath(c.script) && isArray(c.scopes)
68 function areTranslationPathsValid (translations: any) {
69 if (!exists(translations) || typeof translations !== 'object') return false
71 for (const key of Object.keys(translations)) {
72 if (!isSafePath(translations[key])) return false
78 function areCSSPathsValid (css: any[]) {
79 return isArray(css) && css.every(c => isSafePath(c))
82 function isThemeNameValid (name: string) {
83 return isPluginNameValid(name)
86 function isPackageJSONValid (packageJSON: PluginPackageJson, pluginType: PluginType) {
88 const badFields: string[] = []
90 if (!isNpmPluginNameValid(packageJSON.name)) {
92 badFields.push('name')
95 if (!isPluginDescriptionValid(packageJSON.description)) {
97 badFields.push('description')
100 if (!isPluginEngineValid(packageJSON.engine)) {
102 badFields.push('engine')
105 if (!isPluginHomepage(packageJSON.homepage)) {
107 badFields.push('homepage')
110 if (!exists(packageJSON.author)) {
112 badFields.push('author')
115 if (!isPluginBugs(packageJSON.bugs)) {
117 badFields.push('bugs')
120 if (pluginType === PluginType.PLUGIN && !isSafePath(packageJSON.library)) {
122 badFields.push('library')
125 if (!areStaticDirectoriesValid(packageJSON.staticDirs)) {
127 badFields.push('staticDirs')
130 if (!areCSSPathsValid(packageJSON.css)) {
132 badFields.push('css')
135 if (!areClientScriptsValid(packageJSON.clientScripts)) {
137 badFields.push('clientScripts')
140 if (!areTranslationPathsValid(packageJSON.translations)) {
142 badFields.push('translations')
145 return { result, badFields }
148 function isLibraryCodeValid (library: any) {
149 return typeof library.register === 'function' &&
150 typeof library.unregister === 'function'
158 isPluginVersionValid,
160 isPluginDescriptionValid,