Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Pin and unpin message were not checking permissions #12739

Merged
merged 5 commits into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/rocketchat-message-pin/server/pinMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ Meteor.methods({
});
}

if (!RocketChat.authz.hasPermission(Meteor.userId(), 'pin-message')) {
throw new Meteor.Error('not-authorized', 'Not Authorized', { method: 'pinMessage' });
}

const subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(message.rid, Meteor.userId(), { fields: { _id: 1 } });
if (!subscription) {
return false;
Expand Down Expand Up @@ -113,6 +117,10 @@ Meteor.methods({
});
}

if (!RocketChat.authz.hasPermission(Meteor.userId(), 'pin-message')) {
throw new Meteor.Error('not-authorized', 'Not Authorized', { method: 'pinMessage' });
}

const subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(message.rid, Meteor.userId(), { fields: { _id: 1 } });
if (!subscription) {
return false;
Expand Down
9 changes: 9 additions & 0 deletions tests/data/permissions.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@ export const updatePermission = (permission, roles) => new Promise((resolve) =>
.expect(200)
.end(resolve);
});

export const updateSetting = (setting, value) => new Promise((resolve) => {
request.post(`/api/v1/settings/${ setting }`)
.set(credentials)
.send({ value })
.expect('Content-Type', 'application/json')
.expect(200)
.end(resolve);
});
25 changes: 1 addition & 24 deletions tests/end-to-end/api/01-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,7 @@ import {
import { adminEmail, preferences, password, adminUsername } from '../../data/user.js';
import { imgURL } from '../../data/interactions.js';
import { customFieldText, clearCustomFields, setCustomFields } from '../../data/custom-fields.js';

const updatePermission = (permission, roles) => new Promise((resolve) => {
request.post(api('permissions.update'))
.set(credentials)
.send({ permissions: [{ _id: permission, roles }] })
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
})
.end(resolve);
});

const updateSetting = (setting, value) => new Promise((resolve) => {
request.post(`/api/v1/settings/${ setting }`)
.set(credentials)
.send({ value })
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
})
.end(resolve);
});
import { updatePermission, updateSetting } from '../../data/permissions.helper';

describe('[Users]', function() {
this.retries(0);
Expand Down
110 changes: 110 additions & 0 deletions tests/end-to-end/api/05-chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
message,
} from '../../data/api-data.js';
import { password } from '../../data/user';
import { updatePermission, updateSetting } from '../../data/permissions.helper';

describe('[Chat]', function() {
this.retries(0);
Expand Down Expand Up @@ -668,4 +669,113 @@ describe('[Chat]', function() {
});
});

describe('[/chat.pinMessage]', () => {
it('should return an error when pinMessage is not allowed in this server', (done) => {
updateSetting('Message_AllowPinning', false).then(() => {
request.post(api('chat.pinMessage'))
.set(credentials)
.send({
messageId: message._id,
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('error');
})
.end(done);
});
});

it('should return an error when pinMessage is allowed in server but user dont have permission', (done) => {
updateSetting('Message_AllowPinning', true).then(() => {
updatePermission('pin-message', []).then(() => {
request.post(api('chat.pinMessage'))
.set(credentials)
.send({
messageId: message._id,
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('error');
})
.end(done);
});
});
});

it('should pin Message successfully', (done) => {
updatePermission('pin-message', ['admin']).then(() => {
request.post(api('chat.pinMessage'))
.set(credentials)
.send({
messageId: message._id,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.not.have.property('error');
})
.end(done);
});
});
});

describe('[/chat.unPinMessage]', () => {
it('should return an error when pinMessage is not allowed in this server', (done) => {
updateSetting('Message_AllowPinning', false).then(() => {
request.post(api('chat.unPinMessage'))
.set(credentials)
.send({
messageId: message._id,
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('error');
})
.end(done);
});
});

it('should return an error when pinMessage is allowed in server but users dont have permission', (done) => {
updateSetting('Message_AllowPinning', true).then(() => {
updatePermission('pin-message', []).then(() => {
request.post(api('chat.unPinMessage'))
.set(credentials)
.send({
messageId: message._id,
})
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('error');
})
.end(done);
});
});
});

it('should unpin Message successfully', (done) => {
updatePermission('pin-message', ['admin']).then(() => {
request.post(api('chat.unPinMessage'))
.set(credentials)
.send({
messageId: message._id,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.not.have.property('error');
})
.end(done);
});
});
});
});