Skip to content
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

Catch exceptions from the widget driver in the event relations endpoint #73

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
79 changes: 43 additions & 36 deletions src/ClientWidgetApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IWidgetApiErrorResponseData>(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<IWidgetApiErrorResponseData>(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);
robintown marked this conversation as resolved.
Show resolved Hide resolved

// 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<IWidgetApiErrorResponseData>(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<IWidgetApiErrorResponseData>(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<IReadRelationsFromWidgetResponseData>(
request,
{
original_event: result.originalEvent,
chunk,
prev_batch: result.prevBatch,
next_batch: result.nextBatch,
},
);
return this.transport.reply<IReadRelationsFromWidgetResponseData>(
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<IWidgetApiErrorResponseData>(request, {
error: { message: "Unexpected error while reading relations" },
});
}
}

private handleMessage(ev: CustomEvent<IWidgetApiRequest>) {
Expand Down
25 changes: 24 additions & 1 deletion test/ClientWidgetApi-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ describe('ClientWidgetApi', () => {
);
});


it('should reject requests without event_id', async () => {
const event: IWidgetApiRequest = {
api: WidgetApiDirection.FromWidget,
Expand Down Expand Up @@ -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' },
});
});
});
});
});