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

Commit 77c62fb

Browse files
authored
Merge pull request #1980 from swilliamset/update-clean-input-utility
refactor `cleanInput` utility method to prevent double encoding
2 parents 2c7c6e6 + 239ca18 commit 77c62fb

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

js/utilities.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,18 @@
5353
var isUpArrow = isKey(CONST.UP_ARROW_KEYCODE);
5454
var isDownArrow = isKey(CONST.DOWN_ARROW_KEYCODE);
5555

56-
// https://github.com/ExactTarget/fuelux/issues/1841
57-
var xssRegex = /<.*>/;
58-
var cleanInput = function cleanInput (questionableInput) {
59-
var cleanedInput = questionableInput;
60-
61-
if (xssRegex.test(cleanedInput)) {
62-
cleanedInput = $('<i>').text(questionableInput).html();
56+
var ENCODED_REGEX = /&[^\s]*;/;
57+
/*
58+
* to prevent double encoding decodes content in loop until content is encoding free
59+
*/
60+
var cleanInput = function cleanInput (questionableMarkup) {
61+
// check for encoding and decode
62+
while (ENCODED_REGEX.test(questionableMarkup)) {
63+
questionableMarkup = $('<i>').html(questionableMarkup).text();
6364
}
6465

65-
return cleanedInput;
66+
// string completely decoded now encode it
67+
return $('<i>').text(questionableMarkup).html();
6668
};
6769

6870
$.fn.utilities = {
@@ -79,4 +81,3 @@
7981
// -- BEGIN UMD WRAPPER AFTERWORD --
8082
}));
8183
// -- END UMD WRAPPER AFTERWORD --
82-

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
"updatereferences": "grunt shell:copyToReference"
2121
},
2222
"dependencies": {
23+
"babel-eslint": "^7.2.3",
2324
"bootstrap": "3.3.7",
25+
"eslint-plugin-react": "^7.0.1",
2426
"jquery": "3.2.1",
2527
"moment": "2.18.1"
2628
},

test/tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,5 @@ define(function testWrapper (require) {
6060
require('./test/picker-test');
6161
require('./test/tree-test');
6262
require('./test/wizard-test');
63+
require('./test/utilities-test');
6364
});

test/utilities-test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
define( function utilitiesTestModule(require) {
2+
var QUnit = require('qunit');
3+
var $ = require('jquery');
4+
5+
require('fuelux/utilities');
6+
7+
QUnit.module( 'Fuel UX Utilities', function utilitiesTests() {
8+
QUnit.test( 'should be defined on jquery object', function utilitiesObjectDefinedTest( assert ) {
9+
assert.equal(typeof $().utilities, 'object', 'utilities object is defined' );
10+
});
11+
12+
QUnit.module( 'cleanInput Method', {
13+
beforeEach: function beforeEachUtilitiesCleanInputTests() {
14+
this.utilities = $().utilities;
15+
this.cleanInput = this.utilities.cleanInput;
16+
}
17+
}, function utilitiesCleanInputTests() {
18+
QUnit.test( 'should be defined on utilities object', function cleanInputMethodDefinedTest( assert ) {
19+
assert.equal(typeof this.utilities.cleanInput, 'function', 'cleanInput function is defined' );
20+
});
21+
22+
QUnit.test( 'should encode strings', function cleanInputMethodEncodeTest( assert ) {
23+
var dirtyString = '<script>';
24+
var cleanString = '&lt;script&gt;';
25+
assert.equal(this.cleanInput(dirtyString), cleanString, 'string should be encoded' );
26+
});
27+
28+
QUnit.test( 'should not double encode strings', function cleanInputMethodEncodeTest( assert ) {
29+
var variants = [
30+
{dirtyString: '&lt;&gt;', cleanString: '&lt;&gt;'},
31+
{dirtyString: '&lt;script&gt;', cleanString: '&lt;script&gt;'},
32+
{dirtyString: '<&lt;&gt;>', cleanString: '&lt;&lt;&gt;&gt;'}
33+
];
34+
35+
variants.forEach(function forEachDoubleEncodeVariant(variant, index) {
36+
assert.equal(this.cleanInput(variant.dirtyString), variant.cleanString, 'variant ' + (index + 1) + ' string should be encoded' );
37+
}, this);
38+
});
39+
});
40+
});
41+
});

0 commit comments

Comments
 (0)