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

Commit f6681d4

Browse files
committed
fix(ngSanitize): follow HTML parser rules for start tags / allow < in text content
ngSanitize will now permit opening braces in text content, provided they are not followed by either an unescaped backslash, or by an ASCII letter (u+0041 - u+005A, u+0061 - u+007A), in compliance with rules of the parsing spec, without taking insertion mode into account. BREAKING CHANGE Previously, $sanitize would "fix" invalid markup in which a space preceded alphanumeric characters in a start-tag. Following this change, any opening angle bracket which is not followed by either a forward slash, or by an ASCII letter (a-z | A-Z) will not be considered a start tag delimiter, per the HTML parsing spec (http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html). Closes #8212 Closes #8193
1 parent d259754 commit f6681d4

File tree

2 files changed

+56
-15
lines changed

2 files changed

+56
-15
lines changed

src/ngSanitize/sanitize.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ function sanitizeText(chars) {
154154

155155
// Regular Expressions for parsing tags and attributes
156156
var START_TAG_REGEXP =
157-
/^<\s*([\w:-]+)((?:\s+[\w:-]+(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)\s*>/,
158-
END_TAG_REGEXP = /^<\s*\/\s*([\w:-]+)[^>]*>/,
157+
/^<((?:[a-zA-Z])[\w:-]*)((?:\s+[\w:-]+(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)\s*(>?)/,
158+
END_TAG_REGEXP = /^<\/\s*([\w:-]+)[^>]*>/,
159159
ATTR_REGEXP = /([\w:-]+)(?:\s*=\s*(?:(?:"((?:[^"])*)")|(?:'((?:[^'])*)')|([^>\s]+)))?/g,
160160
BEGIN_TAG_REGEXP = /^</,
161-
BEGING_END_TAGE_REGEXP = /^<\s*\//,
161+
BEGING_END_TAGE_REGEXP = /^<\//,
162162
COMMENT_REGEXP = /<!--(.*?)-->/g,
163163
DOCTYPE_REGEXP = /<!DOCTYPE([^>]*?)>/i,
164164
CDATA_REGEXP = /<!\[CDATA\[(.*?)]]>/g,
@@ -232,10 +232,11 @@ function makeMap(str) {
232232
* @param {object} handler
233233
*/
234234
function htmlParser( html, handler ) {
235-
var index, chars, match, stack = [], last = html;
235+
var index, chars, match, stack = [], last = html, text;
236236
stack.last = function() { return stack[ stack.length - 1 ]; };
237237

238238
while ( html ) {
239+
text = '';
239240
chars = true;
240241

241242
// Make sure we're not in a script or style element
@@ -274,16 +275,23 @@ function htmlParser( html, handler ) {
274275
match = html.match( START_TAG_REGEXP );
275276

276277
if ( match ) {
277-
html = html.substring( match[0].length );
278-
match[0].replace( START_TAG_REGEXP, parseStartTag );
278+
// We only have a valid start-tag if there is a '>'.
279+
if ( match[4] ) {
280+
html = html.substring( match[0].length );
281+
match[0].replace( START_TAG_REGEXP, parseStartTag );
282+
}
279283
chars = false;
284+
} else {
285+
// no ending tag found --- this piece should be encoded as an entity.
286+
text += '<';
287+
html = html.substring(1);
280288
}
281289
}
282290

283291
if ( chars ) {
284292
index = html.indexOf("<");
285293

286-
var text = index < 0 ? html : html.substring( 0, index );
294+
text += index < 0 ? html : html.substring( 0, index );
287295
html = index < 0 ? "" : html.substring( index );
288296

289297
if (handler.chars) handler.chars( decodeEntities(text) );

test/ngSanitize/sanitizeSpec.js

+41-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe('HTML', function() {
2121

2222
var handler, start, text, comment;
2323
beforeEach(function() {
24+
text = "";
2425
handler = {
2526
start: function(tag, attrs, unary){
2627
start = {
@@ -35,7 +36,7 @@ describe('HTML', function() {
3536
});
3637
},
3738
chars: function(text_){
38-
text = text_;
39+
text += text_;
3940
},
4041
end:function(tag) {
4142
expect(tag).toEqual(start.tag);
@@ -81,8 +82,31 @@ describe('HTML', function() {
8182
expect(text).toEqual('text');
8283
});
8384

85+
it('should not treat "<" followed by a non-/ or non-letter as a tag', function() {
86+
expectHTML('<- text1 text2 <1 text1 text2 <{', handler).
87+
toBe('&lt;- text1 text2 &lt;1 text1 text2 &lt;{');
88+
});
89+
90+
it('should throw badparse if text content contains "<" followed by "/" without matching ">"', function() {
91+
expect(function() {
92+
htmlParser('foo </ bar', handler);
93+
}).toThrowMinErr('$sanitize', 'badparse', 'The sanitizer was unable to parse the following block of html: </ bar');
94+
});
95+
96+
it('should throw badparse if text content contains "<" followed by an ASCII letter without matching ">"', function() {
97+
expect(function() {
98+
htmlParser('foo <a bar', handler);
99+
}).toThrowMinErr('$sanitize', 'badparse', 'The sanitizer was unable to parse the following block of html: <a bar');
100+
});
101+
102+
it('should accept tag delimiters such as "<" inside real tags', function() {
103+
// Assert that the < is part of the text node content, and not part of a tag name.
104+
htmlParser('<p> 10 < 100 </p>', handler);
105+
expect(text).toEqual(' 10 < 100 ');
106+
});
107+
84108
it('should parse newlines in tags', function() {
85-
htmlParser('<\ntag\n attr="value"\n>text<\n/\ntag\n>', handler);
109+
htmlParser('<tag\n attr="value"\n>text</\ntag\n>', handler);
86110
expect(start).toEqual({tag:'tag', attrs:{attr:'value'}, unary:false});
87111
expect(text).toEqual('text');
88112
});
@@ -123,8 +147,9 @@ describe('HTML', function() {
123147
expectHTML('a<!DocTyPe html>c.').toEqual('ac.');
124148
});
125149

126-
it('should remove nested script', function() {
127-
expectHTML('a< SCRIPT >A< SCRIPT >evil< / scrIpt >B< / scrIpt >c.').toEqual('ac.');
150+
it('should escape non-start tags', function() {
151+
expectHTML('a< SCRIPT >A< SCRIPT >evil< / scrIpt >B< / scrIpt >c.').
152+
toBe('a&lt; SCRIPT &gt;A&lt; SCRIPT &gt;evil&lt; / scrIpt &gt;B&lt; / scrIpt &gt;c.');
128153
});
129154

130155
it('should remove attrs', function() {
@@ -165,14 +190,16 @@ describe('HTML', function() {
165190
expectHTML(everything).toEqual(everything);
166191
});
167192

168-
it('should handle improper html', function() {
193+
it('should mangle improper html', function() {
194+
// This text is encoded more than a real HTML parser would, but it should render the same.
169195
expectHTML('< div rel="</div>" alt=abc dir=\'"\' >text< /div>').
170-
toEqual('<div rel="&lt;/div&gt;" alt="abc" dir="&#34;">text</div>');
196+
toBe('&lt; div rel=&#34;&#34; alt=abc dir=\'&#34;\' &gt;text&lt; /div&gt;');
171197
});
172198

173-
it('should handle improper html2', function() {
199+
it('should mangle improper html2', function() {
200+
// A proper HTML parser would clobber this more in most cases, but it looks reasonable.
174201
expectHTML('< div rel="</div>" / >').
175-
toEqual('<div rel="&lt;/div&gt;"/>');
202+
toBe('&lt; div rel=&#34;&#34; / &gt;');
176203
});
177204

178205
it('should ignore back slash as escape', function() {
@@ -195,6 +222,12 @@ describe('HTML', function() {
195222
expectHTML('\na\n').toEqual('&#10;a&#10;');
196223
});
197224

225+
it('should accept tag delimiters such as "<" inside real tags (with nesting)', function() {
226+
//this is an integrated version of the 'should accept tag delimiters such as "<" inside real tags' test
227+
expectHTML('<p> 10 < <span>100</span> </p>')
228+
.toEqual('<p> 10 &lt; <span>100</span> </p>');
229+
});
230+
198231
describe('htmlSanitizerWriter', function() {
199232
/* global htmlSanitizeWriter: false */
200233
if (angular.isUndefined(window.htmlSanitizeWriter)) return;

0 commit comments

Comments
 (0)