Skip to content

Commit 9c7c494

Browse files
gkalpakNarretz
authored andcommitted
docs(errorDisplay): encode < and > in error messages
When an error message contains an HTML string (e.g. `$location:nobase` containing `<base>`), it was interpreted as a literal HTML element, instead of text. Error messages are not expected to render as HTML, but we still need to use `.html()` in `errorDisplay`, so that the links created by `errorLinkFilter` are properly displayed. This commit solves this issue by replacing `<`/`>` with `&lt;`/`&gt;`. Related to angular#14016.
1 parent 321180a commit 9c7c494

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

docs/app/src/errors.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ angular.module('errors', ['ngSanitize'])
1313
};
1414

1515
return function (text, target) {
16-
var targetHtml = target ? ' target="' + target + '"' : '';
17-
1816
if (!text) return text;
1917

18+
var targetHtml = target ? ' target="' + target + '"' : '';
19+
2020
return $sanitize(text.replace(LINKY_URL_REGEXP, function (url) {
2121
if (STACK_TRACE_REGEXP.test(url)) {
2222
return url;
@@ -34,6 +34,10 @@ angular.module('errors', ['ngSanitize'])
3434

3535

3636
.directive('errorDisplay', ['$location', 'errorLinkFilter', function ($location, errorLinkFilter) {
37+
var encodeAngleBrackets = function (text) {
38+
return text.replace(/</g, '&lt;').replace(/>/g, '&gt;');
39+
};
40+
3741
var interpolate = function (formatString) {
3842
var formatArgs = arguments;
3943
return formatString.replace(/\{\d+\}/g, function (match) {
@@ -51,12 +55,15 @@ angular.module('errors', ['ngSanitize'])
5155
link: function (scope, element, attrs) {
5256
var search = $location.search(),
5357
formatArgs = [attrs.errorDisplay],
58+
formattedText,
5459
i;
5560

5661
for (i = 0; angular.isDefined(search['p'+i]); i++) {
5762
formatArgs.push(search['p'+i]);
5863
}
59-
element.html(errorLinkFilter(interpolate.apply(null, formatArgs), '_blank'));
64+
65+
formattedText = encodeAngleBrackets(interpolate.apply(null, formatArgs));
66+
element.html(errorLinkFilter(formattedText, '_blank'));
6067
}
6168
};
6269
}]);

docs/app/test/errorsSpec.js

+6
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,11 @@ describe('errors', function() {
156156
expect(errorLinkFilter.callCount).toBe(1);
157157
expect(errorLinkFilter).toHaveBeenCalledWith('foo = foo', '_blank');
158158
});
159+
160+
161+
it('should encode `<` and `>`', function() {
162+
var elem = $compile('<span error-display="&lt;xyz&gt;"></span>')($rootScope);
163+
expect(elem.text()).toBe('<xyz>');
164+
});
159165
});
160166
});

0 commit comments

Comments
 (0)