]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/plugins.ts
replace numbers with typed http status codes (#3409)
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / plugins.ts
1 import * as express from 'express'
2 import { body, param, query, ValidationChain } from 'express-validator'
3 import { logger } from '../../helpers/logger'
4 import { areValidationErrors } from './utils'
5 import { isNpmPluginNameValid, isPluginNameValid, isPluginTypeValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins'
6 import { PluginManager } from '../../lib/plugins/plugin-manager'
7 import { isBooleanValid, isSafePath, toBooleanOrNull, exists, toIntOrNull } from '../../helpers/custom-validators/misc'
8 import { PluginModel } from '../../models/server/plugin'
9 import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/install-plugin.model'
10 import { PluginType } from '../../../shared/models/plugins/plugin.type'
11 import { CONFIG } from '../../initializers/config'
12 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
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) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
35 if (withVersion && plugin.version !== req.params.pluginVersion) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
36
37 res.locals.registeredPlugin = plugin
38
39 return next()
40 }
41 ])
42 }
43
44 const getExternalAuthValidator = [
45 param('authName').custom(exists).withMessage('Should have a valid auth name'),
46
47 (req: express.Request, res: express.Response, next: express.NextFunction) => {
48 logger.debug('Checking getExternalAuthValidator parameters', { parameters: req.params })
49
50 if (areValidationErrors(req, res)) return
51
52 const plugin = res.locals.registeredPlugin
53 if (!plugin.registerHelpersStore) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
54
55 const externalAuth = plugin.registerHelpersStore.getExternalAuths().find(a => a.authName === req.params.authName)
56 if (!externalAuth) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
57
58 res.locals.externalAuth = externalAuth
59
60 return next()
61 }
62 ]
63
64 const pluginStaticDirectoryValidator = [
65 param('staticEndpoint').custom(isSafePath).withMessage('Should have a valid static endpoint'),
66
67 (req: express.Request, res: express.Response, next: express.NextFunction) => {
68 logger.debug('Checking pluginStaticDirectoryValidator parameters', { parameters: req.params })
69
70 if (areValidationErrors(req, res)) return
71
72 return next()
73 }
74 ]
75
76 const listPluginsValidator = [
77 query('pluginType')
78 .optional()
79 .customSanitizer(toIntOrNull)
80 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
81 query('uninstalled')
82 .optional()
83 .customSanitizer(toBooleanOrNull)
84 .custom(isBooleanValid).withMessage('Should have a valid uninstalled attribute'),
85
86 (req: express.Request, res: express.Response, next: express.NextFunction) => {
87 logger.debug('Checking listPluginsValidator parameters', { parameters: req.query })
88
89 if (areValidationErrors(req, res)) return
90
91 return next()
92 }
93 ]
94
95 const installOrUpdatePluginValidator = [
96 body('npmName')
97 .optional()
98 .custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
99 body('path')
100 .optional()
101 .custom(isSafePath).withMessage('Should have a valid safe path'),
102
103 (req: express.Request, res: express.Response, next: express.NextFunction) => {
104 logger.debug('Checking installOrUpdatePluginValidator parameters', { parameters: req.body })
105
106 if (areValidationErrors(req, res)) return
107
108 const body: InstallOrUpdatePlugin = req.body
109 if (!body.path && !body.npmName) {
110 return res.status(HttpStatusCode.BAD_REQUEST_400)
111 .json({ error: 'Should have either a npmName or a path' })
112 .end()
113 }
114
115 return next()
116 }
117 ]
118
119 const uninstallPluginValidator = [
120 body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
121
122 (req: express.Request, res: express.Response, next: express.NextFunction) => {
123 logger.debug('Checking uninstallPluginValidator parameters', { parameters: req.body })
124
125 if (areValidationErrors(req, res)) return
126
127 return next()
128 }
129 ]
130
131 const existingPluginValidator = [
132 param('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid plugin name'),
133
134 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
135 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.params })
136
137 if (areValidationErrors(req, res)) return
138
139 const plugin = await PluginModel.loadByNpmName(req.params.npmName)
140 if (!plugin) {
141 return res.status(HttpStatusCode.NOT_FOUND_404)
142 .json({ error: 'Plugin not found' })
143 .end()
144 }
145
146 res.locals.plugin = plugin
147
148 return next()
149 }
150 ]
151
152 const updatePluginSettingsValidator = [
153 body('settings').exists().withMessage('Should have settings'),
154
155 (req: express.Request, res: express.Response, next: express.NextFunction) => {
156 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body })
157
158 if (areValidationErrors(req, res)) return
159
160 return next()
161 }
162 ]
163
164 const listAvailablePluginsValidator = [
165 query('search')
166 .optional()
167 .exists().withMessage('Should have a valid search'),
168 query('pluginType')
169 .optional()
170 .customSanitizer(toIntOrNull)
171 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
172 query('currentPeerTubeEngine')
173 .optional()
174 .custom(isPluginVersionValid).withMessage('Should have a valid current peertube engine'),
175
176 (req: express.Request, res: express.Response, next: express.NextFunction) => {
177 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.query })
178
179 if (areValidationErrors(req, res)) return
180
181 if (CONFIG.PLUGINS.INDEX.ENABLED === false) {
182 return res.status(HttpStatusCode.BAD_REQUEST_400)
183 .json({ error: 'Plugin index is not enabled' })
184 .end()
185 }
186
187 return next()
188 }
189 ]
190
191 // ---------------------------------------------------------------------------
192
193 export {
194 pluginStaticDirectoryValidator,
195 getPluginValidator,
196 updatePluginSettingsValidator,
197 uninstallPluginValidator,
198 listAvailablePluginsValidator,
199 existingPluginValidator,
200 installOrUpdatePluginValidator,
201 listPluginsValidator,
202 getExternalAuthValidator
203 }