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