Skip to content

Commit

Permalink
Guarded ensureScrollValueMonitoring against some malicious script on …
Browse files Browse the repository at this point in the history
…the Internet overriding native document.createEvent (fixes #6887) (#7621)
  • Loading branch information
Andarist authored and gaearon committed Aug 31, 2016
1 parent 7d7defe commit 51476de
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/renderers/dom/client/ReactBrowserEventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,19 @@ var ReactBrowserEventEmitter = Object.assign({}, ReactEventEmitterMixin, {
);
},

/**
* Protect against document.createEvent() returning null
* Some popup blocker extensions appear to do this:
* https://github.com/facebook/react/issues/6887
*/
supportsEventPageXY: function() {
if (!document.createEvent) {
return false;
}
var ev = document.createEvent('MouseEvent');
return ev != null && 'pageX' in ev;
},

/**
* Listens to window scroll and resize events. We cache scroll values so that
* application code can access them without triggering reflows.
Expand All @@ -357,8 +370,7 @@ var ReactBrowserEventEmitter = Object.assign({}, ReactEventEmitterMixin, {
*/
ensureScrollValueMonitoring: function() {
if (hasEventPageXY === undefined) {
hasEventPageXY =
document.createEvent && 'pageX' in document.createEvent('MouseEvent');
hasEventPageXY = ReactBrowserEventEmitter.supportsEventPageXY();
}
if (!hasEventPageXY && !isMonitoringScrollValue) {
var refresh = ViewportMetrics.refreshScrollValues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,20 @@ describe('ReactBrowserEventEmitter', function() {
expect(idCallOrder[2]).toBe(getInternal(GRANDPARENT));
});

it('should not crash ensureScrollValueMonitoring when createEvent returns null', function() {
var originalCreateEvent = document.createEvent;
document.createEvent = function() {
return null;
};
spyOn(document, 'createEvent');

try {
var hasEventPageXY = ReactBrowserEventEmitter.supportsEventPageXY();
expect(document.createEvent.calls.count()).toBe(1);
expect(hasEventPageXY).toBe(false);
} finally {
document.createEvent = originalCreateEvent;
}
});

});

0 comments on commit 51476de

Please sign in to comment.