Validate conversationId
Validate conversationId in WebRTC and Call message listeners.
Add conversationId in message interfaces.
GitLab: #181
Change-Id: If92a4c484b65d3f06a6c42a1c1a4463425546a6b
diff --git a/client/src/contexts/CallProvider.tsx b/client/src/contexts/CallProvider.tsx
index 93ec5b5..e602508 100644
--- a/client/src/contexts/CallProvider.tsx
+++ b/client/src/contexts/CallProvider.tsx
@@ -228,6 +228,11 @@
if (callRole === 'caller' && callStatus === CallStatus.Ringing) {
const callAcceptListener = (data: CallAction) => {
console.info('Received event on CallAccept', data);
+ if (data.conversationId !== conversationId) {
+ console.warn('Wrong incoming conversationId, ignoring action');
+ return;
+ }
+
setCallStatus(CallStatus.Connecting);
webRtcConnection
@@ -246,7 +251,7 @@
webSocket.unbind(WebSocketMessageType.CallAccept, callAcceptListener);
};
}
- }, [callRole, webSocket, webRtcConnection, sendWebRtcOffer, callStatus]);
+ }, [callRole, webSocket, webRtcConnection, sendWebRtcOffer, callStatus, conversationId]);
const quitCall = useCallback(() => {
if (!webRtcConnection) {
@@ -271,6 +276,11 @@
const callEndListener = (data: CallAction) => {
console.info('Received event on CallEnd', data);
+ if (data.conversationId !== conversationId) {
+ console.warn('Wrong incoming conversationId, ignoring action');
+ return;
+ }
+
quitCall();
// TODO: write in chat that the call ended
};
diff --git a/client/src/contexts/WebRtcProvider.tsx b/client/src/contexts/WebRtcProvider.tsx
index f1c9add..13a30a2 100644
--- a/client/src/contexts/WebRtcProvider.tsx
+++ b/client/src/contexts/WebRtcProvider.tsx
@@ -45,7 +45,7 @@
export default ({ children }: WithChildren) => {
const { account } = useAuthContext();
const webSocket = useContext(WebSocketContext);
- const { conversation } = useContext(ConversationContext);
+ const { conversation, conversationId } = useContext(ConversationContext);
const [webRtcConnection, setWebRtcConnection] = useState<RTCPeerConnection | undefined>();
const [remoteStreams, setRemoteStreams] = useState<readonly MediaStream[]>();
const [isConnected, setIsConnected] = useState(false);
@@ -83,6 +83,7 @@
const webRtcOffer: WebRtcSdp = {
contactId: contactUri,
+ conversationId: conversationId,
sdp,
};
@@ -90,7 +91,7 @@
console.info('Sending WebRtcOffer', webRtcOffer);
webSocket.send(WebSocketMessageType.WebRtcOffer, webRtcOffer);
},
- [webRtcConnection, webSocket, contactUri]
+ [webRtcConnection, webSocket, conversationId, contactUri]
);
const sendWebRtcAnswer = useCallback(
@@ -101,13 +102,14 @@
const webRtcAnswer: WebRtcSdp = {
contactId: contactUri,
+ conversationId: conversationId,
sdp,
};
console.info('Sending WebRtcAnswer', webRtcAnswer);
webSocket.send(WebSocketMessageType.WebRtcAnswer, webRtcAnswer);
},
- [contactUri, webRtcConnection, webSocket]
+ [contactUri, conversationId, webRtcConnection, webSocket]
);
useEffect(() => {
@@ -117,6 +119,11 @@
const webRtcOfferListener = async (data: WebRtcSdp) => {
console.info('Received event on WebRtcOffer', data);
+ if (data.conversationId !== conversationId) {
+ console.warn('Wrong incoming conversationId, ignoring action');
+ return;
+ }
+
await webRtcConnection.setRemoteDescription(new RTCSessionDescription(data.sdp));
const sdp = await webRtcConnection.createAnswer({
@@ -129,11 +136,21 @@
const webRtcAnswerListener = async (data: WebRtcSdp) => {
console.info('Received event on WebRtcAnswer', data);
+ if (data.conversationId !== conversationId) {
+ console.warn('Wrong incoming conversationId, ignoring action');
+ return;
+ }
+
await webRtcConnection.setRemoteDescription(new RTCSessionDescription(data.sdp));
};
const webRtcIceCandidateListener = async (data: WebRtcIceCandidate) => {
console.info('Received event on WebRtcIceCandidate', data);
+ if (data.conversationId !== conversationId) {
+ console.warn('Wrong incoming conversationId, ignoring action');
+ return;
+ }
+
await webRtcConnection.addIceCandidate(data.candidate);
};
@@ -146,7 +163,7 @@
webSocket.unbind(WebSocketMessageType.WebRtcAnswer, webRtcAnswerListener);
webSocket.unbind(WebSocketMessageType.WebRtcIceCandidate, webRtcIceCandidateListener);
};
- }, [webSocket, webRtcConnection, sendWebRtcAnswer]);
+ }, [webSocket, webRtcConnection, sendWebRtcAnswer, conversationId]);
useEffect(() => {
if (!webRtcConnection || !webSocket) {
@@ -162,6 +179,7 @@
if (event.candidate) {
const webRtcIceCandidate: WebRtcIceCandidate = {
contactId: contactUri,
+ conversationId: conversationId,
candidate: event.candidate,
};
@@ -190,7 +208,7 @@
webRtcConnection.removeEventListener('track', trackEventListener);
webRtcConnection.removeEventListener('iceconnectionstatechange', iceConnectionStateChangeEventListener);
};
- }, [webRtcConnection, webSocket, contactUri]);
+ }, [webRtcConnection, webSocket, contactUri, conversationId]);
return (
<WebRtcContext.Provider