]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/plugins.ts
a1634ded466ecc6258449ccdbd259291fc2875b7
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / plugins.ts
1 import * as express from 'express'
2 import { param, query, body } from 'express-validator/check'
3 import { logger } from '../../helpers/logger'
4 import { areValidationErrors } from './utils'
5 import { isPluginNameValid, isPluginTypeValid, isPluginVersionValid, isNpmPluginNameValid } from '../../helpers/custom-validators/plugins'
6 import { PluginManager } from '../../lib/plugins/plugin-manager'
7 import { isBooleanValid, isSafePath } from '../../helpers/custom-validators/misc'
8 import { PluginModel } from '../../models/server/plugin'
9 import { InstallPlugin } from '../../../shared/models/plugins/install-plugin.model'
10
11 const servePluginStaticDirectoryValidator = [
12 param('pluginName').custom(isPluginNameValid).withMessage('Should have a valid plugin name'),
13 param('pluginVersion').custom(isPluginVersionValid).withMessage('Should have a valid plugin version'),
14 param('staticEndpoint').custom(isSafePath).withMessage('Should have a valid static endpoint'),
15
16 (req: express.Request, res: express.Response, next: express.NextFunction) => {
17 logger.debug('Checking servePluginStaticDirectory parameters', { parameters: req.params })
18
19 if (areValidationErrors(req, res)) return
20
21 const plugin = PluginManager.Instance.getRegisteredPluginOrTheme(req.params.pluginName)
22
23 if (!plugin || plugin.version !== req.params.pluginVersion) {
24 return res.sendStatus(404)
25 }
26
27 res.locals.registeredPlugin = plugin
28
29 return next()
30 }
31 ]
32
33 const listPluginsValidator = [
34 query('type')
35 .optional()
36 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
37 query('uninstalled')
38 .optional()
39 .toBoolean()
40 .custom(isBooleanValid).withMessage('Should have a valid uninstalled attribute'),
41
42 (req: express.Request, res: express.Response, next: express.NextFunction) => {
43 logger.debug('Checking listPluginsValidator parameters', { parameters: req.query })
44
45 if (areValidationErrors(req, res)) return
46
47 return next()
48 }
49 ]
50
51 const installPluginValidator = [
52 body('npmName')
53 .optional()
54 .custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
55 body('path')
56 .optional()
57 .custom(isSafePath).withMessage('Should have a valid safe path'),
58
59 (req: express.Request, res: express.Response, next: express.NextFunction) => {
60 logger.debug('Checking installPluginValidator parameters', { parameters: req.body })
61
62 if (areValidationErrors(req, res)) return
63
64 const body: InstallPlugin = req.body
65 if (!body.path && !body.npmName) {
66 return res.status(400)
67 .json({ error: 'Should have either a npmName or a path' })
68 .end()
69 }
70
71 return next()
72 }
73 ]
74
75 const uninstallPluginValidator = [
76 body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
77
78 (req: express.Request, res: express.Response, next: express.NextFunction) => {
79 logger.debug('Checking uninstallPluginValidator parameters', { parameters: req.body })
80
81 if (areValidationErrors(req, res)) return
82
83 return next()
84 }
85 ]
86
87 const existingPluginValidator = [
88 param('npmName').custom(isPluginNameValid).withMessage('Should have a valid plugin name'),
89
90 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
91 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.params })
92
93 if (areValidationErrors(req, res)) return
94
95 const plugin = await PluginModel.loadByNpmName(req.params.npmName)
96 if (!plugin) {
97 return res.status(404)
98 .json({ error: 'Plugin not found' })
99 .end()
100 }
101
102 res.locals.plugin = plugin
103
104 return next()
105 }
106 ]
107
108 const updatePluginSettingsValidator = [
109 body('settings').exists().withMessage('Should have settings'),
110
111 (req: express.Request, res: express.Response, next: express.NextFunction) => {
112 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body })
113
114 if (areValidationErrors(req, res)) return
115
116 return next()
117 }
118 ]
119
120 // ---------------------------------------------------------------------------
121
122 export {
123 servePluginStaticDirectoryValidator,
124 updatePluginSettingsValidator,
125 uninstallPluginValidator,
126 existingPluginValidator,
127 installPluginValidator,
128 listPluginsValidator
129 }