Skip to content

Commit 9d84120

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

File tree

2 files changed

+34
-40
lines changed

2 files changed

+34
-40
lines changed
Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,47 @@
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 = {}
7-
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')
17-
}
6+
export const lastActivity = {};
7+
8+
export const getTargetOrigin = (origin = window.document.referrer) => {
9+
if (origin && /\.availity\.com\/?$/.test(origin)) {
10+
return origin;
1811
}
1912

20-
return undefined
21-
}
13+
return undefined;
14+
};
2215

23-
const targetOrigin = getTargetOrigin()
16+
const targetOrigin = getTargetOrigin();
2417

2518
// PostMessage Logic
2619
export const handleActivityUpdate = () => {
27-
window.top.postMessage({
28-
event: eventName,
29-
time: lastActivity.time
30-
}, targetOrigin)
31-
}
20+
window.top.postMessage(
21+
{
22+
event: eventName,
23+
time: lastActivity.time,
24+
},
25+
targetOrigin
26+
);
27+
};
3228

3329
// Debounce Logic
34-
let activityIntervalId = setInterval(handleActivityUpdate, interval)
30+
let activityIntervalId = setInterval(handleActivityUpdate, interval);
3531
// Re-assignable for testing
3632
export const updateInterval = (newInterval) => {
37-
clearInterval(activityIntervalId)
38-
activityIntervalId = setInterval(handleActivityUpdate, newInterval)
39-
}
33+
clearInterval(activityIntervalId);
34+
activityIntervalId = setInterval(handleActivityUpdate, newInterval);
35+
};
4036

4137
// Event Handlers
4238
export const handleActivity = () => {
43-
lastActivity.time = Date.now().toString()
44-
}
39+
lastActivity.time = Date.now().toString();
40+
};
4541

4642
// Add ability to test handleActivity and events
4743
export const addEventListeners = () => {
48-
document.addEventListener('mousedown', handleActivity)
49-
document.addEventListener('keydown', handleActivity)
50-
}
51-
addEventListeners()
44+
document.addEventListener('mousedown', handleActivity);
45+
document.addEventListener('keydown', handleActivity);
46+
};
47+
addEventListeners();

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,20 @@ const {
1010

1111
describe('user-activity-broadcaster', () => {
1212
describe('targetOrigin', () => {
13-
test('essentials.availity.com origin should have targetOrigion of apps', () => {
13+
test('should return the provided origin if it ends with .availity.com', () => {
1414
const testOrigin = 'essentials.availity.com'
15-
const expected = 'apps.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 = 'essentials.availity.com.malicious.com'
2523

2624
const targetOrigin = getTargetOrigin(testOrigin)
2725

28-
expect(targetOrigin).toBe(expected)
26+
expect(targetOrigin).toBe(undefined)
2927
})
3028
})
3129

0 commit comments

Comments
 (0)