Skip to content

Commit

Permalink
fix(responsible): Restrict error construction to known errors
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoFiers committed Sep 7, 2017
1 parent 29e54df commit 0128a7e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/core/utils/respondable.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
(function (exports) {
'use strict';
var messages = {},
subscribers = {};
subscribers = {},
errorTypes = Object.freeze(['EvalError', 'RangeError', 'ReferenceError',
'SyntaxError', 'TypeError', 'URIError']);

/**
* get the unique string to be used to identify our instance of aXe
Expand Down Expand Up @@ -154,7 +156,8 @@
*/
function buildErrorObject(error) {
var msg = error.message || 'Unknown error occurred';
var ErrConstructor = window[error.name] || Error;
var errorName = errorTypes.includes(error.name) ? error.name : 'Error';
var ErrConstructor = window[errorName] || Error;

if (error.stack) {
msg += '\n' + error.stack.replace(error.message, '');
Expand Down
30 changes: 30 additions & 0 deletions test/core/utils/respondable.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,36 @@ describe('axe.utils.respondable', function () {
assert.isTrue(success);
});

it('should create an Error if an invalid error type is passed', function () {
var success = false;
var event = document.createEvent('Event');
window.evil = function () {};
// Define that the event name is 'build'.
event.initEvent('message', true, true);
event.data = JSON.stringify({
_respondable: true,
_source: 'axe.2.0.0',
topic: 'Death star',
error: {
name: 'evil',
message: 'The exhaust port is open!',
trail: '... boom'
},
uuid: mockUUID
});
event.source = window;

axe.utils.respondable(window, 'Death star', null, true, function (data) {
success = true;
assert.instanceOf(data, Error);
assert.equal(data.message, 'The exhaust port is open!');
});

document.dispatchEvent(event);
assert.isTrue(success);
window.evil = undefined;
});

it('uses respondable.isInFrame() to check if the page is in a frame or not', function() {
assert.equal(axe.utils.respondable.isInFrame(), !!window.frameElement);

Expand Down

0 comments on commit 0128a7e

Please sign in to comment.