This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 833
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate and filter m.direct before use
- Loading branch information
1 parent
e4dfb21
commit 8ab4f43
Showing
4 changed files
with
217 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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,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); | ||
roomIds.forEach((roomId: unknown) => { | ||
if (typeof roomId === "string") { | ||
filteredRoomIds.push(roomId); | ||
return; | ||
} | ||
|
||
valid = false; | ||
}); | ||
} | ||
|
||
return { | ||
valid, | ||
filteredContent: Object.fromEntries(filteredContent.entries()), | ||
}; | ||
}; |
This file contains 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 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,77 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
import { filterValidMDirect } from "../../../src/utils/dm/filterValidMDirect"; | ||
|
||
const roomId1 = "!room1:example.com"; | ||
const roomId2 = "!room2:example.com"; | ||
const userId1 = "@user1:example.com"; | ||
const userId2 = "@user2:example.com"; | ||
const userId3 = "@user3:example.com"; | ||
|
||
describe("filterValidMDirect", () => { | ||
it("should return an empty object as valid content", () => { | ||
expect(filterValidMDirect({})).toEqual({ | ||
valid: true, | ||
filteredContent: {}, | ||
}); | ||
}); | ||
|
||
it("should return valid content", () => { | ||
expect( | ||
filterValidMDirect({ | ||
[userId1]: [roomId1, roomId2], | ||
[userId2]: [roomId1], | ||
}), | ||
).toEqual({ | ||
valid: true, | ||
filteredContent: { | ||
[userId1]: [roomId1, roomId2], | ||
[userId2]: [roomId1], | ||
}, | ||
}); | ||
}); | ||
|
||
it("should return an empy object for null", () => { | ||
expect(filterValidMDirect(null)).toEqual({ | ||
valid: false, | ||
filteredContent: {}, | ||
}); | ||
}); | ||
|
||
it("should return an empy object for a non-object", () => { | ||
expect(filterValidMDirect(23)).toEqual({ | ||
valid: false, | ||
filteredContent: {}, | ||
}); | ||
}); | ||
|
||
it("should only return valid content", () => { | ||
const invalidContent = { | ||
[userId1]: [23], | ||
[userId2]: [roomId2], | ||
[userId3]: "room1", | ||
}; | ||
|
||
expect(filterValidMDirect(invalidContent)).toEqual({ | ||
valid: false, | ||
filteredContent: { | ||
[userId1]: [], | ||
[userId2]: [roomId2], | ||
}, | ||
}); | ||
}); | ||
}); |