Skip to content

Commit 43af42d

Browse files
committed
fixing decryption and tests
1 parent b987a64 commit 43af42d

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/rudder-compat.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// import CryptoJS from 'crypto-js'; // For v1 decryption
22
import { AES } from 'crypto-js';
3+
import CryptoJS from 'crypto-js'; // Needed for tests
34
import { getCookie } from './cookie.js'; // Reuse TinyTag's getCookie
45

56
// RudderStack v1 decryption key (placeholder, needs actual key)
@@ -26,7 +27,7 @@ function decryptV1Cookie(value) {
2627
const decrypted = bytes.toString(CryptoJS.enc.Utf8);
2728
return decrypted || value; // Return original if decryption fails or is empty
2829
} catch (e) {
29-
return 'whoa'; // Return original on exception
30+
return value; // Return original on exception
3031
}
3132
}
3233
return value; // Not v3 or encrypted, return as-is

tests/rudder-compat.test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11

2-
/**
3-
* @jest-environment jsdom
4-
*/
52
import '../src/index.js'; // Load tt.js first to set up window.tt
63
import '../src/rudder-compat.js'; // Then load rudder-compat.js to enhance it
74
import { setCookie, getCookie } from '../src/cookie.js';
@@ -57,4 +54,12 @@ describe('RudderStack Compatibility', () => {
5754
setCookie('rl_trait', v3Value);
5855
expect(window.tt.getTraits()).toEqual(traits);
5956
});
57+
58+
test('decryptV1Cookie decrypts v1 RudderStack cookie', () => {
59+
const plainValue = 'anon123asdasdasdlskdjasdlkajdalskdjasldsdl';
60+
const encrypted = CryptoJS.AES.encrypt(plainValue, 'Rudder').toString();
61+
const decrypted = decryptV1Cookie(encrypted);
62+
console.log('Encrypted:', encrypted, 'Decrypted:', decrypted);
63+
expect(decrypted).toBe(plainValue);
64+
});
6065
});

0 commit comments

Comments
 (0)