Skip to content
This repository was archived by the owner on Feb 9, 2020. It is now read-only.

Commit 56c3ad7

Browse files
John VilkSomeKittens
authored andcommitted
fix(inject.js): Appropriately process+filter msgs received via postMessage
The Batarang uses postMessage to send data from the webpage context to the devtools panel, but the current implementation does not appropriately filter messages that originate from the webpage itself rather than scripts we inject into the webpage context. As a result, the Batarang attempts to forward every message the webpage sends via postMessage to the Batarang. I have added a __fromBatarang property to posted messages, and modified the handler to check for the property before forwarding it to the devtools panel. There is also a benign typo in the handler code that references the global event object rather than the local evt argument. Since these alias, it's not a problem in practice, but fixing it leads to clearer code. In addition, the Batarang uses chrome.extension.connect / chrome.extension.sendMessage, when these methods are supposed to only exist on chrome.runtime according to the Chrome API docs. In practice, they exist on chrome.extension too, but since this is not a documented API call using the proper interface is likely to be more future-proof.
1 parent a2c6795 commit 56c3ad7

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

hint.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require('angular-hint');
88

99
angular.hint.onAny(function (data, severity) {
1010
window.postMessage({
11+
__fromBatarang: true,
1112
module: this.event.split(':')[0],
1213
event: this.event,
1314
data: data,

inject.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ if (document.cookie.indexOf('__ngDebug=true') != -1) {
33
}
44

55
function bootstrapHint () {
6-
chrome.extension.sendMessage('refresh');
6+
chrome.runtime.sendMessage('refresh');
77

88
var html = document.getElementsByTagName('html')[0];
99

@@ -12,11 +12,20 @@ function bootstrapHint () {
1212
script.src = chrome.extension.getURL('dist/hint.js');
1313

1414
window.addEventListener('message', function (evt) {
15-
// We only accept messages from ourselves
16-
if (event.source !== window) {
17-
return;
15+
// There's no good way to verify the provenance of the message.
16+
// evt.source === window is true for all messages sent from
17+
// the main frame. evt.origin is going to be the webpage's origin,
18+
// even if the message originated from a chrome:// script you injected.
19+
20+
// The only thing we can do is see if the message *looks* like something
21+
// we would send, cross our fingers, and send it on.
22+
// Thus, we check for one of the properties known to be on *all* of our
23+
// messages (__fromBatarang === true).
24+
var eventData = evt.data;
25+
// NOTE: Check for null before checking for the property, since typeof null === 'object'.
26+
if (typeof eventData === 'object' && eventData !== null && eventData.hasOwnProperty('__fromBatarang') && eventData.__fromBatarang) {
27+
chrome.runtime.sendMessage(eventData);
1828
}
19-
chrome.extension.sendMessage(evt.data);
2029
});
2130

2231
html.setAttribute('ng-hint', '');

panel/components/inspected-app/inspected-app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function inspectedAppService($rootScope, $q) {
5656
chrome.devtools.inspectedWindow.eval('angular.hint.' + method + '(' + args + ')');
5757
}
5858

59-
var port = chrome.extension.connect();
59+
var port = chrome.runtime.connect();
6060
port.postMessage(chrome.devtools.inspectedWindow.tabId);
6161
port.onMessage.addListener(function(msg) {
6262
$rootScope.$applyAsync(function () {

panel/components/inspected-app/inspected-app.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ describe('inspectedApp', function() {
170170

171171
function createMockChrome() {
172172
return {
173-
extension: {
173+
runtime: {
174174
connect: function () {
175175
return port = createMockSocket();
176176
}

0 commit comments

Comments
 (0)