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