From a3bdb70617312af71ec3bf201b588a17c8678cdb Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 1 Dec 2021 16:18:57 -0500 Subject: [PATCH] feat: ensure that all requests handled by NodeBB fall under the relative_path as configured --- src/middleware/index.js | 13 +++++++++++++ src/webserver.js | 2 ++ 2 files changed, 15 insertions(+) diff --git a/src/middleware/index.js b/src/middleware/index.js index ea22f285af..d42ef87436 100644 --- a/src/middleware/index.js +++ b/src/middleware/index.js @@ -59,6 +59,19 @@ middleware.ensureLoggedIn = (req, res, next) => { setImmediate(next); }; +middleware.ensureRelativePath = (req, res, next) => { + const prefix = nconf.get('relative_path'); + if (!prefix) { + return setImmediate(next); + } + + if (!req.path.startsWith(prefix)) { + return controllers.helpers.redirect(res, req.path); + } + + return next(); +}; + Object.assign(middleware, { admin: require('./admin'), ...require('./header'), diff --git a/src/webserver.js b/src/webserver.js index e75c59eb00..9dae76d496 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -138,6 +138,8 @@ function setupExpressApp(app) { app.use(compression()); } + app.use(middleware.ensureRelativePath); + app.get(`${relativePath}/ping`, pingController.ping); app.get(`${relativePath}/sping`, pingController.ping);