| 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 |
/** @fileoverview testcases for CSS Sanitizer.*/ |
| 16 |
|
| 17 |
goog.provide('goog.html.CssSanitizerTest'); |
| 18 |
goog.setTestOnly(); |
| 19 |
|
| 20 |
goog.require('goog.array'); |
| 21 |
goog.require('goog.html.SafeStyle'); |
| 22 |
goog.require('goog.html.SafeStyleSheet'); |
| 23 |
goog.require('goog.html.SafeUrl'); |
| 24 |
goog.require('goog.html.sanitizer.CssSanitizer'); |
| 25 |
goog.require('goog.html.testing'); |
| 26 |
goog.require('goog.string'); |
| 27 |
goog.require('goog.testing.dom'); |
| 28 |
goog.require('goog.testing.jsunit'); |
| 29 |
goog.require('goog.userAgent'); |
| 30 |
goog.require('goog.userAgent.product'); |
| 31 |
goog.require('goog.userAgent.product.isVersion'); |
| 32 |
|
| 33 |
|
| 34 |
var isIE8 = goog.userAgent.IE && !goog.userAgent.isVersionOrHigher(9); |
| 35 |
|
| 36 |
|
| 37 |
var isSafari9OrOlder = |
| 38 |
goog.userAgent.product.SAFARI && !goog.userAgent.product.isVersion(10); |
| 39 |
|
| 40 |
|
| 41 |
var supportsDomParser = |
| 42 |
!goog.userAgent.IE || goog.userAgent.isVersionOrHigher(10); |
| 43 |
|
| 44 |
|
| 45 |
/** |
| 46 |
* @param {string} cssText CSS text usually associated with an inline style. |
| 47 |
* @return {!CSSStyleDeclaration} A styleSheet object. |
| 48 |
*/ |
| 49 |
function getStyleFromCssText(cssText) { |
| 50 |
var styleDecleration = document.createElement('div').style; |
| 51 |
styleDecleration.cssText = cssText || ''; |
| 52 |
return styleDecleration; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
/** |
| 57 |
* Asserts that the expected CSS text is equal to the actual CSS text. |
| 58 |
* @param {string} expectedCssText Expected CSS text. |
| 59 |
* @param {string} actualCssText Actual CSS text. |
| 60 |
*/ |
| 61 |
function assertCSSTextEquals(expectedCssText, actualCssText) { |
| 62 |
if (isIE8) { |
| 63 |
// We get a bunch of default values set in IE8 because of the way we iterate |
| 64 |
// over the CSSStyleDecleration keys. |
| 65 |
// TODO(danesh): Fix IE8 or remove this hack. It will be problematic for |
| 66 |
// tests which have an extra semi-colon in the value (even if quoted). |
| 67 |
var actualCssArray = actualCssText.split(/\s*;\s*/); |
| 68 |
var ie8StyleString = 'WIDTH: 0px; BOTTOM: 0px; HEIGHT: 0px; TOP: 0px; ' + |
| 69 |
'RIGHT: 0px; TEXT-DECORATION: none underline overline line-through; ' + |
| 70 |
'LEFT: 0px; TEXT-DECORATION: underline line-through;'; |
| 71 |
goog.array.forEach(ie8StyleString.split(/\s*;\s*/), function(ie8Css) { |
| 72 |
goog.array.remove(actualCssArray, ie8Css); |
| 73 |
}); |
| 74 |
actualCssText = actualCssArray.join('; '); |
| 75 |
} |
| 76 |
assertEquals( |
| 77 |
getStyleFromCssText(expectedCssText).cssText, |
| 78 |
getStyleFromCssText(actualCssText).cssText); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
/** |
| 83 |
* Gets sanitized inline style. |
| 84 |
* @param {string} sourceCss CSS to be sanitized. |
| 85 |
* @param {function (string, string):?goog.html.SafeUrl=} opt_urlRewrite URL |
| 86 |
* rewriter that only returns a goog.html.SafeUrl. |
| 87 |
* @return {string} Sanitized inline style. |
| 88 |
*/ |
| 89 |
function getSanitizedInlineStyle(sourceCss, opt_urlRewrite) { |
| 90 |
try { |
| 91 |
return goog.html.SafeStyle.unwrap( |
| 92 |
goog.html.sanitizer.CssSanitizer.sanitizeInlineStyle( |
| 93 |
getStyleFromCssText(sourceCss), opt_urlRewrite)) || |
| 94 |
''; |
| 95 |
} catch (err) { |
| 96 |
// IE8 doesn't like setting invalid properties. It throws an "Invalid |
| 97 |
// Argument" exception. |
| 98 |
if (!isIE8) { |
| 99 |
throw err; |
| 100 |
} |
| 101 |
return ''; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
/** |
| 107 |
* Asserts that the input CSS text is equal to the expected CSS text after |
| 108 |
* sanitization using {@link CssSanitizer.sanitizeStyleSheetString}. |
| 109 |
* @param {string} expectedCssText |
| 110 |
* @param {string} inputCssText |
| 111 |
* @param {?string=} opt_containerId |
| 112 |
* @param {function(string, string):?SafeUrl=} opt_uriRewriter |
| 113 |
*/ |
| 114 |
function assertSanitizedCssEquals( |
| 115 |
expectedCssText, inputCssText, opt_containerId, opt_uriRewriter) { |
| 116 |
assertBrowserSanitizedCssEquals( |
| 117 |
{chrome: expectedCssText}, inputCssText, opt_containerId, |
| 118 |
opt_uriRewriter); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
/** |
| 123 |
* Asserts that on each browser the input CSS text is equal to the expected CSS |
| 124 |
* text after sanitization using {@link CssSanitizer.sanitizeStyleSheetString}. |
| 125 |
* Automatically verifies that on older browsers the sanitizer returns an empty |
| 126 |
* string. |
| 127 |
* @param {!{ |
| 128 |
* chrome: string, |
| 129 |
* firefox: (string|undefined), |
| 130 |
* safari: (string|undefined), |
| 131 |
* newIE: (string|undefined), |
| 132 |
* ie10: (string|undefined)}} expectedCssTextByBrowser An object that maps |
| 133 |
* each browser to a different expected value. All browsers but chrome are |
| 134 |
* optional. If a browser is missing, the chrome expected value is used |
| 135 |
* instead. |
| 136 |
* @param {string} inputCssText |
| 137 |
* @param {?string=} opt_containerId |
| 138 |
* @param {function(string, string):?SafeUrl=} opt_uriRewriter |
| 139 |
*/ |
| 140 |
function assertBrowserSanitizedCssEquals( |
| 141 |
expectedCssTextByBrowser, inputCssText, opt_containerId, opt_uriRewriter) { |
| 142 |
var expectedCssText = undefined; |
| 143 |
if (goog.userAgent.product.CHROME) { |
| 144 |
expectedCssText = expectedCssTextByBrowser.chrome; |
| 145 |
} else if (goog.userAgent.product.FIREFOX) { |
| 146 |
expectedCssText = expectedCssTextByBrowser.firefox; |
| 147 |
} else if (goog.userAgent.product.SAFARI) { |
| 148 |
expectedCssText = expectedCssTextByBrowser.safari; |
| 149 |
} else if (goog.userAgent.product.IE && document.documentMode == 10) { |
| 150 |
expectedCssText = expectedCssTextByBrowser.ie10; |
| 151 |
console.log('ie10'); |
| 152 |
} else if ( |
| 153 |
goog.userAgent.product.IE && !goog.userAgent.isVersionOrHigher(10)) { |
| 154 |
// Don't even try with chrome as default for IE8 and IE9. |
| 155 |
expectedCssText = ''; |
| 156 |
} else if (goog.userAgent.product.IE || goog.userAgent.product.EDGE) { |
| 157 |
expectedCssText = expectedCssTextByBrowser.newIE; |
| 158 |
} else { |
| 159 |
throw new Error('Unrecognized browser, this function needs to be updated.'); |
| 160 |
} |
| 161 |
if (expectedCssText == undefined) { |
| 162 |
expectedCssText = expectedCssTextByBrowser.chrome; |
| 163 |
} |
| 164 |
assertEquals( |
| 165 |
expectedCssText, |
| 166 |
goog.html.SafeStyleSheet.unwrap( |
| 167 |
goog.html.sanitizer.CssSanitizer.sanitizeStyleSheetString( |
| 168 |
inputCssText, |
| 169 |
opt_containerId === undefined ? 'foo' : opt_containerId, |
| 170 |
opt_uriRewriter))); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
/** |
| 175 |
* Converts rules in STYLE tags into style attributes on the tags they apply to, |
| 176 |
* and returns a new string. |
| 177 |
* @param {string} html |
| 178 |
* @return {string} |
| 179 |
*/ |
| 180 |
function inlineStyleRulesString(html) { |
| 181 |
var tree = goog.html.sanitizer.CssSanitizer.safeParseHtmlAndGetInertElement( |
| 182 |
'<span>' + html + '</span>'); |
| 183 |
if (tree == null) { |
| 184 |
return ''; |
| 185 |
} |
| 186 |
goog.html.sanitizer.CssSanitizer.inlineStyleRules(tree); |
| 187 |
return tree.innerHTML; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
/** |
| 192 |
* Inlines the rules in STYLE tags in the original HTML and asserts that it is |
| 193 |
* the same as the expected HTML. |
| 194 |
* @param {string} expectedHtml |
| 195 |
* @param {string} originalHtml |
| 196 |
*/ |
| 197 |
function assertInlinedStyles(expectedHtml, originalHtml) { |
| 198 |
var inlinedHtml = inlineStyleRulesString(originalHtml); |
| 199 |
if (!supportsDomParser) { |
| 200 |
assertEquals('', inlinedHtml); |
| 201 |
return; |
| 202 |
} |
| 203 |
goog.testing.dom.assertHtmlMatches( |
| 204 |
expectedHtml, inlinedHtml, true /* opt_strictAttributes */); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
function testValidCss() { |
| 209 |
var actualCSS = 'font-family: inherit'; |
| 210 |
var expectedCSS = 'font-family: inherit'; |
| 211 |
assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS)); |
| 212 |
|
| 213 |
// .1 -> 0.1; 1.0 -> 1 |
| 214 |
actualCSS = 'padding: 1pt .1pt 1pt 1.0em'; |
| 215 |
expectedCSS = 'padding: 1pt 0.1pt 1pt 1em'; |
| 216 |
assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS)); |
| 217 |
|
| 218 |
// Negative margins are allowed. |
| 219 |
actualCSS = 'margin: -7px -.5px -23px -1.25px'; |
| 220 |
expectedCSS = 'margin: -7px -0.5px -23px -1.25px'; |
| 221 |
if (isIE8) { |
| 222 |
// IE8 doesn't like sub-pixels |
| 223 |
// https://blogs.msdn.microsoft.com/ie/2010/11/03/sub-pixel-fonts-in-ie9/ |
| 224 |
expectedCSS = expectedCSS.replace('-0.5px', '0px'); |
| 225 |
expectedCSS = expectedCSS.replace('-1.25px', '-1px'); |
| 226 |
} |
| 227 |
assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS)); |
| 228 |
|
| 229 |
actualCSS = 'quotes: "{" "}" "<" ">"'; |
| 230 |
expectedCSS = 'quotes: "{" "}" "<" ">";'; |
| 231 |
if (isSafari9OrOlder) { |
| 232 |
// We never figured out why Safari didn't work here, but it's obsolete now. |
| 233 |
expectedCSS = 'quotes: \'{\';'; |
| 234 |
} |
| 235 |
assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS)); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
function testInvalidCssRemoved() { |
| 240 |
var actualCSS; |
| 241 |
|
| 242 |
// Tests all have null results. |
| 243 |
var expectedCSS = ''; |
| 244 |
|
| 245 |
actualCSS = 'font: Arial Black,monospace,Helvetica,#88ff88'; |
| 246 |
// Hash values are not allowed so are dropped. |
| 247 |
assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS)); |
| 248 |
|
| 249 |
// Negative numbers for border not allowed. |
| 250 |
actualCSS = 'border : -7px -0.5px -23px -1.25px'; |
| 251 |
assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS)); |
| 252 |
|
| 253 |
// Negative numbers converted to empty. |
| 254 |
actualCSS = 'padding: -0 -.0 -0. -0.0 '; |
| 255 |
assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS)); |
| 256 |
|
| 257 |
// Invalid values not allowed. |
| 258 |
actualCSS = 'padding : #123 - 5 "5"'; |
| 259 |
assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS)); |
| 260 |
|
| 261 |
// Font-family does not allow quantities at all. |
| 262 |
actualCSS = 'font-family: 7 .5 23 1.25 -7 -.5 -23 -1.25 +7 +.5 +23 +1.25 ' + |
| 263 |
'7cm .5em 23.mm 1.25px -7cm -.5em -23.mm -1.25px ' + |
| 264 |
'+7cm +.5em +23.mm +1.25px 0 .0 -0+00.0 /'; |
| 265 |
assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS)); |
| 266 |
|
| 267 |
actualCSS = 'background: bogus url("foo.png") transparent'; |
| 268 |
assertCSSTextEquals( |
| 269 |
expectedCSS, |
| 270 |
getSanitizedInlineStyle(actualCSS, goog.html.SafeUrl.sanitize)); |
| 271 |
|
| 272 |
// expression(...) is not allowed for font so is rejected wholesale -- the |
| 273 |
// internal string "pwned" is not passed through. |
| 274 |
actualCSS = 'font-family: Arial Black,monospace,expression(return "pwned"),' + |
| 275 |
'Helvetica,#88ff88'; |
| 276 |
assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS)); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
function testCssBackground() { |
| 281 |
var actualCSS, expectedCSS; |
| 282 |
|
| 283 |
function proxyUrl(url) { |
| 284 |
return goog.html.testing.newSafeUrlForTest( |
| 285 |
'https://goo.gl/proxy?url=' + url); |
| 286 |
} |
| 287 |
|
| 288 |
// Don't require the URL sanitizer to protect string boundaries. |
| 289 |
actualCSS = 'background-image: url("javascript:evil(1337)")'; |
| 290 |
expectedCSS = ''; |
| 291 |
assertCSSTextEquals( |
| 292 |
expectedCSS, |
| 293 |
getSanitizedInlineStyle(actualCSS, goog.html.SafeUrl.sanitize)); |
| 294 |
|
| 295 |
actualCSS = 'background-image: url("http://goo.gl/foo.png")'; |
| 296 |
expectedCSS = |
| 297 |
'background-image: url(https://goo.gl/proxy?url=http://goo.gl/foo.png)'; |
| 298 |
assertCSSTextEquals( |
| 299 |
expectedCSS, getSanitizedInlineStyle(actualCSS, proxyUrl)); |
| 300 |
|
| 301 |
// Without any URL sanitizer. |
| 302 |
actualCSS = 'background: transparent url("Bar.png")'; |
| 303 |
var sanitizedCss = getSanitizedInlineStyle(actualCSS); |
| 304 |
assertFalse(goog.string.contains(sanitizedCss, 'background-image')); |
| 305 |
assertFalse(goog.string.contains(sanitizedCss, 'Bar.png')); |
| 306 |
} |
| 307 |
|
| 308 |
function testVendorPrefixed() { |
| 309 |
var actualCSS = '-webkit-text-stroke: 1px red'; |
| 310 |
var expectedCSS = ''; |
| 311 |
assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS)); |
| 312 |
} |
| 313 |
|
| 314 |
function testDisallowedFunction() { |
| 315 |
var actualCSS = 'border-width: calc(10px + 20px)'; |
| 316 |
var expectedCSS = ''; |
| 317 |
assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS)); |
| 318 |
} |
| 319 |
|
| 320 |
function testColor() { |
| 321 |
var colors = [ |
| 322 |
'red', 'Red', 'RED', 'Gray', 'grey', '#abc', '#123', '#ABC123', |
| 323 |
'rgb( 127, 64 , 255 )' |
| 324 |
]; |
| 325 |
var notcolors = [ |
| 326 |
// Finding words that are not X11 colors is harder than you think. |
| 327 |
'killitwithfire', 'invisible', 'expression(red=blue)', '#aa-1bb', |
| 328 |
'#expression', '#doevil' |
| 329 |
// 'rgb(0, 0, 100%)' // Invalid in all browsers |
| 330 |
// 'rgba(128,255,128,50%)', // Invalid in all browsers |
| 331 |
]; |
| 332 |
|
| 333 |
for (var i = 0; i < colors.length; ++i) { |
| 334 |
var validColorValue = 'color: ' + colors[i]; |
| 335 |
assertCSSTextEquals( |
| 336 |
validColorValue, getSanitizedInlineStyle(validColorValue)); |
| 337 |
} |
| 338 |
|
| 339 |
for (var i = 0; i < notcolors.length; ++i) { |
| 340 |
var invalidColorValue = 'color: ' + notcolors[i]; |
| 341 |
assertCSSTextEquals('', getSanitizedInlineStyle(invalidColorValue)); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
function testCustomVariablesSanitized() { |
| 347 |
var actualCSS = '\\2d-leak: leakTest; background: var(--leak);'; |
| 348 |
assertCSSTextEquals('', getSanitizedInlineStyle(actualCSS)); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
function testExpressionsPreserved() { |
| 353 |
if (isIE8) { |
| 354 |
// Disable this test as IE8 doesn't support expressions. |
| 355 |
// https://msdn.microsoft.com/en-us/library/ms537634(v=VS.85).aspx |
| 356 |
return; |
| 357 |
} |
| 358 |
|
| 359 |
var actualCSS, expectedCSS; |
| 360 |
actualCSS = 'background-image: linear-gradient(to bottom right, red, blue)'; |
| 361 |
expectedCSS = 'background-image: linear-gradient(to right bottom, red, blue)'; |
| 362 |
assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS)); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
function testMultipleInlineStyles() { |
| 367 |
var actualCSS = 'margin: 1px ; padding: 0'; |
| 368 |
var expectedCSS = 'margin: 1px; padding: 0px;'; |
| 369 |
assertCSSTextEquals(expectedCSS, getSanitizedInlineStyle(actualCSS)); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
function testSanitizeInlineStyleString() { |
| 374 |
var tests = [ |
| 375 |
{ |
| 376 |
// empty string |
| 377 |
inputCss: '', |
| 378 |
sanitizedCss: '' |
| 379 |
}, |
| 380 |
{ |
| 381 |
// one rule |
| 382 |
inputCss: 'color: red', |
| 383 |
sanitizedCss: 'color: red;' |
| 384 |
}, |
| 385 |
{ |
| 386 |
// two rules |
| 387 |
inputCss: 'color: green; padding: 10px', |
| 388 |
sanitizedCss: 'color: green; padding: 10px;' |
| 389 |
}, |
| 390 |
{ |
| 391 |
// malicious rule |
| 392 |
inputCss: 'color: expression("pwned")', |
| 393 |
sanitizedCss: '' |
| 394 |
}, |
| 395 |
{ |
| 396 |
// disallowed URL |
| 397 |
inputCss: 'background-image: url("http://example.com")', |
| 398 |
sanitizedCss: '' |
| 399 |
}, |
| 400 |
{ |
| 401 |
// disallowed URL |
| 402 |
inputCss: 'background-image: url("http://example.com")', |
| 403 |
sanitizedCss: '', |
| 404 |
uriRewriter: function(uri) { |
| 405 |
return null; |
| 406 |
} |
| 407 |
}, |
| 408 |
{ |
| 409 |
// allowed URL |
| 410 |
inputCss: 'background-image: url("http://example.com")', |
| 411 |
sanitizedCss: 'background-image: url("http://example.com");', |
| 412 |
uriRewriter: goog.html.SafeUrl.sanitize |
| 413 |
}, |
| 414 |
{ |
| 415 |
// preserves case |
| 416 |
inputCss: 'font-family: Roboto, sans-serif', |
| 417 |
sanitizedCss: 'font-family: Roboto, sans-serif' |
| 418 |
} |
| 419 |
]; |
| 420 |
|
| 421 |
for (var i = 0; i < tests.length; i++) { |
| 422 |
var test = tests[i]; |
| 423 |
|
| 424 |
var expectedOutput = test.sanitizedCss; |
| 425 |
if (goog.userAgent.IE && document.documentMode < 10) { |
| 426 |
expectedOutput = ''; |
| 427 |
} |
| 428 |
|
| 429 |
var safeStyle = goog.html.sanitizer.CssSanitizer.sanitizeInlineStyleString( |
| 430 |
test.inputCss, test.uriRewriter); |
| 431 |
var output = goog.html.SafeStyle.unwrap(safeStyle); |
| 432 |
assertCSSTextEquals(expectedOutput, output); |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
/** |
| 438 |
* @suppress {accessControls} |
| 439 |
*/ |
| 440 |
function testInertDocument() { |
| 441 |
if (!document.implementation.createHTMLDocument) { |
| 442 |
return; // skip test |
| 443 |
} |
| 444 |
|
| 445 |
window.xssFiredInertDocument = false; |
| 446 |
var doc = goog.html.sanitizer.CssSanitizer.createInertDocument_(); |
| 447 |
try { |
| 448 |
doc.write('<script> window.xssFiredInertDocument = true; </script>'); |
| 449 |
} catch (e) { |
| 450 |
// ignore |
| 451 |
} |
| 452 |
assertFalse(window.xssFiredInertDocument); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
/** |
| 457 |
* @suppress {accessControls} |
| 458 |
*/ |
| 459 |
function testInertCustomElements() { |
| 460 |
if (typeof HTMLTemplateElement != 'function' || !document.registerElement) { |
| 461 |
return; // skip test |
| 462 |
} |
| 463 |
|
| 464 |
var inertDoc = goog.html.sanitizer.CssSanitizer.createInertDocument_(); |
| 465 |
var xFooConstructor = document.registerElement('x-foo'); |
| 466 |
var xFooElem = |
| 467 |
document.implementation.createHTMLDocument('').createElement('x-foo'); |
| 468 |
assertTrue(xFooElem instanceof xFooConstructor); // sanity check |
| 469 |
|
| 470 |
var inertXFooElem = inertDoc.createElement('x-foo'); |
| 471 |
assertFalse(inertXFooElem instanceof xFooConstructor); |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
function testSanitizeStyleSheetString_basic() { |
| 476 |
var input = ''; |
| 477 |
assertSanitizedCssEquals(input, input); |
| 478 |
|
| 479 |
input = 'a {color: red}'; |
| 480 |
var expected = '#foo a{color: red;}'; |
| 481 |
assertSanitizedCssEquals(expected, input); |
| 482 |
|
| 483 |
input = 'a {color: red} b {color:red; not-allowed: 1; ' + |
| 484 |
'background-image: url(\'http://not.allowed\');}'; |
| 485 |
expected = '#foo a{color: red;}#foo b{color: red;}'; |
| 486 |
assertSanitizedCssEquals(expected, input); |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
function testSanitizeInlineStyleString_noSelector() { |
| 491 |
var input = 'a{color: red;}'; |
| 492 |
assertSanitizedCssEquals(input, input, null /* opt_containerId */); |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
function testSanitizeStyleSheetString_comma() { |
| 497 |
var input = 'a, b, c > d {color: red}'; |
| 498 |
var expected = '#foo a,#foo b,#foo c > d{color: red;}'; |
| 499 |
assertSanitizedCssEquals(expected, input); |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
function testSanitizeStyleSheetString_atRule() { |
| 504 |
var input = '@media screen { a { color: red; } }'; |
| 505 |
var expected = ''; |
| 506 |
assertSanitizedCssEquals(expected, input); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
function testSanitizeStyleSheetString_urlRewrite() { |
| 511 |
var urlRewriter = function(url) { |
| 512 |
if (input.indexOf('bar') > -1) { |
| 513 |
return goog.html.SafeUrl.sanitize(url); |
| 514 |
} else { |
| 515 |
return null; |
| 516 |
} |
| 517 |
}; |
| 518 |
|
| 519 |
var input = 'a {background-image: url("http://bar.com")}'; |
| 520 |
var quoted = '#foo a{background-image: url("http://bar.com");}'; |
| 521 |
// Safari will strip quotes if they are not needed and add a slash. |
| 522 |
var unquoted = '#foo a{background-image: url(http://bar.com/);}'; |
| 523 |
assertBrowserSanitizedCssEquals( |
| 524 |
{safari: unquoted, chrome: quoted}, input, |
| 525 |
undefined /* opt_containerId */, urlRewriter); |
| 526 |
|
| 527 |
input = 'a {background-image: url("http://nope.com")}'; |
| 528 |
var expected = '#foo a{}'; |
| 529 |
assertSanitizedCssEquals( |
| 530 |
expected, input, undefined /* opt_containerId */, urlRewriter); |
| 531 |
|
| 532 |
input = 'a{background-image: url("javascript:alert(\"bar\")")}'; |
| 533 |
expected = '#foo a{}'; |
| 534 |
assertSanitizedCssEquals( |
| 535 |
expected, input, undefined /* opt_containerId */, urlRewriter); |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
function testSanitizeStyleSheetString_unrecognized() { |
| 540 |
var input = 'a {mso-font-size: 2px; color: black}'; |
| 541 |
var expected = 'a{color: black;}'; |
| 542 |
assertSanitizedCssEquals(expected, input, null /* opt_containerId */); |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
function testSanitizeStyleSheetString_malformed() { |
| 547 |
var input = '<script>alert(1)</script>'; |
| 548 |
var expected = ''; |
| 549 |
assertSanitizedCssEquals(expected, input); |
| 550 |
|
| 551 |
input = 'a { } </style><script>alert(1)</script><style>'; |
| 552 |
expected = '#foo a{}'; |
| 553 |
assertSanitizedCssEquals(expected, input); |
| 554 |
|
| 555 |
input = 'a < b { } a { font-size: 10px }'; |
| 556 |
expected = '#foo a{font-size: 10px;}'; |
| 557 |
assertSanitizedCssEquals(expected, input); |
| 558 |
|
| 559 |
input = 'a {;;;} a { font-size: 10px } ;;; a { background-image: url(() }};;'; |
| 560 |
expected = '#foo a{}#foo a{font-size: 10px;}'; |
| 561 |
assertSanitizedCssEquals(expected, input); |
| 562 |
|
| 563 |
input = 'a[a=\"ccc] { color: red;}'; |
| 564 |
expected = ''; |
| 565 |
assertSanitizedCssEquals(expected, input); |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
function testSanitizeStyleSheetString_stringWithNoEscapedQuotesInSelector() { |
| 570 |
var input = 'a[data-foo="foo,bar"], b { color: red }'; |
| 571 |
// IE converts the string to single quotes. |
| 572 |
var doubleQuoted = '#foo a[data-foo="foo,bar"],#foo b{color: red;}'; |
| 573 |
var singleQuoted = '#foo a[data-foo=\'foo,bar\'],#foo b{color: red;}'; |
| 574 |
assertBrowserSanitizedCssEquals( |
| 575 |
{chrome: doubleQuoted, newIE: singleQuoted, ie10: singleQuoted}, input); |
| 576 |
|
| 577 |
input = 'a[data-foo=\'foo,bar\'], b { color: red }'; |
| 578 |
// Chrome converts the string to double quotes. |
| 579 |
doubleQuoted = '#foo a[data-foo="foo,bar"],#foo b{color: red;}'; |
| 580 |
singleQuoted = '#foo a[data-foo=\'foo,bar\'],#foo b{color: red;}'; |
| 581 |
assertBrowserSanitizedCssEquals( |
| 582 |
{chrome: doubleQuoted, newIE: singleQuoted, ie10: singleQuoted}, input); |
| 583 |
|
| 584 |
input = 'a[foo="foo,bar"][bar="baz"], b { color: blue }'; |
| 585 |
doubleQuoted = '#foo a[foo="foo,bar"][bar="baz"],#foo b{color: blue;}'; |
| 586 |
singleQuoted = '#foo a[foo=\'foo,bar\'][bar=\'baz\'],#foo b{color: blue;}'; |
| 587 |
assertBrowserSanitizedCssEquals( |
| 588 |
{chrome: doubleQuoted, newIE: singleQuoted, ie10: singleQuoted}, input); |
| 589 |
|
| 590 |
input = 'a[foo="foo,bar"], b, c[foo="f,b"][bar="f,b"] { color: red }'; |
| 591 |
doubleQuoted = '#foo a[foo="foo,bar"],#foo b,#foo c[foo="f,b"][bar="f,b"]' + |
| 592 |
'{color: red;}'; |
| 593 |
singleQuoted = |
| 594 |
'#foo a[foo=\'foo,bar\'],#foo b,#foo c[bar=\'f,b\'][foo=\'f,b\']' + |
| 595 |
'{color: red;}'; |
| 596 |
assertBrowserSanitizedCssEquals( |
| 597 |
{chrome: doubleQuoted, newIE: singleQuoted, ie10: singleQuoted}, input); |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
function testSanitizeStyleSheetString_stringWithEscapedQuotesInSelector() { |
| 602 |
// Contains an escaped string, but the selector is converted to a[a='a"b'] |
| 603 |
// before the regex is executed. |
| 604 |
var input = 'a[a="a\\"b"] { color: black; }'; |
| 605 |
var doubleQuoted = '#foo a[a="a\\"b"]{color: black;}'; |
| 606 |
var singleQuoted = '#foo a[a=\'a"b\']{color: black;}'; |
| 607 |
assertBrowserSanitizedCssEquals( |
| 608 |
{chrome: doubleQuoted, ie10: singleQuoted, newIE: singleQuoted}, input); |
| 609 |
|
| 610 |
input = 'a[a="a\\\'b"] { color: grey; }'; |
| 611 |
doubleQuoted = '#foo a[a="a\'b"]{color: grey;}'; |
| 612 |
singleQuoted = '#foo a[a=\'a\\\'b\']{color: grey;}'; |
| 613 |
assertBrowserSanitizedCssEquals( |
| 614 |
{ |
| 615 |
chrome: doubleQuoted, |
| 616 |
firefox: doubleQuoted, |
| 617 |
ie10: '', |
| 618 |
newIE: singleQuoted |
| 619 |
}, |
| 620 |
input); |
| 621 |
|
| 622 |
input = 'a[a=\'\\\'b\'] {color: red; }'; |
| 623 |
doubleQuoted = '#foo a[a="\'b"]{color: red;}'; |
| 624 |
singleQuoted = '#foo a[a=\'\\\'b\']{color: red;}'; |
| 625 |
assertBrowserSanitizedCssEquals( |
| 626 |
{ |
| 627 |
chrome: doubleQuoted, |
| 628 |
firefox: doubleQuoted, |
| 629 |
newIE: singleQuoted, |
| 630 |
ie10: '' |
| 631 |
}, |
| 632 |
input); |
| 633 |
|
| 634 |
input = 'a[foo=\'b\\\'a, ,\'] { color: blue; }'; |
| 635 |
doubleQuoted = '#foo a[foo="b\'a, ,"]{color: blue;}'; |
| 636 |
singleQuoted = '#foo a[foo=\'b\\\'a, ,\']{color: blue;}'; |
| 637 |
assertBrowserSanitizedCssEquals( |
| 638 |
{ |
| 639 |
chrome: doubleQuoted, |
| 640 |
firefox: doubleQuoted, |
| 641 |
newIE: singleQuoted, |
| 642 |
ie10: '' |
| 643 |
}, |
| 644 |
input); |
| 645 |
|
| 646 |
input = 'a[a=\'a\\"b\'] { color: black; }'; |
| 647 |
doubleQuoted = '#foo a[a="a\\"b"]{color: black;}'; |
| 648 |
singleQuoted = '#foo a[a=\'a"b\']{color: black;}'; |
| 649 |
assertBrowserSanitizedCssEquals( |
| 650 |
{chrome: doubleQuoted, ie10: singleQuoted, newIE: singleQuoted}, input); |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
function testSanitizeInlineStyleString_invalidSelector() { |
| 655 |
var input = 'a{}'; |
| 656 |
if (supportsDomParser) { |
| 657 |
assertThrows(function() { |
| 658 |
goog.html.sanitizer.CssSanitizer.sanitizeStyleSheetString( |
| 659 |
input, '</style>'); |
| 660 |
}); |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
function testInlineStyleRules_empty() { |
| 666 |
assertInlinedStyles('', ''); |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
function testInlineStyleRules_basic() { |
| 671 |
var input = '<style>a{color:red}</style><a>foo</a>'; |
| 672 |
var expected = '<a style="color:red;">foo</a>'; |
| 673 |
assertInlinedStyles(expected, input); |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
function testInlineStyleRules_onlyStyle() { |
| 678 |
var input = '<style>a{color:red}</style>'; |
| 679 |
assertInlinedStyles('', input); |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
function testInlineStyleRules_noStyle() { |
| 684 |
var input = '<a>hi</a>'; |
| 685 |
assertInlinedStyles(input, input); |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
function testInlineStyleRules_onlyText() { |
| 690 |
var input = 'hello'; |
| 691 |
assertInlinedStyles(input, input); |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
function testInlineStyleRules_specificity() { |
| 696 |
var input = '<style>a{color: red; border: 1px}' + |
| 697 |
'#foo{color: white; border: 2px}</style>' + |
| 698 |
'<a id="foo" style="color: black">foo</a>'; |
| 699 |
var expected = '<a id="foo" style="color: black; border: 2px">foo</a>'; |
| 700 |
assertInlinedStyles(expected, input); |
| 701 |
} |
| 702 |
|
| 703 |
|
| 704 |
function testInlineStyleRules_media() { |
| 705 |
var input = '<style>a{color: red;} @media screen { border: 1px; }</style>' + |
| 706 |
'<a id="foo">foo</a>'; |
| 707 |
var expected = '<a id="foo" style="color: red;">foo</a>'; |
| 708 |
assertInlinedStyles(expected, input); |
| 709 |
} |