1 import * as express from 'express'
2 import { body, param, query } 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 servePluginStaticDirectoryValidator = (pluginType: PluginType) => [
14 param('pluginName').custom(isPluginNameValid).withMessage('Should have a valid plugin name'),
15 param('pluginVersion').custom(isPluginVersionValid).withMessage('Should have a valid plugin version'),
16 param('staticEndpoint').custom(isSafePath).withMessage('Should have a valid static endpoint'),
18 (req: express.Request, res: express.Response, next: express.NextFunction) => {
19 logger.debug('Checking servePluginStaticDirectory parameters', { parameters: req.params })
21 if (areValidationErrors(req, res)) return
23 const npmName = PluginModel.buildNpmName(req.params.pluginName, pluginType)
24 const plugin = PluginManager.Instance.getRegisteredPluginOrTheme(npmName)
26 if (!plugin || plugin.version !== req.params.pluginVersion) {
27 return res.sendStatus(404)
30 res.locals.registeredPlugin = plugin
36 const listPluginsValidator = [
39 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
42 .customSanitizer(toBooleanOrNull)
43 .custom(isBooleanValid).withMessage('Should have a valid uninstalled attribute'),
45 (req: express.Request, res: express.Response, next: express.NextFunction) => {
46 logger.debug('Checking listPluginsValidator parameters', { parameters: req.query })
48 if (areValidationErrors(req, res)) return
54 const installOrUpdatePluginValidator = [
57 .custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
60 .custom(isSafePath).withMessage('Should have a valid safe path'),
62 (req: express.Request, res: express.Response, next: express.NextFunction) => {
63 logger.debug('Checking installOrUpdatePluginValidator parameters', { parameters: req.body })
65 if (areValidationErrors(req, res)) return
67 const body: InstallOrUpdatePlugin = req.body
68 if (!body.path && !body.npmName) {
69 return res.status(400)
70 .json({ error: 'Should have either a npmName or a path' })
78 const uninstallPluginValidator = [
79 body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
81 (req: express.Request, res: express.Response, next: express.NextFunction) => {
82 logger.debug('Checking uninstallPluginValidator parameters', { parameters: req.body })
84 if (areValidationErrors(req, res)) return
90 const existingPluginValidator = [
91 param('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid plugin name'),
93 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
94 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.params })
96 if (areValidationErrors(req, res)) return
98 const plugin = await PluginModel.loadByNpmName(req.params.npmName)
100 return res.status(404)
101 .json({ error: 'Plugin not found' })
105 res.locals.plugin = plugin
111 const updatePluginSettingsValidator = [
112 body('settings').exists().withMessage('Should have settings'),
114 (req: express.Request, res: express.Response, next: express.NextFunction) => {
115 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body })
117 if (areValidationErrors(req, res)) return
123 const listAvailablePluginsValidator = [
126 .exists().withMessage('Should have a valid search'),
129 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
130 query('currentPeerTubeEngine')
132 .custom(isPluginVersionValid).withMessage('Should have a valid current peertube engine'),
134 (req: express.Request, res: express.Response, next: express.NextFunction) => {
135 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.query })
137 if (areValidationErrors(req, res)) return
139 if (CONFIG.PLUGINS.INDEX.ENABLED === false) {
140 return res.status(400)
141 .json({ error: 'Plugin index is not enabled' })
149 // ---------------------------------------------------------------------------
152 servePluginStaticDirectoryValidator,
153 updatePluginSettingsValidator,
154 uninstallPluginValidator,
155 listAvailablePluginsValidator,
156 existingPluginValidator,
157 installOrUpdatePluginValidator,