From ea57ef222c9f26ea2721be6c9e264e3a938a3cc7 Mon Sep 17 00:00:00 2001 From: Wilco Fiers Date: Wed, 25 Oct 2017 23:24:17 +0200 Subject: [PATCH] fix: Ensure overloaded Array.prototype won't crash axe --- lib/core/public/load.js | 2 +- lib/core/utils/rule-should-run.js | 2 +- test/core/public/run-rules.js | 28 +++++++++++++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/core/public/load.js b/lib/core/public/load.js index d328a9d03c..d0f8f2ea50 100644 --- a/lib/core/public/load.js +++ b/lib/core/public/load.js @@ -10,7 +10,7 @@ function runCommand(data, keepalive, callback) { }; var context = (data && data.context) || {}; - if (context.include && !context.include.length) { + if (context.hasOwnProperty('include') && !context.include.length) { context.include = [document]; } var options = (data && data.options) || {}; diff --git a/lib/core/utils/rule-should-run.js b/lib/core/utils/rule-should-run.js index 2803885ec1..de86379a46 100644 --- a/lib/core/utils/rule-should-run.js +++ b/lib/core/utils/rule-should-run.js @@ -12,7 +12,7 @@ function matchTags(rule, runOnly) { var defaultExclude = (axe._audit && axe._audit.tagExclude) ? axe._audit.tagExclude : []; // normalize include/exclude - if (runOnly.include || runOnly.exclude) { + if (runOnly.hasOwnProperty('include') || runOnly.hasOwnProperty('exclude')) { // Wrap include and exclude if it's not already an array include = runOnly.include || []; include = Array.isArray(include) ? include : [include]; diff --git a/test/core/public/run-rules.js b/test/core/public/run-rules.js index c4175a554f..bf376e3bb9 100644 --- a/test/core/public/run-rules.js +++ b/test/core/public/run-rules.js @@ -67,7 +67,6 @@ describe('runRules', function () { } }], messages: {}}); - var frame = document.createElement('iframe'); frame.src = '../mock/frames/frame-frame.html'; @@ -621,4 +620,31 @@ describe('runRules', function () { }); fixture.appendChild(frame); }); + + it('should not fail if `include` / `exclude` is overwritten', function (done) { + function invalid () { + throw new Error('nope!'); + } + Array.prototype.include = invalid; + Array.prototype.exclude = invalid; + + axe._load({ rules: [{ + id: 'html', + selector: 'html', + any: ['html'] + }], checks: [{ + id: 'html', + evaluate: function () { + return true; + } + }], messages: {}}); + + runRules([document], {}, function (r) { + assert.lengthOf(r[0].passes, 1); + + delete Array.prototype.include; + delete Array.prototype.exclude; + done(); + }, isNotCalled); + }); });