]>
Commit | Line | Data |
---|---|---|
345da516 C |
1 | import { exists, isArray, isSafePath } from './misc' |
2 | import * as 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' | |
7cd4d2ba | 7 | import { isThemeRegistered } from '../../lib/plugins/theme-utils' |
345da516 C |
8 | |
9 | const PLUGINS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.PLUGINS | |
10 | ||
11 | function isPluginTypeValid (value: any) { | |
12 | return exists(value) && validator.isInt('' + value) && PluginType[value] !== undefined | |
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\-]+$/) | |
19 | } | |
20 | ||
f023a19c C |
21 | function 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 |
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 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 | ||
54 | function isClientScriptsValid (clientScripts: any[]) { | |
55 | return isArray(clientScripts) && | |
56 | clientScripts.every(c => { | |
57 | return isSafePath(c.script) && isArray(c.scopes) | |
58 | }) | |
59 | } | |
60 | ||
61 | function isCSSPathsValid (css: any[]) { | |
62 | return isArray(css) && css.every(c => isSafePath(c)) | |
63 | } | |
64 | ||
7cd4d2ba C |
65 | function isThemeValid (name: string) { |
66 | return isPluginNameValid(name) && isThemeRegistered(name) | |
67 | } | |
68 | ||
345da516 | 69 | function 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 | ||
82 | function isLibraryCodeValid (library: any) { | |
83 | return typeof library.register === 'function' | |
84 | && typeof library.unregister === 'function' | |
85 | } | |
86 | ||
87 | export { | |
88 | isPluginTypeValid, | |
89 | isPackageJSONValid, | |
7cd4d2ba | 90 | isThemeValid, |
345da516 C |
91 | isPluginVersionValid, |
92 | isPluginNameValid, | |
93 | isPluginDescriptionValid, | |
f023a19c C |
94 | isLibraryCodeValid, |
95 | isNpmPluginNameValid | |
345da516 | 96 | } |