Skip to content

Commit

Permalink
[lib] Use getMessageForException instead of e.message in DM related code
Browse files Browse the repository at this point in the history
Summary:
Use getMessageForException instead of e.message in DM related code in order to avoid bugs when throwing primitives.
Depends on D13757

Test Plan: 1. Run flow checks, run the app, verify that nothing crashes.

Reviewers: kamil, tomek, ashoat

Reviewed By: ashoat

Subscribers: ashoat, tomek

Differential Revision: https://phab.comm.dev/D13761
  • Loading branch information
graszka22 committed Oct 24, 2024
1 parent 4723c3b commit 4af5518
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
10 changes: 7 additions & 3 deletions lib/tunnelbroker/peer-to-peer-message-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
type PeerToPeerMessage,
} from '../types/tunnelbroker/peer-to-peer-message-types.js';
import { getConfig } from '../utils/config.js';
import { getMessageForException } from '../utils/errors.js';

type Props = {
+socketSend: (message: string) => void,
Expand Down Expand Up @@ -78,7 +79,7 @@ function PeerToPeerMessageHandler(props: Props): React.Node {
try {
await peerToPeerMessageHandler(peerToPeerMessage, messageID);
} catch (e) {
console.log(e.message);
console.log(getMessageForException(e));
} finally {
if (
localSocketSessionCounter === getSessionCounter() &&
Expand Down Expand Up @@ -119,7 +120,7 @@ function PeerToPeerMessageHandler(props: Props): React.Node {
} catch (e) {
console.log(
'error while parsing Tunnelbroker peer-to-peer message:',
e.message,
getMessageForException(e),
);
// Client received incorrect message, confirm to remove from
// Tunnelbroker queue.
Expand Down Expand Up @@ -167,7 +168,10 @@ function PeerToPeerMessageHandler(props: Props): React.Node {
})),
);
} catch (e) {
console.log('error while reading persisted inbound messages:', e.message);
console.log(
'error while reading persisted inbound messages:',
getMessageForException(e),
);
}
}, [enqueue]);

Expand Down
22 changes: 13 additions & 9 deletions lib/tunnelbroker/use-peer-to-peer-message-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ function usePeerToPeerMessageHandler(): (
await identityClient.getInboundKeysForUser(senderUserID);
deviceKeys = keys[senderDeviceID];
} catch (e) {
console.log(e.message);
console.log(getMessageForException(e));
}

if (!deviceKeys) {
Expand All @@ -242,12 +242,13 @@ function usePeerToPeerMessageHandler(): (
`session version: ${sessionVersion}`,
);
} catch (e) {
if (e.message?.includes(olmSessionErrors.alreadyCreated)) {
const errorMessage = getMessageForException(e) ?? '';
if (errorMessage.includes(olmSessionErrors.alreadyCreated)) {
console.log(
'Received session request with lower session version from ' +
`${senderDeviceID}, session version: ${sessionVersion}`,
);
} else if (e.message?.includes(olmSessionErrors.raceCondition)) {
} else if (errorMessage.includes(olmSessionErrors.raceCondition)) {
const currentDeviceID = await getContentSigningKey();
if (hasHigherDeviceID(currentDeviceID, senderDeviceID)) {
console.log(
Expand All @@ -272,7 +273,7 @@ function usePeerToPeerMessageHandler(): (
} else {
console.log(
'Error creating inbound session with device ' +
`${senderDeviceID}: ${e.message}, ` +
`${senderDeviceID}: ${errorMessage}, ` +
`session version: ${sessionVersion}`,
);
}
Expand Down Expand Up @@ -301,7 +302,8 @@ function usePeerToPeerMessageHandler(): (
console.log('Failed processing Olm P2P message:', e);
}
} catch (e) {
if (e.message?.includes(olmSessionErrors.invalidSessionVersion)) {
const errorMessage = getMessageForException(e) ?? '';
if (errorMessage.includes(olmSessionErrors.invalidSessionVersion)) {
console.log(
'Received message decrypted with different session from ' +
`${message.senderInfo.deviceID}.`,
Expand All @@ -310,12 +312,12 @@ function usePeerToPeerMessageHandler(): (
}
console.log(
'Error decrypting message from device ' +
`${message.senderInfo.deviceID}: ${e.message}`,
`${message.senderInfo.deviceID}: ${errorMessage}`,
);

if (
!e.message?.includes(OLM_ERROR_FLAG) &&
!e.message?.includes(olmSessionErrors.sessionDoesNotExist)
!errorMessage.includes(OLM_ERROR_FLAG) &&
!errorMessage.includes(olmSessionErrors.sessionDoesNotExist)
) {
throw e;
}
Expand All @@ -334,7 +336,9 @@ function usePeerToPeerMessageHandler(): (
const oneTimeKeys = await olmAPI.getOneTimeKeys(message.numberOfKeys);
await identityClient.uploadOneTimeKeys(oneTimeKeys);
} catch (e) {
console.log(`Error uploading one-time keys: ${e.message}`);
console.log(
`Error uploading one-time keys: ${getMessageForException(e) ?? ''}`,
);
}
} else if (message.type === peerToPeerMessageTypes.DEVICE_LIST_UPDATED) {
try {
Expand Down
9 changes: 7 additions & 2 deletions lib/utils/peer-to-peer-communication-utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow

import { getConfig } from './config.js';
import { getMessageForException } from './errors.js';
import { olmSessionErrors } from './olm-utils.js';
import { type AuthMetadata } from '../shared/identity-client-context.js';
import { type P2PMessageRecipient } from '../tunnelbroker/peer-to-peer-context.js';
Expand Down Expand Up @@ -88,7 +89,9 @@ async function encryptAndSendMessageToPeer(
};
return await sendMessageToPeer(encryptedMessage, authMetadata, sendMessage);
} catch (e) {
if (e.message?.includes(olmSessionErrors.sessionDoesNotExist)) {
if (
getMessageForException(e)?.includes(olmSessionErrors.sessionDoesNotExist)
) {
return 'missing_session';
}
console.log(`Error sending messages to peer ${message.deviceID}`, e);
Expand Down Expand Up @@ -178,7 +181,9 @@ async function ephemeralEncryptAndSendMessageToPeer(

return { status: 'success', recipient };
} catch (e) {
if (e.message?.includes(olmSessionErrors.sessionDoesNotExist)) {
if (
getMessageForException(e)?.includes(olmSessionErrors.sessionDoesNotExist)
) {
return { status: 'missing_session', recipient };
}
console.log(`Error sending messages to peer ${recipient.deviceID}`, e);
Expand Down

0 comments on commit 4af5518

Please sign in to comment.