Skip to content

Node.js based forum software built for the modern web

fix(security): return 404 instead of 403 for restricted topics/posts/feeds

Julian Lamb5fb06acecf0
diff --git a/src/controllers/posts.js b/src/controllers/posts.js
index 603e060d68..d29b17fdec 100644
--- a/src/controllers/posts.js
+++ b/src/controllers/posts.js
@@ -39,7 +39,7 @@ postsController.redirectToPost = async function (req, res, next) {
return next();
}
if (!canRead) {
- return helpers.notAllowed(req, res);
+ return next();
}
if (meta.config.activitypubEnabled) {
diff --git a/src/controllers/topics.js b/src/controllers/topics.js
index 48e1aa2ee3..409ae14112 100644
--- a/src/controllers/topics.js
+++ b/src/controllers/topics.js
@@ -72,7 +72,8 @@ topicsController.get = async function getTopic(req, res, next) {
(!topicData.scheduled && topicData.deleted && !userPrivileges.view_deleted) ||
await shouldHideTopicFromGuest(req.uid, tid, topicData.cid)
) {
- return helpers.notAllowed(req, res);
+ // Respond 404 (not 403) so restricted topics are indistinguishable from missing ones
+ return next();
}
if (req.params.post_index === 'unread') {
@@ -429,7 +430,7 @@ topicsController.teaser = async function (req, res, next) {
}
const canRead = await privileges.topics.can('topics:read', tid, req.uid);
if (!canRead) {
- return res.status(403).json('[[error:no-privileges]]');
+ return res.status(404).json('not-found');
}
const pid = await topics.getLatestUndeletedPid(tid);
if (!pid) {
@@ -454,7 +455,7 @@ topicsController.pagination = async function (req, res, next) {
return next();
}
if (!await privileges.topics.canRead(tid, req.uid)) {
- return helpers.notAllowed(req, res);
+ return next();
}
const settings = await user.getSettings(req.uid);
diff --git a/src/routes/feeds.js b/src/routes/feeds.js
index 9e0f927522..a4df06e561 100644
--- a/src/routes/feeds.js
+++ b/src/routes/feeds.js
@@ -62,7 +62,7 @@ async function generateForTopic(req, res, next) {
}
if (!canRead) {
- return await controllerHelpers.notAllowed(req, res);
+ return next();
}
const topicData = await topics.getTopicWithPosts(topic, `tid:${tid}:posts`, uid, 0, 24, true);
@@ -130,7 +130,7 @@ async function generateForCategory(req, res, next) {
}
if (!userPrivileges.read) {
- return await controllerHelpers.notAllowed(req, res);
+ return next();
}
let topicsData = await topics.getTopicsByTids(tids, uid);
@@ -347,7 +347,7 @@ async function generateForCategoryRecentPosts(req, res, next) {
}
if (!userPrivileges['topics:read']) {
- return await controllerHelpers.notAllowed(req, res);
+ return next();
}
const feed = generateForPostsFeed({
diff --git a/test/feeds.js b/test/feeds.js
index 23be4080c4..0c1ef194d5 100644
--- a/test/feeds.js
+++ b/test/feeds.js
@@ -67,12 +67,10 @@ describe('feeds', () => {
assert.equal(response.statusCode, 404);
});
- it('should redirect if we do not have topics:read privilege', async () => {
+ it('should 404 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);
- assert(body);
- assert(body.includes('Login to your account'));
+ const { response } = await request.get(`${nconf.get('url')}/topic/${tid}.rss`);
+ assert.equal(response.statusCode, 404);
await privileges.categories.give(['groups:topics:read'], cid, 'guests');
});
@@ -81,12 +79,10 @@ describe('feeds', () => {
assert.equal(response.statusCode, 404);
});
- it('should redirect if we do not have read privilege', async () => {
+ it('should 404 if we do not have read privilege', async () => {
await privileges.categories.rescind(['groups:read'], cid, 'guests');
- const { response, body } = await request.get(`${nconf.get('url')}/category/${cid}.rss`);
- assert.equal(response.statusCode, 200);
- assert(body);
- assert(body.includes('Login to your account'));
+ const { response } = await request.get(`${nconf.get('url')}/category/${cid}.rss`);
+ assert.equal(response.statusCode, 404);
await privileges.categories.give(['groups:read'], cid, 'guests');
});
@@ -130,23 +126,23 @@ describe('feeds', () => {
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, 403);
+ const { response } = await request.get(`${nconf.get('url')}/category/${cid}.rss?uid=${fooUid}&token=${rssToken}`);
+ assert.equal(response.statusCode, 404);
});
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, 403);
+ const { response } = await request.get(`${nconf.get('url')}/topic/${tid}.rss?uid=${fooUid}&token=${rssToken}`);
+ assert.equal(response.statusCode, 404);
});
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')}/category/${cid}/recentposts.rss?uid=${fooUid}&token=${rssToken}`);
+ const { response } = await request.get(`${nconf.get('url')}/category/${cid}/recentposts.rss?uid=${fooUid}&token=${rssToken}`);
assert.equal(response.statusCode, 403);
diff --git a/test/topics.js b/test/topics.js
index bfff070412..39713ae8cb 100644
--- a/test/topics.js
+++ b/test/topics.js
@@ -1111,12 +1111,12 @@ describe('Topic\'s', () => {
assert.equal(response.statusCode, 404);
});
- it('should 401 if not allowed to read as guest', async () => {
+ it('should 404 if not allowed to read as guest', async () => {
const privileges = require('../src/privileges');
await privileges.categories.rescind(['groups:topics:read'], topicData.cid, 'guests');
const { response, body } = await request.get(`${nconf.get('url')}/api/topic/${topicData.slug}`);
- assert.equal(response.statusCode, 401);
+ assert.equal(response.statusCode, 404);
assert(body);
await privileges.categories.give(['groups:topics:read'], topicData.cid, 'guests');
});
@@ -1155,10 +1155,10 @@ describe('Topic\'s', () => {
assert.equal(response.statusCode, 404);
});
- it('should 403 if cant read', async () => {
+ it('should 404 if cant read', async () => {
const { response, body } = await request.get(`${nconf.get('url')}/api/topic/teaser/${123123}`);
- assert.equal(response.statusCode, 403);
- assert.equal(body, '[[error:no-privileges]]');
+ assert.equal(response.statusCode, 404);
+ assert.equal(body, 'not-found');
});
it('should load topic teaser', async () => {