1 import * as express from 'express'
2 import { body, param, query } from 'express-validator/check'
3 import { logger } from '../../helpers/logger'
4 import { areValidationErrors } from './utils'
5 import { isNpmPluginNameValid, isPluginNameValid, isPluginTypeValid, isPluginVersionValid } 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 { InstallOrUpdatePlugin } from '../../../shared/models/plugins/install-plugin.model'
10 import { PluginType } from '../../../shared/models/plugins/plugin.type'
12 const servePluginStaticDirectoryValidator = (pluginType: PluginType) => [
13 param('pluginName').custom(isPluginNameValid).withMessage('Should have a valid plugin name'),
14 param('pluginVersion').custom(isPluginVersionValid).withMessage('Should have a valid plugin version'),
15 param('staticEndpoint').custom(isSafePath).withMessage('Should have a valid static endpoint'),
17 (req: express.Request, res: express.Response, next: express.NextFunction) => {
18 logger.debug('Checking servePluginStaticDirectory parameters', { parameters: req.params })
20 if (areValidationErrors(req, res)) return
22 const npmName = PluginModel.buildNpmName(req.params.pluginName, pluginType)
23 const plugin = PluginManager.Instance.getRegisteredPluginOrTheme(npmName)
25 if (!plugin || plugin.version !== req.params.pluginVersion) {
26 return res.sendStatus(404)
29 res.locals.registeredPlugin = plugin
35 const listPluginsValidator = [
38 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
42 .custom(isBooleanValid).withMessage('Should have a valid uninstalled attribute'),
44 (req: express.Request, res: express.Response, next: express.NextFunction) => {
45 logger.debug('Checking listPluginsValidator parameters', { parameters: req.query })
47 if (areValidationErrors(req, res)) return
53 const installOrUpdatePluginValidator = [
56 .custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
59 .custom(isSafePath).withMessage('Should have a valid safe path'),
61 (req: express.Request, res: express.Response, next: express.NextFunction) => {
62 logger.debug('Checking installOrUpdatePluginValidator parameters', { parameters: req.body })
64 if (areValidationErrors(req, res)) return
66 const body: InstallOrUpdatePlugin = req.body
67 if (!body.path && !body.npmName) {
68 return res.status(400)
69 .json({ error: 'Should have either a npmName or a path' })
77 const uninstallPluginValidator = [
78 body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
80 (req: express.Request, res: express.Response, next: express.NextFunction) => {
81 logger.debug('Checking uninstallPluginValidator parameters', { parameters: req.body })
83 if (areValidationErrors(req, res)) return
89 const existingPluginValidator = [
90 param('npmName').custom(isPluginNameValid).withMessage('Should have a valid plugin name'),
92 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
93 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.params })
95 if (areValidationErrors(req, res)) return
97 const plugin = await PluginModel.loadByNpmName(req.params.npmName)
99 return res.status(404)
100 .json({ error: 'Plugin not found' })
104 res.locals.plugin = plugin
110 const updatePluginSettingsValidator = [
111 body('settings').exists().withMessage('Should have settings'),
113 (req: express.Request, res: express.Response, next: express.NextFunction) => {
114 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body })
116 if (areValidationErrors(req, res)) return
122 // ---------------------------------------------------------------------------
125 servePluginStaticDirectoryValidator,
126 updatePluginSettingsValidator,
127 uninstallPluginValidator,
128 existingPluginValidator,
129 installOrUpdatePluginValidator,