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, exists } 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 getExternalAuthValidator = [
44 param('authName').custom(exists).withMessage('Should have a valid auth name'),
46 (req: express.Request, res: express.Response, next: express.NextFunction) => {
47 logger.debug('Checking getExternalAuthValidator parameters', { parameters: req.params })
49 if (areValidationErrors(req, res)) return
51 const plugin = res.locals.registeredPlugin
52 if (!plugin.registerHelpersStore) return res.sendStatus(404)
54 const externalAuth = plugin.registerHelpersStore.getExternalAuths().find(a => a.authName === req.params.authName)
55 if (!externalAuth) return res.sendStatus(404)
57 res.locals.externalAuth = externalAuth
63 const pluginStaticDirectoryValidator = [
64 param('staticEndpoint').custom(isSafePath).withMessage('Should have a valid static endpoint'),
66 (req: express.Request, res: express.Response, next: express.NextFunction) => {
67 logger.debug('Checking pluginStaticDirectoryValidator parameters', { parameters: req.params })
69 if (areValidationErrors(req, res)) return
75 const listPluginsValidator = [
78 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
81 .customSanitizer(toBooleanOrNull)
82 .custom(isBooleanValid).withMessage('Should have a valid uninstalled attribute'),
84 (req: express.Request, res: express.Response, next: express.NextFunction) => {
85 logger.debug('Checking listPluginsValidator parameters', { parameters: req.query })
87 if (areValidationErrors(req, res)) return
93 const installOrUpdatePluginValidator = [
96 .custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
99 .custom(isSafePath).withMessage('Should have a valid safe path'),
101 (req: express.Request, res: express.Response, next: express.NextFunction) => {
102 logger.debug('Checking installOrUpdatePluginValidator parameters', { parameters: req.body })
104 if (areValidationErrors(req, res)) return
106 const body: InstallOrUpdatePlugin = req.body
107 if (!body.path && !body.npmName) {
108 return res.status(400)
109 .json({ error: 'Should have either a npmName or a path' })
117 const uninstallPluginValidator = [
118 body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
120 (req: express.Request, res: express.Response, next: express.NextFunction) => {
121 logger.debug('Checking uninstallPluginValidator parameters', { parameters: req.body })
123 if (areValidationErrors(req, res)) return
129 const existingPluginValidator = [
130 param('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid plugin name'),
132 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
133 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.params })
135 if (areValidationErrors(req, res)) return
137 const plugin = await PluginModel.loadByNpmName(req.params.npmName)
139 return res.status(404)
140 .json({ error: 'Plugin not found' })
144 res.locals.plugin = plugin
150 const updatePluginSettingsValidator = [
151 body('settings').exists().withMessage('Should have settings'),
153 (req: express.Request, res: express.Response, next: express.NextFunction) => {
154 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body })
156 if (areValidationErrors(req, res)) return
162 const listAvailablePluginsValidator = [
165 .exists().withMessage('Should have a valid search'),
168 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
169 query('currentPeerTubeEngine')
171 .custom(isPluginVersionValid).withMessage('Should have a valid current peertube engine'),
173 (req: express.Request, res: express.Response, next: express.NextFunction) => {
174 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.query })
176 if (areValidationErrors(req, res)) return
178 if (CONFIG.PLUGINS.INDEX.ENABLED === false) {
179 return res.status(400)
180 .json({ error: 'Plugin index is not enabled' })
188 // ---------------------------------------------------------------------------
191 pluginStaticDirectoryValidator,
193 updatePluginSettingsValidator,
194 uninstallPluginValidator,
195 listAvailablePluginsValidator,
196 existingPluginValidator,
197 installOrUpdatePluginValidator,
198 listPluginsValidator,
199 getExternalAuthValidator