]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/plugins.ts
Cleanup useless express validator messages
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / plugins.ts
CommitLineData
41fb13c3 1import express from 'express'
5e2b2e27 2import { body, param, query, ValidationChain } from 'express-validator'
c0e8b12e 3import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
428ccb8b
C
4import { PluginType } from '../../../shared/models/plugins/plugin.type'
5import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/server/api/install-plugin.model'
6import { exists, isBooleanValid, isSafePath, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc'
b5f919ac 7import { isNpmPluginNameValid, isPluginNameValid, isPluginTypeValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins'
428ccb8b
C
8import { logger } from '../../helpers/logger'
9import { CONFIG } from '../../initializers/config'
345da516 10import { PluginManager } from '../../lib/plugins/plugin-manager'
ad91e700 11import { PluginModel } from '../../models/server/plugin'
10363c74 12import { areValidationErrors } from './shared'
345da516 13
5e2b2e27
C
14const getPluginValidator = (pluginType: PluginType, withVersion = true) => {
15 const validators: (ValidationChain | express.Handler)[] = [
396f6f01
C
16 param('pluginName')
17 .custom(isPluginNameValid)
5e2b2e27 18 ]
345da516 19
5e2b2e27
C
20 if (withVersion) {
21 validators.push(
396f6f01
C
22 param('pluginVersion')
23 .custom(isPluginVersionValid)
5e2b2e27
C
24 )
25 }
345da516 26
5e2b2e27
C
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
76148b27
RK
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 }
345da516 48
5e2b2e27 49 res.locals.registeredPlugin = plugin
345da516 50
5e2b2e27 51 return next()
345da516 52 }
5e2b2e27
C
53 ])
54}
55
4a8d113b 56const getExternalAuthValidator = [
396f6f01
C
57 param('authName')
58 .custom(exists),
4a8d113b
C
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
76148b27
RK
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 }
4a8d113b 72
1896bca0 73 const externalAuth = plugin.registerHelpers.getExternalAuths().find(a => a.authName === req.params.authName)
76148b27
RK
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 }
4a8d113b
C
80
81 res.locals.externalAuth = externalAuth
82
83 return next()
84 }
85]
86
5e2b2e27 87const pluginStaticDirectoryValidator = [
396f6f01
C
88 param('staticEndpoint')
89 .custom(isSafePath),
345da516 90
5e2b2e27
C
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
345da516
C
95
96 return next()
97 }
98]
99
ad91e700 100const listPluginsValidator = [
6702a1b2 101 query('pluginType')
ad91e700 102 .optional()
a02b93ce 103 .customSanitizer(toIntOrNull)
396f6f01 104 .custom(isPluginTypeValid),
ad91e700
C
105 query('uninstalled')
106 .optional()
c8861d5d 107 .customSanitizer(toBooleanOrNull)
396f6f01 108 .custom(isBooleanValid),
ad91e700
C
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
b5f919ac 119const installOrUpdatePluginValidator = [
8d2be0ed
C
120 body('npmName')
121 .optional()
396f6f01 122 .custom(isNpmPluginNameValid),
3a1157a6
JL
123 body('pluginVersion')
124 .optional()
396f6f01 125 .custom(isPluginVersionValid),
8d2be0ed
C
126 body('path')
127 .optional()
396f6f01 128 .custom(isSafePath),
ad91e700
C
129
130 (req: express.Request, res: express.Response, next: express.NextFunction) => {
b5f919ac 131 logger.debug('Checking installOrUpdatePluginValidator parameters', { parameters: req.body })
ad91e700
C
132
133 if (areValidationErrors(req, res)) return
134
b5f919ac 135 const body: InstallOrUpdatePlugin = req.body
8d2be0ed 136 if (!body.path && !body.npmName) {
76148b27 137 return res.fail({ message: 'Should have either a npmName or a path' })
8d2be0ed 138 }
3a1157a6
JL
139 if (body.pluginVersion && !body.npmName) {
140 return res.fail({ message: 'Should have a npmName when specifying a pluginVersion' })
141 }
8d2be0ed 142
ad91e700
C
143 return next()
144 }
145]
146
147const uninstallPluginValidator = [
396f6f01
C
148 body('npmName')
149 .custom(isNpmPluginNameValid),
ad91e700
C
150
151 (req: express.Request, res: express.Response, next: express.NextFunction) => {
dba85a1e 152 logger.debug('Checking uninstallPluginValidator parameters', { parameters: req.body })
ad91e700
C
153
154 if (areValidationErrors(req, res)) return
155
156 return next()
157 }
158]
159
dba85a1e 160const existingPluginValidator = [
396f6f01
C
161 param('npmName')
162 .custom(isNpmPluginNameValid),
ad91e700
C
163
164 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
dba85a1e 165 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.params })
ad91e700
C
166
167 if (areValidationErrors(req, res)) return
168
dba85a1e 169 const plugin = await PluginModel.loadByNpmName(req.params.npmName)
ad91e700 170 if (!plugin) {
76148b27
RK
171 return res.fail({
172 status: HttpStatusCode.NOT_FOUND_404,
173 message: 'Plugin not found'
174 })
ad91e700
C
175 }
176
177 res.locals.plugin = plugin
ad91e700
C
178 return next()
179 }
180]
181
182const updatePluginSettingsValidator = [
396f6f01
C
183 body('settings')
184 .exists(),
ad91e700
C
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
6702a1b2 195const listAvailablePluginsValidator = [
6702a1b2
C
196 query('search')
197 .optional()
396f6f01 198 .exists(),
6702a1b2
C
199 query('pluginType')
200 .optional()
a02b93ce 201 .customSanitizer(toIntOrNull)
396f6f01 202 .custom(isPluginTypeValid),
09071200
C
203 query('currentPeerTubeEngine')
204 .optional()
396f6f01 205 .custom(isPluginVersionValid),
6702a1b2
C
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) {
76148b27 213 return res.fail({ message: 'Plugin index is not enabled' })
6702a1b2
C
214 }
215
216 return next()
217 }
218]
219
345da516
C
220// ---------------------------------------------------------------------------
221
222export {
5e2b2e27
C
223 pluginStaticDirectoryValidator,
224 getPluginValidator,
ad91e700
C
225 updatePluginSettingsValidator,
226 uninstallPluginValidator,
6702a1b2 227 listAvailablePluginsValidator,
dba85a1e 228 existingPluginValidator,
b5f919ac 229 installOrUpdatePluginValidator,
4a8d113b
C
230 listPluginsValidator,
231 getExternalAuthValidator
345da516 232}