Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 0ec0cc5

Browse files
psamimSplaktar
andauthored
fix(select): sanitize user input before searching options (#11855)
Fixes #11854 Co-authored-by: Michael Prentice <[email protected]>
1 parent 4a4dde4 commit 0ec0cc5

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

src/components/autocomplete/js/highlightController.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ angular
22
.module('material.components.autocomplete')
33
.controller('MdHighlightCtrl', MdHighlightCtrl);
44

5-
function MdHighlightCtrl ($scope, $element, $attrs) {
5+
function MdHighlightCtrl ($scope, $element, $attrs, $mdUtil) {
66
this.$scope = $scope;
77
this.$element = $element;
88
this.$attrs = $attrs;
9+
this.$mdUtil = $mdUtil;
910

1011
// Cache the Regex to avoid rebuilding each time.
1112
this.regex = null;
@@ -104,15 +105,10 @@ MdHighlightCtrl.prototype.resolveTokens = function(string) {
104105
/** Creates a regex for the specified text with the given flags. */
105106
MdHighlightCtrl.prototype.createRegex = function(term, flags) {
106107
var startFlag = '', endFlag = '';
107-
var regexTerm = this.sanitizeRegex(term);
108+
var regexTerm = this.$mdUtil.sanitize(term);
108109

109110
if (flags.indexOf('^') >= 0) startFlag = '^';
110111
if (flags.indexOf('$') >= 0) endFlag = '$';
111112

112113
return new RegExp(startFlag + regexTerm + endFlag, flags.replace(/[$^]/g, ''));
113114
};
114-
115-
/** Sanitizes a regex by removing all common RegExp identifiers */
116-
MdHighlightCtrl.prototype.sanitizeRegex = function(term) {
117-
return term && term.toString().replace(/[\\^$*+?.()|{}[\]]/g, '\\$&');
118-
};

src/components/select/select.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ function SelectMenuDirective($parse, $mdUtil, $mdConstant, $mdTheming) {
717717
}, CLEAR_SEARCH_AFTER);
718718

719719
searchStr += e.key;
720-
var search = new RegExp('^' + searchStr, 'i');
720+
var search = new RegExp('^' + $mdUtil.sanitize(searchStr), 'i');
721721
if (!optNodes) {
722722
optNodes = $element.find('md-option');
723723
optText = new Array(optNodes.length);
@@ -730,6 +730,7 @@ function SelectMenuDirective($parse, $mdUtil, $mdConstant, $mdTheming) {
730730
return optNodes[i];
731731
}
732732
}
733+
733734
};
734735

735736
self.init = function(ngModel, binding) {

src/core/util/util.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,16 @@ function UtilFactory($document, $timeout, $compile, $rootScope, $$mdAnimate, $in
996996
if (path.indexOf(window) === -1)
997997
path.push(window);
998998
return path;
999+
},
1000+
1001+
/**
1002+
* Gets the string the user has entered and removes Regex identifiers
1003+
* @param {string} term
1004+
* @returns {string} sanitized string
1005+
*/
1006+
sanitize: function(term) {
1007+
if (!term) return term;
1008+
return term.replace(/[\\^$*+?.()|{}[]]/g, '\\$&');
9991009
}
10001010
};
10011011

src/core/util/util.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,4 +752,20 @@ describe('util', function() {
752752
expect($mdUtil.uniq(myArray)).toEqual([1, 2, 3, 4]);
753753
});
754754
});
755+
756+
describe('sanitize', function() {
757+
var $mdUtil;
758+
759+
beforeEach(inject(function(_$mdUtil_) {
760+
$mdUtil = _$mdUtil_;
761+
}));
762+
763+
it('Removes Regex indentifiers in a text', function() {
764+
765+
// eslint-disable-next-line no-useless-escape
766+
var myText = '\+98';
767+
768+
expect($mdUtil.sanitize(myText)).toEqual('+98');
769+
});
770+
});
755771
});

0 commit comments

Comments
 (0)