Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit bb9fa1a

Browse files
fernando-sendMailwesleycho
authored andcommitted
fix(typeahead): use ng-bind-html
- Switches to use `ng-bind-html` from `bind-html-unsafe` BREAKING CHANGE: This modifies the typeahead to use the the `$sce` service instead when `ngSanitize` is present Closes #3463 Resolves #4073
1 parent aaec2f4 commit bb9fa1a

File tree

9 files changed

+66
-19
lines changed

9 files changed

+66
-19
lines changed

karma.conf.js

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = function(config) {
1818
'misc/test-lib/jquery-1.8.2.min.js',
1919
'node_modules/angular/angular.js',
2020
'node_modules/angular-mocks/angular-mocks.js',
21+
'node_modules/angular-sanitize/angular-sanitize.js',
2122
'misc/test-lib/helpers.js',
2223
'src/**/*.js',
2324
'template/**/*.js'

misc/demo/assets/app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* global FastClick, smoothScroll */
2-
angular.module('ui.bootstrap.demo', ['ui.bootstrap', 'plunker', 'ngTouch', 'ngAnimate'], function($httpProvider){
2+
angular.module('ui.bootstrap.demo', ['ui.bootstrap', 'plunker', 'ngTouch', 'ngAnimate', 'ngSanitize'], function($httpProvider){
33
FastClick.attach(document.body);
44
delete $httpProvider.defaults.headers.common['X-Requested-With'];
55
}).run(['$location', function($location){

misc/demo/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<script src="//ajax.googleapis.com/ajax/libs/angularjs/<%= ngversion %>/angular.min.js"></script>
1515
<script src="//ajax.googleapis.com/ajax/libs/angularjs/<%= ngversion %>/angular-animate.min.js"></script>
1616
<script src="//ajax.googleapis.com/ajax/libs/angularjs/<%= ngversion %>/angular-touch.min.js"></script>
17+
<script src="//ajax.googleapis.com/ajax/libs/angularjs/<%= ngversion %>/angular-sanitize.js"></script>
1718
<script src="ui-bootstrap-tpls-<%= pkg.version%>.min.js"></script>
1819
<script src="assets/plunker.js"></script>
1920
<script src="assets/app.js"></script>

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"devDependencies": {
1515
"angular": "^1.4.3",
1616
"angular-mocks": "^1.4.3",
17+
"angular-sanitize": "^1.4.3",
1718
"grunt": "^0.4.5",
1819
"grunt-contrib-concat": "^0.5.1",
1920
"grunt-contrib-copy": "^0.8.0",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
describe('Security concerns', function() {
2+
var highlightFilter, $sanitize, logSpy;
3+
4+
beforeEach(module('ui.bootstrap.typeahead', 'ngSanitize'));
5+
6+
beforeEach(inject(function (typeaheadHighlightFilter, _$sanitize_, $log) {
7+
highlightFilter = typeaheadHighlightFilter;
8+
$sanitize = _$sanitize_;
9+
logSpy = spyOn($log, 'warn');
10+
}));
11+
12+
it('should not call the $log service when ngSanitize is present', function() {
13+
highlightFilter('before <script src="">match</script> after', 'match');
14+
expect(logSpy).not.toHaveBeenCalled();
15+
});
16+
});
+24-11
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,51 @@
1-
describe('typeaheadHighlight', function() {
2-
var highlightFilter;
1+
describe('typeaheadHighlight', function () {
2+
3+
var highlightFilter, $log, $sce, logSpy;
34

45
beforeEach(module('ui.bootstrap.typeahead'));
6+
7+
beforeEach(inject(function(_$log_, _$sce_) {
8+
$log = _$log_;
9+
$sce = _$sce_;
10+
logSpy = spyOn($log, 'warn');
11+
}));
12+
513
beforeEach(inject(function(typeaheadHighlightFilter) {
614
highlightFilter = typeaheadHighlightFilter;
715
}));
816

917
it('should higlight a match', function() {
10-
expect(highlightFilter('before match after', 'match')).toEqual('before <strong>match</strong> after');
18+
expect($sce.getTrustedHtml(highlightFilter('before match after', 'match'))).toEqual('before <strong>match</strong> after');
1119
});
1220

1321
it('should higlight a match with mixed case', function() {
14-
expect(highlightFilter('before MaTch after', 'match')).toEqual('before <strong>MaTch</strong> after');
22+
expect($sce.getTrustedHtml(highlightFilter('before MaTch after', 'match'))).toEqual('before <strong>MaTch</strong> after');
1523
});
1624

1725
it('should higlight all matches', function() {
18-
expect(highlightFilter('before MaTch after match', 'match')).toEqual('before <strong>MaTch</strong> after <strong>match</strong>');
26+
expect($sce.getTrustedHtml(highlightFilter('before MaTch after match', 'match'))).toEqual('before <strong>MaTch</strong> after <strong>match</strong>');
1927
});
2028

2129
it('should do nothing if no match', function() {
22-
expect(highlightFilter('before match after', 'nomatch')).toEqual('before match after');
30+
expect($sce.getTrustedHtml(highlightFilter('before match after', 'nomatch'))).toEqual('before match after');
2331
});
2432

2533
it('should do nothing if no or empty query', function() {
26-
expect(highlightFilter('before match after', '')).toEqual('before match after');
27-
expect(highlightFilter('before match after', null)).toEqual('before match after');
28-
expect(highlightFilter('before match after', undefined)).toEqual('before match after');
34+
expect($sce.getTrustedHtml(highlightFilter('before match after', ''))).toEqual('before match after');
35+
expect($sce.getTrustedHtml(highlightFilter('before match after', null))).toEqual('before match after');
36+
expect($sce.getTrustedHtml(highlightFilter('before match after', undefined))).toEqual('before match after');
2937
});
3038

3139
it('issue 316 - should work correctly for regexp reserved words', function() {
32-
expect(highlightFilter('before (match after', '(match')).toEqual('before <strong>(match</strong> after');
40+
expect($sce.getTrustedHtml(highlightFilter('before (match after', '(match'))).toEqual('before <strong>(match</strong> after');
3341
});
3442

3543
it('issue 1777 - should work correctly with numeric values', function() {
36-
expect(highlightFilter(123, '2')).toEqual('1<strong>2</strong>3');
44+
expect($sce.getTrustedHtml(highlightFilter(123, '2'))).toEqual('1<strong>2</strong>3');
45+
});
46+
47+
it('should show a warning when this component is being used unsafely', function() {
48+
highlightFilter('<i>before</i> match after', 'match');
49+
expect(logSpy).toHaveBeenCalled();
3750
});
3851
});

src/typeahead/test/typeahead.spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ describe('typeahead tests', function() {
33
var changeInputValueTo;
44

55
beforeEach(module('ui.bootstrap.typeahead'));
6+
beforeEach(module('ngSanitize'));
67
beforeEach(module('template/typeahead/typeahead-popup.html'));
78
beforeEach(module('template/typeahead/typeahead-match.html'));
89
beforeEach(module(function($compileProvider) {

src/typeahead/typeahead.js

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap.bindHtml'])
1+
angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
22

33
/**
44
* A helper service that can parse typeahead's syntax (string provided by users)
@@ -36,7 +36,6 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
3636
return {
3737
require: 'ngModel',
3838
link: function(originalScope, element, attrs, modelCtrl) {
39-
4039
//SUPPORTED ATTRIBUTES (OPTIONS)
4140

4241
//minimal no of characters that needs to be entered before typeahead kicks-in
@@ -347,7 +346,6 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
347346

348347
//bind keyboard events: arrows up(38) / down(40), enter(13) and tab(9), esc(27)
349348
element.bind('keydown', function(evt) {
350-
351349
//typeahead is open and an "interesting" key was pressed
352350
if (scope.matches.length === 0 || HOT_KEYS.indexOf(evt.which) === -1) {
353351
return;
@@ -483,12 +481,28 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
483481
};
484482
}])
485483

486-
.filter('typeaheadHighlight', function() {
484+
.filter('typeaheadHighlight', ['$sce', '$injector', '$log', function($sce, $injector, $log) {
485+
var isSanitizePresent;
486+
isSanitizePresent = $injector.has('$sanitize');
487+
487488
function escapeRegexp(queryToEscape) {
489+
// Regex: capture the whole query string and replace it with the string that will be used to match
490+
// the results, for example if the capture is "a" the result will be \a
488491
return queryToEscape.replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1');
489492
}
490493

494+
function containsHtml(matchItem) {
495+
return /<.*>/g.test(matchItem);
496+
}
497+
491498
return function(matchItem, query) {
492-
return query ? ('' + matchItem).replace(new RegExp(escapeRegexp(query), 'gi'), '<strong>$&</strong>') : matchItem;
499+
if (!isSanitizePresent && containsHtml(matchItem)) {
500+
$log.warn('Unsafe use of typeahead please use ngSanitize'); // Warn the user about the danger
501+
}
502+
matchItem = query? ('' + matchItem).replace(new RegExp(escapeRegexp(query), 'gi'), '<strong>$&</strong>') : matchItem; // Replaces the capture string with a the same string inside of a "strong" tag
503+
if (!isSanitizePresent) {
504+
matchItem = $sce.trustAsHtml(matchItem); // If $sanitize is not present we pack the string in a $sce object for the ng-bind-html directive
505+
}
506+
return matchItem;
493507
};
494-
});
508+
}]);
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<a href tabindex="-1" bind-html-unsafe="match.label | typeaheadHighlight:query"></a>
1+
<a href tabindex="-1" ng-bind-html="match.label | typeaheadHighlight:query"></a>

0 commit comments

Comments
 (0)