X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Fvalidators%2Fplugins.ts;h=c1e9ebefbb25f8aa2af3a9d6bf642426ab2d24a1;hb=ebee0c0427800e74800e21fd0e1d7550f7130270;hp=2c47ec5bbd03aef78f0a852c98cae228d2f7357a;hpb=5b1a6d45b5d6e50102e1c7c8c845401b76b11b4d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/validators/plugins.ts b/server/middlewares/validators/plugins.ts index 2c47ec5bb..c1e9ebefb 100644 --- a/server/middlewares/validators/plugins.ts +++ b/server/middlewares/validators/plugins.ts @@ -1,6 +1,6 @@ -import * as express from 'express' +import express from 'express' import { body, param, query, ValidationChain } from 'express-validator' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' +import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' import { PluginType } from '../../../shared/models/plugins/plugin.type' import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/server/api/install-plugin.model' import { exists, isBooleanValid, isSafePath, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc' @@ -9,7 +9,7 @@ import { logger } from '../../helpers/logger' import { CONFIG } from '../../initializers/config' import { PluginManager } from '../../lib/plugins/plugin-manager' import { PluginModel } from '../../models/server/plugin' -import { areValidationErrors } from './utils' +import { areValidationErrors } from './shared' const getPluginValidator = (pluginType: PluginType, withVersion = true) => { const validators: (ValidationChain | express.Handler)[] = [ @@ -31,8 +31,18 @@ const getPluginValidator = (pluginType: PluginType, withVersion = true) => { const npmName = PluginModel.buildNpmName(req.params.pluginName, pluginType) const plugin = PluginManager.Instance.getRegisteredPluginOrTheme(npmName) - if (!plugin) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) - if (withVersion && plugin.version !== req.params.pluginVersion) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) + if (!plugin) { + return res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'No plugin found named ' + npmName + }) + } + if (withVersion && plugin.version !== req.params.pluginVersion) { + return res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'No plugin found named ' + npmName + ' with version ' + req.params.pluginVersion + }) + } res.locals.registeredPlugin = plugin @@ -50,10 +60,20 @@ const getExternalAuthValidator = [ if (areValidationErrors(req, res)) return const plugin = res.locals.registeredPlugin - if (!plugin.registerHelpers) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) + if (!plugin.registerHelpers) { + return res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'No registered helpers were found for this plugin' + }) + } const externalAuth = plugin.registerHelpers.getExternalAuths().find(a => a.authName === req.params.authName) - if (!externalAuth) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) + if (!externalAuth) { + return res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'No external auths were found for this plugin' + }) + } res.locals.externalAuth = externalAuth @@ -96,6 +116,9 @@ const installOrUpdatePluginValidator = [ body('npmName') .optional() .custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'), + body('pluginVersion') + .optional() + .custom(isPluginVersionValid).withMessage('Should have a valid plugin version'), body('path') .optional() .custom(isSafePath).withMessage('Should have a valid safe path'), @@ -107,8 +130,10 @@ const installOrUpdatePluginValidator = [ const body: InstallOrUpdatePlugin = req.body if (!body.path && !body.npmName) { - return res.status(HttpStatusCode.BAD_REQUEST_400) - .json({ error: 'Should have either a npmName or a path' }) + return res.fail({ message: 'Should have either a npmName or a path' }) + } + if (body.pluginVersion && !body.npmName) { + return res.fail({ message: 'Should have a npmName when specifying a pluginVersion' }) } return next() @@ -137,12 +162,13 @@ const existingPluginValidator = [ const plugin = await PluginModel.loadByNpmName(req.params.npmName) if (!plugin) { - return res.status(HttpStatusCode.NOT_FOUND_404) - .json({ error: 'Plugin not found' }) + return res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'Plugin not found' + }) } res.locals.plugin = plugin - return next() } ] @@ -177,9 +203,7 @@ const listAvailablePluginsValidator = [ if (areValidationErrors(req, res)) return if (CONFIG.PLUGINS.INDEX.ENABLED === false) { - return res.status(HttpStatusCode.BAD_REQUEST_400) - .json({ error: 'Plugin index is not enabled' }) - .end() + return res.fail({ message: 'Plugin index is not enabled' }) } return next()