// Copyright 2016 The Closure Library Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS-IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * @fileoverview Unit tests for HTML Sanitizer */ goog.provide('goog.html.HtmlSanitizerTest'); goog.setTestOnly(); goog.require('goog.array'); goog.require('goog.dom'); goog.require('goog.functions'); goog.require('goog.html.SafeHtml'); goog.require('goog.html.SafeUrl'); goog.require('goog.html.sanitizer.HtmlSanitizer'); goog.require('goog.html.sanitizer.HtmlSanitizer.Builder'); goog.require('goog.html.sanitizer.TagBlacklist'); goog.require('goog.html.sanitizer.TagWhitelist'); goog.require('goog.html.sanitizer.unsafe'); goog.require('goog.html.testing'); goog.require('goog.object'); goog.require('goog.string.Const'); goog.require('goog.testing.dom'); goog.require('goog.testing.jsunit'); goog.require('goog.userAgent'); goog.require('goog.userAgent.product'); var isIE8 = goog.userAgent.IE && !goog.userAgent.isVersionOrHigher(9); var isIE9 = goog.userAgent.IE && !goog.userAgent.isVersionOrHigher(10) && !isIE8; var justification = goog.string.Const.from('test'); /** * Sanitizes the original HTML and asserts that it is the same as the expected * HTML. If present the config is passed through to the sanitizer. Supports * approximate matching using a RegExp. * @param {string} originalHtml * @param {string|!RegExp} expectedHtml * @param {?goog.html.sanitizer.HtmlSanitizer=} opt_sanitizer */ function assertSanitizedHtml(originalHtml, expectedHtml, opt_sanitizer) { var sanitizer = opt_sanitizer || new goog.html.sanitizer.HtmlSanitizer.Builder().build(); try { var sanitized = goog.html.SafeHtml.unwrap(sanitizer.sanitize(originalHtml)); if (isIE9) { assertEquals('', sanitized); return; } if (typeof expectedHtml == 'string') { goog.testing.dom.assertHtmlMatches( expectedHtml, sanitized, true /* opt_strictAttributes */); } else { assertRegExp(expectedHtml, sanitized); } } catch (err) { if (!isIE8) { throw err; } } if (!opt_sanitizer) { // Retry with raw sanitizer created without the builder. assertSanitizedHtml( originalHtml, expectedHtml, new goog.html.sanitizer.HtmlSanitizer()); // Retry with an explicitly passed in Builder. var builder = new goog.html.sanitizer.HtmlSanitizer.Builder(); assertSanitizedHtml( originalHtml, expectedHtml, new goog.html.sanitizer.HtmlSanitizer(builder)); } } /** * @param {!goog.html.SafeHtml} safeHtml Sanitized HTML which contains a style. * @return {string} cssText contained within SafeHtml. */ function getStyle(safeHtml) { var tmpElement = goog.dom.safeHtmlToNode(safeHtml); return tmpElement.style ? tmpElement.style.cssText : ''; } /** * Shorthand for sanitized tags * @param {string} tag * @return {string} */ function otag(tag) { return 'data-sanitizer-original-tag="' + tag + '"'; } /** * Sanitize content, let the browser apply its own HTML tree correction by * attaching the content to the document, and then assert it matches the * expected value. * @param {string} expected * @param {string} input */ function assertAfterInsertionEquals(expected, input) { var sanitizer = new Builder().allowFormTag().allowStyleTag().withStyleContainer().build(); input = SafeHtml.unwrap(sanitizer.sanitize(input)); var div = document.createElement('div'); document.body.appendChild(div); div.innerHTML = input; googTestingDom.assertHtmlMatches( expected, div.innerHTML, true /* opt_strictAttributes */); div.parentNode.removeChild(div); } function testHtmlSanitizeSafeHtml() { var html; html = 'hello world'; assertSanitizedHtml(html, html); html = 'hello world'; assertSanitizedHtml(html, html); html = 'hello world'; assertSanitizedHtml(html, html); html = 'hello world'; assertSanitizedHtml(html, html); // NOTE(user): original did not have tbody html = '
| hello world |
a
a
' + 'a
'; // TODO(pelizzi): use unblockTag once it's available delete goog.html.sanitizer.TagBlacklist['TEMPLATE']; var builder = new goog.html.sanitizer.HtmlSanitizer.Builder(); goog.html.sanitizer.unsafe.alsoAllowTags( justification, builder, ['TEMPLATE']); assertSanitizedHtml(input, expected, builder.build()); goog.html.sanitizer.TagBlacklist['TEMPLATE'] = true; } function testOnlyAllowEmptyAttrList() { var input = 'b
' + 'c'; var expected = 'b
c'; assertSanitizedHtml( input, expected, new goog.html.sanitizer.HtmlSanitizer.Builder() .onlyAllowAttributes([]) .build()); } function testOnlyAllowUnWhitelistedAttr() { assertThrows(function() { new goog.html.sanitizer.HtmlSanitizer.Builder().onlyAllowAttributes( ['alt', 'zzz']); }); } function testOnlyAllowAttributeWildCard() { var input = '