1 import express from 'express'
2 import { body, param, query, ValidationChain } from 'express-validator'
3 import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
4 import { PluginType } from '../../../shared/models/plugins/plugin.type'
5 import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/server/api/install-plugin.model'
6 import { exists, isBooleanValid, isSafePath, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc'
7 import { isNpmPluginNameValid, isPluginNameValid, isPluginTypeValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins'
8 import { logger } from '../../helpers/logger'
9 import { CONFIG } from '../../initializers/config'
10 import { PluginManager } from '../../lib/plugins/plugin-manager'
11 import { PluginModel } from '../../models/server/plugin'
12 import { areValidationErrors } from './shared'
14 const getPluginValidator = (pluginType: PluginType, withVersion = true) => {
15 const validators: (ValidationChain | express.Handler)[] = [
16 param('pluginName').custom(isPluginNameValid).withMessage('Should have a valid plugin name')
21 param('pluginVersion').custom(isPluginVersionValid).withMessage('Should have a valid plugin version')
25 return validators.concat([
26 (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 logger.debug('Checking getPluginValidator parameters', { parameters: req.params })
29 if (areValidationErrors(req, res)) return
31 const npmName = PluginModel.buildNpmName(req.params.pluginName, pluginType)
32 const plugin = PluginManager.Instance.getRegisteredPluginOrTheme(npmName)
36 status: HttpStatusCode.NOT_FOUND_404,
37 message: 'No plugin found named ' + npmName
40 if (withVersion && plugin.version !== req.params.pluginVersion) {
42 status: HttpStatusCode.NOT_FOUND_404,
43 message: 'No plugin found named ' + npmName + ' with version ' + req.params.pluginVersion
47 res.locals.registeredPlugin = plugin
54 const getExternalAuthValidator = [
55 param('authName').custom(exists).withMessage('Should have a valid auth name'),
57 (req: express.Request, res: express.Response, next: express.NextFunction) => {
58 logger.debug('Checking getExternalAuthValidator parameters', { parameters: req.params })
60 if (areValidationErrors(req, res)) return
62 const plugin = res.locals.registeredPlugin
63 if (!plugin.registerHelpers) {
65 status: HttpStatusCode.NOT_FOUND_404,
66 message: 'No registered helpers were found for this plugin'
70 const externalAuth = plugin.registerHelpers.getExternalAuths().find(a => a.authName === req.params.authName)
73 status: HttpStatusCode.NOT_FOUND_404,
74 message: 'No external auths were found for this plugin'
78 res.locals.externalAuth = externalAuth
84 const pluginStaticDirectoryValidator = [
85 param('staticEndpoint').custom(isSafePath).withMessage('Should have a valid static endpoint'),
87 (req: express.Request, res: express.Response, next: express.NextFunction) => {
88 logger.debug('Checking pluginStaticDirectoryValidator parameters', { parameters: req.params })
90 if (areValidationErrors(req, res)) return
96 const listPluginsValidator = [
99 .customSanitizer(toIntOrNull)
100 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
103 .customSanitizer(toBooleanOrNull)
104 .custom(isBooleanValid).withMessage('Should have a valid uninstalled attribute'),
106 (req: express.Request, res: express.Response, next: express.NextFunction) => {
107 logger.debug('Checking listPluginsValidator parameters', { parameters: req.query })
109 if (areValidationErrors(req, res)) return
115 const installOrUpdatePluginValidator = [
118 .custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
119 body('pluginVersion')
121 .custom(isPluginVersionValid).withMessage('Should have a valid plugin version'),
124 .custom(isSafePath).withMessage('Should have a valid safe path'),
126 (req: express.Request, res: express.Response, next: express.NextFunction) => {
127 logger.debug('Checking installOrUpdatePluginValidator parameters', { parameters: req.body })
129 if (areValidationErrors(req, res)) return
131 const body: InstallOrUpdatePlugin = req.body
132 if (!body.path && !body.npmName) {
133 return res.fail({ message: 'Should have either a npmName or a path' })
135 if (body.pluginVersion && !body.npmName) {
136 return res.fail({ message: 'Should have a npmName when specifying a pluginVersion' })
143 const uninstallPluginValidator = [
144 body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
146 (req: express.Request, res: express.Response, next: express.NextFunction) => {
147 logger.debug('Checking uninstallPluginValidator parameters', { parameters: req.body })
149 if (areValidationErrors(req, res)) return
155 const existingPluginValidator = [
156 param('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid plugin name'),
158 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
159 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.params })
161 if (areValidationErrors(req, res)) return
163 const plugin = await PluginModel.loadByNpmName(req.params.npmName)
166 status: HttpStatusCode.NOT_FOUND_404,
167 message: 'Plugin not found'
171 res.locals.plugin = plugin
176 const updatePluginSettingsValidator = [
177 body('settings').exists().withMessage('Should have settings'),
179 (req: express.Request, res: express.Response, next: express.NextFunction) => {
180 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body })
182 if (areValidationErrors(req, res)) return
188 const listAvailablePluginsValidator = [
191 .exists().withMessage('Should have a valid search'),
194 .customSanitizer(toIntOrNull)
195 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
196 query('currentPeerTubeEngine')
198 .custom(isPluginVersionValid).withMessage('Should have a valid current peertube engine'),
200 (req: express.Request, res: express.Response, next: express.NextFunction) => {
201 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.query })
203 if (areValidationErrors(req, res)) return
205 if (CONFIG.PLUGINS.INDEX.ENABLED === false) {
206 return res.fail({ message: 'Plugin index is not enabled' })
213 // ---------------------------------------------------------------------------
216 pluginStaticDirectoryValidator,
218 updatePluginSettingsValidator,
219 uninstallPluginValidator,
220 listAvailablePluginsValidator,
221 existingPluginValidator,
222 installOrUpdatePluginValidator,
223 listPluginsValidator,
224 getExternalAuthValidator