]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/plugins.ts
CLI: plugins install command accept a --plugin-version parameter. (#4599)
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / plugins.ts
1 import express from 'express'
2 import { body, param, query, ValidationChain } from 'express-validator'
3 import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
4 import { PluginType } from '../../../shared/models/plugins/plugin.type'
5 import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/server/api/install-plugin.model'
6 import { exists, isBooleanValid, isSafePath, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc'
7 import { isNpmPluginNameValid, isPluginNameValid, isPluginTypeValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins'
8 import { logger } from '../../helpers/logger'
9 import { CONFIG } from '../../initializers/config'
10 import { PluginManager } from '../../lib/plugins/plugin-manager'
11 import { PluginModel } from '../../models/server/plugin'
12 import { areValidationErrors } from './shared'
13
14 const getPluginValidator = (pluginType: PluginType, withVersion = true) => {
15 const validators: (ValidationChain | express.Handler)[] = [
16 param('pluginName').custom(isPluginNameValid).withMessage('Should have a valid plugin name')
17 ]
18
19 if (withVersion) {
20 validators.push(
21 param('pluginVersion').custom(isPluginVersionValid).withMessage('Should have a valid plugin version')
22 )
23 }
24
25 return validators.concat([
26 (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 logger.debug('Checking getPluginValidator parameters', { parameters: req.params })
28
29 if (areValidationErrors(req, res)) return
30
31 const npmName = PluginModel.buildNpmName(req.params.pluginName, pluginType)
32 const plugin = PluginManager.Instance.getRegisteredPluginOrTheme(npmName)
33
34 if (!plugin) {
35 return res.fail({
36 status: HttpStatusCode.NOT_FOUND_404,
37 message: 'No plugin found named ' + npmName
38 })
39 }
40 if (withVersion && plugin.version !== req.params.pluginVersion) {
41 return res.fail({
42 status: HttpStatusCode.NOT_FOUND_404,
43 message: 'No plugin found named ' + npmName + ' with version ' + req.params.pluginVersion
44 })
45 }
46
47 res.locals.registeredPlugin = plugin
48
49 return next()
50 }
51 ])
52 }
53
54 const getExternalAuthValidator = [
55 param('authName').custom(exists).withMessage('Should have a valid auth name'),
56
57 (req: express.Request, res: express.Response, next: express.NextFunction) => {
58 logger.debug('Checking getExternalAuthValidator parameters', { parameters: req.params })
59
60 if (areValidationErrors(req, res)) return
61
62 const plugin = res.locals.registeredPlugin
63 if (!plugin.registerHelpers) {
64 return res.fail({
65 status: HttpStatusCode.NOT_FOUND_404,
66 message: 'No registered helpers were found for this plugin'
67 })
68 }
69
70 const externalAuth = plugin.registerHelpers.getExternalAuths().find(a => a.authName === req.params.authName)
71 if (!externalAuth) {
72 return res.fail({
73 status: HttpStatusCode.NOT_FOUND_404,
74 message: 'No external auths were found for this plugin'
75 })
76 }
77
78 res.locals.externalAuth = externalAuth
79
80 return next()
81 }
82 ]
83
84 const pluginStaticDirectoryValidator = [
85 param('staticEndpoint').custom(isSafePath).withMessage('Should have a valid static endpoint'),
86
87 (req: express.Request, res: express.Response, next: express.NextFunction) => {
88 logger.debug('Checking pluginStaticDirectoryValidator parameters', { parameters: req.params })
89
90 if (areValidationErrors(req, res)) return
91
92 return next()
93 }
94 ]
95
96 const listPluginsValidator = [
97 query('pluginType')
98 .optional()
99 .customSanitizer(toIntOrNull)
100 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
101 query('uninstalled')
102 .optional()
103 .customSanitizer(toBooleanOrNull)
104 .custom(isBooleanValid).withMessage('Should have a valid uninstalled attribute'),
105
106 (req: express.Request, res: express.Response, next: express.NextFunction) => {
107 logger.debug('Checking listPluginsValidator parameters', { parameters: req.query })
108
109 if (areValidationErrors(req, res)) return
110
111 return next()
112 }
113 ]
114
115 const installOrUpdatePluginValidator = [
116 body('npmName')
117 .optional()
118 .custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
119 body('pluginVersion')
120 .optional()
121 .custom(isPluginVersionValid).withMessage('Should have a valid plugin version'),
122 body('path')
123 .optional()
124 .custom(isSafePath).withMessage('Should have a valid safe path'),
125
126 (req: express.Request, res: express.Response, next: express.NextFunction) => {
127 logger.debug('Checking installOrUpdatePluginValidator parameters', { parameters: req.body })
128
129 if (areValidationErrors(req, res)) return
130
131 const body: InstallOrUpdatePlugin = req.body
132 if (!body.path && !body.npmName) {
133 return res.fail({ message: 'Should have either a npmName or a path' })
134 }
135 if (body.pluginVersion && !body.npmName) {
136 return res.fail({ message: 'Should have a npmName when specifying a pluginVersion' })
137 }
138
139 return next()
140 }
141 ]
142
143 const uninstallPluginValidator = [
144 body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
145
146 (req: express.Request, res: express.Response, next: express.NextFunction) => {
147 logger.debug('Checking uninstallPluginValidator parameters', { parameters: req.body })
148
149 if (areValidationErrors(req, res)) return
150
151 return next()
152 }
153 ]
154
155 const existingPluginValidator = [
156 param('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid plugin name'),
157
158 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
159 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.params })
160
161 if (areValidationErrors(req, res)) return
162
163 const plugin = await PluginModel.loadByNpmName(req.params.npmName)
164 if (!plugin) {
165 return res.fail({
166 status: HttpStatusCode.NOT_FOUND_404,
167 message: 'Plugin not found'
168 })
169 }
170
171 res.locals.plugin = plugin
172 return next()
173 }
174 ]
175
176 const updatePluginSettingsValidator = [
177 body('settings').exists().withMessage('Should have settings'),
178
179 (req: express.Request, res: express.Response, next: express.NextFunction) => {
180 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body })
181
182 if (areValidationErrors(req, res)) return
183
184 return next()
185 }
186 ]
187
188 const listAvailablePluginsValidator = [
189 query('search')
190 .optional()
191 .exists().withMessage('Should have a valid search'),
192 query('pluginType')
193 .optional()
194 .customSanitizer(toIntOrNull)
195 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
196 query('currentPeerTubeEngine')
197 .optional()
198 .custom(isPluginVersionValid).withMessage('Should have a valid current peertube engine'),
199
200 (req: express.Request, res: express.Response, next: express.NextFunction) => {
201 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.query })
202
203 if (areValidationErrors(req, res)) return
204
205 if (CONFIG.PLUGINS.INDEX.ENABLED === false) {
206 return res.fail({ message: 'Plugin index is not enabled' })
207 }
208
209 return next()
210 }
211 ]
212
213 // ---------------------------------------------------------------------------
214
215 export {
216 pluginStaticDirectoryValidator,
217 getPluginValidator,
218 updatePluginSettingsValidator,
219 uninstallPluginValidator,
220 listAvailablePluginsValidator,
221 existingPluginValidator,
222 installOrUpdatePluginValidator,
223 listPluginsValidator,
224 getExternalAuthValidator
225 }