Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private readonly sendMessage = (
context: any,
conversationId: ConversationId,
userId: UserId,
clientId: DeviceId,
destinationUserId: UserId,
destinationClientId: DeviceId,
payload: string,
): number => {
const protoCalling = new Calling({content: payload});
const genericMessage = new GenericMessage({
[GENERIC_MESSAGE_TYPE.CALLING]: protoCalling,
messageId: createRandomUuid(),
});
const call = this.findCall(conversationId);
if (call?.blockMessages) {
return 0;
}
const options = this.targetMessageRecipients(payload, destinationUserId, destinationClientId);
const eventInfoEntity = new EventInfoEntity(genericMessage, conversationId, options);
this.conversationRepository.sendCallingMessage(eventInfoEntity, conversationId).catch(() => {
if (call) {
// we flag the call in order to prevent sending further messages
call.blockMessages = true;
}
this.leaveCall(conversationId);
async function createEncodedCiphertext(
preKey,
text = 'Hello, World!',
receivingIdentity = TestFactory.cryptography_repository.cryptobox.identity,
) {
const bobEngine = new MemoryEngine();
await bobEngine.init('bob');
const sender = new Cryptobox(bobEngine, 1);
await sender.create();
const genericMessage = new GenericMessage({
[GENERIC_MESSAGE_TYPE.TEXT]: new Text({content: text}),
messageId: createRandomUuid(),
});
const sessionId = `from-${sender.identity.public_key.fingerprint()}-to-${preKey.key_pair.public_key.fingerprint()}`;
const preKeyBundle = Proteus.keys.PreKeyBundle.new(receivingIdentity.public_key, preKey);
const cipherText = await sender.encrypt(
sessionId,
GenericMessage.encode(genericMessage).finish(),
preKeyBundle.serialise(),
);
return arrayToBase64(cipherText);
}
it('can map a ping wrapped inside an external message', async () => {
let external_message = undefined;
const genericMessage = new GenericMessage({
knock: new Knock({hotKnock: false}),
messageId: createRandomUuid(),
});
const encryptedAsset = await encryptAesAsset(GenericMessage.encode(genericMessage).finish());
const keyBytes = new Uint8Array(encryptedAsset.keyBytes);
const sha256 = new Uint8Array(encryptedAsset.sha256);
event.data.data = await arrayToBase64(encryptedAsset.cipherText);
external_message = new GenericMessage({
external: new External({otrKey: keyBytes, sha256}),
messageId: createRandomUuid(),
});
const event_json = await mapper.mapGenericMessage(external_message, event);
expect(isObject(event_json)).toBeTruthy();
expect(event_json.type).toBe(ClientEvent.CONVERSATION.KNOCK);
expect(event_json.conversation).toBe(event.conversation);
expect(event_json.from).toBe(event.from);
expect(event_json.time).toBe(event.time);
expect(event_json.id).toBe(genericMessage.messageId);
});
const preKeys = await TestFactory.cryptography_repository.createCryptobox(database);
const alice = TestFactory.cryptography_repository.cryptobox.identity;
expect(alice).toBeDefined();
const aliceBundle = Proteus.keys.PreKeyBundle.new(alice.public_key, preKeys[0]);
const bobEngine = new MemoryEngine();
await bobEngine.init('bob');
const bob = new Cryptobox(bobEngine, 1);
await bob.create();
const plainText = 'Hello, Alice!';
const genericMessage = new GenericMessage({
[GENERIC_MESSAGE_TYPE.TEXT]: new Text({content: plainText}),
messageId: createRandomUuid(),
});
const cipherText = await bob.encrypt(
'session-with-alice',
GenericMessage.encode(genericMessage).finish(),
aliceBundle.serialise(),
);
const encodedCipherText = await arrayToBase64(cipherText);
const mockedEvent = {
data: {
text: encodedCipherText,
},
from: createRandomUuid(),
it('rejects with an error for a preview image message', done => {
const generic_message = new GenericMessage({
[GENERIC_MESSAGE_TYPE.IMAGE]: new ImageAsset({tag: 'preview'}),
messageId: createRandomUuid(),
});
mapper
.mapGenericMessage(generic_message, event)
.then(done.fail)
.catch(error => {
expect(error instanceof z.error.CryptographyError).toBeTruthy();
expect(error.type).toBe(z.error.CryptographyError.TYPE.IGNORED_PREVIEW);
done();
});
});
it('resolves with a mapped location message', () => {
const location = new Location({
latitude: 13.409779,
longitude: 52.520645,
name: 'Berlin',
zoom: 1,
});
const generic_message = new GenericMessage({
[GENERIC_MESSAGE_TYPE.LOCATION]: location,
messageId: createRandomUuid(),
});
return mapper.mapGenericMessage(generic_message, event).then(event_json => {
expect(isObject(event_json)).toBeTruthy();
expect(event_json.type).toBe(ClientEvent.CONVERSATION.LOCATION);
expect(event_json.conversation).toBe(event.conversation);
expect(event_json.from).toBe(event.from);
expect(event_json.time).toBe(event.time);
expect(event_json.id).toBe(generic_message.messageId);
expect(event_json.data.location.longitude).toBe(generic_message.location.longitude);
expect(event_json.data.location.latitude).toBe(generic_message.location.latitude);
expect(event_json.data.location.name).toBe(generic_message.location.name);
expect(event_json.data.location.zoom).toBe(generic_message.location.zoom);
});
it('can map a text wrapped inside an external message', async () => {
const plaintext = 'Test';
const text = new Text({content: plaintext});
const generic_message = new GenericMessage({
[GENERIC_MESSAGE_TYPE.TEXT]: text,
messageId: createRandomUuid(),
});
const encryptedAsset = await encryptAesAsset(GenericMessage.encode(generic_message).finish());
const keyBytes = new Uint8Array(encryptedAsset.keyBytes);
const sha256 = new Uint8Array(encryptedAsset.sha256);
event.data.data = await arrayToBase64(encryptedAsset.cipherText);
const external_message = new GenericMessage({
external: new External({otrKey: keyBytes, sha256}),
messageId: createRandomUuid(),
});
const event_json = await mapper.mapGenericMessage(external_message, event);
expect(event_json.data.content).toBe(plaintext);
client_ids.forEach(client_id => {
prekey_map[user_id][client_id] = {
id: 65535,
key:
'pQABARn//wKhAFgg3OpuTCUwDZMt1fklZB4M+fjDx/3fyx78gJ6j3H3dM2YDoQChAFggQU1orulueQHLv5YDYqEYl3D4O0zA9d+TaGGXXaBJmK0E9g==',
};
});
}
}
resolve(prekey_map);
}),
);
const generic_message = new GenericMessage({
[GENERIC_MESSAGE_TYPE.TEXT]: new Text({content: 'Unit test'}),
messageId: createRandomUuid(),
});
const recipients = {
[john_doe.id]: [john_doe.clients.phone_id, john_doe.clients.desktop_id],
[jane_roe.id]: [jane_roe.clients.phone_id],
};
return TestFactory.cryptography_repository.encryptGenericMessage(recipients, generic_message).then(payload => {
expect(payload.recipients).toBeTruthy();
expect(Object.keys(payload.recipients).length).toBe(2);
expect(Object.keys(payload.recipients[john_doe.id]).length).toBe(2);
expect(Object.keys(payload.recipients[jane_roe.id]).length).toBe(1);
expect(payload.recipients[jane_roe.id][jane_roe.clients.phone_id]).toEqual(jasmine.any(String));
});
.then(() => {
const text = new Text({
content:
'massive external message massive external message massive external message massive external message massive external message massive external message massive external message massive external message massive external messagemassive external message massive external message massive external message massive external message massive external message massive external message massive external message massive external message massive external messagemassive external message massive external message massive external message massive external message massive external message massive external message massive external message massive external message massive external messagemassive external message massive external message massive external message massive external message massive external message massive external message massive external message massive external message massive external message',
});
const genericMessage = new GenericMessage({
[GENERIC_MESSAGE_TYPE.TEXT]: text,
messageId: createRandomUuid(),
});
const eventInfoEntity = new EventInfoEntity(genericMessage, largeConversationEntity.id);
return TestFactory.conversation_repository._shouldSendAsExternal(eventInfoEntity);
})
.then(shouldSendAsExternal => {