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

Don't allow mods to demote bot or members of management room in protected rooms #555

Merged
merged 8 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
2 changes: 1 addition & 1 deletion src/commands/CommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export async function handleCommand(roomId: string, event: { content: { body: st
"!mjolnir resolve <room alias> - Resolves a room alias to a room ID\n" +
"!mjolnir since <date>/<duration> <action> <limit> [rooms...] [reason] - Apply an action ('kick', 'ban', 'mute', 'unmute' or 'show') to all users who joined a room since <date>/<duration> (up to <limit> users)\n" +
"!mjolnir shutdown room <room alias/ID> [message] - Uses the bot's account to shut down a room, preventing access to the room on this server\n" +
"!mjolnir powerlevel <user ID> <power level> [room alias/ID] - Sets the power level of the user in the specified room (or all protected rooms)\n" +
"!mjolnir powerlevel <user ID> <power level> [room alias/ID] - Sets the power level of the user in the specified room (or all protected rooms) - mjolnir will resist lowering the power level of the bot/users in the moderation room unless a --force argument is added\n" +
"!mjolnir make admin <room alias> [user alias/ID] - Make the specified user or the bot itself admin of the room\n" +
"!mjolnir suspend <user ID> - Suspend the specified user\n" +
"!mjolnir unsuspend <user ID> - Unsuspend the specified user\n" +
Expand Down
27 changes: 27 additions & 0 deletions src/commands/SetPowerLevelCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,39 @@ export async function execSetPowerLevelCommand(roomId: string, event: any, mjoln
const level = Math.round(Number(parts[3]));
const inRoom = parts[4];

const mjolnirId = await mjolnir.client.getUserId();

let targetRooms = inRoom
? [await mjolnir.client.resolveRoom(inRoom)]
: mjolnir.protectedRoomsTracker.getProtectedRooms();

let force = false;
if (parts[parts.length - 1] === "--force") {
force = true;
parts.pop();
}

for (const targetRoomId of targetRooms) {
try {
if (!force) {
if (target === mjolnirId || mjolnir.moderators.checkMembership(target)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should fail if target === mjolnirId, and level < targetLevel, even with --force

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait, can you break that one down real quick? this sounds like you're saying it should fail when it tries to promote itself, which sounds like expected API behavior anyways?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

targetLevel is the current level - see other comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, you're trying to prevent demoting the bot itself? how would you handle gracefully removing the bot from a room, given you cant explicitly have it demote itself?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you would puppet the bot and have it demote itself. Mjolnir is tolerable to a human operator using the account at the same time (deliberately - this is also why it doesn't filter itself out when checking for commands. Worst case, a human operator can go to the management room as the bot, send a ban command, and it all works fine)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't the bot immune to self-bans via how powerlevels work? or is that not why protection-invoked selfbans fail?
besides, how does this work in an environment where humans dont possess either the login nor homeserver admin privilges, and use mjolnir to delegate permissions in an auditable fashion?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Power levels don't prevent the bot from banning itself. They also don't prevent the bot from demoting itself, either. I'm not sure what you mean by "protection-invoked selfbans", but it sounds like an unrelated issue.

At the current stage in the project, Mjolnir isn't really meant to be run on behalf of someone else. We aren't able to commit a ton of time into supporting alternative use cases at the moment, so have narrowed our maintenance scope to, roughly speaking, "matrix.org plus 10%". This means some features required by matrix.org may interfere with usage that falls outside the plus 10% range. Alternative bots, like Draupnir, have different allocations for maintenance and feature set which may be more beneficial for your particular use case.

// don't let the bot demote itself or members of moderation room
const currentLevels = await mjolnir.client.getRoomStateEvent(
targetRoomId,
"m.room.power_levels",
"",
);
const targetLevel = currentLevels["users"][mjolnirId];
if (level < targetLevel) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

targetLevel at a glance feels like the destination level, making this statement a bit confusing. Maybe currentLevel would fit better?

await mjolnir.managementRoomOutput.logMessage(
LogLevel.INFO,
"PowerLevelCommand",
`You are attempting to lower the bot/a moderator's power level: current level ${targetLevel}, requested level ${level}, aborting. This check can be overriden with a --force argument at the end of the command.`,
);
return;
}
}
}
await mjolnir.client.setUserPowerLevel(target, targetRoomId, level);
} catch (e) {
const message = e.message || (e.body ? e.body.error : "<no message>");
Expand Down
131 changes: 131 additions & 0 deletions test/integration/commands/powerLevelCommandTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
Copyright 2024 The Matrix.org Foundation C.I.C.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { strict as assert } from "assert";
import { newTestUser } from "../clientHelper";
import { getFirstReaction } from "./commandUtils";

describe("Test: power levels", function () {
it("Does not allow the bot to demote itself or members of management room in a protected room.", async function () {
this.timeout(60000);
const mod = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator" } });
const mod2 = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator2" } });
await mod.joinRoom(this.config.managementRoom);
await mod2.joinRoom(this.config.managementRoom);

const targetRoom = await mod.createRoom({ preset: "public_chat" });
await this.mjolnir.client.joinRoom(targetRoom);
await mod2.joinRoom(targetRoom);
const botId = await this.mjolnir.client.getUserId();
await mod.setUserPowerLevel(botId, targetRoom, 100);
const mod2Id = await mod2.getUserId();
await mod.setUserPowerLevel(mod2Id, targetRoom, 100);

await mod.sendMessage(this.mjolnir.managementRoomId, {
msgtype: "m.text.",
body: `!mjolnir rooms add ${targetRoom}`,
});

await mod.sendMessage(this.mjolnir.managementRoomId, {
msgtype: "m.text",
body: `!mjolnir powerlevel ${botId} 50 ${targetRoom}`,
});

mod.start();
let reply = new Promise((resolve, reject) => {
mod.on("room.message", (roomId: string, event: any) => {
if (
roomId === this.mjolnir.managementRoomId &&
event.content?.body.includes("You are attempting to lower the bot/a moderator's power level")
) {
resolve(event);
}
});
});
await reply;

let currentLevels = await mod.getRoomStateEvent(targetRoom, "m.room.power_levels", "");
const botLevel = currentLevels["users"][botId];
assert.equal(botLevel, 100);

await mod.sendMessage(this.mjolnir.managementRoomId, {
msgtype: "m.text",
body: `!mjolnir powerlevel ${mod2Id} 50 ${targetRoom}`,
});

let reply2 = new Promise((resolve, reject) => {
mod.on("room.message", (roomId: string, event: any) => {
if (
roomId === this.mjolnir.managementRoomId &&
event.content?.body.includes("You are attempting to lower the bot/a moderator's power level")
) {
resolve(event);
}
});
});
await reply2;

currentLevels = await mod.getRoomStateEvent(targetRoom, "m.room.power_levels", "");
const mod2Level = currentLevels["users"][mod2Id];
assert.equal(mod2Level, 100);

mod.stop();
});

it("Does allow the bot to demote itself or members of management room in a protected room with a --force argument.", async function () {
this.timeout(60000);
const mod = await newTestUser(this.config.homeserverUrl, { name: { contains: "force-moderator" } });
const mod2 = await newTestUser(this.config.homeserverUrl, { name: { contains: "force-moderator2" } });
await mod.joinRoom(this.config.managementRoom);
await mod2.joinRoom(this.config.managementRoom);

const targetRoom = await mod.createRoom({ preset: "public_chat" });
await this.mjolnir.client.joinRoom(targetRoom);
await mod2.joinRoom(targetRoom);
const botId = await this.mjolnir.client.getUserId();
await mod.setUserPowerLevel(botId, targetRoom, 100);
const mod2Id = await mod2.getUserId();
await mod.setUserPowerLevel(mod2Id, targetRoom, 75);

await mod.sendMessage(this.mjolnir.managementRoomId, {
msgtype: "m.text.",
body: `!mjolnir rooms add ${targetRoom}`,
});

mod.start();
await getFirstReaction(mod, this.mjolnir.managementRoomId, "✅", async () => {
return await mod.sendMessage(this.mjolnir.managementRoomId, {
msgtype: "m.text",
body: `!mjolnir powerlevel ${mod2Id} 50 ${targetRoom} --force`,
});
});
let currentLevels = await mod.getRoomStateEvent(targetRoom, "m.room.power_levels", "");
const mod2Level = currentLevels["users"][mod2Id];
assert.equal(mod2Level, 50);

await getFirstReaction(mod, this.mjolnir.managementRoomId, "✅", async () => {
return await mod.sendMessage(this.mjolnir.managementRoomId, {
msgtype: "m.text",
body: `!mjolnir powerlevel ${botId} 50 ${targetRoom} --force`,
});
});
currentLevels = await mod.getRoomStateEvent(targetRoom, "m.room.power_levels", "");
const botLevel = currentLevels["users"][botId];
assert.equal(botLevel, 50);

mod.stop();
});
});
Loading