-
Notifications
You must be signed in to change notification settings - Fork 21
Ban Propagation protection (that is enabled by default) #36
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fdf51eb
Experimental Protection to propagate room level bans to policies.
Gnuxie 6e49605
Use MatrixDataManager for enabled protections.
Gnuxie f1ede5e
Enable BanPropagationProtection by default
Gnuxie 88f264b
BanPropagation: only prompt when user is not already banned.
Gnuxie 73b15c3
Test for BanPropagationProtection.
Gnuxie d078c9b
clearTimeout for prompt reactions if we got a reaction.
Gnuxie 948eec1
Allow renderMatrixAndSend to not need a reply.
Gnuxie 1d62ec2
document getFirstEventMatching
Gnuxie 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,108 @@ | ||
| /** | ||
| * Copyright (C) 2022-2023 Gnuxie <[email protected]> | ||
| * All rights reserved. | ||
| * | ||
| * This file is modified and is NOT licensed under the Apache License. | ||
| * This modified file incorperates work from mjolnir | ||
| * https://github.com/matrix-org/mjolnir | ||
| * which included the following license notice: | ||
|
|
||
| Copyright 2019-2022 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. | ||
| * | ||
| * However, this file is modified and the modifications in this file | ||
| * are NOT distributed, contributed, committed, or licensed under the Apache License. | ||
| */ | ||
|
|
||
| import { Protection } from "./IProtection"; | ||
| import { Mjolnir } from "../Mjolnir"; | ||
| import { LogService } from "matrix-bot-sdk"; | ||
| import { PromptResponseListener } from "../commands/interface-manager/MatrixPromptUX"; | ||
| import { JSXFactory } from "../commands/interface-manager/JSXFactory"; | ||
| import { renderMatrixAndSend } from "../commands/interface-manager/DeadDocumentMatrix"; | ||
| import { renderMentionPill } from "../commands/interface-manager/MatrixHelpRenderer"; | ||
| import { RULE_USER } from "../models/ListRule"; | ||
|
|
||
| /** | ||
| * Prompt the management room to propagate a user ban to a policy list of their choice. | ||
| * @param mjolnir Mjolnir. | ||
| * @param event The ban event. | ||
| * @param roomId The room that the ban happened in. | ||
| * @returns An event id which can be used by the `PromptResponseListener`. | ||
| */ | ||
| async function promptBanPropagation( | ||
| mjolnir: Mjolnir, | ||
| event: any, | ||
| roomId: string | ||
| ): Promise</*event id*/string> { | ||
| return (await renderMatrixAndSend( | ||
| <root>The user {renderMentionPill(event["state_key"], event["content"]?.["displayname"] ?? event["state_key"])} was banned | ||
| in <a href={`https://matrix.to/#/${roomId}`}>{roomId}</a> for <code>{event["content"]?.["reason"] ?? '<no reason supplied>'}</code>.<br/> | ||
| Would you like to add the ban to a policy list? | ||
| <ol> | ||
| {mjolnir.policyListManager.lists.map(list => <li>{list}</li>)} | ||
| </ol> | ||
| </root>, | ||
| mjolnir.managementRoomId, | ||
| undefined, | ||
| mjolnir.client | ||
| )).at(0) as string; | ||
| } | ||
|
|
||
| export class BanPropagation extends Protection { | ||
|
|
||
| settings = {}; | ||
|
|
||
| public get name(): string { | ||
| return 'BanPropagationProtection'; | ||
| } | ||
| public get description(): string { | ||
| return "When you ban a user in any protected room with a client, this protection\ | ||
| will turn the room level ban into a policy for a policy list of your choice.\ | ||
| This will then allow the bot to ban the user from all of your rooms."; | ||
| } | ||
|
|
||
| public async handleEvent(mjolnir: Mjolnir, roomId: string, event: any): Promise<any> { | ||
| if (event['type'] !== 'm.room.member' || event['content']?.['membership'] !== 'ban') { | ||
| return; | ||
| } | ||
| if (mjolnir.policyListManager.lists.map( | ||
| list => list.rulesMatchingEntity(event['state_key'], RULE_USER) | ||
| ).some(rules => rules.length > 0) | ||
| ) { | ||
| return; // The user is already banned. | ||
| } | ||
| const promptListener = new PromptResponseListener( | ||
| mjolnir.matrixEmitter, | ||
| await mjolnir.client.getUserId(), | ||
| mjolnir.client | ||
| ); | ||
| // do not await, we don't want to block the protection manager | ||
| promptListener.waitForPresentationList( | ||
| mjolnir.policyListManager.lists, | ||
| mjolnir.managementRoomId, | ||
| promptBanPropagation(mjolnir, event, roomId) | ||
| ).then(listResult => { | ||
| if (listResult.isOk()) { | ||
| const list = listResult.ok; | ||
| return list.banEntity(RULE_USER, event['state_key'], event['content']?.["reason"]); | ||
| } else { | ||
| LogService.warn("BanPropagation", "Timed out waiting for a response to a room level ban", listResult.err); | ||
| return; | ||
| } | ||
| }).catch(e => { | ||
| LogService.error('BanPropagation', "Encountered an error while prompting the user for instructions to propagate a room level ban", e); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,58 @@ | ||
| import expect from "expect"; | ||
| import { Mjolnir } from "../../src/Mjolnir"; | ||
| import { newTestUser } from "./clientHelper"; | ||
| import { getFirstEventMatching } from './commands/commandUtils'; | ||
| import { Permalinks } from "matrix-bot-sdk"; | ||
| import { RULE_USER } from "../../src/models/ListRule"; | ||
|
|
||
| // We will need to disable this in tests that are banning people otherwise it will cause | ||
| // mocha to hang for awhile until it times out waiting for a response to a prompt. | ||
| describe("Ban propagation test", function() { | ||
| it("Should be enabled by default", async function() { | ||
| const mjolnir: Mjolnir = this.mjolnir | ||
| expect(mjolnir.protectionManager.getProtection("BanPropagationProtection")?.enabled).toBeTruthy(); | ||
| }) | ||
| it("Should prompt to add bans to a policy list, then add the ban", async function() { | ||
| const mjolnir: Mjolnir = this.mjolnir | ||
| const mjolnirId = await mjolnir.client.getUserId(); | ||
| const moderator = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator" } }); | ||
| await moderator.joinRoom(mjolnir.managementRoomId); | ||
| const protectedRooms = await Promise.all([...Array(5)].map(async _ => { | ||
| const room = await moderator.createRoom({ invite: [mjolnirId] }); | ||
| await mjolnir.client.joinRoom(room); | ||
| await moderator.setUserPowerLevel(mjolnirId, room, 100); | ||
| await mjolnir.addProtectedRoom(room); | ||
| return room; | ||
| })); | ||
| // create a policy list so that we can check it for a user rule later | ||
| const policyListId = await moderator.createRoom({ invite: [mjolnirId] }); | ||
| await moderator.setUserPowerLevel(mjolnirId, policyListId, 100); | ||
| await mjolnir.client.joinRoom(policyListId); | ||
| await mjolnir.policyListManager.watchList(Permalinks.forRoom(policyListId)); | ||
|
|
||
| // check for the prompt | ||
| const promptEvent = await getFirstEventMatching({ | ||
| matrix: mjolnir.matrixEmitter, | ||
| targetRoom: mjolnir.managementRoomId, | ||
| lookAfterEvent: async function () { | ||
| // ban a user in one of our protected rooms using the moderator | ||
| await moderator.banUser('@test:example.com', protectedRooms[0], "spam"); | ||
| return undefined; | ||
| }, | ||
| predicate: function (event: any): boolean { | ||
| return (event['content']?.['body'] ?? '').startsWith('The user') | ||
| } | ||
| }) | ||
| // select the prompt | ||
| await moderator.unstableApis.addReactionToEvent( | ||
| mjolnir.managementRoomId, promptEvent['event_id'], '1.' | ||
| ); | ||
| // check the policy list, after waiting a few seconds. | ||
| await new Promise(resolve => setTimeout(resolve, 10000)); | ||
| const policyList = mjolnir.policyListManager.lists[0]; | ||
| const rules = policyList.rulesMatchingEntity('@test:example.com', RULE_USER); | ||
| expect(rules.length).toBe(1); | ||
| expect(rules[0].entity).toBe('@test:example.com'); | ||
| expect(rules[0].reason).toBe('spam'); | ||
| }) | ||
| }) |
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.
Uh oh!
There was an error while loading. Please reload this page.