Skip to content

Commit 1eb618f

Browse files
committed
Do not parse messages from other scripts
Fix #11.
1 parent 8b272b8 commit 1eb618f

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

src/browser/extension/background/messaging.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ onConnect(() => ({
1414
source: 'redux-page'
1515
}), {}, connections, window.store, sendNAMessage);
1616

17+
function parseJSON(data) {
18+
try {
19+
return JSON.parse(data);
20+
} catch (e) {
21+
// console.error(data + 'is not a valid JSON', e);
22+
return null;
23+
}
24+
}
25+
1726
// Receive message from content script and relay to the devTools page
1827
function messaging(request, sender) {
1928
const tabId = sender.tab ? sender.tab.id : sender.id;
@@ -22,7 +31,9 @@ function messaging(request, sender) {
2231
if (connections[ tabId ]) sendNAMessage(connections[ tabId ]);
2332
return true;
2433
}
25-
if (request.payload) store.liftedStore.setState(request.payload);
34+
const payload = typeof request.payload === 'string' ? parseJSON(request.payload) : request.payload;
35+
if (!payload) return true;
36+
store.liftedStore.setState(payload);
2637
if (request.init) {
2738
store.id = tabId;
2839
if (typeof tabId === 'number') {
@@ -31,7 +42,7 @@ function messaging(request, sender) {
3142
}
3243
}
3344
if (tabId in connections) {
34-
connections[ tabId ].postMessage(request);
45+
connections[ tabId ].postMessage({payload: payload});
3546
}
3647
}
3748
return true;

src/browser/extension/inject/contentScript.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,14 @@ if (window.devToolsExtensionID) { // Send external messages
2828
(document.head || document.documentElement).appendChild(s);
2929
}
3030

31-
function parseJSON(data) {
32-
try {
33-
return JSON.parse(data);
34-
} catch (e) {
35-
// console.error(data + 'is not a valid JSON', e);
36-
return {};
37-
}
38-
}
39-
4031
// Resend messages from the page to the background script
4132
window.addEventListener('message', function(event) {
42-
if (!event || event.source !== window || typeof event.data !== 'string') return;
43-
const message = parseJSON(event.data);
33+
if (!event || event.source !== window || typeof event.data !== 'object') return;
34+
const message = event.data;
4435
if (message.source !== 'redux-page') return;
4536
payload = message.payload;
4637
sendMessage(message);
47-
});
38+
}, false);
4839

4940
if (typeof window.onbeforeunload !== 'undefined') {
5041
// Prevent adding beforeunload listener for Chrome apps
@@ -62,4 +53,4 @@ document.addEventListener('visibilitychange', function() {
6253
init: true
6354
});
6455
}
65-
});
56+
}, false);

src/browser/extension/inject/pageScript.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,19 @@ import { ACTION, UPDATE } from '../../../app/constants/ActionTypes';
44

55
window.devToolsInit = function(store) {
66
function onChange(init) {
7-
window.postMessage(stringify({
8-
payload: store.liftedStore.getState(),
7+
window.postMessage({
8+
payload: stringify(store.liftedStore.getState()),
99
source: 'redux-page',
1010
init: init || false
11-
}), '*');
11+
}, '*');
1212
}
1313

1414
function onMessage(event) {
15-
let message;
16-
17-
if (event && event.source !== window) {
15+
if (!event || event.source !== window) {
1816
return;
1917
}
2018

21-
message = event.data;
19+
const message = event.data;
2220

2321
if (!message || message.source !== 'redux-cs') {
2422
return;
@@ -34,7 +32,7 @@ window.devToolsInit = function(store) {
3432
}
3533

3634
store.liftedStore.subscribe(onChange);
37-
window.addEventListener('message', onMessage);
35+
window.addEventListener('message', onMessage, false);
3836

3937
onChange(true);
4038
};

0 commit comments

Comments
 (0)