| 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 Type-safe wrappers for unsafe DOM APIs. |
| 17 |
* |
| 18 |
* This file provides type-safe wrappers for DOM APIs that can result in |
| 19 |
* cross-site scripting (XSS) vulnerabilities, if the API is supplied with |
| 20 |
* untrusted (attacker-controlled) input. Instead of plain strings, the type |
| 21 |
* safe wrappers consume values of types from the goog.html package whose |
| 22 |
* contract promises that values are safe to use in the corresponding context. |
| 23 |
* |
| 24 |
* Hence, a program that exclusively uses the wrappers in this file (i.e., whose |
| 25 |
* only reference to security-sensitive raw DOM APIs are in this file) is |
| 26 |
* guaranteed to be free of XSS due to incorrect use of such DOM APIs (modulo |
| 27 |
* correctness of code that produces values of the respective goog.html types, |
| 28 |
* and absent code that violates type safety). |
| 29 |
* |
| 30 |
* For example, assigning to an element's .innerHTML property a string that is |
| 31 |
* derived (even partially) from untrusted input typically results in an XSS |
| 32 |
* vulnerability. The type-safe wrapper goog.dom.safe.setInnerHtml consumes a |
| 33 |
* value of type goog.html.SafeHtml, whose contract states that using its values |
| 34 |
* in a HTML context will not result in XSS. Hence a program that is free of |
| 35 |
* direct assignments to any element's innerHTML property (with the exception of |
| 36 |
* the assignment to .innerHTML in this file) is guaranteed to be free of XSS |
| 37 |
* due to assignment of untrusted strings to the innerHTML property. |
| 38 |
*/ |
| 39 |
|
| 40 |
goog.provide('goog.dom.safe'); |
| 41 |
goog.provide('goog.dom.safe.InsertAdjacentHtmlPosition'); |
| 42 |
|
| 43 |
goog.require('goog.asserts'); |
| 44 |
goog.require('goog.dom.asserts'); |
| 45 |
goog.require('goog.html.SafeHtml'); |
| 46 |
goog.require('goog.html.SafeScript'); |
| 47 |
goog.require('goog.html.SafeStyle'); |
| 48 |
goog.require('goog.html.SafeUrl'); |
| 49 |
goog.require('goog.html.TrustedResourceUrl'); |
| 50 |
goog.require('goog.string'); |
| 51 |
goog.require('goog.string.Const'); |
| 52 |
|
| 53 |
|
| 54 |
/** @enum {string} */ |
| 55 |
goog.dom.safe.InsertAdjacentHtmlPosition = { |
| 56 |
AFTERBEGIN: 'afterbegin', |
| 57 |
AFTEREND: 'afterend', |
| 58 |
BEFOREBEGIN: 'beforebegin', |
| 59 |
BEFOREEND: 'beforeend' |
| 60 |
}; |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Inserts known-safe HTML into a Node, at the specified position. |
| 65 |
* @param {!Node} node The node on which to call insertAdjacentHTML. |
| 66 |
* @param {!goog.dom.safe.InsertAdjacentHtmlPosition} position Position where |
| 67 |
* to insert the HTML. |
| 68 |
* @param {!goog.html.SafeHtml} html The known-safe HTML to insert. |
| 69 |
*/ |
| 70 |
goog.dom.safe.insertAdjacentHtml = function(node, position, html) { |
| 71 |
node.insertAdjacentHTML(position, goog.html.SafeHtml.unwrap(html)); |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* Tags not allowed in goog.dom.safe.setInnerHtml. |
| 77 |
* @private @const {!Object<string, boolean>} |
| 78 |
*/ |
| 79 |
goog.dom.safe.SET_INNER_HTML_DISALLOWED_TAGS_ = { |
| 80 |
'MATH': true, |
| 81 |
'SCRIPT': true, |
| 82 |
'STYLE': true, |
| 83 |
'SVG': true, |
| 84 |
'TEMPLATE': true |
| 85 |
}; |
| 86 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* Assigns known-safe HTML to an element's innerHTML property. |
| 90 |
* @param {!Element} elem The element whose innerHTML is to be assigned to. |
| 91 |
* @param {!goog.html.SafeHtml} html The known-safe HTML to assign. |
| 92 |
* @throws {Error} If called with one of these tags: math, script, style, svg, |
| 93 |
* template. |
| 94 |
*/ |
| 95 |
goog.dom.safe.setInnerHtml = function(elem, html) { |
| 96 |
if (goog.asserts.ENABLE_ASSERTS) { |
| 97 |
var tagName = elem.tagName.toUpperCase(); |
| 98 |
if (goog.dom.safe.SET_INNER_HTML_DISALLOWED_TAGS_[tagName]) { |
| 99 |
throw new Error( |
| 100 |
'goog.dom.safe.setInnerHtml cannot be used to set content of ' + |
| 101 |
elem.tagName + '.'); |
| 102 |
} |
| 103 |
} |
| 104 |
elem.innerHTML = goog.html.SafeHtml.unwrap(html); |
| 105 |
}; |
| 106 |
|
| 107 |
|
| 108 |
/** |
| 109 |
* Assigns known-safe HTML to an element's outerHTML property. |
| 110 |
* @param {!Element} elem The element whose outerHTML is to be assigned to. |
| 111 |
* @param {!goog.html.SafeHtml} html The known-safe HTML to assign. |
| 112 |
*/ |
| 113 |
goog.dom.safe.setOuterHtml = function(elem, html) { |
| 114 |
elem.outerHTML = goog.html.SafeHtml.unwrap(html); |
| 115 |
}; |
| 116 |
|
| 117 |
|
| 118 |
/** |
| 119 |
* Safely assigns a URL a form element's action property. |
| 120 |
* |
| 121 |
* If url is of type goog.html.SafeUrl, its value is unwrapped and assigned to |
| 122 |
* anchor's href property. If url is of type string however, it is first |
| 123 |
* sanitized using goog.html.SafeUrl.sanitize. |
| 124 |
* |
| 125 |
* Example usage: |
| 126 |
* goog.dom.safe.setFormElementAction(formEl, url); |
| 127 |
* which is a safe alternative to |
| 128 |
* formEl.action = url; |
| 129 |
* The latter can result in XSS vulnerabilities if url is a |
| 130 |
* user-/attacker-controlled value. |
| 131 |
* |
| 132 |
* @param {!Element} form The form element whose action property |
| 133 |
* is to be assigned to. |
| 134 |
* @param {string|!goog.html.SafeUrl} url The URL to assign. |
| 135 |
* @see goog.html.SafeUrl#sanitize |
| 136 |
*/ |
| 137 |
goog.dom.safe.setFormElementAction = function(form, url) { |
| 138 |
/** @type {!goog.html.SafeUrl} */ |
| 139 |
var safeUrl; |
| 140 |
if (url instanceof goog.html.SafeUrl) { |
| 141 |
safeUrl = url; |
| 142 |
} else { |
| 143 |
safeUrl = goog.html.SafeUrl.sanitizeAssertUnchanged(url); |
| 144 |
} |
| 145 |
goog.dom.asserts.assertIsHTMLFormElement(form).action = |
| 146 |
goog.html.SafeUrl.unwrap(safeUrl); |
| 147 |
}; |
| 148 |
|
| 149 |
/** |
| 150 |
* Safely assigns a URL to a button element's formaction property. |
| 151 |
* |
| 152 |
* If url is of type goog.html.SafeUrl, its value is unwrapped and assigned to |
| 153 |
* anchor's href property. If url is of type string however, it is first |
| 154 |
* sanitized using goog.html.SafeUrl.sanitize. |
| 155 |
* |
| 156 |
* Example usage: |
| 157 |
* goog.dom.safe.setButtonFormAction(buttonEl, url); |
| 158 |
* which is a safe alternative to |
| 159 |
* buttonEl.action = url; |
| 160 |
* The latter can result in XSS vulnerabilities if url is a |
| 161 |
* user-/attacker-controlled value. |
| 162 |
* |
| 163 |
* @param {!Element} button The button element whose action property |
| 164 |
* is to be assigned to. |
| 165 |
* @param {string|!goog.html.SafeUrl} url The URL to assign. |
| 166 |
* @see goog.html.SafeUrl#sanitize |
| 167 |
*/ |
| 168 |
goog.dom.safe.setButtonFormAction = function(button, url) { |
| 169 |
/** @type {!goog.html.SafeUrl} */ |
| 170 |
var safeUrl; |
| 171 |
if (url instanceof goog.html.SafeUrl) { |
| 172 |
safeUrl = url; |
| 173 |
} else { |
| 174 |
safeUrl = goog.html.SafeUrl.sanitizeAssertUnchanged(url); |
| 175 |
} |
| 176 |
goog.dom.asserts.assertIsHTMLButtonElement(button).formaction = |
| 177 |
goog.html.SafeUrl.unwrap(safeUrl); |
| 178 |
}; |
| 179 |
/** |
| 180 |
* Safely assigns a URL to an input element's formaction property. |
| 181 |
* |
| 182 |
* If url is of type goog.html.SafeUrl, its value is unwrapped and assigned to |
| 183 |
* anchor's href property. If url is of type string however, it is first |
| 184 |
* sanitized using goog.html.SafeUrl.sanitize. |
| 185 |
* |
| 186 |
* Example usage: |
| 187 |
* goog.dom.safe.setInputFormAction(inputEl, url); |
| 188 |
* which is a safe alternative to |
| 189 |
* inputEl.action = url; |
| 190 |
* The latter can result in XSS vulnerabilities if url is a |
| 191 |
* user-/attacker-controlled value. |
| 192 |
* |
| 193 |
* @param {!Element} input The input element whose action property |
| 194 |
* is to be assigned to. |
| 195 |
* @param {string|!goog.html.SafeUrl} url The URL to assign. |
| 196 |
* @see goog.html.SafeUrl#sanitize |
| 197 |
*/ |
| 198 |
goog.dom.safe.setInputFormAction = function(input, url) { |
| 199 |
/** @type {!goog.html.SafeUrl} */ |
| 200 |
var safeUrl; |
| 201 |
if (url instanceof goog.html.SafeUrl) { |
| 202 |
safeUrl = url; |
| 203 |
} else { |
| 204 |
safeUrl = goog.html.SafeUrl.sanitizeAssertUnchanged(url); |
| 205 |
} |
| 206 |
goog.dom.asserts.assertIsHTMLInputElement(input).formaction = |
| 207 |
goog.html.SafeUrl.unwrap(safeUrl); |
| 208 |
}; |
| 209 |
|
| 210 |
/** |
| 211 |
* Sets the given element's style property to the contents of the provided |
| 212 |
* SafeStyle object. |
| 213 |
* @param {!Element} elem |
| 214 |
* @param {!goog.html.SafeStyle} style |
| 215 |
*/ |
| 216 |
goog.dom.safe.setStyle = function(elem, style) { |
| 217 |
elem.style.cssText = goog.html.SafeStyle.unwrap(style); |
| 218 |
}; |
| 219 |
|
| 220 |
|
| 221 |
/** |
| 222 |
* Writes known-safe HTML to a document. |
| 223 |
* @param {!Document} doc The document to be written to. |
| 224 |
* @param {!goog.html.SafeHtml} html The known-safe HTML to assign. |
| 225 |
*/ |
| 226 |
goog.dom.safe.documentWrite = function(doc, html) { |
| 227 |
doc.write(goog.html.SafeHtml.unwrap(html)); |
| 228 |
}; |
| 229 |
|
| 230 |
|
| 231 |
/** |
| 232 |
* Safely assigns a URL to an anchor element's href property. |
| 233 |
* |
| 234 |
* If url is of type goog.html.SafeUrl, its value is unwrapped and assigned to |
| 235 |
* anchor's href property. If url is of type string however, it is first |
| 236 |
* sanitized using goog.html.SafeUrl.sanitize. |
| 237 |
* |
| 238 |
* Example usage: |
| 239 |
* goog.dom.safe.setAnchorHref(anchorEl, url); |
| 240 |
* which is a safe alternative to |
| 241 |
* anchorEl.href = url; |
| 242 |
* The latter can result in XSS vulnerabilities if url is a |
| 243 |
* user-/attacker-controlled value. |
| 244 |
* |
| 245 |
* @param {!HTMLAnchorElement} anchor The anchor element whose href property |
| 246 |
* is to be assigned to. |
| 247 |
* @param {string|!goog.html.SafeUrl} url The URL to assign. |
| 248 |
* @see goog.html.SafeUrl#sanitize |
| 249 |
*/ |
| 250 |
goog.dom.safe.setAnchorHref = function(anchor, url) { |
| 251 |
goog.dom.asserts.assertIsHTMLAnchorElement(anchor); |
| 252 |
/** @type {!goog.html.SafeUrl} */ |
| 253 |
var safeUrl; |
| 254 |
if (url instanceof goog.html.SafeUrl) { |
| 255 |
safeUrl = url; |
| 256 |
} else { |
| 257 |
safeUrl = goog.html.SafeUrl.sanitizeAssertUnchanged(url); |
| 258 |
} |
| 259 |
anchor.href = goog.html.SafeUrl.unwrap(safeUrl); |
| 260 |
}; |
| 261 |
|
| 262 |
|
| 263 |
/** |
| 264 |
* Safely assigns a URL to an image element's src property. |
| 265 |
* |
| 266 |
* If url is of type goog.html.SafeUrl, its value is unwrapped and assigned to |
| 267 |
* image's src property. If url is of type string however, it is first |
| 268 |
* sanitized using goog.html.SafeUrl.sanitize. |
| 269 |
* |
| 270 |
* @param {!HTMLImageElement} imageElement The image element whose src property |
| 271 |
* is to be assigned to. |
| 272 |
* @param {string|!goog.html.SafeUrl} url The URL to assign. |
| 273 |
* @see goog.html.SafeUrl#sanitize |
| 274 |
*/ |
| 275 |
goog.dom.safe.setImageSrc = function(imageElement, url) { |
| 276 |
goog.dom.asserts.assertIsHTMLImageElement(imageElement); |
| 277 |
/** @type {!goog.html.SafeUrl} */ |
| 278 |
var safeUrl; |
| 279 |
if (url instanceof goog.html.SafeUrl) { |
| 280 |
safeUrl = url; |
| 281 |
} else { |
| 282 |
safeUrl = goog.html.SafeUrl.sanitizeAssertUnchanged(url); |
| 283 |
} |
| 284 |
imageElement.src = goog.html.SafeUrl.unwrap(safeUrl); |
| 285 |
}; |
| 286 |
|
| 287 |
/** |
| 288 |
* Safely assigns a URL to a video element's src property. |
| 289 |
* |
| 290 |
* If url is of type goog.html.SafeUrl, its value is unwrapped and assigned to |
| 291 |
* video's src property. If url is of type string however, it is first |
| 292 |
* sanitized using goog.html.SafeUrl.sanitize. |
| 293 |
* |
| 294 |
* @param {!HTMLVideoElement} videoElement The video element whose src property |
| 295 |
* is to be assigned to. |
| 296 |
* @param {string|!goog.html.SafeUrl} url The URL to assign. |
| 297 |
* @see goog.html.SafeUrl#sanitize |
| 298 |
*/ |
| 299 |
goog.dom.safe.setVideoSrc = function(videoElement, url) { |
| 300 |
goog.dom.asserts.assertIsHTMLVideoElement(videoElement); |
| 301 |
/** @type {!goog.html.SafeUrl} */ |
| 302 |
var safeUrl; |
| 303 |
if (url instanceof goog.html.SafeUrl) { |
| 304 |
safeUrl = url; |
| 305 |
} else { |
| 306 |
safeUrl = goog.html.SafeUrl.sanitizeAssertUnchanged(url); |
| 307 |
} |
| 308 |
videoElement.src = goog.html.SafeUrl.unwrap(safeUrl); |
| 309 |
}; |
| 310 |
|
| 311 |
/** |
| 312 |
* Safely assigns a URL to an embed element's src property. |
| 313 |
* |
| 314 |
* Example usage: |
| 315 |
* goog.dom.safe.setEmbedSrc(embedEl, url); |
| 316 |
* which is a safe alternative to |
| 317 |
* embedEl.src = url; |
| 318 |
* The latter can result in loading untrusted code unless it is ensured that |
| 319 |
* the URL refers to a trustworthy resource. |
| 320 |
* |
| 321 |
* @param {!HTMLEmbedElement} embed The embed element whose src property |
| 322 |
* is to be assigned to. |
| 323 |
* @param {!goog.html.TrustedResourceUrl} url The URL to assign. |
| 324 |
*/ |
| 325 |
goog.dom.safe.setEmbedSrc = function(embed, url) { |
| 326 |
goog.dom.asserts.assertIsHTMLEmbedElement(embed); |
| 327 |
embed.src = goog.html.TrustedResourceUrl.unwrap(url); |
| 328 |
}; |
| 329 |
|
| 330 |
|
| 331 |
/** |
| 332 |
* Safely assigns a URL to a frame element's src property. |
| 333 |
* |
| 334 |
* Example usage: |
| 335 |
* goog.dom.safe.setFrameSrc(frameEl, url); |
| 336 |
* which is a safe alternative to |
| 337 |
* frameEl.src = url; |
| 338 |
* The latter can result in loading untrusted code unless it is ensured that |
| 339 |
* the URL refers to a trustworthy resource. |
| 340 |
* |
| 341 |
* @param {!HTMLFrameElement} frame The frame element whose src property |
| 342 |
* is to be assigned to. |
| 343 |
* @param {!goog.html.TrustedResourceUrl} url The URL to assign. |
| 344 |
*/ |
| 345 |
goog.dom.safe.setFrameSrc = function(frame, url) { |
| 346 |
goog.dom.asserts.assertIsHTMLFrameElement(frame); |
| 347 |
frame.src = goog.html.TrustedResourceUrl.unwrap(url); |
| 348 |
}; |
| 349 |
|
| 350 |
|
| 351 |
/** |
| 352 |
* Safely assigns a URL to an iframe element's src property. |
| 353 |
* |
| 354 |
* Example usage: |
| 355 |
* goog.dom.safe.setIframeSrc(iframeEl, url); |
| 356 |
* which is a safe alternative to |
| 357 |
* iframeEl.src = url; |
| 358 |
* The latter can result in loading untrusted code unless it is ensured that |
| 359 |
* the URL refers to a trustworthy resource. |
| 360 |
* |
| 361 |
* @param {!HTMLIFrameElement} iframe The iframe element whose src property |
| 362 |
* is to be assigned to. |
| 363 |
* @param {!goog.html.TrustedResourceUrl} url The URL to assign. |
| 364 |
*/ |
| 365 |
goog.dom.safe.setIframeSrc = function(iframe, url) { |
| 366 |
goog.dom.asserts.assertIsHTMLIFrameElement(iframe); |
| 367 |
iframe.src = goog.html.TrustedResourceUrl.unwrap(url); |
| 368 |
}; |
| 369 |
|
| 370 |
|
| 371 |
/** |
| 372 |
* Safely assigns HTML to an iframe element's srcdoc property. |
| 373 |
* |
| 374 |
* Example usage: |
| 375 |
* goog.dom.safe.setIframeSrcdoc(iframeEl, safeHtml); |
| 376 |
* which is a safe alternative to |
| 377 |
* iframeEl.srcdoc = html; |
| 378 |
* The latter can result in loading untrusted code. |
| 379 |
* |
| 380 |
* @param {!HTMLIFrameElement} iframe The iframe element whose srcdoc property |
| 381 |
* is to be assigned to. |
| 382 |
* @param {!goog.html.SafeHtml} html The HTML to assign. |
| 383 |
*/ |
| 384 |
goog.dom.safe.setIframeSrcdoc = function(iframe, html) { |
| 385 |
goog.dom.asserts.assertIsHTMLIFrameElement(iframe); |
| 386 |
iframe.srcdoc = goog.html.SafeHtml.unwrap(html); |
| 387 |
}; |
| 388 |
|
| 389 |
|
| 390 |
/** |
| 391 |
* Safely sets a link element's href and rel properties. Whether or not |
| 392 |
* the URL assigned to href has to be a goog.html.TrustedResourceUrl |
| 393 |
* depends on the value of the rel property. If rel contains "stylesheet" |
| 394 |
* then a TrustedResourceUrl is required. |
| 395 |
* |
| 396 |
* Example usage: |
| 397 |
* goog.dom.safe.setLinkHrefAndRel(linkEl, url, 'stylesheet'); |
| 398 |
* which is a safe alternative to |
| 399 |
* linkEl.rel = 'stylesheet'; |
| 400 |
* linkEl.href = url; |
| 401 |
* The latter can result in loading untrusted code unless it is ensured that |
| 402 |
* the URL refers to a trustworthy resource. |
| 403 |
* |
| 404 |
* @param {!HTMLLinkElement} link The link element whose href property |
| 405 |
* is to be assigned to. |
| 406 |
* @param {string|!goog.html.SafeUrl|!goog.html.TrustedResourceUrl} url The URL |
| 407 |
* to assign to the href property. Must be a TrustedResourceUrl if the |
| 408 |
* value assigned to rel contains "stylesheet". A string value is |
| 409 |
* sanitized with goog.html.SafeUrl.sanitize. |
| 410 |
* @param {string} rel The value to assign to the rel property. |
| 411 |
* @throws {Error} if rel contains "stylesheet" and url is not a |
| 412 |
* TrustedResourceUrl |
| 413 |
* @see goog.html.SafeUrl#sanitize |
| 414 |
*/ |
| 415 |
goog.dom.safe.setLinkHrefAndRel = function(link, url, rel) { |
| 416 |
goog.dom.asserts.assertIsHTMLLinkElement(link); |
| 417 |
link.rel = rel; |
| 418 |
if (goog.string.caseInsensitiveContains(rel, 'stylesheet')) { |
| 419 |
goog.asserts.assert( |
| 420 |
url instanceof goog.html.TrustedResourceUrl, |
| 421 |
'URL must be TrustedResourceUrl because "rel" contains "stylesheet"'); |
| 422 |
link.href = goog.html.TrustedResourceUrl.unwrap(url); |
| 423 |
} else if (url instanceof goog.html.TrustedResourceUrl) { |
| 424 |
link.href = goog.html.TrustedResourceUrl.unwrap(url); |
| 425 |
} else if (url instanceof goog.html.SafeUrl) { |
| 426 |
link.href = goog.html.SafeUrl.unwrap(url); |
| 427 |
} else { // string |
| 428 |
// SafeUrl.sanitize must return legitimate SafeUrl when passed a string. |
| 429 |
link.href = |
| 430 |
goog.html.SafeUrl.sanitizeAssertUnchanged(url).getTypedStringValue(); |
| 431 |
} |
| 432 |
}; |
| 433 |
|
| 434 |
|
| 435 |
/** |
| 436 |
* Safely assigns a URL to an object element's data property. |
| 437 |
* |
| 438 |
* Example usage: |
| 439 |
* goog.dom.safe.setObjectData(objectEl, url); |
| 440 |
* which is a safe alternative to |
| 441 |
* objectEl.data = url; |
| 442 |
* The latter can result in loading untrusted code unless setit is ensured that |
| 443 |
* the URL refers to a trustworthy resource. |
| 444 |
* |
| 445 |
* @param {!HTMLObjectElement} object The object element whose data property |
| 446 |
* is to be assigned to. |
| 447 |
* @param {!goog.html.TrustedResourceUrl} url The URL to assign. |
| 448 |
*/ |
| 449 |
goog.dom.safe.setObjectData = function(object, url) { |
| 450 |
goog.dom.asserts.assertIsHTMLObjectElement(object); |
| 451 |
object.data = goog.html.TrustedResourceUrl.unwrap(url); |
| 452 |
}; |
| 453 |
|
| 454 |
|
| 455 |
/** |
| 456 |
* Safely assigns a URL to a script element's src property. |
| 457 |
* |
| 458 |
* Example usage: |
| 459 |
* goog.dom.safe.setScriptSrc(scriptEl, url); |
| 460 |
* which is a safe alternative to |
| 461 |
* scriptEl.src = url; |
| 462 |
* The latter can result in loading untrusted code unless it is ensured that |
| 463 |
* the URL refers to a trustworthy resource. |
| 464 |
* |
| 465 |
* @param {!HTMLScriptElement} script The script element whose src property |
| 466 |
* is to be assigned to. |
| 467 |
* @param {!goog.html.TrustedResourceUrl} url The URL to assign. |
| 468 |
*/ |
| 469 |
goog.dom.safe.setScriptSrc = function(script, url) { |
| 470 |
goog.dom.asserts.assertIsHTMLScriptElement(script); |
| 471 |
script.src = goog.html.TrustedResourceUrl.unwrap(url); |
| 472 |
}; |
| 473 |
|
| 474 |
|
| 475 |
/** |
| 476 |
* Safely assigns a value to a script element's content. |
| 477 |
* |
| 478 |
* Example usage: |
| 479 |
* goog.dom.safe.setScriptContent(scriptEl, content); |
| 480 |
* which is a safe alternative to |
| 481 |
* scriptEl.text = content; |
| 482 |
* The latter can result in executing untrusted code unless it is ensured that |
| 483 |
* the code is loaded from a trustworthy resource. |
| 484 |
* |
| 485 |
* @param {!HTMLScriptElement} script The script element whose content is being |
| 486 |
* set. |
| 487 |
* @param {!goog.html.SafeScript} content The content to assign. |
| 488 |
*/ |
| 489 |
goog.dom.safe.setScriptContent = function(script, content) { |
| 490 |
goog.dom.asserts.assertIsHTMLScriptElement(script); |
| 491 |
script.text = goog.html.SafeScript.unwrap(content); |
| 492 |
}; |
| 493 |
|
| 494 |
|
| 495 |
/** |
| 496 |
* Safely assigns a URL to a Location object's href property. |
| 497 |
* |
| 498 |
* If url is of type goog.html.SafeUrl, its value is unwrapped and assigned to |
| 499 |
* loc's href property. If url is of type string however, it is first sanitized |
| 500 |
* using goog.html.SafeUrl.sanitize. |
| 501 |
* |
| 502 |
* Example usage: |
| 503 |
* goog.dom.safe.setLocationHref(document.location, redirectUrl); |
| 504 |
* which is a safe alternative to |
| 505 |
* document.location.href = redirectUrl; |
| 506 |
* The latter can result in XSS vulnerabilities if redirectUrl is a |
| 507 |
* user-/attacker-controlled value. |
| 508 |
* |
| 509 |
* @param {!Location} loc The Location object whose href property is to be |
| 510 |
* assigned to. |
| 511 |
* @param {string|!goog.html.SafeUrl} url The URL to assign. |
| 512 |
* @see goog.html.SafeUrl#sanitize |
| 513 |
*/ |
| 514 |
goog.dom.safe.setLocationHref = function(loc, url) { |
| 515 |
goog.dom.asserts.assertIsLocation(loc); |
| 516 |
/** @type {!goog.html.SafeUrl} */ |
| 517 |
var safeUrl; |
| 518 |
if (url instanceof goog.html.SafeUrl) { |
| 519 |
safeUrl = url; |
| 520 |
} else { |
| 521 |
safeUrl = goog.html.SafeUrl.sanitizeAssertUnchanged(url); |
| 522 |
} |
| 523 |
loc.href = goog.html.SafeUrl.unwrap(safeUrl); |
| 524 |
}; |
| 525 |
|
| 526 |
|
| 527 |
/** |
| 528 |
* Safely replaces the URL of a Location object. |
| 529 |
* |
| 530 |
* If url is of type goog.html.SafeUrl, its value is unwrapped and |
| 531 |
* passed to Location#replace. If url is of type string however, it is |
| 532 |
* first sanitized using goog.html.SafeUrl.sanitize. |
| 533 |
* |
| 534 |
* Example usage: |
| 535 |
* goog.dom.safe.replaceHref(document.location, newUrl); |
| 536 |
* which is a safe alternative to |
| 537 |
* document.location.replace(newUrl); |
| 538 |
* The latter can result in XSS vulnerabilities if newUrl is a |
| 539 |
* user-/attacker-controlled value. |
| 540 |
* |
| 541 |
* @param {!Location} loc The Location object which is to be replaced. |
| 542 |
* @param {string|!goog.html.SafeUrl} url The URL to assign. |
| 543 |
* @see goog.html.SafeUrl#sanitize |
| 544 |
*/ |
| 545 |
goog.dom.safe.replaceLocation = function(loc, url) { |
| 546 |
goog.dom.asserts.assertIsLocation(loc); |
| 547 |
/** @type {!goog.html.SafeUrl} */ |
| 548 |
var safeUrl; |
| 549 |
if (url instanceof goog.html.SafeUrl) { |
| 550 |
safeUrl = url; |
| 551 |
} else { |
| 552 |
safeUrl = goog.html.SafeUrl.sanitizeAssertUnchanged(url); |
| 553 |
} |
| 554 |
loc.replace(goog.html.SafeUrl.unwrap(safeUrl)); |
| 555 |
}; |
| 556 |
|
| 557 |
|
| 558 |
/** |
| 559 |
* Safely opens a URL in a new window (via window.open). |
| 560 |
* |
| 561 |
* If url is of type goog.html.SafeUrl, its value is unwrapped and passed in to |
| 562 |
* window.open. If url is of type string however, it is first sanitized |
| 563 |
* using goog.html.SafeUrl.sanitize. |
| 564 |
* |
| 565 |
* Note that this function does not prevent leakages via the referer that is |
| 566 |
* sent by window.open. It is advised to only use this to open 1st party URLs. |
| 567 |
* |
| 568 |
* Example usage: |
| 569 |
* goog.dom.safe.openInWindow(url); |
| 570 |
* which is a safe alternative to |
| 571 |
* window.open(url); |
| 572 |
* The latter can result in XSS vulnerabilities if redirectUrl is a |
| 573 |
* user-/attacker-controlled value. |
| 574 |
* |
| 575 |
* @param {string|!goog.html.SafeUrl} url The URL to open. |
| 576 |
* @param {Window=} opt_openerWin Window of which to call the .open() method. |
| 577 |
* Defaults to the global window. |
| 578 |
* @param {!goog.string.Const=} opt_name Name of the window to open in. Can be |
| 579 |
* _top, etc as allowed by window.open(). |
| 580 |
* @param {string=} opt_specs Comma-separated list of specifications, same as |
| 581 |
* in window.open(). |
| 582 |
* @param {boolean=} opt_replace Whether to replace the current entry in browser |
| 583 |
* history, same as in window.open(). |
| 584 |
* @return {Window} Window the url was opened in. |
| 585 |
*/ |
| 586 |
goog.dom.safe.openInWindow = function( |
| 587 |
url, opt_openerWin, opt_name, opt_specs, opt_replace) { |
| 588 |
/** @type {!goog.html.SafeUrl} */ |
| 589 |
var safeUrl; |
| 590 |
if (url instanceof goog.html.SafeUrl) { |
| 591 |
safeUrl = url; |
| 592 |
} else { |
| 593 |
safeUrl = goog.html.SafeUrl.sanitizeAssertUnchanged(url); |
| 594 |
} |
| 595 |
var win = opt_openerWin || window; |
| 596 |
return win.open( |
| 597 |
goog.html.SafeUrl.unwrap(safeUrl), |
| 598 |
// If opt_name is undefined, simply passing that in to open() causes IE to |
| 599 |
// reuse the current window instead of opening a new one. Thus we pass '' |
| 600 |
// in instead, which according to spec opens a new window. See |
| 601 |
// https://html.spec.whatwg.org/multipage/browsers.html#dom-open . |
| 602 |
opt_name ? goog.string.Const.unwrap(opt_name) : '', opt_specs, |
| 603 |
opt_replace); |
| 604 |
}; |