Skip to content

Commit 8a6cd3c

Browse files
committed
remove ability to inject arbitrary scripts
1 parent d51f062 commit 8a6cd3c

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

packages/react-devtools-extensions/src/inject.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
export default function inject(scriptName: string, done: ?Function) {
44
const source = `
5+
// the prototype stuff is in case document.createElement has been modified
56
(function () {
6-
window.postMessage({ source: 'react-devtools-inject-script', scriptName: "${scriptName}" }, "*");
7+
var script = document.constructor.prototype.createElement.call(document, 'script');
8+
script.src = "${scriptName}";
9+
script.charset = "utf-8";
10+
document.documentElement.appendChild(script);
11+
script.parentNode.removeChild(script);
712
})()
813
`;
914

@@ -16,4 +21,4 @@ export default function inject(scriptName: string, done: ?Function) {
1621
done();
1722
}
1823
});
19-
}
24+
}

packages/react-devtools-extensions/src/injectGlobalHook.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ window.addEventListener('message', function(evt) {
3131
reactBuildType: evt.data.reactBuildType,
3232
};
3333
chrome.runtime.sendMessage(lastDetectionResult);
34-
} else if (evt.data.source === 'react-devtools-inject-script' && evt.data.scriptName) {
34+
} else if (evt.data.source === 'react-devtools-inject-backend') {
3535
//Inject the specified script
3636
var script = document.constructor.prototype.createElement.call(document, 'script');
37-
script.src = evt.data.scriptName;
37+
script.src = chrome.runtime.getURL('build/backend.js');
3838
script.charset = "utf-8";
3939
document.documentElement.appendChild(script);
4040
script.parentNode.removeChild(script);

packages/react-devtools-extensions/src/main.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,18 @@ function createPanelIfReactLoaded() {
135135

136136
// Initialize the backend only once the Store has been initialized.
137137
// Otherwise the Store may miss important initial tree op codes.
138-
inject(chrome.runtime.getURL('build/backend.js'));
138+
chrome.devtools.inspectedWindow.eval(
139+
`window.postMessage({ source: 'react-devtools-inject-backend' });`,
140+
function(response, error) {
141+
if (error) {
142+
console.log(error);
143+
}
144+
145+
if (typeof done === 'function') {
146+
done();
147+
}
148+
}
149+
);
139150

140151
const viewElementSourceFunction = createViewElementSource(
141152
bridge,

0 commit comments

Comments
 (0)