]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/plugins.ts
Improve plugin package.json error message
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / plugins.ts
CommitLineData
345da516
C
1import { exists, isArray, isSafePath } from './misc'
2import * as validator from 'validator'
3import { PluginType } from '../../../shared/models/plugins/plugin.type'
4import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
5import { PluginPackageJson } from '../../../shared/models/plugins/plugin-package-json.model'
6import { isUrlValid } from './activitypub/misc'
7
8const PLUGINS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.PLUGINS
9
10function isPluginTypeValid (value: any) {
11 return exists(value) && validator.isInt('' + value) && PluginType[value] !== undefined
12}
13
14function isPluginNameValid (value: string) {
15 return exists(value) &&
16 validator.isLength(value, PLUGINS_CONSTRAINTS_FIELDS.NAME) &&
17 validator.matches(value, /^[a-z\-]+$/)
18}
19
f023a19c
C
20function 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
345da516
C
27function isPluginDescriptionValid (value: string) {
28 return exists(value) && validator.isLength(value, PLUGINS_CONSTRAINTS_FIELDS.DESCRIPTION)
29}
30
31function 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
39function isPluginEngineValid (engine: any) {
40 return exists(engine) && exists(engine.peertube)
41}
42
dba85a1e 43function isPluginHomepage (value: string) {
485b2fb2
C
44 return exists(value) && (!value || isUrlValid(value))
45}
46
47function isPluginBugs (value: string) {
48 return exists(value) && (!value || isUrlValid(value))
dba85a1e
C
49}
50
d75db01f 51function areStaticDirectoriesValid (staticDirs: any) {
345da516
C
52 if (!exists(staticDirs) || typeof staticDirs !== 'object') return false
53
54 for (const key of Object.keys(staticDirs)) {
55 if (!isSafePath(staticDirs[key])) return false
56 }
57
58 return true
59}
60
d75db01f 61function areClientScriptsValid (clientScripts: any[]) {
345da516
C
62 return isArray(clientScripts) &&
63 clientScripts.every(c => {
64 return isSafePath(c.script) && isArray(c.scopes)
65 })
66}
67
d75db01f
C
68function areTranslationPathsValid (translations: any) {
69 if (!exists(translations) || typeof translations !== 'object') return false
70
71 for (const key of Object.keys(translations)) {
72 if (!isSafePath(translations[key])) return false
73 }
74
75 return true
76}
77
78function areCSSPathsValid (css: any[]) {
345da516
C
79 return isArray(css) && css.every(c => isSafePath(c))
80}
81
503c6f44
C
82function isThemeNameValid (name: string) {
83 return isPluginNameValid(name)
7cd4d2ba
C
84}
85
345da516 86function isPackageJSONValid (packageJSON: PluginPackageJson, pluginType: PluginType) {
9157d598
C
87 let result = true
88 const badFields: string[] = []
89
90 if (!isNpmPluginNameValid(packageJSON.name)) {
91 result = false
92 badFields.push('name')
93 }
94
95 if (!isPluginDescriptionValid(packageJSON.description)) {
96 result = false
97 badFields.push('description')
98 }
99
100 if (!isPluginEngineValid(packageJSON.engine)) {
101 result = false
102 badFields.push('engine')
103 }
104
105 if (!isPluginHomepage(packageJSON.homepage)) {
106 result = false
107 badFields.push('homepage')
108 }
109
110 if (!exists(packageJSON.author)) {
111 result = false
112 badFields.push('author')
113 }
114
115 if (!isPluginBugs(packageJSON.bugs)) {
116 result = false
117 badFields.push('bugs')
118 }
119
120 if (pluginType === PluginType.PLUGIN && !isSafePath(packageJSON.library)) {
121 result = false
122 badFields.push('library')
123 }
124
125 if (!areStaticDirectoriesValid(packageJSON.staticDirs)) {
126 result = false
127 badFields.push('staticDirs')
128 }
129
130 if (!areCSSPathsValid(packageJSON.css)) {
131 result = false
132 badFields.push('css')
133 }
134
135 if (!areClientScriptsValid(packageJSON.clientScripts)) {
136 result = false
137 badFields.push('clientScripts')
138 }
139
140 if (!areTranslationPathsValid(packageJSON.translations)) {
141 result = false
142 badFields.push('translations')
143 }
144
145 return { result, badFields }
345da516
C
146}
147
148function isLibraryCodeValid (library: any) {
149 return typeof library.register === 'function'
150 && typeof library.unregister === 'function'
151}
152
153export {
154 isPluginTypeValid,
155 isPackageJSONValid,
503c6f44 156 isThemeNameValid,
dba85a1e 157 isPluginHomepage,
345da516
C
158 isPluginVersionValid,
159 isPluginNameValid,
160 isPluginDescriptionValid,
f023a19c
C
161 isLibraryCodeValid,
162 isNpmPluginNameValid
345da516 163}