From 345da516fae80f24c90c2196e96393b489af2243 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 5 Jul 2019 13:54:32 +0200 Subject: WIP plugins: add ability to register plugins --- server/middlewares/validators/plugins.ts | 35 ++++++++++++++++++++++++++++ server/middlewares/validators/themes.ts | 39 ++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 server/middlewares/validators/plugins.ts create mode 100644 server/middlewares/validators/themes.ts (limited to 'server/middlewares') diff --git a/server/middlewares/validators/plugins.ts b/server/middlewares/validators/plugins.ts new file mode 100644 index 000000000..672299ee1 --- /dev/null +++ b/server/middlewares/validators/plugins.ts @@ -0,0 +1,35 @@ +import * as express from 'express' +import { param } from 'express-validator/check' +import { logger } from '../../helpers/logger' +import { areValidationErrors } from './utils' +import { isPluginNameValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins' +import { PluginManager } from '../../lib/plugins/plugin-manager' +import { isSafePath } from '../../helpers/custom-validators/misc' + +const servePluginStaticDirectoryValidator = [ + param('pluginName').custom(isPluginNameValid).withMessage('Should have a valid plugin name'), + param('pluginVersion').custom(isPluginVersionValid).withMessage('Should have a valid plugin version'), + param('staticEndpoint').custom(isSafePath).withMessage('Should have a valid static endpoint'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking servePluginStaticDirectory parameters', { parameters: req.params }) + + if (areValidationErrors(req, res)) return + + const plugin = PluginManager.Instance.getRegisteredPlugin(req.params.pluginName) + + if (!plugin || plugin.version !== req.params.pluginVersion) { + return res.sendStatus(404) + } + + res.locals.registeredPlugin = plugin + + return next() + } +] + +// --------------------------------------------------------------------------- + +export { + servePluginStaticDirectoryValidator +} diff --git a/server/middlewares/validators/themes.ts b/server/middlewares/validators/themes.ts new file mode 100644 index 000000000..642f2df78 --- /dev/null +++ b/server/middlewares/validators/themes.ts @@ -0,0 +1,39 @@ +import * as express from 'express' +import { param } from 'express-validator/check' +import { logger } from '../../helpers/logger' +import { areValidationErrors } from './utils' +import { isPluginNameValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins' +import { PluginManager } from '../../lib/plugins/plugin-manager' +import { isSafePath } from '../../helpers/custom-validators/misc' + +const serveThemeCSSValidator = [ + param('themeName').custom(isPluginNameValid).withMessage('Should have a valid theme name'), + param('themeVersion').custom(isPluginVersionValid).withMessage('Should have a valid theme version'), + param('staticEndpoint').custom(isSafePath).withMessage('Should have a valid static endpoint'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking serveThemeCSS parameters', { parameters: req.params }) + + if (areValidationErrors(req, res)) return + + const theme = PluginManager.Instance.getRegisteredTheme(req.params.themeName) + + if (!theme || theme.version !== req.params.themeVersion) { + return res.sendStatus(404) + } + + if (theme.css.includes(req.params.staticEndpoint) === false) { + return res.sendStatus(404) + } + + res.locals.registeredPlugin = theme + + return next() + } +] + +// --------------------------------------------------------------------------- + +export { + serveThemeCSSValidator +} -- cgit v1.2.3