'use strict';
describe('HTML', function() {
var ua = window.navigator.userAgent;
var isChrome = /Chrome/.test(ua) && !/Edge/.test(ua);
var expectHTML;
beforeEach(module('ngSanitize'));
beforeEach(function() {
expectHTML = function(html) {
var sanitize;
inject(function($sanitize) {
sanitize = $sanitize;
});
return expect(sanitize(html));
};
});
describe('htmlParser', function() {
/* global htmlParser */
var handler, start, text, comment;
beforeEach(function() {
text = '';
start = null;
handler = {
start: function(tag, attrs) {
start = {
tag: tag,
attrs: attrs
};
// Since different browsers handle newlines differently we trim
// so that it is easier to write tests.
for (var i = 0, ii = attrs.length; i < ii; i++) {
var keyValue = attrs[i];
var key = keyValue.key;
var value = keyValue.value;
attrs[key] = value.replace(/^\s*/, '').replace(/\s*$/, '');
}
},
chars: function(text_) {
text += text_;
},
end:function(tag) {
expect(tag).toEqual(start.tag);
},
comment:function(comment_) {
comment = comment_;
}
};
// Trigger the $sanitizer provider to execute, which initializes the `htmlParser` function.
inject(function($sanitize) {});
});
it('should not parse comments', function() {
htmlParser('', handler);
expect(comment).not.toBeDefined();
});
it('should parse basic format', function() {
htmlParser('
10 < 100
', handler); expect(text).toEqual(' 10 < 100 '); }); it('should parse newlines in tags', function() { htmlParser('
').toEqual('
');
});
it('should handle self closed elements', function() {
expectHTML('a10 < 100
') .toEqual('10 < 100
'); }); it('should accept non-string arguments', function() { expectHTML(null).toBe(''); expectHTML(undefined).toBe(''); expectHTML(42).toBe('42'); expectHTML({}).toBe('[object Object]'); expectHTML([1, 2, 3]).toBe('1,2,3'); expectHTML(true).toBe('true'); expectHTML(false).toBe('false'); }); it('should strip svg elements if not enabled via provider', function() { expectHTML('') .toEqual(''); }); it('should prevent mXSS attacks', function() { expectHTML('CLICKME').toBe('CLICKME'); }); it('should strip html comments', function() { expectHTML('text1text2
') .toEqual('text1text2
'); }); describe('clobbered elements', function() { it('should throw on a form with an input named "parentNode"', function() { inject(function($sanitize) { expect(function() { $sanitize(''); }).toThrowMinErr('$sanitize', 'elclob'); expect(function() { $sanitize(''); }).toThrowMinErr('$sanitize', 'elclob'); }); }); if (!/Edge\/\d{2,}/.test(window.navigator.userAgent)) { // Skip test on Edge due to a browser bug. it('should throw on a form with an input named "nextSibling"', function() { inject(function($sanitize) { expect(function() { $sanitize(''); }).toThrowMinErr('$sanitize', 'elclob'); expect(function() { $sanitize(''); }).toThrowMinErr('$sanitize', 'elclob'); }); }); } }); // See https://github.com/cure53/DOMPurify/blob/a992d3a75031cb8bb032e5ea8399ba972bdf9a65/src/purify.js#L439-L449 it('should not allow JavaScript execution when creating inert document', inject(function($sanitize) { $sanitize(''); expect(window.xxx).toBe(undefined); delete window.xxx; })); // See https://github.com/cure53/DOMPurify/releases/tag/0.6.7 it('should not allow JavaScript hidden in badly formed HTML to get through sanitization (Firefox bug)', inject(function($sanitize) { var doc = $sanitize('