]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/plugins.ts
WIP plugins: list installed plugins in client
[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'
7cd4d2ba 7import { isThemeRegistered } from '../../lib/plugins/theme-utils'
345da516
C
8
9const PLUGINS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.PLUGINS
10
11function isPluginTypeValid (value: any) {
12 return exists(value) && validator.isInt('' + value) && PluginType[value] !== undefined
13}
14
15function isPluginNameValid (value: string) {
16 return exists(value) &&
17 validator.isLength(value, PLUGINS_CONSTRAINTS_FIELDS.NAME) &&
18 validator.matches(value, /^[a-z\-]+$/)
19}
20
f023a19c
C
21function isNpmPluginNameValid (value: string) {
22 return exists(value) &&
23 validator.isLength(value, PLUGINS_CONSTRAINTS_FIELDS.NAME) &&
24 validator.matches(value, /^[a-z\-]+$/) &&
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
44function isStaticDirectoriesValid (staticDirs: any) {
45 if (!exists(staticDirs) || typeof staticDirs !== 'object') return false
46
47 for (const key of Object.keys(staticDirs)) {
48 if (!isSafePath(staticDirs[key])) return false
49 }
50
51 return true
52}
53
54function isClientScriptsValid (clientScripts: any[]) {
55 return isArray(clientScripts) &&
56 clientScripts.every(c => {
57 return isSafePath(c.script) && isArray(c.scopes)
58 })
59}
60
61function isCSSPathsValid (css: any[]) {
62 return isArray(css) && css.every(c => isSafePath(c))
63}
64
7cd4d2ba
C
65function isThemeValid (name: string) {
66 return isPluginNameValid(name) && isThemeRegistered(name)
67}
68
345da516 69function isPackageJSONValid (packageJSON: PluginPackageJson, pluginType: PluginType) {
f023a19c 70 return isNpmPluginNameValid(packageJSON.name) &&
345da516
C
71 isPluginDescriptionValid(packageJSON.description) &&
72 isPluginEngineValid(packageJSON.engine) &&
73 isUrlValid(packageJSON.homepage) &&
74 exists(packageJSON.author) &&
75 isUrlValid(packageJSON.bugs) &&
76 (pluginType === PluginType.THEME || isSafePath(packageJSON.library)) &&
77 isStaticDirectoriesValid(packageJSON.staticDirs) &&
78 isCSSPathsValid(packageJSON.css) &&
79 isClientScriptsValid(packageJSON.clientScripts)
80}
81
82function isLibraryCodeValid (library: any) {
83 return typeof library.register === 'function'
84 && typeof library.unregister === 'function'
85}
86
87export {
88 isPluginTypeValid,
89 isPackageJSONValid,
7cd4d2ba 90 isThemeValid,
345da516
C
91 isPluginVersionValid,
92 isPluginNameValid,
93 isPluginDescriptionValid,
f023a19c
C
94 isLibraryCodeValid,
95 isNpmPluginNameValid
345da516 96}