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) &&
12 (value === PluginType.PLUGIN || value === PluginType.THEME)
15 function isPluginNameValid (value: string) {
16 return exists(value) &&
17 validator.isLength(value, PLUGINS_CONSTRAINTS_FIELDS.NAME) &&
18 validator.matches(value, /^[a-z-0-9]+$/)
21 function isNpmPluginNameValid (value: string) {
22 return exists(value) &&
23 validator.isLength(value, PLUGINS_CONSTRAINTS_FIELDS.NAME) &&
24 validator.matches(value, /^[a-z\-._0-9]+$/) &&
25 (value.startsWith('peertube-plugin-') || value.startsWith('peertube-theme-'))
28 function isPluginDescriptionValid (value: string) {
29 return exists(value) && validator.isLength(value, PLUGINS_CONSTRAINTS_FIELDS.DESCRIPTION)
32 function isPluginVersionValid (value: string) {
33 if (!exists(value)) return false
35 const parts = (value + '').split('.')
37 return parts.length === 3 && parts.every(p => validator.isInt(p))
40 function isPluginEngineValid (engine: any) {
41 return exists(engine) && exists(engine.peertube)
44 function isPluginHomepage (value: string) {
45 return exists(value) && (!value || isUrlValid(value))
48 function isPluginBugs (value: string) {
49 return exists(value) && (!value || isUrlValid(value))
52 function areStaticDirectoriesValid (staticDirs: any) {
53 if (!exists(staticDirs) || typeof staticDirs !== 'object') return false
55 for (const key of Object.keys(staticDirs)) {
56 if (!isSafePath(staticDirs[key])) return false
62 function areClientScriptsValid (clientScripts: any[]) {
63 return isArray(clientScripts) &&
64 clientScripts.every(c => {
65 return isSafePath(c.script) && isArray(c.scopes)
69 function areTranslationPathsValid (translations: any) {
70 if (!exists(translations) || typeof translations !== 'object') return false
72 for (const key of Object.keys(translations)) {
73 if (!isSafePath(translations[key])) return false
79 function areCSSPathsValid (css: any[]) {
80 return isArray(css) && css.every(c => isSafePath(c))
83 function isThemeNameValid (name: string) {
84 return isPluginNameValid(name)
87 function isPackageJSONValid (packageJSON: PluginPackageJson, pluginType: PluginType) {
89 const badFields: string[] = []
91 if (!isNpmPluginNameValid(packageJSON.name)) {
93 badFields.push('name')
96 if (!isPluginDescriptionValid(packageJSON.description)) {
98 badFields.push('description')
101 if (!isPluginEngineValid(packageJSON.engine)) {
103 badFields.push('engine')
106 if (!isPluginHomepage(packageJSON.homepage)) {
108 badFields.push('homepage')
111 if (!exists(packageJSON.author)) {
113 badFields.push('author')
116 if (!isPluginBugs(packageJSON.bugs)) {
118 badFields.push('bugs')
121 if (pluginType === PluginType.PLUGIN && !isSafePath(packageJSON.library)) {
123 badFields.push('library')
126 if (!areStaticDirectoriesValid(packageJSON.staticDirs)) {
128 badFields.push('staticDirs')
131 if (!areCSSPathsValid(packageJSON.css)) {
133 badFields.push('css')
136 if (!areClientScriptsValid(packageJSON.clientScripts)) {
138 badFields.push('clientScripts')
141 if (!areTranslationPathsValid(packageJSON.translations)) {
143 badFields.push('translations')
146 return { result, badFields }
149 function isLibraryCodeValid (library: any) {
150 return typeof library.register === 'function' &&
151 typeof library.unregister === 'function'
159 isPluginVersionValid,
161 isPluginDescriptionValid,