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

feat(messaging): only create reaction hmac if doing encryption #4032

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
53 changes: 28 additions & 25 deletions packages/@webex/internal-plugin-conversation/src/conversation.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,33 +340,36 @@ const Conversation = WebexPlugin.extend({
* @param {String} recipientId,
* @returns {Promise<Activity>}
*/
addReaction(conversation, displayName, activity, recipientId) {
return this.createReactionHmac(displayName, activity).then((hmac) => {
const addReactionPayload = {
actor: {objectType: 'person', id: this.webex.internal.device.userId},
target: {
id: conversation.id,
objectType: 'conversation',
},
verb: 'add',
objectType: 'activity',
parent: {
type: 'reaction',
id: activity.id,
},
object: {
objectType: 'reaction2',
displayName,
hmac,
},
};
async addReaction(conversation, displayName, activity, recipientId) {
let hmac;
if (this.config.includeEncryptionTransforms) {
hmac = await this.createReactionHmac(displayName, activity);
}
Comment on lines +343 to +347
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for HMAC creation.

While the async/await conversion improves readability, consider adding try/catch for HMAC creation to handle potential errors gracefully.

 async addReaction(conversation, displayName, activity, recipientId) {
   let hmac;
   if (this.config.includeEncryptionTransforms) {
-    hmac = await this.createReactionHmac(displayName, activity);
+    try {
+      hmac = await this.createReactionHmac(displayName, activity);
+    } catch (error) {
+      this.logger.error('Failed to create reaction HMAC:', error);
+      throw new Error('Failed to create reaction HMAC: ' + error.message);
+    }
   }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async addReaction(conversation, displayName, activity, recipientId) {
let hmac;
if (this.config.includeEncryptionTransforms) {
hmac = await this.createReactionHmac(displayName, activity);
}
async addReaction(conversation, displayName, activity, recipientId) {
let hmac;
if (this.config.includeEncryptionTransforms) {
try {
hmac = await this.createReactionHmac(displayName, activity);
} catch (error) {
this.logger.error('Failed to create reaction HMAC:', error);
throw new Error('Failed to create reaction HMAC: ' + error.message);
}
}


if (recipientId) {
addReactionPayload.recipients = {items: [{id: recipientId, objectType: 'person'}]};
}
const addReactionPayload = {
actor: {objectType: 'person', id: this.webex.internal.device.userId},
target: {
id: conversation.id,
objectType: 'conversation',
},
verb: 'add',
objectType: 'activity',
parent: {
type: 'reaction',
id: activity.id,
},
object: {
objectType: 'reaction2',
displayName,
hmac,
},
};
Comment on lines +349 to +366
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add input validation for displayName parameter.

The displayName parameter should be validated to ensure it matches the allowed reaction types.

+  const VALID_REACTIONS = [
+    'celebrate',
+    'heart',
+    'thumbsup',
+    'smiley',
+    'haha',
+    'confused',
+    'sad'
+  ];
+
   const addReactionPayload = {
     actor: {objectType: 'person', id: this.webex.internal.device.userId},
     target: {
       id: conversation.id,
       objectType: 'conversation',
     },
     verb: 'add',
     objectType: 'activity',
     parent: {
       type: 'reaction',
       id: activity.id,
     },
     object: {
+      objectType: 'reaction2',
+      displayName: VALID_REACTIONS.includes(displayName) ? displayName :
+        (() => { throw new Error(`Invalid reaction type. Must be one of: ${VALID_REACTIONS.join(', ')}`); })(),
-      objectType: 'reaction2',
-      displayName,
       hmac,
     },
   };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const addReactionPayload = {
actor: {objectType: 'person', id: this.webex.internal.device.userId},
target: {
id: conversation.id,
objectType: 'conversation',
},
verb: 'add',
objectType: 'activity',
parent: {
type: 'reaction',
id: activity.id,
},
object: {
objectType: 'reaction2',
displayName,
hmac,
},
};
const VALID_REACTIONS = [
'celebrate',
'heart',
'thumbsup',
'smiley',
'haha',
'confused',
'sad'
];
const addReactionPayload = {
actor: {objectType: 'person', id: this.webex.internal.device.userId},
target: {
id: conversation.id,
objectType: 'conversation',
},
verb: 'add',
objectType: 'activity',
parent: {
type: 'reaction',
id: activity.id,
},
object: {
objectType: 'reaction2',
displayName: VALID_REACTIONS.includes(displayName) ? displayName :
(() => { throw new Error(`Invalid reaction type. Must be one of: ${VALID_REACTIONS.join(', ')}`); })(),
hmac,
},
};


return this.sendReaction(conversation, addReactionPayload);
});
if (recipientId) {
addReactionPayload.recipients = {items: [{id: recipientId, objectType: 'person'}]};
}

return this.sendReaction(conversation, addReactionPayload);
},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,49 +41,27 @@ describe('plugin-conversation', () => {
const {conversation} = webex.internal;
const recipientId = 'example-recipient-id';
const expected = {items: [{id: recipientId, objectType: 'person'}]};
conversation.config.includeEncryptionTransforms = true;
conversation.sendReaction = sinon.stub().returns(Promise.resolve());
conversation.createReactionHmac = sinon.stub().returns(Promise.resolve('hmac'));

return conversation.addReaction({}, 'example-display-name', {}, recipientId).then(() => {
assert.called(conversation.createReactionHmac);
assert.deepEqual(conversation.sendReaction.args[0][1].recipients, expected);
});
});
});

describe('deleteReaction()', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there was duplicated code here and the exact same test blocks were here twice. i deleted the duplicated blocks

it('should add recipients to the payload if provided', () => {
const {conversation} = webex.internal;
const recipientId = 'example-recipient-id';
const expected = {items: [{id: recipientId, objectType: 'person'}]};
conversation.sendReaction = sinon.stub().returns(Promise.resolve());

return conversation.deleteReaction({}, 'example-reaction-id', recipientId).then(() => {
assert.deepEqual(conversation.sendReaction.args[0][1].recipients, expected);
});
});
});

describe('prepare()', () => {
it('should ammend activity recipients to the returned object', () => {
const {conversation} = webex.internal;
const activity = {recipients: 'example-recipients'};

return conversation.prepare(activity).then((results) => {
assert.deepEqual(results.recipients, activity.recipients);
});
});
});

describe('addReaction()', () => {
it('should add recipients to the payload if provided', () => {
it('will not call createReactionHmac if config prohibits', () => {
const {conversation} = webex.internal;
const recipientId = 'example-recipient-id';
const expected = {items: [{id: recipientId, objectType: 'person'}]};
conversation.config.includeEncryptionTransforms = false;
conversation.sendReaction = sinon.stub().returns(Promise.resolve());
conversation.createReactionHmac = sinon.stub().returns(Promise.resolve('hmac'));
conversation.createReactionHmac = sinon.stub();

return conversation.addReaction({}, 'example-display-name', {}, recipientId).then(() => {
assert.deepEqual(conversation.sendReaction.args[0][1].recipients, expected);
assert.notCalled(conversation.createReactionHmac);
});
});
});
Expand Down
Loading