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

Commit 4a3ccf7

Browse files
fix(typeahead): Typeahead uses ngSanitize when present
1 parent 739b1d1 commit 4a3ccf7

File tree

5 files changed

+62
-15
lines changed

5 files changed

+62
-15
lines changed

Diff for: 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){

Diff for: 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>
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
17+
});

Diff for: src/typeahead/test/typeahead-highlight.spec.js

+23-10
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,52 @@
11
describe('typeaheadHighlight', function () {
22

3-
var highlightFilter;
3+
var highlightFilter, $log, $sce, logSpy;
44

55
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+
613
beforeEach(inject(function (typeaheadHighlightFilter) {
714
highlightFilter = typeaheadHighlightFilter;
815
}));
916

1017
it('should higlight a match', function () {
11-
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');
1219
});
1320

1421
it('should higlight a match with mixed case', function () {
15-
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');
1623
});
1724

1825
it('should higlight all matches', function () {
19-
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>');
2027
});
2128

2229
it('should do nothing if no match', function () {
23-
expect(highlightFilter('before match after', 'nomatch')).toEqual('before match after');
30+
expect($sce.getTrustedHtml(highlightFilter('before match after', 'nomatch'))).toEqual('before match after');
2431
});
2532

2633
it('should do nothing if no or empty query', function () {
27-
expect(highlightFilter('before match after', '')).toEqual('before match after');
28-
expect(highlightFilter('before match after', null)).toEqual('before match after');
29-
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');
3037
});
3138

3239
it('issue 316 - should work correctly for regexp reserved words', function () {
33-
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');
3441
});
3542

3643
it('issue 1777 - should work correctly with numeric values', function () {
37-
expect(highlightFilter(123, '2')).toEqual('1<strong>2</strong>3');
44+
expect($sce.getTrustedHtml(highlightFilter(123, '2'))).toEqual('1<strong>2</strong>3');
3845
});
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();
50+
});
51+
3952
});

Diff for: src/typeahead/typeahead.js

+20-4
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)
@@ -492,13 +492,29 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap
492492
};
493493
}])
494494

495-
.filter('typeaheadHighlight', function() {
495+
.filter('typeaheadHighlight', ['$sce', '$injector', '$log', function($sce, $injector, $log) {
496+
497+
var isSanitizePresent;
498+
isSanitizePresent = $injector.has('$sanitize');
496499

497500
function escapeRegexp(queryToEscape) {
501+
// Regex: capture the whole query string and replace it with the string that will be used to match
502+
// the results, for example if the capture is "a" the result will be \a
498503
return queryToEscape.replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1');
499504
}
500505

506+
function containsHtml(matchItem) {
507+
return /<.*>/g.test(matchItem);
508+
}
509+
501510
return function(matchItem, query) {
502-
return query ? ('' + matchItem).replace(new RegExp(escapeRegexp(query), 'gi'), '<strong>$&</strong>') : matchItem;
511+
if(!isSanitizePresent && containsHtml(matchItem)) {
512+
$log.warn('Unsafe use of typeahead please use ngSanitize'); // Warn the user about the danger
513+
}
514+
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
515+
if(!isSanitizePresent) {
516+
matchItem = $sce.trustAsHtml(matchItem); // If $sanitize is not present we pack the string in a $sce object for the ng-bind-html directive
517+
}
518+
return matchItem;
503519
};
504-
});
520+
}]);

0 commit comments

Comments
 (0)