From e9f544fe2fccdf055e3cbc84ab32a54e7bca2e14 Mon Sep 17 00:00:00 2001 From: Dominik Henneke Date: Tue, 6 Sep 2022 19:23:14 +0200 Subject: [PATCH 1/2] Catch exceptions from the widget driver in the event relations endpoint Signed-off-by: Dominik Henneke --- src/ClientWidgetApi.ts | 79 ++++++++++++++++++++---------------- test/ClientWidgetApi-test.ts | 25 +++++++++++- 2 files changed, 67 insertions(+), 37 deletions(-) diff --git a/src/ClientWidgetApi.ts b/src/ClientWidgetApi.ts index e9a276c..780f9d3 100644 --- a/src/ClientWidgetApi.ts +++ b/src/ClientWidgetApi.ts @@ -587,46 +587,53 @@ export class ClientWidgetApi extends EventEmitter { }); } - const result = await this.driver.readEventRelations( - request.data.event_id, request.data.room_id, request.data.rel_type, - request.data.event_type, request.data.from, request.data.to, - request.data.limit, request.data.direction); - - // check if the user is permitted to receive the event in question - if (result.originalEvent) { - if (result.originalEvent.state_key !== undefined) { - if (!this.canReceiveStateEvent(result.originalEvent.type, result.originalEvent.state_key)) { - return this.transport.reply(request, { - error: { message: "Cannot read state events of this type" }, - }); - } - } else { - if (!this.canReceiveRoomEvent(result.originalEvent.type, result.originalEvent.content['msgtype'])) { - return this.transport.reply(request, { - error: { message: "Cannot read room events of this type" }, - }); + try { + const result = await this.driver.readEventRelations( + request.data.event_id, request.data.room_id, request.data.rel_type, + request.data.event_type, request.data.from, request.data.to, + request.data.limit, request.data.direction); + + // check if the user is permitted to receive the event in question + if (result.originalEvent) { + if (result.originalEvent.state_key !== undefined) { + if (!this.canReceiveStateEvent(result.originalEvent.type, result.originalEvent.state_key)) { + return this.transport.reply(request, { + error: { message: "Cannot read state events of this type" }, + }); + } + } else { + if (!this.canReceiveRoomEvent(result.originalEvent.type, result.originalEvent.content['msgtype'])) { + return this.transport.reply(request, { + error: { message: "Cannot read room events of this type" }, + }); + } } } - } - // only return events that the user has the permission to receive - const chunk = result.chunk.filter(e => { - if (e.state_key !== undefined) { - return this.canReceiveStateEvent(e.type, e.state_key); - } else { - return this.canReceiveRoomEvent(e.type, e.content['msgtype']); - } - }); + // only return events that the user has the permission to receive + const chunk = result.chunk.filter(e => { + if (e.state_key !== undefined) { + return this.canReceiveStateEvent(e.type, e.state_key); + } else { + return this.canReceiveRoomEvent(e.type, e.content['msgtype']); + } + }); - return this.transport.reply( - request, - { - original_event: result.originalEvent, - chunk, - prev_batch: result.prevBatch, - next_batch: result.nextBatch, - }, - ); + return this.transport.reply( + request, + { + original_event: result.originalEvent, + chunk, + prev_batch: result.prevBatch, + next_batch: result.nextBatch, + }, + ); + } catch (e) { + console.error("error getting the relations", e); + await this.transport.reply(request, { + error: { message: "Unexpected error while reading relations" }, + }); + } } private handleMessage(ev: CustomEvent) { diff --git a/test/ClientWidgetApi-test.ts b/test/ClientWidgetApi-test.ts index 376c278..5b55d1a 100644 --- a/test/ClientWidgetApi-test.ts +++ b/test/ClientWidgetApi-test.ts @@ -245,7 +245,6 @@ describe('ClientWidgetApi', () => { ); }); - it('should reject requests without event_id', async () => { const event: IWidgetApiRequest = { api: WidgetApiDirection.FromWidget, @@ -345,5 +344,29 @@ describe('ClientWidgetApi', () => { }); }); }); + + it('should reject requests when the driver throws an exception', async () => { + driver.readEventRelations.mockRejectedValue( + new Error("M_FORBIDDEN: You don't have permission to access that event"), + ); + + const event: IReadRelationsFromWidgetActionRequest = { + api: WidgetApiDirection.FromWidget, + widgetId: 'test', + requestId: '0', + action: WidgetApiFromWidgetAction.MSC3869ReadRelations, + data: { event_id: '$event' }, + }; + + await loadIframe(); + + emitEvent(new CustomEvent('', { detail: event })); + + await waitFor(() => { + expect(transport.reply).toBeCalledWith(event, { + error: { message: 'Unexpected error while reading relations' }, + }); + }); + }); }); }); From bfedf9d58f691e7b4afbf4e76e97afeb9c421d5a Mon Sep 17 00:00:00 2001 From: Dominik Henneke Date: Wed, 7 Sep 2022 17:27:51 +0200 Subject: [PATCH 2/2] Fix formatting Signed-off-by: Dominik Henneke --- src/ClientWidgetApi.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ClientWidgetApi.ts b/src/ClientWidgetApi.ts index 780f9d3..a41d5d8 100644 --- a/src/ClientWidgetApi.ts +++ b/src/ClientWidgetApi.ts @@ -591,7 +591,8 @@ export class ClientWidgetApi extends EventEmitter { const result = await this.driver.readEventRelations( request.data.event_id, request.data.room_id, request.data.rel_type, request.data.event_type, request.data.from, request.data.to, - request.data.limit, request.data.direction); + request.data.limit, request.data.direction, + ); // check if the user is permitted to receive the event in question if (result.originalEvent) {