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

Commit e66c23f

Browse files
paolodmpetebacondarwin
authored andcommitted
fix($sanitize): sanitize DOCTYPE declarations correctly
HTML to be sanitized that contains a DOCTYPE declaration were causing the HTML parser to throw an error. Now the parser correctly removes the declarations when sanitizing HTML. Closes #3931
1 parent e36e28e commit e66c23f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/ngSanitize/sanitize.js

+8
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ var START_TAG_REGEXP = /^<\s*([\w:-]+)((?:\s+[\w:-]+(?:\s*=\s*(?:(?:"[^"]*")|(?:
135135
BEGIN_TAG_REGEXP = /^</,
136136
BEGING_END_TAGE_REGEXP = /^<\s*\//,
137137
COMMENT_REGEXP = /<!--(.*?)-->/g,
138+
DOCTYPE_REGEXP = /<!DOCTYPE([^>]*?)>/i,
138139
CDATA_REGEXP = /<!\[CDATA\[(.*?)]]>/g,
139140
URI_REGEXP = /^((ftp|https?):\/\/|mailto:|tel:|#)/i,
140141
NON_ALPHANUMERIC_REGEXP = /([^\#-~| |!])/g; // Match everything outside of normal chars and " (quote character)
@@ -218,7 +219,14 @@ function htmlParser( html, handler ) {
218219
html = html.substring( index + 3 );
219220
chars = false;
220221
}
222+
// DOCTYPE
223+
} else if ( DOCTYPE_REGEXP.test(html) ) {
224+
match = html.match( DOCTYPE_REGEXP );
221225

226+
if ( match ) {
227+
html = html.replace( match[0] , '');
228+
chars = false;
229+
}
222230
// end tag
223231
} else if ( BEGING_END_TAGE_REGEXP.test(html) ) {
224232
match = html.match( END_TAG_REGEXP );

test/ngSanitize/sanitizeSpec.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('HTML', function() {
2424
attrs: attrs,
2525
unary: unary
2626
};
27-
// Since different browsers handle newlines differenttly we trim
27+
// Since different browsers handle newlines differently we trim
2828
// so that it is easier to write tests.
2929
angular.forEach(attrs, function(value, key) {
3030
attrs[key] = value.replace(/^\s*/, '').replace(/\s*$/, '')
@@ -112,6 +112,13 @@ describe('HTML', function() {
112112
expectHTML('a<SCRIPT>evil< / scrIpt >c.').toEqual('ac.');
113113
});
114114

115+
it('should remove DOCTYPE header', function() {
116+
expectHTML('<!DOCTYPE html>').toEqual('');
117+
expectHTML('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"\n"http://www.w3.org/TR/html4/strict.dtd">').toEqual('');
118+
expectHTML('a<!DOCTYPE html>c.').toEqual('ac.');
119+
expectHTML('a<!DocTyPe html>c.').toEqual('ac.');
120+
});
121+
115122
it('should remove nested script', function() {
116123
expectHTML('a< SCRIPT >A< SCRIPT >evil< / scrIpt >B< / scrIpt >c.').toEqual('ac.');
117124
});
@@ -320,5 +327,6 @@ describe('HTML', function() {
320327
});
321328
});
322329

330+
323331
});
324332
});

0 commit comments

Comments
 (0)