aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/plugins.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/middlewares/validators/plugins.ts')
-rw-r--r--server/middlewares/validators/plugins.ts89
1 files changed, 85 insertions, 4 deletions
diff --git a/server/middlewares/validators/plugins.ts b/server/middlewares/validators/plugins.ts
index fcb461624..265ac7c17 100644
--- a/server/middlewares/validators/plugins.ts
+++ b/server/middlewares/validators/plugins.ts
@@ -1,10 +1,11 @@
1import * as express from 'express' 1import * as express from 'express'
2import { param } from 'express-validator/check' 2import { param, query, body } from 'express-validator/check'
3import { logger } from '../../helpers/logger' 3import { logger } from '../../helpers/logger'
4import { areValidationErrors } from './utils' 4import { areValidationErrors } from './utils'
5import { isPluginNameValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins' 5import { isPluginNameValid, isPluginTypeValid, isPluginVersionValid, isNpmPluginNameValid } from '../../helpers/custom-validators/plugins'
6import { PluginManager } from '../../lib/plugins/plugin-manager' 6import { PluginManager } from '../../lib/plugins/plugin-manager'
7import { isSafePath } from '../../helpers/custom-validators/misc' 7import { isBooleanValid, isSafePath } from '../../helpers/custom-validators/misc'
8import { PluginModel } from '../../models/server/plugin'
8 9
9const servePluginStaticDirectoryValidator = [ 10const servePluginStaticDirectoryValidator = [
10 param('pluginName').custom(isPluginNameValid).withMessage('Should have a valid plugin name'), 11 param('pluginName').custom(isPluginNameValid).withMessage('Should have a valid plugin name'),
@@ -28,8 +29,88 @@ const servePluginStaticDirectoryValidator = [
28 } 29 }
29] 30]
30 31
32const listPluginsValidator = [
33 query('type')
34 .optional()
35 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
36 query('uninstalled')
37 .optional()
38 .toBoolean()
39 .custom(isBooleanValid).withMessage('Should have a valid uninstalled attribute'),
40
41 (req: express.Request, res: express.Response, next: express.NextFunction) => {
42 logger.debug('Checking listPluginsValidator parameters', { parameters: req.query })
43
44 if (areValidationErrors(req, res)) return
45
46 return next()
47 }
48]
49
50const installPluginValidator = [
51 body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
52
53 (req: express.Request, res: express.Response, next: express.NextFunction) => {
54 logger.debug('Checking installPluginValidator parameters', { parameters: req.body })
55
56 if (areValidationErrors(req, res)) return
57
58 return next()
59 }
60]
61
62const uninstallPluginValidator = [
63 body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
64
65 (req: express.Request, res: express.Response, next: express.NextFunction) => {
66 logger.debug('Checking managePluginValidator parameters', { parameters: req.body })
67
68 if (areValidationErrors(req, res)) return
69
70 return next()
71 }
72]
73
74const enabledPluginValidator = [
75 body('name').custom(isPluginNameValid).withMessage('Should have a valid plugin name'),
76
77 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
78 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body })
79
80 if (areValidationErrors(req, res)) return
81
82 const plugin = await PluginModel.load(req.body.name)
83 if (!plugin) {
84 return res.status(404)
85 .json({ error: 'Plugin not found' })
86 .end()
87 }
88
89 res.locals.plugin = plugin
90
91 return next()
92 }
93]
94
95const updatePluginSettingsValidator = [
96 body('settings').exists().withMessage('Should have settings'),
97
98 (req: express.Request, res: express.Response, next: express.NextFunction) => {
99 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body })
100
101 if (areValidationErrors(req, res)) return
102
103 return next()
104 }
105]
106
31// --------------------------------------------------------------------------- 107// ---------------------------------------------------------------------------
32 108
33export { 109export {
34 servePluginStaticDirectoryValidator 110 servePluginStaticDirectoryValidator,
111 updatePluginSettingsValidator,
112 uninstallPluginValidator,
113 enabledPluginValidator,
114 installPluginValidator,
115 listPluginsValidator
35} 116}