Node.js based forum software built for the modern web
fix: rss feed privilege check for topic feeds
Barış Soner Uşaklı2cc3ae15801d
diff --git a/src/routes/feeds.js b/src/routes/feeds.jsindex 141c957768..df20795eb0 100644--- a/src/routes/feeds.js+++ b/src/routes/feeds.js@@ -32,13 +32,12 @@ module.exports = function (app, middleware) {app.get('/tags/:tag.rss', middleware.maintenanceMode, routeHelpers.tryRoute(generateForTag));};-async function validateTokenIfRequiresLogin(requiresLogin, cid, req, res) {- const uid = parseInt(req.query.uid, 10) || 0;- const { token } = req.query;-+async function validateTokenIfRequiresLogin(requiresLogin, cid, req, res, privilege) {if (!requiresLogin) {return true;}+ const uid = parseInt(req.query.uid, 10) || 0;+ const { token } = req.query;if (uid <= 0 || !token) {return controllerHelpers.notAllowed(req, res);@@ -60,7 +59,7 @@ async function validateTokenIfRequiresLogin(requiresLogin, cid, req, res) {}await db.delete(rateLimitKey);const userPrivileges = await privileges.categories.get(cid, uid);- if (!userPrivileges.read) {+ if (!userPrivileges[privilege]) {return controllerHelpers.notAllowed(req, res);}return true;@@ -87,7 +86,7 @@ async function generateForTopic(req, res, next) {return next();}- if (await validateTokenIfRequiresLogin(!userPrivileges['topics:read'], topic.cid, req, res)) {+ if (await validateTokenIfRequiresLogin(!userPrivileges['topics:read'], topic.cid, req, res, 'topics:read')) {const topicData = await topics.getTopicWithPosts(topic, `tid:${tid}:posts`, req.uid || req.query.uid || 0, 0, 24, true);const mainPost = topicData.posts[0];@@ -153,7 +152,7 @@ async function generateForCategory(req, res, next) {return next();}- if (await validateTokenIfRequiresLogin(!userPrivileges.read, cid, req, res)) {+ if (await validateTokenIfRequiresLogin(!userPrivileges.read, cid, req, res, 'read')) {let topicsData = await topics.getTopicsByTids(tids, uid);topicsData = await user.blocks.filter(uid, topicsData);const feed = await generateTopicsFeed({@@ -353,7 +352,7 @@ async function generateForCategoryRecentPosts(req, res) {return controllers404.handle404(req, res);}- if (await validateTokenIfRequiresLogin(!userPrivileges.read, cid, req, res)) {+ if (await validateTokenIfRequiresLogin(!userPrivileges.read, cid, req, res, 'read')) {const feed = generateForPostsFeed({title: `${category.name} Recent Posts`,description: `A list of recent posts from ${category.name}`,diff --git a/test/feeds.js b/test/feeds.jsindex a54b485981..028b884b91 100644--- a/test/feeds.js+++ b/test/feeds.js@@ -66,7 +66,7 @@ describe('feeds', () => {assert.equal(response.statusCode, 404);});- it('should redirect if we do not have read privilege', async () => {+ it('should redirect if we do not have topics:read privilege', async () => {await privileges.categories.rescind(['groups:topics:read'], cid, 'guests');const { response, body } = await request.get(`${nconf.get('url')}/topic/${tid}.rss`);assert.equal(response.statusCode, 200);@@ -130,11 +130,22 @@ describe('feeds', () => {assert(body2.startsWith('<?xml version="1.0"'));});- it('should not allow access if token is correct but has no privilege', async () => {+ it('should not allow access if token is correct but has no privilege(read)', async () => {await privileges.categories.rescind(['groups:read'], cid, 'registered-users');const { response, body } = await request.get(`${nconf.get('url')}/category/${cid}.rss?uid=${fooUid}&token=${rssToken}`);assert.equal(response.statusCode, 200);assert(body.includes('Login to your account'));+ assert(false);+ });++ it('should not allow access if token is correct but has no privilege(topics:read)', async () => {+ await privileges.categories.rescind(['groups:topics:read'], cid, 'registered-users');+ await privileges.categories.rescind(['groups:topics:read'], cid, 'guests');++ const { response, body } = await request.get(`${nconf.get('url')}/topic/${tid}.rss?uid=${fooUid}&token=${rssToken}`);+ assert.equal(response.statusCode, 200);+ // console.log(body);+ assert(body.includes('Login to your account'));});});});