| 1 |
// Copyright 2013 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 tests for goog.html.SafeUrl and its builders. |
| 17 |
*/ |
| 18 |
|
| 19 |
goog.provide('goog.html.safeUrlTest'); |
| 20 |
|
| 21 |
goog.require('goog.html.SafeUrl'); |
| 22 |
goog.require('goog.html.TrustedResourceUrl'); |
| 23 |
goog.require('goog.html.safeUrlTestVectors'); |
| 24 |
goog.require('goog.i18n.bidi.Dir'); |
| 25 |
goog.require('goog.object'); |
| 26 |
goog.require('goog.string.Const'); |
| 27 |
goog.require('goog.testing.jsunit'); |
| 28 |
goog.require('goog.userAgent'); |
| 29 |
|
| 30 |
goog.setTestOnly('goog.html.safeUrlTest'); |
| 31 |
|
| 32 |
|
| 33 |
function testSafeUrl() { |
| 34 |
var safeUrl = goog.html.SafeUrl.fromConstant( |
| 35 |
goog.string.Const.from('javascript:trusted();')); |
| 36 |
var extracted = goog.html.SafeUrl.unwrap(safeUrl); |
| 37 |
assertEquals('javascript:trusted();', extracted); |
| 38 |
assertEquals('javascript:trusted();', goog.html.SafeUrl.unwrap(safeUrl)); |
| 39 |
assertEquals('SafeUrl{javascript:trusted();}', String(safeUrl)); |
| 40 |
|
| 41 |
// URLs are always LTR. |
| 42 |
assertEquals(goog.i18n.bidi.Dir.LTR, safeUrl.getDirection()); |
| 43 |
|
| 44 |
// Interface markers are present. |
| 45 |
assertTrue(safeUrl.implementsGoogStringTypedString); |
| 46 |
assertTrue(safeUrl.implementsGoogI18nBidiDirectionalString); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
function testSafeUrlFromBlob_withSafeType() { |
| 51 |
if (isIE9OrLower()) { |
| 52 |
return; |
| 53 |
} |
| 54 |
assertBlobTypeIsSafe('audio/ogg', true); |
| 55 |
assertBlobTypeIsSafe('image/png', true); |
| 56 |
assertBlobTypeIsSafe('iMage/pNg', true); |
| 57 |
assertBlobTypeIsSafe('video/mpeg', true); |
| 58 |
assertBlobTypeIsSafe('video/ogg', true); |
| 59 |
assertBlobTypeIsSafe('video/mp4', true); |
| 60 |
assertBlobTypeIsSafe('video/ogg', true); |
| 61 |
assertBlobTypeIsSafe('video/webm', true); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
function testSafeUrlFromBlob_withUnsafeType() { |
| 66 |
if (isIE9OrLower()) { |
| 67 |
return; |
| 68 |
} |
| 69 |
assertBlobTypeIsSafe('', false); |
| 70 |
assertBlobTypeIsSafe('ximage/png', false); |
| 71 |
assertBlobTypeIsSafe('image/pngx', false); |
| 72 |
assertBlobTypeIsSafe('video/whatever', false); |
| 73 |
assertBlobTypeIsSafe('video/', false); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
/** @return {boolean} True if running on IE9 or lower. */ |
| 78 |
function isIE9OrLower() { |
| 79 |
return goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('10'); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
/** |
| 84 |
* Tests creating a SafeUrl from a blob with the given MIME type, asserting |
| 85 |
* whether or not the SafeUrl returned is innocuous or not depending on the |
| 86 |
* given boolean. |
| 87 |
* @param {string} type MIME type to test |
| 88 |
* @param {boolean} isSafe Whether the given MIME type should be considered safe |
| 89 |
* by {@link SafeUrl.fromBlob}. |
| 90 |
*/ |
| 91 |
function assertBlobTypeIsSafe(type, isSafe) { |
| 92 |
var safeUrl = goog.html.SafeUrl.fromBlob(new Blob(['test'], {type: type})); |
| 93 |
var extracted = goog.html.SafeUrl.unwrap(safeUrl); |
| 94 |
if (isSafe) { |
| 95 |
assertEquals('blob:', extracted.substring(0, 5)); |
| 96 |
} else { |
| 97 |
assertEquals(goog.html.SafeUrl.INNOCUOUS_STRING, extracted); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
function testSafeUrlSanitize_sanitizeTelUrl() { |
| 103 |
var vectors = goog.html.safeUrlTestVectors.TEL_VECTORS; |
| 104 |
for (var i = 0; i < vectors.length; ++i) { |
| 105 |
var v = vectors[i]; |
| 106 |
var observed = goog.html.SafeUrl.fromTelUrl(v.input); |
| 107 |
assertEquals(v.expected, goog.html.SafeUrl.unwrap(observed)); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
function testFromTrustedResourceUrl() { |
| 113 |
var url = goog.string.Const.from('test'); |
| 114 |
var trustedResourceUrl = goog.html.TrustedResourceUrl.fromConstant(url); |
| 115 |
var safeUrl = goog.html.SafeUrl.fromTrustedResourceUrl(trustedResourceUrl); |
| 116 |
assertEquals( |
| 117 |
goog.string.Const.unwrap(url), goog.html.SafeUrl.unwrap(safeUrl)); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
/** @suppress {checkTypes} */ |
| 122 |
function testUnwrap() { |
| 123 |
var privateFieldName = 'privateDoNotAccessOrElseSafeHtmlWrappedValue_'; |
| 124 |
var markerFieldName = 'SAFE_URL_TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_'; |
| 125 |
var propNames = goog.object.getKeys(goog.html.SafeUrl.sanitize('')); |
| 126 |
assertContains(privateFieldName, propNames); |
| 127 |
assertContains(markerFieldName, propNames); |
| 128 |
var evil = {}; |
| 129 |
evil[privateFieldName] = 'javascript:evil()'; |
| 130 |
evil[markerFieldName] = {}; |
| 131 |
|
| 132 |
var exception = assertThrows(function() { goog.html.SafeUrl.unwrap(evil); }); |
| 133 |
assertContains('expected object of type SafeUrl', exception.message); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
function testSafeUrlSanitize_sanitizeUrl() { |
| 138 |
var vectors = goog.html.safeUrlTestVectors.BASE_VECTORS; |
| 139 |
for (var i = 0; i < vectors.length; ++i) { |
| 140 |
var v = vectors[i]; |
| 141 |
if (v.input.match(/^data:/i)) { |
| 142 |
var observed = goog.html.SafeUrl.fromDataUrl(v.input); |
| 143 |
assertEquals(v.expected, goog.html.SafeUrl.unwrap(observed)); |
| 144 |
} else { |
| 145 |
var observed = goog.html.SafeUrl.sanitize(v.input); |
| 146 |
assertEquals(v.expected, goog.html.SafeUrl.unwrap(observed)); |
| 147 |
if (v.safe) { |
| 148 |
var asserted = goog.html.SafeUrl.sanitizeAssertUnchanged(v.input); |
| 149 |
assertEquals(v.expected, goog.html.SafeUrl.unwrap(asserted)); |
| 150 |
} else { |
| 151 |
assertThrows(function() { |
| 152 |
goog.html.SafeUrl.sanitizeAssertUnchanged(v.input); |
| 153 |
}); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
function testSafeUrlSanitize_sanitizeProgramConstants() { |
| 161 |
// .sanitize() works on program constants. |
| 162 |
var good = goog.string.Const.from('http://example.com/'); |
| 163 |
var goodOutput = goog.html.SafeUrl.sanitize(good); |
| 164 |
assertEquals('http://example.com/', goog.html.SafeUrl.unwrap(goodOutput)); |
| 165 |
var asserted = goog.html.SafeUrl.sanitizeAssertUnchanged(good); |
| 166 |
assertEquals('http://example.com/', goog.html.SafeUrl.unwrap(asserted)); |
| 167 |
|
| 168 |
// .sanitize() does not exempt values known to be program constants. |
| 169 |
var bad = goog.string.Const.from('data:blah'); |
| 170 |
var badOutput = goog.html.SafeUrl.sanitize(bad); |
| 171 |
assertEquals('about:invalid#zClosurez', goog.html.SafeUrl.unwrap(badOutput)); |
| 172 |
assertThrows(function() { |
| 173 |
goog.html.SafeUrl.sanitizeAssertUnchanged(bad); |
| 174 |
}); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
function testSafeUrlSanitize_idempotentForSafeUrlArgument() { |
| 179 |
// This matches the safe prefix. |
| 180 |
var safeUrl = goog.html.SafeUrl.sanitize('https://www.google.com/'); |
| 181 |
var safeUrl2 = goog.html.SafeUrl.sanitize(safeUrl); |
| 182 |
assertEquals( |
| 183 |
goog.html.SafeUrl.unwrap(safeUrl), goog.html.SafeUrl.unwrap(safeUrl2)); |
| 184 |
|
| 185 |
// This doesn't match the safe prefix, getting converted into an innocuous |
| 186 |
// string. |
| 187 |
safeUrl = goog.html.SafeUrl.sanitize('disallowed:foo'); |
| 188 |
safeUrl2 = goog.html.SafeUrl.sanitize(safeUrl); |
| 189 |
assertEquals( |
| 190 |
goog.html.SafeUrl.unwrap(safeUrl), goog.html.SafeUrl.unwrap(safeUrl2)); |
| 191 |
} |