Skip to content

Commit 2813f6b

Browse files
committed
fix(user-activity-broadcaster): determine the correct origin to postMessage
1 parent a275415 commit 2813f6b

2 files changed

Lines changed: 46 additions & 39 deletions

File tree

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,52 @@
1-
const MINUTE = 60 * 1000
2-
const interval = MINUTE * 5
3-
export const eventName = 'user_activity'
1+
const MINUTE = 60 * 1000;
2+
const interval = MINUTE * 5;
3+
export const eventName = 'user_activity';
44

55
// Must be a dynamic object to test this
6-
export const lastActivity = {}
6+
export const lastActivity = {};
77

8-
export const getTargetOrigin = (origin = window.location.origin) => {
9-
// Setup targetOrigin to alternate origin (because the same origin already works)
10-
if (origin) {
11-
if (origin.includes('apps')) {
12-
return origin.replace('apps', 'essentials')
13-
}
14-
15-
if (origin.includes('essentials')) {
16-
return origin.replace('essentials', 'apps')
8+
export const getTargetOrigin = (origin = window.document.referrer) => {
9+
try {
10+
if (origin && new URL(origin).origin.endsWith('.availity.com')) {
11+
return origin;
1712
}
13+
} catch (error) {
14+
console.error('Invalid URL:', error);
1815
}
16+
// If the origin does not end with .availity.com, return undefined
1917

20-
return undefined
21-
}
18+
return undefined;
19+
};
2220

23-
const targetOrigin = getTargetOrigin()
21+
const targetOrigin = getTargetOrigin();
2422

2523
// PostMessage Logic
2624
export const handleActivityUpdate = () => {
27-
window.top.postMessage({
28-
event: eventName,
29-
time: lastActivity.time
30-
}, targetOrigin)
31-
}
25+
window.top.postMessage(
26+
{
27+
event: eventName,
28+
time: lastActivity.time,
29+
},
30+
targetOrigin
31+
);
32+
};
3233

3334
// Debounce Logic
34-
let activityIntervalId = setInterval(handleActivityUpdate, interval)
35+
let activityIntervalId = setInterval(handleActivityUpdate, interval);
3536
// Re-assignable for testing
3637
export const updateInterval = (newInterval) => {
37-
clearInterval(activityIntervalId)
38-
activityIntervalId = setInterval(handleActivityUpdate, newInterval)
39-
}
38+
clearInterval(activityIntervalId);
39+
activityIntervalId = setInterval(handleActivityUpdate, newInterval);
40+
};
4041

4142
// Event Handlers
4243
export const handleActivity = () => {
43-
lastActivity.time = Date.now().toString()
44-
}
44+
lastActivity.time = Date.now().toString();
45+
};
4546

4647
// Add ability to test handleActivity and events
4748
export const addEventListeners = () => {
48-
document.addEventListener('mousedown', handleActivity)
49-
document.addEventListener('keydown', handleActivity)
50-
}
51-
addEventListeners()
49+
document.addEventListener('mousedown', handleActivity);
50+
document.addEventListener('keydown', handleActivity);
51+
};
52+
addEventListeners();

packages/user-activity-broadcaster/src/tests/index.test.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,28 @@ const {
1010

1111
describe('user-activity-broadcaster', () => {
1212
describe('targetOrigin', () => {
13-
test('essentials.availity.com origin should have targetOrigion of apps', () => {
14-
const testOrigin = 'essentials.availity.com'
15-
const expected = 'apps.availity.com'
13+
test('should return the provided origin if it ends with .availity.com', () => {
14+
const testOrigin = 'https://essentials.availity.com'
1615

1716
const targetOrigin = getTargetOrigin(testOrigin)
1817

19-
expect(targetOrigin).toBe(expected)
18+
expect(targetOrigin).toBe(testOrigin)
2019
})
2120

22-
test('apps.availity.com origin should have targetOrigion of essentials', () => {
23-
const testOrigin = 'apps.availity.com'
24-
const expected = 'essentials.availity.com'
21+
test('should return undefined if the provider origin does not end in .availity.com', () => {
22+
const testOrigin = 'https://essentials.availity.com.malicious.com'
23+
24+
const targetOrigin = getTargetOrigin(testOrigin)
25+
26+
expect(targetOrigin).toBe(undefined)
27+
})
28+
29+
test('should return undefined if the provider origin is not a valid URL', () => {
30+
const testOrigin = 'essentials.availity.com'
2531

2632
const targetOrigin = getTargetOrigin(testOrigin)
2733

28-
expect(targetOrigin).toBe(expected)
34+
expect(targetOrigin).toBe(undefined)
2935
})
3036
})
3137

0 commit comments

Comments
 (0)