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 3 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
127 changes: 127 additions & 0 deletions src/protections/PropagateRoomBan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
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 { Protection } from './IProtection';
import { Mjolnir } from '../Mjolnir';
import { StringProtectionSetting } from './ProtectionSettings';
import { LogLevel, extractRequestError } from 'matrix-bot-sdk';
import { recommendationToStable, RECOMMENDATION_BAN } from '../models/ListRule';
import { RULE_USER } from '../models/BanList';
import { DEFAULT_LIST_EVENT_TYPE } from '../commands/SetDefaultBanListCommand';

export class PropagateRoomBan extends Protection {
settings = {
banListShortcode: new StringProtectionSetting(),
};

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 ' +
'(defaults to the default banlist).'
);
}

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,
};

let banListShortcode: string = this.settings.banListShortcode.value;
if (banListShortcode === '') {
// try to use default banList
try {
const data: { shortcode: string } =
await mjolnir.client.getAccountData(
DEFAULT_LIST_EVENT_TYPE
);
banListShortcode = data['shortcode'];
} catch (e) {
await mjolnir.logMessage(
Copy link
Contributor

Choose a reason for hiding this comment

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

question: How come these are separate messages? Does the error make it look ugly or something? The only reason I ask is because I'm worried someone doesn't understand where the error in the second message comes from (but granted we might even do this elsewhere, so it is probably ok)

Copy link
Author

Choose a reason for hiding this comment

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

It's done in UnbanBanCommand.ts the same way. I've got the inspiration from there.

LogLevel.WARN,
'PropagateRoomBan',
`Can not publish to banlist. User ${bannedUser} was banned in ${roomId}, but protection setting banListShortcode is missing and could not get default banlist`
);
await mjolnir.logMessage(
LogLevel.WARN,
'PropagateRoomBan',
extractRequestError(e)
);
return;
}
}

const banlist = mjolnir.lists.find(
(bl) =>
bl.listShortcode.toLowerCase() ===
banListShortcode.toLowerCase()
);

if (!banlist) {
await mjolnir.logMessage(
LogLevel.WARN,
'PropagateRoomBan',
`Can not publish to banlist. User ${bannedUser} was banned in ${roomId}, but banlist ${banListShortcode} is not found`
);
return;
}

await mjolnir.client.sendStateEvent(
banlist.roomId,
RULE_USER,
stateKey,
ruleContent
);
await mjolnir.logMessage(
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()
];