Skip to content

Use appservice API to check if user can access the event they're repl… #1833

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

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion spec/e2e/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
reporters: [['github-actions', {silent: false}], 'summary'],
// reporters: [['github-actions', {silent: false}], 'summary'],
transformIgnorePatterns: ['<rootDir>/node_modules/'],
testTimeout: 60000,
transform: {
Expand Down
15 changes: 0 additions & 15 deletions spec/e2e/replies.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,6 @@ describe('Reply handling', () => {
event_id: await aliceEventId,
}, "Oh sorry, I meant bob!");
expect((await aliceReplyMsgPromise)[2]).toContain(aliceMsgBody);

// restart the bridge, effectively marking members as "been here forever"
await testEnv.recreateBridge();
await testEnv.setUp();
const postRestartAliceMsg = bob.waitForEvent('message', 10000);
const postRestartAliceMsgBody = "Hello post-restart world!";
const postRestartAliceEventId = alice.sendText(cRoomId, postRestartAliceMsgBody);
await postRestartAliceMsg;

const postRestartCharlieMsg = bob.waitForEvent('message', 10000);
await charlie.replyText(cRoomId, {
event_id: await postRestartAliceEventId,
}, "Hello alice!");
const postRestartCharlieMsgBody = (await postRestartCharlieMsg)[2];
expect(postRestartCharlieMsgBody).toContain(postRestartAliceMsgBody);
});

it('should not leak the contents of messages to leavers', async () => {
Expand Down
33 changes: 12 additions & 21 deletions src/bridge/MatrixHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { trackChannelAndCreateRoom } from "./RoomCreation";
import { renderTemplate } from "../util/Template";
import { trimString } from "../util/TrimString";
import { messageDiff } from "../util/MessageDiff";
import QuickLRU = require("quick-lru");

async function reqHandler(req: BridgeRequest, promise: PromiseLike<unknown>|void) {
try {
Expand Down Expand Up @@ -146,11 +145,6 @@ export class MatrixHandler {
private adminHandler: AdminRoomHandler;
private config: MatrixHandlerConfig = DEFAULTS;

private memberJoinDefaultTs = Date.now();
private memberJoinTs = new QuickLRU<string, number>({
maxSize: 8192,
});

constructor(
private readonly ircBridge: IrcBridge,
config: MatrixHandlerConfig|undefined,
Expand Down Expand Up @@ -413,12 +407,6 @@ export class MatrixHandler {
* @param {Object} event : The Matrix member event.
*/
private _onMemberEvent(req: BridgeRequest, event: OnMemberEventData) {
if (event.content.membership === 'join') {
this.memberJoinTs.set(`${event.room_id}/${event.state_key}`, Date.now());
}
else {
this.memberJoinTs.delete(`${event.room_id}/${event.state_key}`);
}
this.memberTracker?.onEvent(event);
}

Expand Down Expand Up @@ -1350,15 +1338,18 @@ export class MatrixHandler {
rplSource = cachedEvent.body;
}

const senderJoinTs = this.memberJoinTs.get(`${event.room_id}/${event.sender}`) ?? this.memberJoinDefaultTs;
if (senderJoinTs > cachedEvent.timestamp) {
// User joined AFTER the event was sent (or left and joined, but we can't distinguish that).
// Do not treat as a reply.
req.log.warn(`User ${event.sender} attempted to reply to an event before they were joined`);
return {
formatted: rplText,
reply: rplText,
};
try {
await bridgeIntent.matrixClient.doRequest('GET', `/_matrix/client/v3/rooms/${event.room_id}/event/${replyEventId}?user_id=${event.sender}`);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
if (err.body?.errcode === 'M_NOT_FOUND') {
req.log.warn(`User ${event.sender} attempted to reply to an event they cannot access`);
return {
formatted: rplText,
reply: rplText,
};
}
throw err;
}

// Get the first non-blank line from the source.
Expand Down
Loading