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

Commit e2068ad

Browse files
committed
fix(ng-bind-html): watch string value instead of wrapper
Ref: #4045 I have this sinking feeling that support this use case sort of encourages binding to function that blindly trust some html. For now, I'm fixing the issue while I think about the use cases some more. In the case of a function that performs any non-trivial work before wrapping the value (e.g. the showdown filter in issue #3980, or the binding to a simply wrapper function in issue #3932 if it did anything meaty), this fix makes it "work" - but performance is going to suck - you should bind to some other thing on scope that watches the actual source and adjusts itself when that changes (e.g. the showdown filter.) For the case of the wrapper in #3932, if one isn't performing sanitization or some such thing - then you the developer has insight into why that value is safe in that particular context - and it should be available simply by name and not as a result of a function taking any arbitrary input to make auditing of security a little saner. Closes #3932, #3980
1 parent 3ed094d commit e2068ad

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/ng/directive/ngBind.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,15 @@ var ngBindTemplateDirective = ['$interpolate', function($interpolate) {
134134
* @element ANY
135135
* @param {expression} ngBindHtml {@link guide/expression Expression} to evaluate.
136136
*/
137-
var ngBindHtmlDirective = ['$sce', function($sce) {
137+
var ngBindHtmlDirective = ['$sce', '$parse', function($sce, $parse) {
138138
return function(scope, element, attr) {
139139
element.addClass('ng-binding').data('$binding', attr.ngBindHtml);
140-
scope.$watch(attr.ngBindHtml, function ngBindHtmlWatchAction(value) {
141-
element.html($sce.getTrustedHtml(value) || '');
140+
141+
var parsed = $parse(attr.ngBindHtml);
142+
function getStringValue() { return (parsed(scope) || '').toString(); }
143+
144+
scope.$watch(getStringValue, function ngBindHtmlWatchAction(value) {
145+
element.html($sce.getTrustedHtml(parsed(scope)) || '');
142146
});
143147
};
144148
}];

test/ng/directive/ngBindSpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@ describe('ngBind*', function() {
102102
expect(angular.lowercase(element.html())).toEqual('<div onclick="">hello</div>');
103103
}));
104104

105+
it('should watch the string value to avoid infinite recursion', inject(function($rootScope, $compile, $sce) {
106+
// Ref: https://github.com/angular/angular.js/issues/3932
107+
// If the binding is a function that creates a new value on every call via trustAs, we'll
108+
// trigger an infinite digest if we don't take care of it.
109+
element = $compile('<div ng-bind-html="getHtml()"></div>')($rootScope);
110+
$rootScope.getHtml = function() {
111+
return $sce.trustAsHtml('<div onclick="">hello</div>');
112+
};
113+
$rootScope.$digest();
114+
expect(angular.lowercase(element.html())).toEqual('<div onclick="">hello</div>');
115+
}));
116+
105117
describe('when $sanitize is available', function() {
106118
beforeEach(function() { module('ngSanitize'); });
107119

0 commit comments

Comments
 (0)