| 1 |
// Copyright 2016 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Unit Test for the unsafe API of the HTML Sanitizer. |
| 17 |
*/ |
| 18 |
|
| 19 |
goog.provide('goog.html.UnsafeTest'); |
| 20 |
goog.setTestOnly(); |
| 21 |
|
| 22 |
goog.require('goog.html.SafeHtml'); |
| 23 |
goog.require('goog.html.sanitizer.HtmlSanitizer'); |
| 24 |
goog.require('goog.html.sanitizer.TagBlacklist'); |
| 25 |
goog.require('goog.html.sanitizer.unsafe'); |
| 26 |
|
| 27 |
goog.require('goog.string.Const'); |
| 28 |
goog.require('goog.testing.dom'); |
| 29 |
goog.require('goog.testing.jsunit'); |
| 30 |
goog.require('goog.userAgent'); |
| 31 |
|
| 32 |
/** |
| 33 |
* @return {boolean} Whether the browser is IE8 or below. |
| 34 |
*/ |
| 35 |
function isIE8() { |
| 36 |
return goog.userAgent.IE && !goog.userAgent.isVersionOrHigher(9); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* @return {boolean} Whether the browser is IE9. |
| 42 |
*/ |
| 43 |
function isIE9() { |
| 44 |
return goog.userAgent.IE && !goog.userAgent.isVersionOrHigher(10) && !isIE8(); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
var just = goog.string.Const.from('test'); |
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* Sanitizes the original HTML and asserts that it is the same as the expected |
| 53 |
* HTML. Supports adding tags and attributes through the unsafe API. |
| 54 |
* @param {string} originalHtml |
| 55 |
* @param {string} expectedHtml |
| 56 |
* @param {?Array<string>=} opt_tags |
| 57 |
* @param {?Array<(string|!goog.html.sanitizer.HtmlSanitizerAttributePolicy)>=} |
| 58 |
* opt_attrs |
| 59 |
* @param {?goog.html.sanitizer.HtmlSanitizer.Builder=} opt_builder |
| 60 |
*/ |
| 61 |
function assertSanitizedHtml( |
| 62 |
originalHtml, expectedHtml, opt_tags, opt_attrs, opt_builder) { |
| 63 |
var builder = opt_builder || new goog.html.sanitizer.HtmlSanitizer.Builder(); |
| 64 |
if (opt_tags) |
| 65 |
builder = goog.html.sanitizer.unsafe.alsoAllowTags(just, builder, opt_tags); |
| 66 |
if (opt_attrs) |
| 67 |
builder = goog.html.sanitizer.unsafe.alsoAllowAttributes( |
| 68 |
just, builder, opt_attrs); |
| 69 |
var sanitizer = builder.build(); |
| 70 |
try { |
| 71 |
var sanitized = sanitizer.sanitize(originalHtml); |
| 72 |
if (isIE9()) { |
| 73 |
assertEquals('', goog.html.SafeHtml.unwrap(sanitized)); |
| 74 |
return; |
| 75 |
} |
| 76 |
goog.testing.dom.assertHtmlMatches( |
| 77 |
expectedHtml, goog.html.SafeHtml.unwrap(sanitized), |
| 78 |
true /* opt_strictAttributes */); |
| 79 |
} catch (err) { |
| 80 |
if (!isIE8()) { |
| 81 |
throw err; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
function testAllowEmptyTagList() { |
| 88 |
var input = '<sdf><aaa></aaa></sdf><b></b>'; |
| 89 |
var expected = '<span><span></span></span><b></b>'; |
| 90 |
assertSanitizedHtml(input, expected, []); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
function testAllowBlacklistedTag() { |
| 95 |
var input = '<div><script>aaa</script></div>'; |
| 96 |
var expected = '<div></div>'; |
| 97 |
assertSanitizedHtml(input, expected, ['SCriPT']); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
function testAllowUnknownTags() { |
| 102 |
var input = '<hello><bye>aaa</bye></hello><zzz></zzz>'; |
| 103 |
var expected = '<hello><span>aaa</span></hello><zzz></zzz>'; |
| 104 |
assertSanitizedHtml(input, expected, ['HElLO', 'zZZ']); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
function testAllowAlreadyWhiteListedTag() { |
| 109 |
var input = '<hello><p><zzz></zzz></p></hello>'; |
| 110 |
var expected = '<span><p><zzz></zzz></p></span>'; |
| 111 |
assertSanitizedHtml(input, expected, ['p', 'ZZZ']); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
function testAllowEmptyAttrList() { |
| 116 |
var input = '<a href="#" qwe="nope">b</a>'; |
| 117 |
var expected = '<a href="#">b</a>'; |
| 118 |
assertSanitizedHtml(input, expected, null, []); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
function testAllowUnknownAttributeSimple() { |
| 123 |
var input = '<qqq zzz="3" nnn="no"></qqq>'; |
| 124 |
var expected = '<span zzz="3"></span>'; |
| 125 |
assertSanitizedHtml(input, expected, null, ['Zzz']); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
function testAllowUnknownAttributeWildCard() { |
| 130 |
var input = '<div ab="yes" bb="no"><img ab="yep" bb="no" /></div>'; |
| 131 |
var expected = '<div ab="yes"><img ab="yep" /></div>'; |
| 132 |
assertSanitizedHtml( |
| 133 |
input, expected, null, [{tagName: '*', attributeName: 'aB'}]); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
function testAllowUnknownAttributeOnSpecificTag() { |
| 138 |
var input = '<a www="3" zzz="4">fff</a><img www="3" />'; |
| 139 |
var expected = '<a www="3">fff</a><img />'; |
| 140 |
assertSanitizedHtml( |
| 141 |
input, expected, null, [{tagName: 'a', attributeName: 'WwW'}]); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
function testAllowUnknownAttributePolicy() { |
| 146 |
var input = '<img ab="yes" /><img ab="no" />'; |
| 147 |
var expected = '<img ab="yes" /><img />'; |
| 148 |
assertSanitizedHtml(input, expected, null, [{ |
| 149 |
tagName: '*', |
| 150 |
attributeName: 'aB', |
| 151 |
policy: function(value, hints) { |
| 152 |
assertEquals(hints.attributeName, 'ab'); |
| 153 |
return value === 'yes' ? value : null; |
| 154 |
} |
| 155 |
}]); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
function testAllowOverwriteAttrPolicy() { |
| 160 |
var input = '<a href="yes"></a><a href="no"></a>'; |
| 161 |
var expected = '<a href="yes"></a><a></a>'; |
| 162 |
assertSanitizedHtml( |
| 163 |
input, expected, null, [{ |
| 164 |
tagName: 'a', |
| 165 |
attributeName: 'href', |
| 166 |
policy: function(value) { return value === 'yes' ? value : null; } |
| 167 |
}]); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
function testAllowDAttribute() { |
| 172 |
var input = '<path d="1.5 1.5 1.5 14.5 14.5 14.5 14.5 1.5"/>'; |
| 173 |
var expected = '<path d="1.5 1.5 1.5 14.5 14.5 14.5 14.5 1.5"/>'; |
| 174 |
assertSanitizedHtml( |
| 175 |
input, expected, ['path'], [{tagName: 'path', attributeName: 'd'}]); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
function testWhitelistAliasing() { |
| 180 |
var builder = new goog.html.sanitizer.HtmlSanitizer.Builder(); |
| 181 |
goog.html.sanitizer.unsafe.alsoAllowTags(just, builder, ['QqQ']); |
| 182 |
goog.html.sanitizer.unsafe.alsoAllowAttributes(just, builder, ['QqQ']); |
| 183 |
builder.build(); |
| 184 |
assertUndefined(goog.html.sanitizer.TagWhitelist['QQQ']); |
| 185 |
assertUndefined(goog.html.sanitizer.TagWhitelist['QqQ']); |
| 186 |
assertUndefined(goog.html.sanitizer.TagWhitelist['qqq']); |
| 187 |
assertUndefined(goog.html.sanitizer.AttributeWhitelist['* QQQ']); |
| 188 |
assertUndefined(goog.html.sanitizer.AttributeWhitelist['* QqQ']); |
| 189 |
assertUndefined(goog.html.sanitizer.AttributeWhitelist['* qqq']); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
function testTemplateUnsanitized() { |
| 194 |
if (!goog.html.sanitizer.HTML_SANITIZER_TEMPLATE_SUPPORTED) { |
| 195 |
return; |
| 196 |
} |
| 197 |
var input = '<template><div>a</div><script>qqq</script>' + |
| 198 |
'<template>a</template></template>'; |
| 199 |
// TODO(pelizzi): use unblockTag once it's available |
| 200 |
delete goog.html.sanitizer.TagBlacklist['TEMPLATE']; |
| 201 |
var builder = new goog.html.sanitizer.HtmlSanitizer.Builder(); |
| 202 |
goog.html.sanitizer.unsafe.keepUnsanitizedTemplateContents(just, builder); |
| 203 |
assertSanitizedHtml(input, input, ['TEMPLATE'], null, builder); |
| 204 |
goog.html.sanitizer.TagBlacklist['TEMPLATE'] = true; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
function testTemplateSanitizedUnsanitizedXSS() { |
| 209 |
if (!goog.html.sanitizer.HTML_SANITIZER_TEMPLATE_SUPPORTED) { |
| 210 |
return; |
| 211 |
} |
| 212 |
var input = '<template><p>a</p><script>aaaa;</script></template>'; |
| 213 |
var expected = '<span><p>a</p></span>'; |
| 214 |
delete goog.html.sanitizer.TagBlacklist['TEMPLATE']; |
| 215 |
var builder = new goog.html.sanitizer.HtmlSanitizer.Builder(); |
| 216 |
goog.html.sanitizer.unsafe.keepUnsanitizedTemplateContents(just, builder); |
| 217 |
assertSanitizedHtml(input, expected, null, null, builder); |
| 218 |
goog.html.sanitizer.TagBlacklist['TEMPLATE'] = true; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
function testTemplateUnsanitizedThrowsIE() { |
| 223 |
if (goog.html.sanitizer.HTML_SANITIZER_TEMPLATE_SUPPORTED) { |
| 224 |
return; |
| 225 |
} |
| 226 |
var builder = new goog.html.sanitizer.HtmlSanitizer.Builder(); |
| 227 |
assertThrows(function() { |
| 228 |
goog.html.sanitizer.unsafe.keepUnsanitizedTemplateContents(just, builder); |
| 229 |
}); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
function testAllowRelaxExistingAttributePolicyWildcard() { |
| 234 |
var input = '<a href="javascript:alert(1)"></a>'; |
| 235 |
// define a tag-specific one, takes precedence |
| 236 |
assertSanitizedHtml( |
| 237 |
input, input, null, |
| 238 |
[{tagName: 'a', attributeName: 'href', policy: goog.functions.identity}]); |
| 239 |
// overwrite the global one |
| 240 |
assertSanitizedHtml( |
| 241 |
input, input, null, |
| 242 |
[{tagName: '*', attributeName: 'href', policy: goog.functions.identity}]); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
function testAllowRelaxExistingAttributePolicySpecific() { |
| 247 |
var input = '<a target="foo"></a>'; |
| 248 |
var expected = '<a></a>'; |
| 249 |
// overwrite the global one, the specific one still has precedence |
| 250 |
assertSanitizedHtml(input, expected, null, [ |
| 251 |
{tagName: '*', attributeName: 'target', policy: goog.functions.identity} |
| 252 |
]); |
| 253 |
// overwrite the tag-specific one, this one should take precedence |
| 254 |
assertSanitizedHtml(input, input, null, [ |
| 255 |
{tagName: 'a', attributeName: 'target', policy: goog.functions.identity} |
| 256 |
]); |
| 257 |
} |