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

fix: microsoft clarity identify error handling #1948

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,17 @@
if (context?.traits?.customPageId) {
customPageId = context.traits.customPageId;
}
window.clarity('identify', userId, sessionId, customPageId);
if (context?.traits) {
const { traits } = context;
const keys = Object.keys(traits);
keys.forEach(key => {
window.clarity('set', key, traits[key]);
});
}
window.clarity('identify', userId, sessionId, customPageId).then(() => {

Check warning on line 58 in packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js

View check run for this annotation

Codecov / codecov/patch

packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js#L58

Added line #L58 was not covered by tests
if (context?.traits) {
const { traits } = context;
const keys = Object.keys(traits);
keys.forEach(key => {
window.clarity('set', key, traits[key]);

Check warning on line 63 in packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js

View check run for this annotation

Codecov / codecov/patch

packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js#L60-L63

Added lines #L60 - L63 were not covered by tests
});
}
Comment on lines +59 to +65
Copy link
Contributor

@coderabbitai coderabbitai bot Nov 25, 2024

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add validation for trait values before sending to Clarity

The code directly passes trait values to the third-party SDK without any validation or sanitization, which could potentially expose sensitive data.

Consider:

  1. Adding a whitelist of allowed trait keys
  2. Implementing value sanitization
  3. Filtering out sensitive data patterns (e.g., email, phone numbers)
 if (context?.traits) {
   const { traits } = context;
   const keys = Object.keys(traits);
+  const allowedTraits = ['name', 'company', 'role']; // define allowed traits
   keys.forEach(key => {
+    if (!allowedTraits.includes(key)) {
+      logger.debug(`Skipping non-whitelisted trait: ${key}`);
+      return;
+    }
+    const sanitizedValue = sanitizeTraitValue(traits[key]); // implement sanitization
     window.clarity('set', key, traits[key]);
   });
 }

Committable suggestion skipped: line range outside the PR's diff.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't see any requirement of allwoedTraits or sanitization of a value in Microsoft Clarity at this point in time.

Copy link
Contributor

Choose a reason for hiding this comment

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

@sanpj2292, understood. Thank you for the clarification.

(_/)
( •_• )


✏️ Learnings added
Learnt from: sanpj2292
PR: rudderlabs/rudder-sdk-js#1948
File: packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js:59-65
Timestamp: 2024-11-25T11:33:39.579Z
Learning: In the Microsoft Clarity integration (`packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js`), adding validation or sanitization of trait values before sending them to Microsoft Clarity's SDK is not required at this time.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

}).catch(error => {
logger.error('Error in identify call', error);

Check warning on line 67 in packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js

View check run for this annotation

Codecov / codecov/patch

packages/analytics-js-integrations/src/integrations/MicrosoftClarity/browser.js#L67

Added line #L67 was not covered by tests
});
sanpj2292 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down