Add m.direct filter / validation (#10436)

This commit is contained in:
Michael Weimann 2023-03-27 08:37:35 +02:00 committed by GitHub
parent e5f06df3f7
commit ba2608ec74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 267 additions and 15 deletions

View file

@ -23,6 +23,7 @@ import { MatrixEvent } from "matrix-js-sdk/src/models/event";
import { Optional } from "matrix-events-sdk";
import { MatrixClientPeg } from "../MatrixClientPeg";
import { filterValidMDirect } from "./dm/filterValidMDirect";
/**
* Class that takes a Matrix Client and flips the m.direct map
@ -44,8 +45,8 @@ export default class DMRoomMap {
// see onAccountData
this.hasSentOutPatchDirectAccountDataPatch = false;
const mDirectEvent = matrixClient.getAccountData(EventType.Direct)?.getContent() ?? {};
this.mDirectEvent = { ...mDirectEvent }; // copy as we will mutate
const mDirectRawContent = matrixClient.getAccountData(EventType.Direct)?.getContent() ?? {};
this.setMDirectFromContent(mDirectRawContent);
}
/**
@ -84,9 +85,28 @@ export default class DMRoomMap {
this.matrixClient.removeListener(ClientEvent.AccountData, this.onAccountData);
}
/**
* Filter m.direct content to contain only valid data and then sets it.
* Logs if invalid m.direct content occurs.
* {@link filterValidMDirect}
*
* @param content - Raw m.direct content
*/
private setMDirectFromContent(content: unknown): void {
const { valid, filteredContent } = filterValidMDirect(content);
if (!valid) {
logger.warn("Invalid m.direct content occurred", content);
}
this.mDirectEvent = filteredContent;
}
private onAccountData = (ev: MatrixEvent): void => {
console.log("onAccountData");
if (ev.getType() == EventType.Direct) {
this.mDirectEvent = { ...ev.getContent() }; // copy as we will mutate
this.setMDirectFromContent(ev.getContent());
this.userToRooms = null;
this.roomToUser = null;
}

View file

@ -0,0 +1,69 @@
/*
Copyright 2023 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.
*/
interface FilterValidMDirectResult {
/** Whether the entire content is valid */
valid: boolean;
/** Filtered content with only the valid parts */
filteredContent: Record<string, string[]>;
}
/**
* Filter m.direct content to be compliant to https://spec.matrix.org/v1.6/client-server-api/#mdirect.
*
* @param content - Raw event content to be filerted
* @returns value as a flag whether to content was valid.
* filteredContent with only values from the content that are spec compliant.
*/
export const filterValidMDirect = (content: unknown): FilterValidMDirectResult => {
if (content === null || typeof content !== "object") {
return {
valid: false,
filteredContent: {},
};
}
const filteredContent = new Map();
let valid = true;
for (const [userId, roomIds] of Object.entries(content)) {
if (typeof userId !== "string") {
valid = false;
continue;
}
if (!Array.isArray(roomIds)) {
valid = false;
continue;
}
const filteredRoomIds: string[] = [];
filteredContent.set(userId, filteredRoomIds);
for (const roomId of roomIds) {
if (typeof roomId === "string") {
filteredRoomIds.push(roomId);
} else {
valid = false;
}
}
}
return {
valid,
filteredContent: Object.fromEntries(filteredContent.entries()),
};
};