-
Notifications
You must be signed in to change notification settings - Fork 57
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
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3ce930d
don't allow bot to demote itself in protected room
H-Shay af60730
test
H-Shay 72b2025
also apply this behavior to members of moderation room
H-Shay 9b4c806
allow check to be overriden with --force argument
H-Shay 91a947e
refuse to allow bot to lower own power level
H-Shay a463831
Merge branch 'main' into shay/dont_demote
H-Shay b7ac833
lint
H-Shay 11b4cde
amend test to reflect bot not demoting itself
H-Shay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) { | ||
// 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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>"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
, andlevel < targetLevel
, even with--force
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 commentThere was a problem hiding this 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?
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.