| 1 |
// Copyright 2017 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 Utility DOM functions resistant to DOM clobbering. |
| 17 |
* @supported Unless specified in the method documentation, IE 10 and newer. |
| 18 |
*/ |
| 19 |
|
| 20 |
goog.module('goog.html.sanitizer.noclobber'); |
| 21 |
goog.module.declareLegacyNamespace(); |
| 22 |
|
| 23 |
var NodeType = goog.require('goog.dom.NodeType'); |
| 24 |
var googArray = goog.require('goog.array'); |
| 25 |
var googAsserts = goog.require('goog.asserts'); |
| 26 |
var userAgent = goog.require('goog.userAgent'); |
| 27 |
|
| 28 |
/** @const {boolean} */ |
| 29 |
var hasPrototypes = !userAgent.IE || document.documentMode >= 10; |
| 30 |
|
| 31 |
// Functions we use to avoid looking up the prototypes and the descriptors |
| 32 |
// multiple times. |
| 33 |
/** @const @enum {?Function} */ |
| 34 |
var Methods = { |
| 35 |
ATTRIBUTES_GETTER: hasPrototypes ? |
| 36 |
googAsserts.assert( |
| 37 |
(Object.getOwnPropertyDescriptor(Element.prototype, 'attributes') || |
| 38 |
// Edge and IE define this Element property on Node instead of |
| 39 |
// Element. |
| 40 |
Object.getOwnPropertyDescriptor(Node.prototype, 'attributes')) |
| 41 |
.get) : |
| 42 |
null, |
| 43 |
HAS_ATTRIBUTE: hasPrototypes ? Element.prototype.hasAttribute : null, |
| 44 |
GET_ATTRIBUTE: hasPrototypes ? Element.prototype.getAttribute : null, |
| 45 |
SET_ATTRIBUTE: hasPrototypes ? Element.prototype.setAttribute : null, |
| 46 |
REMOVE_ATTRIBUTE: hasPrototypes ? Element.prototype.removeAttribute : null, |
| 47 |
INNER_HTML_GETTER: hasPrototypes ? |
| 48 |
googAsserts.assert( |
| 49 |
(Object.getOwnPropertyDescriptor(Element.prototype, 'innerHTML') || |
| 50 |
// IE defines this Element property on HTMLElement. |
| 51 |
Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'innerHTML')) |
| 52 |
.get) : |
| 53 |
null, |
| 54 |
GET_ELEMENTS_BY_TAG_NAME: hasPrototypes ? |
| 55 |
Element.prototype.getElementsByTagName : |
| 56 |
null, |
| 57 |
MATCHES: hasPrototypes ? |
| 58 |
(Element.prototype.matches || Element.prototype.msMatchesSelector) : |
| 59 |
null, |
| 60 |
NODE_NAME_GETTER: hasPrototypes ? |
| 61 |
googAsserts.assert( |
| 62 |
Object.getOwnPropertyDescriptor(Node.prototype, 'nodeName').get) : |
| 63 |
null, |
| 64 |
NODE_TYPE_GETTER: hasPrototypes ? |
| 65 |
googAsserts.assert( |
| 66 |
Object.getOwnPropertyDescriptor(Node.prototype, 'nodeType').get) : |
| 67 |
null, |
| 68 |
PARENT_NODE_GETTER: hasPrototypes ? |
| 69 |
googAsserts.assert( |
| 70 |
Object.getOwnPropertyDescriptor(Node.prototype, 'parentNode').get) : |
| 71 |
null, |
| 72 |
CHILD_NODES_GETTER: hasPrototypes ? |
| 73 |
googAsserts.assert( |
| 74 |
Object.getOwnPropertyDescriptor(Node.prototype, 'childNodes').get) : |
| 75 |
null, |
| 76 |
STYLE_GETTER: hasPrototypes ? |
| 77 |
googAsserts.assert( |
| 78 |
(Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'style') || |
| 79 |
// Safari 10 defines the property on Element instead of |
| 80 |
// HTMLElement. |
| 81 |
Object.getOwnPropertyDescriptor(Element.prototype, 'style')) |
| 82 |
.get) : |
| 83 |
null, |
| 84 |
SHEET_GETTER: hasPrototypes ? |
| 85 |
googAsserts.assert( |
| 86 |
Object.getOwnPropertyDescriptor(HTMLStyleElement.prototype, 'sheet') |
| 87 |
.get) : |
| 88 |
null, |
| 89 |
GET_PROPERTY_VALUE: hasPrototypes ? |
| 90 |
CSSStyleDeclaration.prototype.getPropertyValue : |
| 91 |
null, |
| 92 |
SET_PROPERTY: hasPrototypes ? CSSStyleDeclaration.prototype.setProperty : null |
| 93 |
}; |
| 94 |
|
| 95 |
/** |
| 96 |
* Calls the provided DOM prototype or property descriptor and returns its |
| 97 |
* result. |
| 98 |
* @param {?Function} fn |
| 99 |
* @param {*} object |
| 100 |
* @param {...*} var_args |
| 101 |
* @return {?} |
| 102 |
*/ |
| 103 |
function genericNoClobberCall(fn, object, var_args) { |
| 104 |
if (fn) { |
| 105 |
return fn.apply(object, googArray.slice(arguments, 2)); |
| 106 |
} |
| 107 |
throw new Error('Not supported'); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Returns an element's attributes without falling prey to things like |
| 112 |
* <form><input name="attributes"></form>. Equivalent to {@code |
| 113 |
* node.attributes}. |
| 114 |
* @param {!Element} element |
| 115 |
* @return {?NamedNodeMap} |
| 116 |
*/ |
| 117 |
function getElementAttributes(element) { |
| 118 |
return genericNoClobberCall(Methods.ATTRIBUTES_GETTER, element); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Returns whether an element has a specific attribute, without falling prey to |
| 123 |
* things like <form><input name="hasAttribute"></form>. |
| 124 |
* Equivalent to {@code element.hasAttribute("foo")}. |
| 125 |
* @param {!Element} element |
| 126 |
* @param {string} attrName |
| 127 |
* @return {boolean} |
| 128 |
*/ |
| 129 |
function hasElementAttribute(element, attrName) { |
| 130 |
return genericNoClobberCall(Methods.HAS_ATTRIBUTE, element, attrName); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Returns a specific attribute from an element without falling prey to |
| 135 |
* things like <form><input name="getAttribute"></form>. |
| 136 |
* Equivalent to {@code element.getAttribute("foo")}. |
| 137 |
* @param {!Element} element |
| 138 |
* @param {string} attrName |
| 139 |
* @return {?string} |
| 140 |
*/ |
| 141 |
function getElementAttribute(element, attrName) { |
| 142 |
return genericNoClobberCall(Methods.GET_ATTRIBUTE, element, attrName); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Sets an element's attributes without falling prey to things like |
| 147 |
* <form><input name="setAttribute"></form>. Equivalent to {@code |
| 148 |
* element.setAttribute("foo", "bar")}. |
| 149 |
* @param {!Element} element |
| 150 |
* @param {string} name |
| 151 |
* @param {string} value |
| 152 |
*/ |
| 153 |
function setElementAttribute(element, name, value) { |
| 154 |
if (Methods.SET_ATTRIBUTE) { |
| 155 |
try { |
| 156 |
Methods.SET_ATTRIBUTE.call(element, name, value); |
| 157 |
return; |
| 158 |
} catch (e) { |
| 159 |
// IE throws an exception if the src attribute contains HTTP credentials. |
| 160 |
// However the attribute gets set anyway. |
| 161 |
if (e.message.indexOf('A security problem occurred') != -1) { |
| 162 |
return; |
| 163 |
} |
| 164 |
throw e; |
| 165 |
} |
| 166 |
} |
| 167 |
throw new Error('Not Supported.'); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Deletes a specific attribute from an element without falling prey to |
| 172 |
* things like <form><input name="removeAttribute"></form>. |
| 173 |
* Equivalent to {@code element.removeAttribute("foo")}. |
| 174 |
* @param {!Element} element |
| 175 |
* @param {string} attrName |
| 176 |
*/ |
| 177 |
function removeElementAttribute(element, attrName) { |
| 178 |
genericNoClobberCall(Methods.REMOVE_ATTRIBUTE, element, attrName); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Returns a node's innerHTML property value without falling prey to things like |
| 183 |
* <form><input name="innerHTML"></form>. Equivalent to {@code |
| 184 |
* element.innerHTML}. |
| 185 |
* @param {!Element} element |
| 186 |
* @return {string} |
| 187 |
*/ |
| 188 |
function getElementInnerHTML(element) { |
| 189 |
return genericNoClobberCall(Methods.INNER_HTML_GETTER, element); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Returns an element's style without falling prey to things like |
| 194 |
* <form><input name="style"></form>. |
| 195 |
* @param {!Element} element |
| 196 |
* @return {?CSSStyleDeclaration} |
| 197 |
*/ |
| 198 |
function getElementStyle(element) { |
| 199 |
assertHTMLElement(element); |
| 200 |
return genericNoClobberCall(Methods.STYLE_GETTER, element); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Asserts that the Element is an HTMLElement, or throws an exception. |
| 205 |
* @param {!Element} element |
| 206 |
*/ |
| 207 |
function assertHTMLElement(element) { |
| 208 |
if (!(element instanceof HTMLElement)) { |
| 209 |
throw new Error('Not an HTMLElement'); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Get the children of a specific tag matching the provided tag name without |
| 215 |
* falling prey to things like <form><input name="getElementsByTagName"></form>. |
| 216 |
* Equivalent to {@code element.getElementsByTagName("foo")}. |
| 217 |
* @param {!Element} element |
| 218 |
* @param {string} name |
| 219 |
* @return {!Array<!Element>} |
| 220 |
*/ |
| 221 |
function getElementsByTagName(element, name) { |
| 222 |
return Array.from( |
| 223 |
genericNoClobberCall(Methods.GET_ELEMENTS_BY_TAG_NAME, element, name)); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Returns an element's style without falling prey to things like |
| 228 |
* <form><input name="style"></form>. |
| 229 |
* @param {!Element} element |
| 230 |
* @return {?CSSStyleSheet} |
| 231 |
*/ |
| 232 |
function getElementStyleSheet(element) { |
| 233 |
assertHTMLElement(element); |
| 234 |
return genericNoClobberCall(Methods.SHEET_GETTER, element); |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Returns true if the element would be selected by the provided selector, |
| 239 |
* without falling prey to things like <form><input name="setAttribute"></form>. |
| 240 |
* Equivalent to {@code element.matches("foo")}. |
| 241 |
* @param {!Element} element |
| 242 |
* @param {string} selector |
| 243 |
* @return {boolean} |
| 244 |
*/ |
| 245 |
function elementMatches(element, selector) { |
| 246 |
return genericNoClobberCall(Methods.MATCHES, element, selector); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Asserts that a Node is an Element, without falling prey to things like |
| 251 |
* <form><input name="nodeType"></form>. |
| 252 |
* @param {!Node} node |
| 253 |
* @return {!Element} |
| 254 |
*/ |
| 255 |
function assertNodeIsElement(node) { |
| 256 |
if (googAsserts.ENABLE_ASSERTS && !isNodeElement(node)) { |
| 257 |
googAsserts.fail( |
| 258 |
'Expected Node of type Element but got Node of type %s', |
| 259 |
getNodeType(node)); |
| 260 |
} |
| 261 |
return /** @type {!Element} */ (node); |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Returns whether the node is an Element, without falling prey to things like |
| 266 |
* <form><input name="nodeType"></form>. |
| 267 |
* @param {!Node} node |
| 268 |
* @return {boolean} |
| 269 |
*/ |
| 270 |
function isNodeElement(node) { |
| 271 |
return getNodeType(node) == NodeType.ELEMENT; |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Returns a node's nodeName without falling prey to things like |
| 276 |
* <form><input name="nodeName"></form>. |
| 277 |
* @param {!Node} node |
| 278 |
* @return {string} |
| 279 |
*/ |
| 280 |
function getNodeName(node) { |
| 281 |
return genericNoClobberCall(Methods.NODE_NAME_GETTER, node); |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Returns a node's nodeType without falling prey to things like |
| 286 |
* `<form><input name="nodeType"></form>`. |
| 287 |
* @param {!Node} node |
| 288 |
* @return {number} |
| 289 |
*/ |
| 290 |
function getNodeType(node) { |
| 291 |
return genericNoClobberCall(Methods.NODE_TYPE_GETTER, node); |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Returns a node's parentNode without falling prey to things like |
| 296 |
* <form><input name="parentNode"></form>. |
| 297 |
* @param {!Node} node |
| 298 |
* @return {?Node} |
| 299 |
*/ |
| 300 |
function getParentNode(node) { |
| 301 |
return genericNoClobberCall(Methods.PARENT_NODE_GETTER, node); |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Returns the value of node.childNodes without falling prey to things like |
| 306 |
* <form><input name="childNodes"></form>. |
| 307 |
* @param {!Node} node |
| 308 |
* @return {?NodeList} |
| 309 |
*/ |
| 310 |
function getChildNodes(node) { |
| 311 |
return genericNoClobberCall(Methods.CHILD_NODES_GETTER, node); |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Provides a way to get a CSS value without falling prey to things like |
| 316 |
* <form><input name="propertyValue"></form>. |
| 317 |
* @param {!CSSStyleDeclaration} cssStyle A CSS style object. |
| 318 |
* @param {string} propName A property name. |
| 319 |
* @param {boolean=} opt_allowClobbering If true, on IE8 and IE9 the |
| 320 |
* method just tries to get the value using the clobber-vulnerable method. |
| 321 |
* If false, it just returns null on those browsers, because there is no |
| 322 |
* way to perform the operation in a clobber-safe manner. |
| 323 |
* @return {string} Value of the property as parsed by the browser. |
| 324 |
* @supported Works with IE8 and newer, but anti-clobbering functionality is |
| 325 |
* only available with IE10 and newer. |
| 326 |
*/ |
| 327 |
function getCssPropertyValue(cssStyle, propName, opt_allowClobbering) { |
| 328 |
if (Methods.GET_PROPERTY_VALUE) { |
| 329 |
return Methods.GET_PROPERTY_VALUE.call(cssStyle, propName); |
| 330 |
} else if (opt_allowClobbering) { |
| 331 |
// In IE8 and IE9 we make a direct call to getAttribute. |
| 332 |
return String(cssStyle.getAttribute(propName) || ''); |
| 333 |
} |
| 334 |
throw new Error('Not Supported.'); |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* Provides a way to set a CSS value without falling prey to things like |
| 339 |
* <form><input name="property"></form>. |
| 340 |
* @param {!CSSStyleDeclaration} cssStyle A CSS style object. |
| 341 |
* @param {string} propName A property name. |
| 342 |
* @param {string} sanitizedValue Sanitized value of the property to be set |
| 343 |
* on the CSS style object. |
| 344 |
* @param {boolean=} opt_allowClobbering If true, on IE8 and IE9 the |
| 345 |
* method just tries to get the value using the clobber-vulnerable method. |
| 346 |
* If false, it just returns null on those browsers, because there is no |
| 347 |
* way to perform the operation in a clobber-safe manner. |
| 348 |
* @supported Works with IE8 and newer, but anti-clobbering functionality is |
| 349 |
* only available with IE10 and newer. |
| 350 |
*/ |
| 351 |
function setCssProperty( |
| 352 |
cssStyle, propName, sanitizedValue, opt_allowClobbering) { |
| 353 |
if (Methods.SET_PROPERTY) { |
| 354 |
Methods.SET_PROPERTY.call(cssStyle, propName, sanitizedValue); |
| 355 |
return; |
| 356 |
} else if (opt_allowClobbering) { |
| 357 |
// In IE8 and IE9 we make a direct call to setAttribute. |
| 358 |
cssStyle.setAttribute(propName, sanitizedValue); |
| 359 |
return; |
| 360 |
} |
| 361 |
throw new Error('Not Supported.'); |
| 362 |
} |
| 363 |
|
| 364 |
exports = { |
| 365 |
getElementAttributes: getElementAttributes, |
| 366 |
hasElementAttribute: hasElementAttribute, |
| 367 |
getElementAttribute: getElementAttribute, |
| 368 |
setElementAttribute: setElementAttribute, |
| 369 |
removeElementAttribute: removeElementAttribute, |
| 370 |
getElementInnerHTML: getElementInnerHTML, |
| 371 |
getElementStyle: getElementStyle, |
| 372 |
getElementsByTagName: getElementsByTagName, |
| 373 |
getElementStyleSheet: getElementStyleSheet, |
| 374 |
elementMatches: elementMatches, |
| 375 |
assertNodeIsElement: assertNodeIsElement, |
| 376 |
isNodeElement: isNodeElement, |
| 377 |
getNodeName: getNodeName, |
| 378 |
getNodeType: getNodeType, |
| 379 |
getParentNode: getParentNode, |
| 380 |
getChildNodes: getChildNodes, |
| 381 |
getCssPropertyValue: getCssPropertyValue, |
| 382 |
setCssProperty: setCssProperty |
| 383 |
}; |