Node.js based forum software built for the modern web
refactor: rss feeds
Barış Soner Uşaklıc51b47ffcfd2
diff --git a/src/routes/feeds.js b/src/routes/feeds.jsindex df20795eb0..955efb59b5 100644--- a/src/routes/feeds.js+++ b/src/routes/feeds.js@@ -18,52 +18,26 @@ const routeHelpers = require('./helpers');const { terms } = controllerHelpers;module.exports = function (app, middleware) {- app.get('/topic/:topic_id.rss', middleware.maintenanceMode, routeHelpers.tryRoute(generateForTopic));- app.get('/category/:category_id.rss', middleware.maintenanceMode, routeHelpers.tryRoute(generateForCategory));- app.get('/topics.rss', middleware.maintenanceMode, routeHelpers.tryRoute(generateForTopics));- app.get('/recent.rss', middleware.maintenanceMode, routeHelpers.tryRoute(generateForRecent));- app.get('/top.rss', middleware.maintenanceMode, routeHelpers.tryRoute(generateForTop));- app.get('/top/:term.rss', middleware.maintenanceMode, routeHelpers.tryRoute(generateForTop));- app.get('/popular.rss', middleware.maintenanceMode, routeHelpers.tryRoute(generateForPopular));- app.get('/popular/:term.rss', middleware.maintenanceMode, routeHelpers.tryRoute(generateForPopular));- app.get('/recentposts.rss', middleware.maintenanceMode, routeHelpers.tryRoute(generateForRecentPosts));- app.get('/category/:category_id/recentposts.rss', middleware.maintenanceMode, routeHelpers.tryRoute(generateForCategoryRecentPosts));- app.get('/user/:userslug/topics.rss', middleware.maintenanceMode, routeHelpers.tryRoute(generateForUserTopics));- app.get('/tags/:tag.rss', middleware.maintenanceMode, routeHelpers.tryRoute(generateForTag));-};--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);- }-- const ip = req.ip || req.connection.remoteAddress;- const rateLimitKey = `rss:token:fail:${ip}`;- const count = await db.increment(rateLimitKey);- if (count === 1) {- await db.pexpire(rateLimitKey, 3600000);- }- if (count > 5) {- return controllerHelpers.notAllowed(req, res);- }-- const userToken = await db.getObjectField(`user:${uid}`, 'rss_token');- if (userToken !== token) {- return controllerHelpers.notAllowed(req, res);- }- await db.delete(rateLimitKey);- const userPrivileges = await privileges.categories.get(cid, uid);- if (!userPrivileges[privilege]) {- return controllerHelpers.notAllowed(req, res);+ function handle404(req, res, next) {+ if (meta.config['feeds:disableRSS']) {+ return controllers404.handle404(req, res);+ }+ next();}- return true;-}+ const middlewares = [handle404, middleware.maintenanceMode];+ app.get('/topic/:topic_id.rss', middlewares, routeHelpers.tryRoute(generateForTopic));+ app.get('/category/:category_id.rss', middlewares, routeHelpers.tryRoute(generateForCategory));+ app.get('/topics.rss', middlewares, routeHelpers.tryRoute(generateForTopics));+ app.get('/recent.rss', middlewares, routeHelpers.tryRoute(generateForRecent));+ app.get('/top.rss', middlewares, routeHelpers.tryRoute(generateForTop));+ app.get('/top/:term.rss', middlewares, routeHelpers.tryRoute(generateForTop));+ app.get('/popular.rss', middlewares, routeHelpers.tryRoute(generateForPopular));+ app.get('/popular/:term.rss', middlewares, routeHelpers.tryRoute(generateForPopular));+ app.get('/recentposts.rss', middlewares, routeHelpers.tryRoute(generateForRecentPosts));+ app.get('/category/:category_id/recentposts.rss', middlewares, routeHelpers.tryRoute(generateForCategoryRecentPosts));+ app.get('/user/:userslug/topics.rss', middlewares, routeHelpers.tryRoute(generateForUserTopics));+ app.get('/tags/:tag.rss', middlewares, routeHelpers.tryRoute(generateForTag));+};function stripUnicodeControlChars(str) {// eslint-disable-next-line no-control-regex@@ -71,67 +45,69 @@ function stripUnicodeControlChars(str) {}async function generateForTopic(req, res, next) {- if (meta.config['feeds:disableRSS']) {- return next();- }-const tid = req.params.topic_id;-- const [userPrivileges, topic] = await Promise.all([- privileges.topics.get(tid, req.uid),+ const { uid, ok } = await getUidFromToken(req, res);+ if (!ok) {+ return;+ }+ const [userPrivileges, canRead, topic] = await Promise.all([+ privileges.topics.get(tid, uid),+ privileges.topics.canRead(tid, uid),topics.getTopicData(tid),]);- if (!privileges.topics.canViewDeletedScheduled(topic, userPrivileges)) {+ if (userPrivileges.disabled || !privileges.topics.canViewDeletedScheduled(topic, userPrivileges)) {return next();}- 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);+ if (!canRead) {+ return controllerHelpers.notAllowed(req, res);+ }- const mainPost = topicData.posts[0];- topics.modifyPostsByPrivilege(topicData, userPrivileges);- const title = stripUnicodeControlChars(topicData.title);- const feed = new rss({- title: utils.stripHTMLTags(title, utils.tags),- description: topicData.posts.length ? stripUnicodeControlChars(mainPost.content) : '',- feed_url: `${nconf.get('url')}/topic/${tid}.rss`,- site_url: `${nconf.get('url')}/topic/${topicData.slug}`,- image_url: topicData.posts.length ? mainPost.picture : '',- author: topicData.posts.length ? mainPost.username : '',- ttl: 60,- });+ const topicData = await topics.getTopicWithPosts(topic, `tid:${tid}:posts`, uid, 0, 24, true);++ const mainPost = topicData.posts[0];+ topics.modifyPostsByPrivilege(topicData, userPrivileges);+ const title = stripUnicodeControlChars(topicData.title);+ const feed = new rss({+ title: utils.stripHTMLTags(title, utils.tags),+ description: topicData.posts.length ? stripUnicodeControlChars(mainPost.content) : '',+ feed_url: `${nconf.get('url')}/topic/${tid}.rss`,+ site_url: `${nconf.get('url')}/topic/${topicData.slug}`,+ image_url: topicData.posts.length ? mainPost.picture : '',+ author: topicData.posts.length ? mainPost.username : '',+ ttl: 60,+ });- if (topicData.posts.length > 0) {- feed.pubDate = new Date(parseInt(mainPost.timestamp, 10)).toUTCString();+ if (topicData.posts.length > 0) {+ feed.pubDate = new Date(parseInt(mainPost.timestamp, 10)).toUTCString();+ }+ const replies = topicData.posts.slice(1);+ replies.forEach((postData) => {+ if (!postData.deleted) {+ const dateStamp = new Date(+ parseInt(parseInt(postData.edited, 10) === 0 ? postData.timestamp : postData.edited, 10)+ ).toUTCString();++ feed.item({+ title: `Reply to ${utils.stripHTMLTags(title, utils.tags)} on ${dateStamp}`,+ description: stripUnicodeControlChars(postData.content),+ url: `${nconf.get('url')}/post/${postData.pid}`,+ author: postData.user ? postData.user.username : '',+ date: dateStamp,+ });}- const replies = topicData.posts.slice(1);- replies.forEach((postData) => {- if (!postData.deleted) {- const dateStamp = new Date(- parseInt(parseInt(postData.edited, 10) === 0 ? postData.timestamp : postData.edited, 10)- ).toUTCString();-- feed.item({- title: `Reply to ${utils.stripHTMLTags(title, utils.tags)} on ${dateStamp}`,- description: stripUnicodeControlChars(postData.content),- url: `${nconf.get('url')}/post/${postData.pid}`,- author: postData.user ? postData.user.username : '',- date: dateStamp,- });- }- });+ });- sendFeed(feed, res);- }+ sendFeed(feed, res);}async function generateForCategory(req, res, next) {const cid = req.params.category_id;- if (meta.config['feeds:disableRSS'] || !parseInt(cid, 10)) {- return next();+ const { uid, ok } = await getUidFromToken(req, res);+ if (!ok) {+ return;}- const uid = req.uid || req.query.uid || 0;async function getRecentlyCreatedTids() {const [pinnedTids, tids] = await Promise.all([db.getSortedSetRevRange(`cid:${cid}:tids:pinned`, 0, -1),@@ -143,43 +119,55 @@ async function generateForCategory(req, res, next) {return topicData.slice(0, 25).map(t => t.tid);}const [userPrivileges, category, tids] = await Promise.all([- privileges.categories.get(cid, req.uid),+ privileges.categories.get(cid, uid),categories.getCategoryData(cid),getRecentlyCreatedTids(),]);- if (!category || !category.name) {+ if (!category || category.disabled) {return next();}- 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({- uid: uid,- title: category.name,- description: category.description,- feed_url: `/category/${cid}.rss`,- site_url: `/category/${category.cid}`,- }, topicsData, 'timestamp');-- sendFeed(feed, res);+ if (!userPrivileges.read) {+ return controllerHelpers.notAllowed(req, res);}++ let topicsData = await topics.getTopicsByTids(tids, uid);+ topicsData = await user.blocks.filter(uid, topicsData);+ const feed = await generateTopicsFeed({+ uid: uid,+ title: category.name,+ description: category.description,+ feed_url: `/category/${cid}.rss`,+ site_url: `/category/${category.cid}`,+ }, topicsData, 'timestamp');++ sendFeed(feed, res);}-async function generateForTopics(req, res, next) {- if (meta.config['feeds:disableRSS']) {- return next();+async function generateForTopics(req, res) {+ const { uid, ok } = await getUidFromToken(req, res);+ if (!ok) {+ return;}- const uid = await getUidFromToken(req);+ let cids = await categories.getCidsByPrivilege('categories:cid', uid, 'topics:read');+ cids = cids.filter(cid => cid !== -1);- await sendTopicsFeed({+ const topicData = await topics.getSortedTopics({+ cids,uid: uid,+ sort: 'create',+ start: 0,+ stop: 19,+ });++ const feed = await generateTopicsFeed({title: 'Most recently created topics',description: 'A list of topics that have been created recently',feed_url: '/topics.rss',useMainPost: true,- }, 'topics:tid', res, 'timestamp');+ }, topicData.topics, 'timestamp');+ sendFeed(feed, res);}async function generateForRecent(req, res, next) {@@ -220,12 +208,15 @@ async function generateForPopular(req, res, next) {async function generateSorted(options, req, res, next) {const termParam = req.params.term || options.term;- if (meta.config['feeds:disableRSS'] || !Object.hasOwn(terms, termParam)) {+ if (!Object.hasOwn(terms, termParam)) {return next();}const term = terms[termParam];- const uid = await getUidFromToken(req);+ const { uid, ok } = await getUidFromToken(req, res);+ if (!ok) {+ return;+ }const params = {uid: uid,@@ -313,15 +304,15 @@ async function generateTopicsFeed(feedOptions, feedTopics, timestampField) {return feed;}-async function generateForRecentPosts(req, res, next) {- if (meta.config['feeds:disableRSS']) {- return next();- }+async function generateForRecentPosts(req, res) {const page = parseInt(req.query.page, 10) || 1;const postsPerPage = 20;const start = Math.max(0, (page - 1) * postsPerPage);const stop = start + postsPerPage - 1;- const uid = await getUidFromToken(req);+ const { uid, ok } = await getUidFromToken(req, res);+ if (!ok) {+ return;+ }const postData = await posts.getRecentPosts(uid, start, stop, 'month');const feed = generateForPostsFeed({title: 'Recent Posts',@@ -333,35 +324,39 @@ async function generateForRecentPosts(req, res, next) {sendFeed(feed, res);}-async function generateForCategoryRecentPosts(req, res) {- if (meta.config['feeds:disableRSS']) {- return controllers404.handle404(req, res);- }+async function generateForCategoryRecentPosts(req, res, next) {const cid = req.params.category_id;const page = parseInt(req.query.page, 10) || 1;const topicsPerPage = 20;const start = Math.max(0, (page - 1) * topicsPerPage);const stop = start + topicsPerPage - 1;+ const { uid, ok } = await getUidFromToken(req, res);+ if (!ok) {+ return;+ }+const [userPrivileges, category, postData] = await Promise.all([- privileges.categories.get(cid, req.uid),+ privileges.categories.get(cid, uid),categories.getCategoryData(cid),- categories.getRecentReplies(cid, req.uid || req.query.uid || 0, start, stop),+ categories.getRecentReplies(cid, uid, start, stop),]);- if (!category) {- return controllers404.handle404(req, res);+ if (!category || category.disabled) {+ return next();}- 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}`,- feed_url: `/category/${cid}/recentposts.rss`,- site_url: `/category/${cid}/recentposts`,- }, postData);-- sendFeed(feed, res);+ if (!userPrivileges['topics:read']) {+ return controllerHelpers.notAllowed(req, res);}++ const feed = generateForPostsFeed({+ title: `${category.name} Recent Posts`,+ description: `A list of recent posts from ${category.name}`,+ feed_url: `/category/${cid}/recentposts.rss`,+ site_url: `/category/${cid}/recentposts`,+ }, postData);++ sendFeed(feed, res);}function generateForPostsFeed(feedOptions, posts) {@@ -390,37 +385,41 @@ function generateForPostsFeed(feedOptions, posts) {}async function generateForUserTopics(req, res, next) {- if (meta.config['feeds:disableRSS']) {- return next();- }-const { userslug } = req.params;const uid = await user.getUidByUserslug(userslug);if (!uid) {return next();}const userData = await user.getUserFields(uid, ['uid', 'username']);- const authUid = await getUidFromToken(req);+ const { uid: authUid, ok } = await getUidFromToken(req, res);+ if (!ok) {+ return;+ }+ let cids = await db.getSortedSetRangeByScore(`uid:${uid}:cids`, 0, -1, 1, '+inf');+ cids = await privileges.categories.filterCids('topics:read', cids, authUid);+ const sets = cids.map(c => `cid:${c}:uid:${userData.uid}:tids`);await sendTopicsFeed({uid: authUid,title: `Topics by ${userData.username}`,description: `A list of topics that are posted by ${userData.username}`,feed_url: `/user/${userslug}/topics.rss`,site_url: `/user/${userslug}/topics`,- }, `uid:${userData.uid}:topics`, res, 'timestamp');+ }, sets, res, 'timestamp');}async function generateForTag(req, res) {- if (meta.config['feeds:disableRSS']) {- return controllers404.handle404(req, res);+ const { uid, ok } = await getUidFromToken(req, res);+ if (!ok) {+ return;}- const uid = await getUidFromToken(req);- const set = `tag:${String(req.params.tag)}:topics`;const tag = stripUnicodeControlChars(String(req.params.tag));const page = parseInt(req.query.page, 10) || 1;const topicsPerPage = meta.config.topicsPerPage || 20;const start = Math.max(0, (page - 1) * topicsPerPage);const stop = start + topicsPerPage - 1;+ let cids = await categories.getCidsByPrivilege('categories:cid', uid, 'topics:read');+ cids = cids.filter(cid => cid !== -1);+ const sets = cids.map(cid => `cid:${cid}:tag:${String(req.params.tag)}:topics`);await sendTopicsFeed({uid: uid,title: `Topics tagged with ${tag}`,@@ -429,16 +428,45 @@ async function generateForTag(req, res) {site_url: `/tags/${tag}`,start: start,stop: stop,- }, set, res, 'timestamp');+ }, sets, res, 'timestamp');}-async function getUidFromToken(req) {- let token = null;- if (req.query.token && req.query.uid) {- token = await db.getObjectField(`user:${req.query.uid}`, 'rss_token');+function getRssTokenRateLimitKey(req) {+ const ip = req.ip || req.connection.remoteAddress;+ return `rss:token:fail:${ip}`;+}++async function getUidFromToken(req, res) {+ const authUid = parseInt(req.uid, 10) || 0;+ const hasUid = Object.hasOwn(req.query, 'uid');+ const hasToken = Object.hasOwn(req.query, 'token');++ if (!hasUid && !hasToken) {+ return { uid: authUid, ok: true };}- return token && token === req.query.token ? req.query.uid : req.uid;+ const queryUid = parseInt(req.query.uid, 10) || 0;+ const { token } = req.query;+ const rateLimitKey = getRssTokenRateLimitKey(req);+ const count = await db.increment(rateLimitKey);+ if (count === 1) {+ await db.pexpire(rateLimitKey, 3600000);+ }+ if (count > 5 || queryUid <= 0 || !token) {+ await controllerHelpers.notAllowed(req, res);+ return { uid: authUid, ok: false };+ }++ const userToken = await db.getObjectField(`user:${queryUid}`, 'rss_token');+ if (userToken !== token) {+ await controllerHelpers.notAllowed(req, res);+ return { uid: authUid, ok: false };+ }++ await db.delete(rateLimitKey);+ req.uid = queryUid;+ req.loggedIn = queryUid > 0;+ return { uid: queryUid, ok: true };}function sendFeed(feed, res) {diff --git a/test/feeds.js b/test/feeds.jsindex 8404aefb31..90701a9dad 100644--- a/test/feeds.js+++ b/test/feeds.js@@ -35,6 +35,7 @@ describe('feeds', () => {});it('should 404', async () => {+ meta.config['feeds:disableRSS'] = 1;const feedUrls = [`${nconf.get('url')}/topic/${tid}.rss`,`${nconf.get('url')}/category/${cid}.rss`,@@ -51,7 +52,7 @@ describe('feeds', () => {for (const url of feedUrls) {// eslint-disable-next-line no-await-in-loopconst { response } = await request.get(url);- assert.equal(response.statusCode, 404);+ assert.equal(response.statusCode, 404, `Expected 404 for ${url}`);}meta.config['feeds:disableRSS'] = 0;});@@ -133,8 +134,7 @@ 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, 200);- assert(body.includes('Login to your account'));+ assert.equal(response.statusCode, 403);});it('should not allow access if token is correct but has no privilege(topics:read)', async () => {@@ -142,8 +142,23 @@ describe('feeds', () => {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);- assert(body.includes('Login to your account'));+ assert.equal(response.statusCode, 403);+ });++ 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}`);++ assert.equal(response.statusCode, 403);++ await privileges.categories.give(['groups:topics:read'], cid, 'registered-users');+ await privileges.categories.give(['groups:topics:read'], cid, 'guests');++ const { response: response1, body: body1 } = await request.get(`${nconf.get('url')}/category/${cid}/recentposts.rss?uid=${fooUid}&token=${rssToken}`);+ assert.equal(response1.statusCode, 200);+ assert(!body1.includes('Login to your account'), 'Should not redirect to login page');});});});