1 import validator from 'validator'
2 import { PluginPackageJSON } from '../../../shared/models/plugins/plugin-package-json.model'
3 import { PluginType } from '../../../shared/models/plugins/plugin.type'
4 import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
5 import { isUrlValid } from './activitypub/misc'
6 import { exists, isArray, isSafePath } from './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 isPluginStableVersionValid (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 isPluginStableOrUnstableVersionValid (value: string) {
41 if (!exists(value)) return false
43 // suffix is beta.x or alpha.x
44 const [ stable, suffix ] = value.split('-')
45 if (!isPluginStableVersionValid(stable)) return false
47 const suffixRegex = /^(rc|alpha|beta)\.\d+$/
48 if (suffix && !suffixRegex.test(suffix)) return false
53 function isPluginEngineValid (engine: any) {
54 return exists(engine) && exists(engine.peertube)
57 function isPluginHomepage (value: string) {
58 return exists(value) && (!value || isUrlValid(value))
61 function isPluginBugs (value: string) {
62 return exists(value) && (!value || isUrlValid(value))
65 function areStaticDirectoriesValid (staticDirs: any) {
66 if (!exists(staticDirs) || typeof staticDirs !== 'object') return false
68 for (const key of Object.keys(staticDirs)) {
69 if (!isSafePath(staticDirs[key])) return false
75 function areClientScriptsValid (clientScripts: any[]) {
76 return isArray(clientScripts) &&
77 clientScripts.every(c => {
78 return isSafePath(c.script) && isArray(c.scopes)
82 function areTranslationPathsValid (translations: any) {
83 if (!exists(translations) || typeof translations !== 'object') return false
85 for (const key of Object.keys(translations)) {
86 if (!isSafePath(translations[key])) return false
92 function areCSSPathsValid (css: any[]) {
93 return isArray(css) && css.every(c => isSafePath(c))
96 function isThemeNameValid (name: string) {
97 return isPluginNameValid(name)
100 function isPackageJSONValid (packageJSON: PluginPackageJSON, pluginType: PluginType) {
102 const badFields: string[] = []
104 if (!isNpmPluginNameValid(packageJSON.name)) {
106 badFields.push('name')
109 if (!isPluginDescriptionValid(packageJSON.description)) {
111 badFields.push('description')
114 if (!isPluginEngineValid(packageJSON.engine)) {
116 badFields.push('engine')
119 if (!isPluginHomepage(packageJSON.homepage)) {
121 badFields.push('homepage')
124 if (!exists(packageJSON.author)) {
126 badFields.push('author')
129 if (!isPluginBugs(packageJSON.bugs)) {
131 badFields.push('bugs')
134 if (pluginType === PluginType.PLUGIN && !isSafePath(packageJSON.library)) {
136 badFields.push('library')
139 if (!areStaticDirectoriesValid(packageJSON.staticDirs)) {
141 badFields.push('staticDirs')
144 if (!areCSSPathsValid(packageJSON.css)) {
146 badFields.push('css')
149 if (!areClientScriptsValid(packageJSON.clientScripts)) {
151 badFields.push('clientScripts')
154 if (!areTranslationPathsValid(packageJSON.translations)) {
156 badFields.push('translations')
159 return { result, badFields }
162 function isLibraryCodeValid (library: any) {
163 return typeof library.register === 'function' &&
164 typeof library.unregister === 'function'
172 isPluginStableVersionValid,
173 isPluginStableOrUnstableVersionValid,
175 isPluginDescriptionValid,