element-hq / element-hq/element-web
sendToDevice in StopGapWidgetDriver doesn't handle encrypted events properly
- Dominant language
- TypeScript
- Stars
- 13.5k
- Forks
- 2.8k
- PR merge metrics
- PR metrics pending
Description
### Steps to reproduce
1. Create a widget that uses `sendToDevice`
2. Send a to device message without encryption:
```ts
widgetApi.sendToDevice( 'com.example.message',
false,
{ '@user-id':
{ '*':
{ hello: 'world' }
}
}
)
```
3. Send a device message with encryption
```ts
widgetApi.sendToDevice( 'com.example.message',
true,
{ '@user-id':
{ '*':
{ hello: 'world' }
}
}
)
```
### Outcome
#### What did you expect?
Both messages can be received by another device.
#### What happened instead?
Only the unencrypted event is received.
I already analyzed it a bit more:
The issue is that the code flow for encrypted and unencrypted devices is different. In [`StopGapWidgetDriver`](https://github.com/matrix-org/matrix-react-sdk/blob/develop/src/stores/widgets/StopGapWidgetDriver.ts#L257-L293), for unencrypted messages the content parameter of `sendToDevice` is forwarded as-is together with the event type, but the event type is not forwarded at all for encrypted messages. I could verify that on the receiver side, the message is received without any event `type`, neither is the content of the message correct, because the payload is not under the `content` property, but applied to the top level.
I think the correct way would be to construct a new payload object inside `StopGapWidgetDriver` that has a top level `type` and all content is placed in `content`. I could verify that doing it manually works:
```ts
widgetApi.sendToDevice( 'com.example.message',
true,
{ '@user-id':
{ '*':
{ type: 'com.example.message', content: {hello: 'world'} }
}
}
)
```
Fixing this issue though might be a breaking change for existing widgets that use this API. Especially for Element Call. Element Call itself [already takes care of this behavior](https://github.com/matrix-org/matrix-js-sdk/blob/develop/src/webrtc/call.ts#L2337-L2361) and would break if the issue is fixed.
### Operating system
macOS
### Browser information
Chrome
### URL for webapp
develop.element.io
### Application version
Element version: a26be2665fc1-react-2da98a6024ff-js-16672b3d0cad Olm version: 3.2.12
### Homeserver
matrix.org
### Will you send logs?
No
Contributor guide
Assessment
This issue has not been assessed yet.