Skip to content

Node.js based forum software built for the modern web

fix: closes #14568, add read check to bookmarks/votes

Barış Soner Uşaklıa235033828b0
diff --git a/src/api/helpers.js b/src/api/helpers.js
index 01f79d9b0a..109549e7e8 100644
--- a/src/api/helpers.js
+++ b/src/api/helpers.js
@@ -118,12 +118,10 @@ exports.postCommand = async function (caller, command, eventName, notification,
throw new Error('[[error:invalid-data]]');
}
- if (!data.room_id) {
- throw new Error(`[[error:invalid-room-id, ${data.room_id}]]`);
- }
- const [exists, deleted] = await Promise.all([
+ const [exists, { deleted, tid }, canRead] = await Promise.all([
posts.exists(data.pid),
- posts.getPostField(data.pid, 'deleted'),
+ posts.getPostFields(data.pid, ['deleted', 'tid']),
+ privileges.posts.canRead(data.pid, caller.uid),
]);
if (!exists) {
@@ -134,6 +132,11 @@ exports.postCommand = async function (caller, command, eventName, notification,
throw new Error('[[error:post-deleted]]');
}
+ if (!canRead) {
+ throw new Error('[[error:no-privileges]]');
+ }
+ data.room_id = `topic_${tid}`;
+
/*
hooks:
filter:post.upvote
diff --git a/src/controllers/write/posts.js b/src/controllers/write/posts.js
index e6d1b6733b..b9ff4d8f37 100644
--- a/src/controllers/write/posts.js
+++ b/src/controllers/write/posts.js
@@ -4,7 +4,6 @@ const nconf = require('nconf');
const db = require('../../database');
const topics = require('../../topics');
-const posts = require('../../posts');
const api = require('../../api');
const helpers = require('../helpers');
@@ -107,13 +106,8 @@ Posts.move = async (req, res) => {
helpers.formatApiResponse(200, res);
};
-async function mock(req) {
- const tid = await posts.getPostField(req.params.pid, 'tid');
- return { pid: req.params.pid, room_id: `topic_${tid}` };
-}
-
Posts.vote = async (req, res) => {
- const data = await mock(req);
+ const data = { pid: req.params.pid };
if (req.body.delta > 0) {
await api.posts.upvote(req, data);
} else if (req.body.delta < 0) {
@@ -126,8 +120,7 @@ Posts.vote = async (req, res) => {
};
Posts.unvote = async (req, res) => {
- const data = await mock(req);
- await api.posts.unvote(req, data);
+ await api.posts.unvote(req, { pid: req.params.pid });
helpers.formatApiResponse(200, res);
};
@@ -152,14 +145,12 @@ Posts.getAnnouncersTooltip = async (req, res) => {
};
Posts.bookmark = async (req, res) => {
- const data = await mock(req);
- await api.posts.bookmark(req, data);
+ await api.posts.bookmark(req, { pid: req.params.pid });
helpers.formatApiResponse(200, res);
};
Posts.unbookmark = async (req, res) => {
- const data = await mock(req);
- await api.posts.unbookmark(req, data);
+ await api.posts.unbookmark(req, { pid: req.params.pid });
helpers.formatApiResponse(200, res);
};
diff --git a/test/activitypub/feps.js b/test/activitypub/feps.js
index 5268759f8f..0bc29925c6 100644
--- a/test/activitypub/feps.js
+++ b/test/activitypub/feps.js
@@ -123,7 +123,7 @@ describe('FEPs', () => {
it('should federate out Announce(Like) on local vote', async () => {
activitypub._sent.clear();
- await api.posts.upvote({ uid: adminUid }, { pid: topicData.mainPid, room_id: `topic_${topicData.tid}` });
+ await api.posts.upvote({ uid: adminUid }, { pid: topicData.mainPid });
await wait(50);
const activities = Array.from(activitypub._sent);
diff --git a/test/posts.js b/test/posts.js
index 26bb488e1a..31735cea4c 100644
--- a/test/posts.js
+++ b/test/posts.js
@@ -138,13 +138,13 @@ describe('Post\'s', () => {
await privileges.categories.rescind(['groups:posts:upvote', 'groups:posts:downvote'], cid, 'registered-users');
let err;
try {
- await apiPosts.upvote({ uid: voterUid }, { pid: postData.pid, room_id: 'topic_1' });
+ await apiPosts.upvote({ uid: voterUid }, { pid: postData.pid });
} catch (_err) {
err = _err;
}
assert.equal(err.message, '[[error:no-privileges]]');
try {
- await apiPosts.downvote({ uid: voterUid }, { pid: postData.pid, room_id: 'topic_1' });
+ await apiPosts.downvote({ uid: voterUid }, { pid: postData.pid });
} catch (_err) {
err = _err;
}
@@ -153,7 +153,7 @@ describe('Post\'s', () => {
});
it('should upvote a post', async () => {
- const result = await apiPosts.upvote({ uid: voterUid }, { pid: postData.pid, room_id: 'topic_1' });
+ const result = await apiPosts.upvote({ uid: voterUid }, { pid: postData.pid });
assert.equal(result.post.upvotes, 1);
assert.equal(result.post.downvotes, 0);
assert.equal(result.post.votes, 1);
@@ -200,7 +200,7 @@ describe('Post\'s', () => {
});
it('should unvote a post', async () => {
- const result = await apiPosts.unvote({ uid: voterUid }, { pid: postData.pid, room_id: 'topic_1' });
+ const result = await apiPosts.unvote({ uid: voterUid }, { pid: postData.pid });
assert.equal(result.post.upvotes, 0);
assert.equal(result.post.downvotes, 0);
assert.equal(result.post.votes, 0);
@@ -211,7 +211,7 @@ describe('Post\'s', () => {
});
it('should downvote a post', async () => {
- const result = await apiPosts.downvote({ uid: voterUid }, { pid: postData.pid, room_id: 'topic_1' });
+ const result = await apiPosts.downvote({ uid: voterUid }, { pid: postData.pid });
assert.equal(result.post.upvotes, 0);
assert.equal(result.post.downvotes, 1);
assert.equal(result.post.votes, -1);
@@ -239,7 +239,7 @@ describe('Post\'s', () => {
content: 'raw content',
});
try {
- await apiPosts.downvote({ uid: voterUid }, { pid: p1.pid, room_id: 'topic_1' });
+ await apiPosts.downvote({ uid: voterUid }, { pid: p1.pid });
} catch (_err) {
err = _err;
}
@@ -257,7 +257,7 @@ describe('Post\'s', () => {
content: 'raw content',
});
try {
- await apiPosts.downvote({ uid: voterUid }, { pid: p1.pid, room_id: 'topic_1' });
+ await apiPosts.downvote({ uid: voterUid }, { pid: p1.pid });
} catch (_err) {
err = _err;
}
@@ -268,18 +268,43 @@ describe('Post\'s', () => {
describe('bookmarking', () => {
it('should bookmark a post', async () => {
- const data = await apiPosts.bookmark({ uid: voterUid }, { pid: postData.pid, room_id: `topic_${postData.tid}` });
+ const data = await apiPosts.bookmark({ uid: voterUid }, { pid: postData.pid });
assert.equal(data.isBookmarked, true);
const hasBookmarked = await posts.hasBookmarked(postData.pid, voterUid);
assert.equal(hasBookmarked, true);
});
it('should unbookmark a post', async () => {
- const data = await apiPosts.unbookmark({ uid: voterUid }, { pid: postData.pid, room_id: `topic_${postData.tid}` });
+ const data = await apiPosts.unbookmark({ uid: voterUid }, { pid: postData.pid });
assert.equal(data.isBookmarked, false);
const hasBookmarked = await posts.hasBookmarked([postData.pid], voterUid);
assert.equal(hasBookmarked[0], false);
});
+
+ it('should fail to bookmark/unbookmark a post the user can not read', async () => {
+ const { cid } = await categories.create({ name: 'Test Category'});
+ const { postData } = await topics.post({
+ uid: voteeUid,
+ cid: cid,
+ title: 'topic to bookmark',
+ content: 'A post to bookmark',
+ });
+ await privileges.categories.rescind(['groups:topics:read'], cid, 'registered-users');
+ await assert.rejects(
+ apiPosts.bookmark({ uid: voterUid }, { pid: postData.pid }),
+ { message: '[[error:no-privileges]]' }
+ );
+
+ await assert.rejects(
+ apiPosts.unbookmark({ uid: voterUid }, { pid: postData.pid }),
+ { message: '[[error:no-privileges]]' }
+ );
+
+ // should work after giving topics:read
+ await privileges.categories.give(['groups:topics:read'], cid, 'registered-users');
+ await apiPosts.bookmark({ uid: voterUid }, { pid: postData.pid });
+ await apiPosts.unbookmark({ uid: voterUid }, { pid: postData.pid });
+ });
});
describe('post tools', () => {