enable noImplicitAny

This commit is contained in:
jesopo 2022-01-31 20:46:39 +00:00
parent 0cde70e846
commit 0b7da86b9c
13 changed files with 75 additions and 63 deletions

View File

@ -16,6 +16,7 @@
"test:manual": "NODE_ENV=harness ts-node test/integration/manualLaunchScript.ts" "test:manual": "NODE_ENV=harness ts-node test/integration/manualLaunchScript.ts"
}, },
"devDependencies": { "devDependencies": {
"@types/config": "0.0.41",
"@types/crypto-js": "^4.0.2", "@types/crypto-js": "^4.0.2",
"@types/jsdom": "^16.2.11", "@types/jsdom": "^16.2.11",
"@types/mocha": "^9.0.0", "@types/mocha": "^9.0.0",

View File

@ -17,7 +17,7 @@ limitations under the License.
export const ERROR_KIND_PERMISSION = "permission"; export const ERROR_KIND_PERMISSION = "permission";
export const ERROR_KIND_FATAL = "fatal"; export const ERROR_KIND_FATAL = "fatal";
const TRIGGER_INTERVALS = { const TRIGGER_INTERVALS: { [key: string]: number } = {
[ERROR_KIND_PERMISSION]: 3 * 60 * 60 * 1000, // 3 hours [ERROR_KIND_PERMISSION]: 3 * 60 * 60 * 1000, // 3 hours
[ERROR_KIND_FATAL]: 15 * 60 * 1000, // 15 minutes [ERROR_KIND_FATAL]: 15 * 60 * 1000, // 15 minutes
}; };

View File

@ -84,7 +84,7 @@ export class Mjolnir {
* @param {boolean} options.autojoinOnlyIfManager Whether to only accept an invitation by a user present in the `managementRoom`. * @param {boolean} options.autojoinOnlyIfManager Whether to only accept an invitation by a user present in the `managementRoom`.
* @param {string} options.acceptInvitesFromGroup A group of users to accept invites from, ignores invites form users not in this group. * @param {string} options.acceptInvitesFromGroup A group of users to accept invites from, ignores invites form users not in this group.
*/ */
private static addJoinOnInviteListener(mjolnir: Mjolnir, client: MatrixClient, options) { private static addJoinOnInviteListener(mjolnir: Mjolnir, client: MatrixClient, options: { [key: string]: any }) {
client.on("room.invite", async (roomId: string, inviteEvent: any) => { client.on("room.invite", async (roomId: string, inviteEvent: any) => {
const membershipEvent = new MembershipEvent(inviteEvent); const membershipEvent = new MembershipEvent(inviteEvent);
@ -271,7 +271,7 @@ export class Mjolnir {
await logMessage(LogLevel.DEBUG, "Mjolnir@startup", "Loading protected rooms..."); await logMessage(LogLevel.DEBUG, "Mjolnir@startup", "Loading protected rooms...");
await this.resyncJoinedRooms(false); await this.resyncJoinedRooms(false);
try { try {
const data: Object | null = await this.client.getAccountData(PROTECTED_ROOMS_EVENT_TYPE); const data: { rooms?: string[] } | null = await this.client.getAccountData(PROTECTED_ROOMS_EVENT_TYPE);
if (data && data['rooms']) { if (data && data['rooms']) {
for (const roomId of data['rooms']) { for (const roomId of data['rooms']) {
this.protectedRooms[roomId] = Permalinks.forRoom(roomId); this.protectedRooms[roomId] = Permalinks.forRoom(roomId);
@ -328,15 +328,15 @@ export class Mjolnir {
if (unprotectedIdx >= 0) this.knownUnprotectedRooms.splice(unprotectedIdx, 1); if (unprotectedIdx >= 0) this.knownUnprotectedRooms.splice(unprotectedIdx, 1);
this.explicitlyProtectedRoomIds.push(roomId); this.explicitlyProtectedRoomIds.push(roomId);
let additionalProtectedRooms; let additionalProtectedRooms: { rooms?: string[] } | null = null;
try { try {
additionalProtectedRooms = await this.client.getAccountData(PROTECTED_ROOMS_EVENT_TYPE); additionalProtectedRooms = await this.client.getAccountData(PROTECTED_ROOMS_EVENT_TYPE);
} catch (e) { } catch (e) {
LogService.warn("Mjolnir", extractRequestError(e)); LogService.warn("Mjolnir", extractRequestError(e));
} }
if (!additionalProtectedRooms || !additionalProtectedRooms['rooms']) additionalProtectedRooms = { rooms: [] }; const rooms = (additionalProtectedRooms?.rooms ?? []);
additionalProtectedRooms.rooms.push(roomId); rooms.push(roomId);
await this.client.setAccountData(PROTECTED_ROOMS_EVENT_TYPE, additionalProtectedRooms); await this.client.setAccountData(PROTECTED_ROOMS_EVENT_TYPE, { rooms: rooms });
await this.syncLists(config.verboseLogging); await this.syncLists(config.verboseLogging);
} }
@ -346,14 +346,13 @@ export class Mjolnir {
const idx = this.explicitlyProtectedRoomIds.indexOf(roomId); const idx = this.explicitlyProtectedRoomIds.indexOf(roomId);
if (idx >= 0) this.explicitlyProtectedRoomIds.splice(idx, 1); if (idx >= 0) this.explicitlyProtectedRoomIds.splice(idx, 1);
let additionalProtectedRooms; let additionalProtectedRooms: { rooms?: string[] } | null = null;
try { try {
additionalProtectedRooms = await this.client.getAccountData(PROTECTED_ROOMS_EVENT_TYPE); additionalProtectedRooms = await this.client.getAccountData(PROTECTED_ROOMS_EVENT_TYPE);
} catch (e) { } catch (e) {
LogService.warn("Mjolnir", extractRequestError(e)); LogService.warn("Mjolnir", extractRequestError(e));
} }
if (!additionalProtectedRooms || !additionalProtectedRooms['rooms']) additionalProtectedRooms = { rooms: [] }; additionalProtectedRooms = { rooms: additionalProtectedRooms?.rooms?.filter(r => r !== roomId) ?? [] };
additionalProtectedRooms.rooms = additionalProtectedRooms.rooms.filter(r => r !== roomId);
await this.client.setAccountData(PROTECTED_ROOMS_EVENT_TYPE, additionalProtectedRooms); await this.client.setAccountData(PROTECTED_ROOMS_EVENT_TYPE, additionalProtectedRooms);
} }
@ -379,7 +378,7 @@ export class Mjolnir {
private async getEnabledProtections() { private async getEnabledProtections() {
let enabled: string[] = []; let enabled: string[] = [];
try { try {
const protections: Object | null = await this.client.getAccountData(ENABLED_PROTECTIONS_EVENT_TYPE); const protections: { enabled: string[] } | null = await this.client.getAccountData(ENABLED_PROTECTIONS_EVENT_TYPE);
if (protections && protections['enabled']) { if (protections && protections['enabled']) {
for (const protection of protections['enabled']) { for (const protection of protections['enabled']) {
enabled.push(protection); enabled.push(protection);
@ -555,8 +554,8 @@ export class Mjolnir {
this.applyUnprotectedRooms(); this.applyUnprotectedRooms();
try { try {
const accountData: Object | null = await this.client.getAccountData(WARN_UNPROTECTED_ROOM_EVENT_PREFIX + roomId); const accountData: { warned: boolean } | null = await this.client.getAccountData(WARN_UNPROTECTED_ROOM_EVENT_PREFIX + roomId);
if (accountData && accountData['warned']) return; // already warned if (accountData && accountData.warned) return; // already warned
} catch (e) { } catch (e) {
// Ignore - probably haven't warned about it yet // Ignore - probably haven't warned about it yet
} }
@ -575,14 +574,14 @@ export class Mjolnir {
const banLists: BanList[] = []; const banLists: BanList[] = [];
const joinedRooms = await this.client.getJoinedRooms(); const joinedRooms = await this.client.getJoinedRooms();
let watchedListsEvent = {}; let watchedListsEvent: { references?: string[] } | null = null;
try { try {
watchedListsEvent = await this.client.getAccountData(WATCHED_LISTS_EVENT_TYPE); watchedListsEvent = await this.client.getAccountData(WATCHED_LISTS_EVENT_TYPE);
} catch (e) { } catch (e) {
// ignore - not important // ignore - not important
} }
for (const roomRef of (watchedListsEvent['references'] || [])) { for (const roomRef of (watchedListsEvent?.references || [])) {
const permalink = Permalinks.parseUrl(roomRef); const permalink = Permalinks.parseUrl(roomRef);
if (!permalink.roomIdOrAlias) continue; if (!permalink.roomIdOrAlias) continue;

View File

@ -40,7 +40,7 @@ import { execKickCommand } from "./KickCommand";
export const COMMAND_PREFIX = "!mjolnir"; export const COMMAND_PREFIX = "!mjolnir";
export async function handleCommand(roomId: string, event: any, mjolnir: Mjolnir) { export async function handleCommand(roomId: string, event: { content: { body: string } }, mjolnir: Mjolnir) {
const cmd = event['content']['body']; const cmd = event['content']['body'];
const parts = cmd.trim().split(' ').filter(p => p.trim().length > 0); const parts = cmd.trim().split(' ').filter(p => p.trim().length > 0);

View File

@ -23,7 +23,7 @@ export async function execCreateListCommand(roomId: string, event: any, mjolnir:
const shortcode = parts[3]; const shortcode = parts[3];
const aliasLocalpart = parts[4]; const aliasLocalpart = parts[4];
const powerLevels = { const powerLevels: { [key: string]: any } = {
"ban": 50, "ban": 50,
"events": { "events": {
"m.room.name": 100, "m.room.name": 100,
@ -38,12 +38,11 @@ export async function execCreateListCommand(roomId: string, event: any, mjolnir:
"redact": 50, "redact": 50,
"state_default": 50, "state_default": 50,
"users": { "users": {
// populated in a moment [await mjolnir.client.getUserId()]: 100,
[event["sender"]]: 50
}, },
"users_default": 0, "users_default": 0,
}; };
powerLevels['users'][await mjolnir.client.getUserId()] = 100;
powerLevels['users'][event['sender']] = 50;
const listRoomId = await mjolnir.client.createRoom({ const listRoomId = await mjolnir.client.createRoom({
preset: "public_chat", preset: "public_chat",

View File

@ -31,9 +31,9 @@ interface Arguments {
// Exported for tests // Exported for tests
export async function parseArguments(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]): Promise<Arguments|null> { export async function parseArguments(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]): Promise<Arguments|null> {
let defaultShortcode = null; let defaultShortcode: string | null = null;
try { try {
const data: Object = await mjolnir.client.getAccountData(DEFAULT_LIST_EVENT_TYPE); const data: { shortcode: string } = await mjolnir.client.getAccountData(DEFAULT_LIST_EVENT_TYPE);
defaultShortcode = data['shortcode']; defaultShortcode = data['shortcode'];
} catch (e) { } catch (e) {
LogService.warn("UnbanBanCommand", "Non-fatal error getting default ban list"); LogService.warn("UnbanBanCommand", "Non-fatal error getting default ban list");

View File

@ -91,7 +91,7 @@ class BanList extends EventEmitter {
* @param roomRef A sharable/clickable matrix URL that refers to the room. * @param roomRef A sharable/clickable matrix URL that refers to the room.
* @param client A matrix client that is used to read the state of the room when `updateList` is called. * @param client A matrix client that is used to read the state of the room when `updateList` is called.
*/ */
constructor(public readonly roomId: string, public readonly roomRef, private client: MatrixClient) { constructor(public readonly roomId: string, public readonly roomRef: string, private client: MatrixClient) {
super(); super();
} }

View File

@ -30,12 +30,11 @@ export class BasicFlooding implements IProtection {
private lastEvents: { [roomId: string]: { [userId: string]: { originServerTs: number, eventId: string }[] } } = {}; private lastEvents: { [roomId: string]: { [userId: string]: { originServerTs: number, eventId: string }[] } } = {};
private recentlyBanned: string[] = []; private recentlyBanned: string[] = [];
maxPerMinute = new NumberProtectionSetting(DEFAULT_MAX_PER_MINUTE); settings = {
settings = {}; maxPerMinute: new NumberProtectionSetting(DEFAULT_MAX_PER_MINUTE)
};
constructor() { constructor() { }
this.settings['maxPerMinute'] = this.maxPerMinute;
}
public get name(): string { public get name(): string {
return 'BasicFloodingProtection'; return 'BasicFloodingProtection';
@ -62,7 +61,7 @@ export class BasicFlooding implements IProtection {
messageCount++; messageCount++;
} }
if (messageCount >= this.maxPerMinute.value) { if (messageCount >= this.settings.maxPerMinute.value) {
await logMessage(LogLevel.WARN, "BasicFlooding", `Banning ${event['sender']} in ${roomId} for flooding (${messageCount} messages in the last minute)`, roomId); await logMessage(LogLevel.WARN, "BasicFlooding", `Banning ${event['sender']} in ${roomId} for flooding (${messageCount} messages in the last minute)`, roomId);
if (!config.noop) { if (!config.noop) {
await mjolnir.client.banUser(event['sender'], roomId, "spam"); await mjolnir.client.banUser(event['sender'], roomId, "spam");
@ -88,8 +87,8 @@ export class BasicFlooding implements IProtection {
} }
// Trim the oldest messages off the user's history if it's getting large // Trim the oldest messages off the user's history if it's getting large
if (forUser.length > this.maxPerMinute.value * 2) { if (forUser.length > this.settings.maxPerMinute.value * 2) {
forUser.splice(0, forUser.length - (this.maxPerMinute.value * 2) - 1); forUser.splice(0, forUser.length - (this.settings.maxPerMinute.value * 2) - 1);
} }
} }
} }

View File

@ -76,13 +76,13 @@ export function isListSetting(object: any): object is AbstractProtectionListSett
export class StringProtectionSetting extends AbstractProtectionSetting<string, string> { export class StringProtectionSetting extends AbstractProtectionSetting<string, string> {
value = ""; value = "";
fromString = (data) => data; fromString = (data: string): string => data;
validate = (data) => true; validate = (data: string): boolean => true;
} }
export class StringListProtectionSetting extends AbstractProtectionListSetting<string, string[]> { export class StringListProtectionSetting extends AbstractProtectionListSetting<string, string[]> {
value: string[] = []; value: string[] = [];
fromString = (data) => data; fromString = (data: string): string => data;
validate = (data) => true; validate = (data: string): boolean => true;
addValue(data: string): string[] { addValue(data: string): string[] {
return [...this.value, data]; return [...this.value, data];
} }
@ -107,11 +107,11 @@ export class NumberProtectionSetting extends AbstractProtectionSetting<number, n
this.max = max; this.max = max;
} }
fromString(data) { fromString(data: string) {
let number = Number(data); let number = Number(data);
return isNaN(number) ? undefined : number; return isNaN(number) ? undefined : number;
} }
validate(data) { validate(data: number) {
return (!isNaN(data) return (!isNaN(data)
&& (this.min === undefined || this.min <= data) && (this.min === undefined || this.min <= data)
&& (this.max === undefined || data <= this.max)) && (this.max === undefined || data <= this.max))

View File

@ -235,9 +235,9 @@ export class ReportManager {
"m.relationship": { "m.relationship": {
"rel_type": "m.reference", "rel_type": "m.reference",
"event_id": relation.event_id, "event_id": relation.event_id,
} },
[ABUSE_ACTION_CONFIRMATION_KEY]: confirmationReport
}; };
confirmation[ABUSE_ACTION_CONFIRMATION_KEY] = confirmationReport;
let requestConfirmationEventId = await this.mjolnir.client.sendMessage(this.mjolnir.managementRoomId, confirmation); let requestConfirmationEventId = await this.mjolnir.client.sendMessage(this.mjolnir.managementRoomId, confirmation);
await this.mjolnir.client.sendEvent(this.mjolnir.managementRoomId, "m.reaction", { await this.mjolnir.client.sendEvent(this.mjolnir.managementRoomId, "m.reaction", {
@ -693,35 +693,41 @@ class DisplayManager {
// Ignore. // Ignore.
} }
let eventContent; enum OutType {
msg, text, html
}
let eventContent: [OutType, string];
try { try {
if (event["type"] === "m.room.encrypted") { if (event["type"] === "m.room.encrypted") {
eventContent = { msg: "<encrypted content>" }; eventContent = [OutType.msg, "<encrypted content>"];
} else if ("content" in event) { } else if ("content" in event) {
const MAX_EVENT_CONTENT_LENGTH = 2048; const MAX_EVENT_CONTENT_LENGTH = 2048;
const MAX_NEWLINES = 64; const MAX_NEWLINES = 64;
if ("formatted_body" in event.content) { if ("formatted_body" in event.content) {
eventContent = { html: this.limitLength(event.content.formatted_body, MAX_EVENT_CONTENT_LENGTH, MAX_NEWLINES) }; eventContent = [OutType.html, this.limitLength(event.content.formatted_body, MAX_EVENT_CONTENT_LENGTH, MAX_NEWLINES)];
} else if ("body" in event.content) { } else if ("body" in event.content) {
eventContent = { text: this.limitLength(event.content.body, MAX_EVENT_CONTENT_LENGTH, MAX_NEWLINES) }; eventContent = [OutType.text, this.limitLength(event.content.body, MAX_EVENT_CONTENT_LENGTH, MAX_NEWLINES)];
} else { } else {
eventContent = { text: this.limitLength(JSON.stringify(event["content"], null, 2), MAX_EVENT_CONTENT_LENGTH, MAX_NEWLINES) }; eventContent = [OutType.text, this.limitLength(JSON.stringify(event["content"], null, 2), MAX_EVENT_CONTENT_LENGTH, MAX_NEWLINES)];
} }
} else {
eventContent = [OutType.msg, "Malformed event, cannot read content."];
} }
} catch (ex) { } catch (ex) {
eventContent = { msg: `<Cannot extract event. Please verify that Mjölnir has been invited to room ${roomAliasOrId} and made room moderator or administrator>.` }; eventContent = [OutType.msg, `<Cannot extract event. Please verify that Mjölnir has been invited to room ${roomAliasOrId} and made room moderator or administrator>.`];
} }
let accusedId = event["sender"]; let accusedId = event["sender"];
let reporterDisplayName: string, accusedDisplayName: string; let reporterDisplayName: string, accusedDisplayName: string;
try { try {
reporterDisplayName = await this.owner.mjolnir.client.getUserProfile(reporterId)["displayname"] || reporterId; reporterDisplayName = (await this.owner.mjolnir.client.getUserProfile(reporterId))["displayname"] || reporterId;
} catch (ex) { } catch (ex) {
reporterDisplayName = "<Error: Cannot extract reporter display name>"; reporterDisplayName = "<Error: Cannot extract reporter display name>";
} }
try { try {
accusedDisplayName = await this.owner.mjolnir.client.getUserProfile(accusedId)["displayname"] || accusedId; accusedDisplayName = (await this.owner.mjolnir.client.getUserProfile(accusedId))["displayname"] || accusedId;
} catch (ex) { } catch (ex) {
accusedDisplayName = "<Error: Cannot extract accused display name>"; accusedDisplayName = "<Error: Cannot extract accused display name>";
} }
@ -832,17 +838,18 @@ class DisplayManager {
} }
// ...insert HTML content // ...insert HTML content
for (let [key, value] of [ for (let {key, value} of [
['event-content', eventContent], { key: 'event-content', value: eventContent },
]) { ]) {
let node = document.getElementById(key); let node = document.getElementById(key);
if (node) { if (node) {
if ("msg" in value) { let [outType, out]: [OutType, string] = value;
node.textContent = value.msg; if (outType === OutType.msg) {
} else if ("text" in value) { node.textContent = out;
node.textContent = value.text; } else if (outType === OutType.text) {
} else if ("html" in value) { node.textContent = out;
node.innerHTML = value.html; } else if (outType === OutType.html) {
node.innerHTML = out;
} }
} }
} }
@ -868,8 +875,8 @@ class DisplayManager {
body: htmlToText(document.body.outerHTML, { wordwrap: false }), body: htmlToText(document.body.outerHTML, { wordwrap: false }),
format: "org.matrix.custom.html", format: "org.matrix.custom.html",
formatted_body: document.body.outerHTML, formatted_body: document.body.outerHTML,
[ABUSE_REPORT_KEY]: report
}; };
notice[ABUSE_REPORT_KEY] = report;
let noticeEventId = await this.owner.mjolnir.client.sendMessage(this.owner.mjolnir.managementRoomId, notice); let noticeEventId = await this.owner.mjolnir.client.sendMessage(this.owner.mjolnir.managementRoomId, notice);
if (kind !== Kind.ERROR) { if (kind !== Kind.ERROR) {

View File

@ -225,11 +225,13 @@ function patchMatrixClientForConciseExceptions() {
return; return;
} }
let originalRequestFn = getRequestFn(); let originalRequestFn = getRequestFn();
setRequestFn((params, cb) => { setRequestFn((params: { [k: string]: any }, cb: any) => {
// Store an error early, to maintain *some* semblance of stack. // Store an error early, to maintain *some* semblance of stack.
// We'll only throw the error if there is one. // We'll only throw the error if there is one.
let error = new Error("STACK CAPTURE"); let error = new Error("STACK CAPTURE");
originalRequestFn(params, function conciseExceptionRequestFn(err, response, resBody) { originalRequestFn(params, function conciseExceptionRequestFn(
err: { [key: string]: any }, response: { [key: string]: any }, resBody: string
) {
if (!err && (response?.statusCode < 200 || response?.statusCode >= 300)) { if (!err && (response?.statusCode < 200 || response?.statusCode >= 300)) {
// Normally, converting HTTP Errors into rejections is done by the caller // Normally, converting HTTP Errors into rejections is done by the caller
// of `requestFn` within matrix-bot-sdk. However, this always ends up rejecting // of `requestFn` within matrix-bot-sdk. However, this always ends up rejecting
@ -332,7 +334,7 @@ function patchMatrixClientForRetry() {
return; return;
} }
let originalRequestFn = getRequestFn(); let originalRequestFn = getRequestFn();
setRequestFn(async (params, cb) => { setRequestFn(async (params: { [k: string]: any }, cb: any) => {
let attempt = 1; let attempt = 1;
numberOfConcurrentRequests += 1; numberOfConcurrentRequests += 1;
if (TRACE_CONCURRENT_REQUESTS) { if (TRACE_CONCURRENT_REQUESTS) {
@ -342,7 +344,9 @@ function patchMatrixClientForRetry() {
while (true) { while (true) {
try { try {
let result: any[] = await new Promise((resolve, reject) => { let result: any[] = await new Promise((resolve, reject) => {
originalRequestFn(params, function requestFnWithRetry(err, response, resBody) { originalRequestFn(params, function requestFnWithRetry(
err: { [key: string]: any }, response: { [key: string]: any }, resBody: string
) {
// Note: There is no data race on `attempt` as we `await` before continuing // Note: There is no data race on `attempt` as we `await` before continuing
// to the next iteration of the loop. // to the next iteration of the loop.
if (attempt < MAX_REQUEST_ATTEMPTS && err?.body?.errcode === 'M_LIMIT_EXCEEDED') { if (attempt < MAX_REQUEST_ATTEMPTS && err?.body?.errcode === 'M_LIMIT_EXCEEDED') {

View File

@ -107,16 +107,19 @@ export class WebAPIs {
{ {
// -- Create a client on behalf of the reporter. // -- Create a client on behalf of the reporter.
// We'll use it to confirm the authenticity of the report. // We'll use it to confirm the authenticity of the report.
let accessToken; let accessToken: string | undefined = undefined;
// Authentication mechanism 1: Request header. // Authentication mechanism 1: Request header.
let authorization = request.get('Authorization'); let authorization = request.get('Authorization');
if (authorization) { if (authorization) {
[, accessToken] = AUTHORIZATION.exec(authorization)!; [, accessToken] = AUTHORIZATION.exec(authorization)!;
} else { } else if (typeof(request.query["access_token"]) === 'string') {
// Authentication mechanism 2: Access token as query parameter. // Authentication mechanism 2: Access token as query parameter.
accessToken = request.query["access_token"]; accessToken = request.query["access_token"];
} else {
response.status(401).send("Missing access token");
return;
} }
// Create a client dedicated to this report. // Create a client dedicated to this report.

View File

@ -9,7 +9,7 @@
"noImplicitReturns": true, "noImplicitReturns": true,
"noUnusedLocals": true, "noUnusedLocals": true,
"target": "es2015", "target": "es2015",
"noImplicitAny": false, "noImplicitAny": true,
"sourceMap": true, "sourceMap": true,
"strictNullChecks": true, "strictNullChecks": true,
"outDir": "./lib", "outDir": "./lib",