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

Introduced a protection which lets the room bans propagate to a defined banlist #223

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
111 changes: 111 additions & 0 deletions src/protections/PropagateRoomBan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
Copyright 2021 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 { StringProtectionSetting } from './ProtectionSettings';
import { Mjolnir } from '../Mjolnir';
import { recommendationToStable, RECOMMENDATION_BAN } from '../models/ListRule';
import { logMessage } from '../LogProxy';
gergof marked this conversation as resolved.
Show resolved Hide resolved
import { LogLevel } from 'matrix-bot-sdk';
import { RULE_USER } from '../models/BanList';
import { Protection } from './IProtection';

export class PropagateRoomBan extends Protection {
settings = {
banlistShortcode: new StringProtectionSetting(),
Copy link
Contributor

Choose a reason for hiding this comment

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

internally we use BanList and banList so this should probably be banListShortcode

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think that this should be tied to to the default list used by Mjolnir's own ban command, to avoid confusion?

(Like this https://github.com/matrix-org/mjolnir/blob/v1.3.2/src/commands/UnbanBanCommand.ts#L33-L42 )

Copy link
Author

Choose a reason for hiding this comment

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

Probably we should default to the default list, but I think we should allow the admins to overwrite it.

Copy link
Author

Choose a reason for hiding this comment

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

So I made a change where it defaults to the default banlist, but still allows it to be overwritten. Tell me what you think. The main reason I want it to be overwritable because I would like to separate the room admin bans from the server admin bans (since the latter have a higher trust level).

Copy link
Contributor

Choose a reason for hiding this comment

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

Probably we should default to the default list, but I think we should allow the admins to overwrite it

That seems fair and the way you have done this works. 👍

};

public get name(): string {
return 'PropagateRoomBan';
}

public get description(): string {
return (
'If a user is banned in a protected room by a room administrator then the ban ' +
'will be published to the banlist defined using the banlistShortcode setting.'
);
}

public async handleEvent(mjolnir: Mjolnir, roomId: string, event: any): Promise<void> {
if (!this.isBanEvent(event)) {
// only interested in ban events
return;
}

const content = event['content'] || {};
const bannedUser = event['state_key'];
const banReason = content['reason'] || '<no reason supplied>';
const sender = event['sender'];
const stateKey = `rule:${bannedUser}`;

const ruleContent = {
entity: bannedUser,
recommendation: recommendationToStable(RECOMMENDATION_BAN),
reason: banReason,
};

if (this.settings.banlistShortcode.value === '') {
await logMessage(
gergof marked this conversation as resolved.
Show resolved Hide resolved
LogLevel.WARN,
'PropagateRoomBan',
`Can not publish to banlist. User ${bannedUser} was banned in ${roomId}, but protection setting banlistShortcode is missing`
);
return;
}

const banlist = mjolnir.lists.find(
(bl) =>
bl.listShortcode.toLowerCase() ===
this.settings.banlistShortcode.value.toLowerCase()
);

if (!banlist) {
await logMessage(
gergof marked this conversation as resolved.
Show resolved Hide resolved
LogLevel.WARN,
'PropagateRoomBan',
`Can not publish to banlist. User ${bannedUser} was banned in ${roomId}, but banlist ${this.settings.banlistShortcode.value} is not found`
);
return;
}

await mjolnir.client.sendStateEvent(
banlist.roomId,
RULE_USER,
stateKey,
ruleContent
);
await logMessage(
gergof marked this conversation as resolved.
Show resolved Hide resolved
LogLevel.INFO,
'PropagateRoomBan',
`User ${bannedUser} added to banlist ${banlist.listShortcode}, because ${sender} banned him in ${roomId} for: ${banReason}`
);
}

private isBanEvent(event: any): boolean {
if (event['type'] !== 'm.room.member') {
return false;
}

const membership: string = event['content']['membership'];
let prevMembership = 'join';

if (event['unsigned'] && event['unsigned']['prev_content']) {
prevMembership =
event['unsigned']['prev_content']['membership'] || 'join';
}

return membership === 'ban' && prevMembership !== 'ban';
}
}
4 changes: 3 additions & 1 deletion src/protections/protections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import { WordList } from "./WordList";
import { MessageIsVoice } from "./MessageIsVoice";
import { MessageIsMedia } from "./MessageIsMedia";
import { TrustedReporters } from "./TrustedReporters";
import { PropagateRoomBan } from './PropagateRoomBan';

export const PROTECTIONS: IProtection[] = [
new FirstMessageIsImage(),
new BasicFlooding(),
new WordList(),
new MessageIsVoice(),
new MessageIsMedia(),
new TrustedReporters()
new TrustedReporters(),
new PropagateRoomBan()
];