1 import * as express from 'express'
2 import { body, param, query, ValidationChain } from 'express-validator'
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, toBooleanOrNull } 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'
11 import { CONFIG } from '../../initializers/config'
13 const getPluginValidator = (pluginType: PluginType, withVersion = true) => {
14 const validators: (ValidationChain | express.Handler)[] = [
15 param('pluginName').custom(isPluginNameValid).withMessage('Should have a valid plugin name')
20 param('pluginVersion').custom(isPluginVersionValid).withMessage('Should have a valid plugin version')
24 return validators.concat([
25 (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 logger.debug('Checking getPluginValidator parameters', { parameters: req.params })
28 if (areValidationErrors(req, res)) return
30 const npmName = PluginModel.buildNpmName(req.params.pluginName, pluginType)
31 const plugin = PluginManager.Instance.getRegisteredPluginOrTheme(npmName)
33 if (!plugin) return res.sendStatus(404)
34 if (withVersion && plugin.version !== req.params.pluginVersion) return res.sendStatus(404)
36 res.locals.registeredPlugin = plugin
43 const pluginStaticDirectoryValidator = [
44 param('staticEndpoint').custom(isSafePath).withMessage('Should have a valid static endpoint'),
46 (req: express.Request, res: express.Response, next: express.NextFunction) => {
47 logger.debug('Checking pluginStaticDirectoryValidator parameters', { parameters: req.params })
49 if (areValidationErrors(req, res)) return
55 const listPluginsValidator = [
58 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
61 .customSanitizer(toBooleanOrNull)
62 .custom(isBooleanValid).withMessage('Should have a valid uninstalled attribute'),
64 (req: express.Request, res: express.Response, next: express.NextFunction) => {
65 logger.debug('Checking listPluginsValidator parameters', { parameters: req.query })
67 if (areValidationErrors(req, res)) return
73 const installOrUpdatePluginValidator = [
76 .custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
79 .custom(isSafePath).withMessage('Should have a valid safe path'),
81 (req: express.Request, res: express.Response, next: express.NextFunction) => {
82 logger.debug('Checking installOrUpdatePluginValidator parameters', { parameters: req.body })
84 if (areValidationErrors(req, res)) return
86 const body: InstallOrUpdatePlugin = req.body
87 if (!body.path && !body.npmName) {
88 return res.status(400)
89 .json({ error: 'Should have either a npmName or a path' })
97 const uninstallPluginValidator = [
98 body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
100 (req: express.Request, res: express.Response, next: express.NextFunction) => {
101 logger.debug('Checking uninstallPluginValidator parameters', { parameters: req.body })
103 if (areValidationErrors(req, res)) return
109 const existingPluginValidator = [
110 param('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid plugin name'),
112 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
113 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.params })
115 if (areValidationErrors(req, res)) return
117 const plugin = await PluginModel.loadByNpmName(req.params.npmName)
119 return res.status(404)
120 .json({ error: 'Plugin not found' })
124 res.locals.plugin = plugin
130 const updatePluginSettingsValidator = [
131 body('settings').exists().withMessage('Should have settings'),
133 (req: express.Request, res: express.Response, next: express.NextFunction) => {
134 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body })
136 if (areValidationErrors(req, res)) return
142 const listAvailablePluginsValidator = [
145 .exists().withMessage('Should have a valid search'),
148 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
149 query('currentPeerTubeEngine')
151 .custom(isPluginVersionValid).withMessage('Should have a valid current peertube engine'),
153 (req: express.Request, res: express.Response, next: express.NextFunction) => {
154 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.query })
156 if (areValidationErrors(req, res)) return
158 if (CONFIG.PLUGINS.INDEX.ENABLED === false) {
159 return res.status(400)
160 .json({ error: 'Plugin index is not enabled' })
168 // ---------------------------------------------------------------------------
171 pluginStaticDirectoryValidator,
173 updatePluginSettingsValidator,
174 uninstallPluginValidator,
175 listAvailablePluginsValidator,
176 existingPluginValidator,
177 installOrUpdatePluginValidator,