X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fplugins.ts;h=51db1ad89f4c5798ffce0d50fd6e0275df2ccd1e;hb=2166c058f34dff6f91566930d12448805d829de7;hp=f12e1c0f596c36c09fd7db1ac6e85ca7222ae135;hpb=4a8d113b9b57d97ff13ad1608798eabca99643e4;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/plugins.ts b/server/controllers/plugins.ts index f12e1c0f5..51db1ad89 100644 --- a/server/controllers/plugins.ts +++ b/server/controllers/plugins.ts @@ -1,17 +1,19 @@ -import * as express from 'express' -import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants' +import express from 'express' import { join } from 'path' +import { logger } from '@server/helpers/logger' +import { optionalAuthenticate } from '@server/middlewares/auth' +import { getCompleteLocale, is18nLocale } from '../../shared/core-utils/i18n' +import { HttpStatusCode } from '../../shared/models/http/http-error-codes' +import { PluginType } from '../../shared/models/plugins/plugin.type' +import { isProdInstance } from '../helpers/core-utils' +import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants' import { PluginManager, RegisteredPlugin } from '../lib/plugins/plugin-manager' -import { getPluginValidator, pluginStaticDirectoryValidator, getExternalAuthValidator } from '../middlewares/validators/plugins' +import { getExternalAuthValidator, getPluginValidator, pluginStaticDirectoryValidator } from '../middlewares/validators/plugins' import { serveThemeCSSValidator } from '../middlewares/validators/themes' -import { PluginType } from '../../shared/models/plugins/plugin.type' -import { isTestInstance } from '../helpers/core-utils' -import { getCompleteLocale, is18nLocale } from '../../shared/models/i18n' -import { logger } from '@server/helpers/logger' const sendFileOptions = { maxAge: '30 days', - immutable: !isTestInstance() + immutable: isProdInstance() } const pluginsRouter = express.Router() @@ -44,11 +46,13 @@ pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEnd pluginsRouter.use('/plugins/:pluginName/router', getPluginValidator(PluginType.PLUGIN, false), + optionalAuthenticate, servePluginCustomRoutes ) pluginsRouter.use('/plugins/:pluginName/:pluginVersion/router', getPluginValidator(PluginType.PLUGIN), + optionalAuthenticate, servePluginCustomRoutes ) @@ -96,7 +100,7 @@ function getPluginTranslations (req: express.Request, res: express.Response) { return res.json(json) } - return res.sendStatus(404) + return res.status(HttpStatusCode.NOT_FOUND_404).end() } function servePluginStaticDirectory (req: express.Request, res: express.Response) { @@ -106,7 +110,7 @@ function servePluginStaticDirectory (req: express.Request, res: express.Response const [ directory, ...file ] = staticEndpoint.split('/') const staticPath = plugin.staticDirs[directory] - if (!staticPath) return res.sendStatus(404) + if (!staticPath) return res.status(HttpStatusCode.NOT_FOUND_404).end() const filepath = file.join('/') return res.sendFile(join(plugin.path, staticPath, filepath), sendFileOptions) @@ -116,7 +120,7 @@ function servePluginCustomRoutes (req: express.Request, res: express.Response, n const plugin: RegisteredPlugin = res.locals.registeredPlugin const router = PluginManager.Instance.getRouter(plugin.npmName) - if (!router) return res.sendStatus(404) + if (!router) return res.status(HttpStatusCode.NOT_FOUND_404).end() return router(req, res, next) } @@ -126,7 +130,7 @@ function servePluginClientScripts (req: express.Request, res: express.Response) const staticEndpoint = req.params.staticEndpoint const file = plugin.clientScripts[staticEndpoint] - if (!file) return res.sendStatus(404) + if (!file) return res.status(HttpStatusCode.NOT_FOUND_404).end() return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions) } @@ -136,7 +140,7 @@ function serveThemeCSSDirectory (req: express.Request, res: express.Response) { const staticEndpoint = req.params.staticEndpoint if (plugin.css.includes(staticEndpoint) === false) { - return res.sendStatus(404) + return res.status(HttpStatusCode.NOT_FOUND_404).end() } return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions) @@ -149,6 +153,6 @@ function handleAuthInPlugin (req: express.Request, res: express.Response) { logger.debug('Forwarding auth plugin request in %s of plugin %s.', authOptions.authName, res.locals.registeredPlugin.npmName) authOptions.onAuthRequest(req, res) } catch (err) { - logger.error('Forward request error in auth %s of plugin %s.', authOptions.authName, res.locals.registeredPlugin.npmName) + logger.error('Forward request error in auth %s of plugin %s.', authOptions.authName, res.locals.registeredPlugin.npmName, { err }) } }