]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/plugins.ts
Disable openapi generation
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / plugins.ts
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'
7
8 const PLUGINS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.PLUGINS
9
10 function isPluginTypeValid (value: any) {
11 return exists(value) &&
12 (value === PluginType.PLUGIN || value === PluginType.THEME)
13 }
14
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]+$/)
19 }
20
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-'))
26 }
27
28 function isPluginDescriptionValid (value: string) {
29 return exists(value) && validator.isLength(value, PLUGINS_CONSTRAINTS_FIELDS.DESCRIPTION)
30 }
31
32 function 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
40 function isPluginEngineValid (engine: any) {
41 return exists(engine) && exists(engine.peertube)
42 }
43
44 function isPluginHomepage (value: string) {
45 return exists(value) && (!value || isUrlValid(value))
46 }
47
48 function isPluginBugs (value: string) {
49 return exists(value) && (!value || isUrlValid(value))
50 }
51
52 function areStaticDirectoriesValid (staticDirs: any) {
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
62 function areClientScriptsValid (clientScripts: any[]) {
63 return isArray(clientScripts) &&
64 clientScripts.every(c => {
65 return isSafePath(c.script) && isArray(c.scopes)
66 })
67 }
68
69 function 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
79 function areCSSPathsValid (css: any[]) {
80 return isArray(css) && css.every(c => isSafePath(c))
81 }
82
83 function isThemeNameValid (name: string) {
84 return isPluginNameValid(name)
85 }
86
87 function isPackageJSONValid (packageJSON: PluginPackageJson, pluginType: PluginType) {
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 }
147 }
148
149 function isLibraryCodeValid (library: any) {
150 return typeof library.register === 'function' &&
151 typeof library.unregister === 'function'
152 }
153
154 export {
155 isPluginTypeValid,
156 isPackageJSONValid,
157 isThemeNameValid,
158 isPluginHomepage,
159 isPluginVersionValid,
160 isPluginNameValid,
161 isPluginDescriptionValid,
162 isLibraryCodeValid,
163 isNpmPluginNameValid
164 }