Skip to content

Node.js based forum software built for the modern web

fix: custom routes bypassing /admin checks

Barış Soner Uşaklı19c7473b48fc
diff --git a/src/routes/index.js b/src/routes/index.js
index 95f3ed58e3..d9d9bcda1a 100644
--- a/src/routes/index.js
+++ b/src/routes/index.js
@@ -132,13 +132,15 @@ module.exports = async function (app, middleware) {
});
router.all('(/+api|/+api/*?)', middleware.prepareAPI);
+
+ // handle custom homepage routes
+ router.use('/', controllers.home.rewrite);
+
router.all(`(/+api/admin|/+api/admin/*?${mounts.admin !== 'admin' ? `|/+api/${mounts.admin}|/+api/${mounts.admin}/*?` : ''})`, middleware.authenticateRequest, middleware.ensureLoggedIn, middleware.admin.checkPrivileges);
router.all(`(/+admin|/+admin/*?${mounts.admin !== 'admin' ? `|/+${mounts.admin}|/+${mounts.admin}/*?` : ''})`, middleware.ensureLoggedIn, middleware.applyCSRF, middleware.admin.checkPrivileges);
- app.use(middleware.stripLeadingSlashes);
- // handle custom homepage routes
- router.use('/', controllers.home.rewrite);
+ app.use(middleware.stripLeadingSlashes);
// homepage handled by `action:homepage.get:[route]`
setupPageRoute(router, '/', [], controllers.home.pluginHook);
diff --git a/test/controllers.js b/test/controllers.js
index 4cfd77dc6d..cf60dad6a7 100644
--- a/test/controllers.js
+++ b/test/controllers.js
@@ -153,6 +153,20 @@ describe('Controllers', () => {
assert(body);
});
+ it('should 403 if route requires privileges', async () => {
+ const uid = await user.create({ username: 'testuser1', password: 'password' });
+ const { jar } = await helpers.loginUser('testuser1', 'password');
+ await api.users.updateSettings({ uid }, {
+ uid,
+ settings: {
+ homePageRoute: 'custom',
+ homePageCustom: 'api/admin/advanced/database',
+ },
+ });
+ const { response, body } = await request.get(nconf.get('url'), { jar });
+ assert.equal(response.statusCode, 403);
+ });
+
it('api should work with hook', async () => {
await meta.configs.set('homePageRoute', 'mycustompage');
const { response, body } = await request.get(`${nconf.get('url')}/api`);