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