purify.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. ;(function (factory) {
  2. 'use strict';
  3. /* global window: false, define: false, module: false */
  4. var root = typeof window === 'undefined' ? null : window;
  5. if (typeof define === 'function' && define.amd) {
  6. define(function () {
  7. return factory(root);
  8. });
  9. } else if (typeof module !== 'undefined') {
  10. module.exports = factory(root);
  11. } else {
  12. root.DOMPurify = factory(root);
  13. }
  14. }(function factory(window) {
  15. 'use strict';
  16. var DOMPurify = function (window) {
  17. return factory(window);
  18. };
  19. /**
  20. * Version label, exposed for easier checks
  21. * if DOMPurify is up to date or not
  22. */
  23. DOMPurify.version = '0.7.4';
  24. if (!window || !window.document || window.document.nodeType !== 9) {
  25. // not running in a browser, provide a factory function
  26. // so that you can pass your own Window
  27. DOMPurify.isSupported = false;
  28. return DOMPurify;
  29. }
  30. var document = window.document;
  31. var originalDocument = document;
  32. var DocumentFragment = window.DocumentFragment;
  33. var HTMLTemplateElement = window.HTMLTemplateElement;
  34. var NodeFilter = window.NodeFilter;
  35. var NamedNodeMap = window.NamedNodeMap || window.MozNamedAttrMap;
  36. var Text = window.Text;
  37. var Comment = window.Comment;
  38. var DOMParser = window.DOMParser;
  39. // As per issue #47, the web-components registry is inherited by a
  40. // new document created via createHTMLDocument. As per the spec
  41. // (http://w3c.github.io/webcomponents/spec/custom/#creating-and-passing-registries)
  42. // a new empty registry is used when creating a template contents owner
  43. // document, so we use that as our parent document to ensure nothing
  44. // is inherited.
  45. if (typeof HTMLTemplateElement === 'function') {
  46. var template = document.createElement('template');
  47. if (template.content && template.content.ownerDocument) {
  48. document = template.content.ownerDocument;
  49. }
  50. }
  51. var implementation = document.implementation;
  52. var createNodeIterator = document.createNodeIterator;
  53. var getElementsByTagName = document.getElementsByTagName;
  54. var createDocumentFragment = document.createDocumentFragment;
  55. var importNode = originalDocument.importNode;
  56. var hooks = {};
  57. /**
  58. * Expose whether this browser supports running the full DOMPurify.
  59. */
  60. DOMPurify.isSupported =
  61. typeof implementation.createHTMLDocument !== 'undefined' &&
  62. document.documentMode !== 9;
  63. /* Add properties to a lookup table */
  64. var _addToSet = function (set, array) {
  65. var l = array.length;
  66. while (l--) {
  67. if (typeof array[l] === 'string') {
  68. array[l] = array[l].toLowerCase();
  69. }
  70. set[array[l]] = true;
  71. }
  72. return set;
  73. };
  74. /* Shallow clone an object */
  75. var _cloneObj = function (object) {
  76. var newObject = {};
  77. var property;
  78. for (property in object) {
  79. if (object.hasOwnProperty(property)) {
  80. newObject[property] = object[property];
  81. }
  82. }
  83. return newObject;
  84. };
  85. /**
  86. * We consider the elements and attributes below to be safe. Ideally
  87. * don't add any new ones but feel free to remove unwanted ones.
  88. */
  89. /* allowed element names */
  90. var ALLOWED_TAGS = null;
  91. var DEFAULT_ALLOWED_TAGS = _addToSet({}, [
  92. // HTML
  93. 'a', 'abbr', 'acronym', 'address', 'area', 'article', 'aside', 'audio', 'b',
  94. 'bdi', 'bdo', 'big', 'blink', 'blockquote', 'body', 'br', 'button', 'canvas',
  95. 'caption', 'center', 'cite', 'code', 'col', 'colgroup', 'content', 'data',
  96. 'datalist', 'dd', 'decorator', 'del', 'details', 'dfn', 'dir', 'div', 'dl', 'dt',
  97. 'element', 'em', 'fieldset', 'figcaption', 'figure', 'font', 'footer', 'form',
  98. 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hgroup', 'hr', 'html', 'i',
  99. 'img', 'input', 'ins', 'kbd', 'label', 'legend', 'li', 'main', 'map', 'mark',
  100. 'marquee', 'menu', 'menuitem', 'meter', 'nav', 'nobr', 'ol', 'optgroup',
  101. 'option', 'output', 'p', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp',
  102. 'section', 'select', 'shadow', 'small', 'source', 'spacer', 'span', 'strike',
  103. 'strong', 'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'template',
  104. 'textarea', 'tfoot', 'th', 'thead', 'time', 'tr', 'track', 'tt', 'u', 'ul', 'var',
  105. 'video', 'wbr',
  106. // SVG
  107. 'svg', 'altglyph', 'altglyphdef', 'altglyphitem', 'animatecolor',
  108. 'animatemotion', 'animatetransform', 'circle', 'clippath', 'defs', 'desc',
  109. 'ellipse', 'filter', 'font', 'g', 'glyph', 'glyphref', 'hkern', 'image', 'line',
  110. 'lineargradient', 'marker', 'mask', 'metadata', 'mpath', 'path', 'pattern',
  111. 'polygon', 'polyline', 'radialgradient', 'rect', 'stop', 'switch', 'symbol',
  112. 'text', 'textpath', 'title', 'tref', 'tspan', 'view', 'vkern',
  113. // SVG Filters
  114. 'feBlend', 'feColorMatrix', 'feComponentTransfer', 'feComposite',
  115. 'feConvolveMatrix', 'feDiffuseLighting', 'feDisplacementMap',
  116. 'feFlood', 'feFuncA', 'feFuncB', 'feFuncG', 'feFuncR', 'feGaussianBlur',
  117. 'feMerge', 'feMergeNode', 'feMorphology', 'feOffset',
  118. 'feSpecularLighting', 'feTile', 'feTurbulence',
  119. //MathML
  120. 'math', 'menclose', 'merror', 'mfenced', 'mfrac', 'mglyph', 'mi', 'mlabeledtr',
  121. 'mmuliscripts', 'mn', 'mo', 'mover', 'mpadded', 'mphantom', 'mroot', 'mrow',
  122. 'ms', 'mpspace', 'msqrt', 'mystyle', 'msub', 'msup', 'msubsup', 'mtable', 'mtd',
  123. 'mtext', 'mtr', 'munder', 'munderover',
  124. //Text
  125. '#text'
  126. ]);
  127. /* Allowed attribute names */
  128. var ALLOWED_ATTR = null;
  129. var DEFAULT_ALLOWED_ATTR = _addToSet({}, [
  130. // HTML
  131. 'accept', 'action', 'align', 'alt', 'autocomplete', 'background', 'bgcolor',
  132. 'border', 'cellpadding', 'cellspacing', 'checked', 'cite', 'class', 'clear', 'color',
  133. 'cols', 'colspan', 'coords', 'datetime', 'default', 'dir', 'disabled',
  134. 'download', 'enctype', 'face', 'for', 'headers', 'height', 'hidden', 'high', 'href',
  135. 'hreflang', 'id', 'ismap', 'label', 'lang', 'list', 'loop', 'low', 'max',
  136. 'maxlength', 'media', 'method', 'min', 'multiple', 'name', 'noshade', 'novalidate',
  137. 'nowrap', 'open', 'optimum', 'pattern', 'placeholder', 'poster', 'preload', 'pubdate',
  138. 'radiogroup', 'readonly', 'rel', 'required', 'rev', 'reversed', 'rows',
  139. 'rowspan', 'spellcheck', 'scope', 'selected', 'shape', 'size', 'span',
  140. 'srclang', 'start', 'src', 'step', 'style', 'summary', 'tabindex', 'title',
  141. 'type', 'usemap', 'valign', 'value', 'width', 'xmlns',
  142. // SVG
  143. 'accent-height', 'accumulate', 'additivive', 'alignment-baseline',
  144. 'ascent', 'attributename', 'attributetype', 'azimuth', 'basefrequency',
  145. 'baseline-shift', 'begin', 'bias', 'by', 'clip', 'clip-path', 'clip-rule',
  146. 'color', 'color-interpolation', 'color-interpolation-filters', 'color-profile',
  147. 'color-rendering', 'cx', 'cy', 'd', 'dx', 'dy', 'diffuseconstant', 'direction',
  148. 'display', 'divisor', 'dur', 'edgemode', 'elevation', 'end', 'fill', 'fill-opacity',
  149. 'fill-rule', 'filter', 'flood-color', 'flood-opacity', 'font-family', 'font-size',
  150. 'font-size-adjust', 'font-stretch', 'font-style', 'font-variant', 'font-weight',
  151. 'fx', 'fy', 'g1', 'g2', 'glyph-name', 'glyphref', 'gradientunits', 'gradienttransform',
  152. 'image-rendering', 'in', 'in2', 'k', 'k1', 'k2', 'k3', 'k4', 'kerning', 'keypoints',
  153. 'keysplines', 'keytimes', 'lengthadjust', 'letter-spacing', 'kernelmatrix',
  154. 'kernelunitlength', 'lighting-color', 'local', 'marker-end', 'marker-mid',
  155. 'marker-start', 'markerheight', 'markerunits', 'markerwidth', 'maskcontentunits',
  156. 'maskunits', 'max', 'mask', 'mode', 'min', 'numoctaves', 'offset', 'operator',
  157. 'opacity', 'order', 'orient', 'orientation', 'origin', 'overflow', 'paint-order',
  158. 'path', 'pathlength', 'patterncontentunits', 'patterntransform', 'patternunits',
  159. 'points', 'preservealpha', 'r', 'rx', 'ry', 'radius', 'refx', 'refy', 'repeatcount',
  160. 'repeatdur', 'restart', 'result', 'rotate', 'scale', 'seed', 'shape-rendering',
  161. 'specularconstant', 'specularexponent', 'spreadmethod', 'stddeviation', 'stitchtiles',
  162. 'stop-color', 'stop-opacity', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap',
  163. 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke', 'stroke-width',
  164. 'surfacescale', 'targetx', 'targety', 'transform', 'text-anchor', 'text-decoration',
  165. 'text-rendering', 'textlength', 'u1', 'u2', 'unicode', 'values', 'viewbox',
  166. 'visibility', 'vert-adv-y', 'vert-origin-x', 'vert-origin-y', 'word-spacing',
  167. 'wrap', 'writing-mode', 'xchannelselector', 'ychannelselector', 'x', 'x1', 'x2',
  168. 'y', 'y1', 'y2', 'z', 'zoomandpan',
  169. // MathML
  170. 'accent', 'accentunder', 'bevelled', 'close', 'columnsalign', 'columnlines',
  171. 'columnspan', 'denomalign', 'depth', 'display', 'displaystyle', 'fence',
  172. 'frame', 'largeop', 'length', 'linethickness', 'lspace', 'lquote',
  173. 'mathbackground', 'mathcolor', 'mathsize', 'mathvariant', 'maxsize',
  174. 'minsize', 'movablelimits', 'notation', 'numalign', 'open', 'rowalign',
  175. 'rowlines', 'rowspacing', 'rowspan', 'rspace', 'rquote', 'scriptlevel',
  176. 'scriptminsize', 'scriptsizemultiplier', 'selection', 'separator',
  177. 'separators', 'stretchy', 'subscriptshift', 'supscriptshift', 'symmetric',
  178. 'voffset',
  179. // XML
  180. 'xlink:href', 'xml:id', 'xlink:title', 'xml:space', 'xmlns:xlink'
  181. ]);
  182. /* Explicitly forbidden tags (overrides ALLOWED_TAGS/ADD_TAGS) */
  183. var FORBID_TAGS = null;
  184. /* Explicitly forbidden attributes (overrides ALLOWED_ATTR/ADD_ATTR) */
  185. var FORBID_ATTR = null;
  186. /* Decide if custom data attributes are okay */
  187. var ALLOW_DATA_ATTR = true;
  188. /* Decide if unknown protocols are okay */
  189. var ALLOW_UNKNOWN_PROTOCOLS = false;
  190. /* Output should be safe for jQuery's $() factory? */
  191. var SAFE_FOR_JQUERY = false;
  192. /* Output should be safe for common template engines.
  193. * This means, DOMPurify removes data attributes, mustaches and ERB
  194. */
  195. var SAFE_FOR_TEMPLATES = false;
  196. /* Specify template detection regex for SAFE_FOR_TEMPLATES mode */
  197. var MUSTACHE_EXPR = /\{\{[\s\S]*|[\s\S]*\}\}/gm;
  198. var ERB_EXPR = /<%[\s\S]*|[\s\S]*%>/gm;
  199. /* Decide if document with <html>... should be returned */
  200. var WHOLE_DOCUMENT = false;
  201. /* Decide if a DOM `HTMLBodyElement` should be returned, instead of a html string.
  202. * If `WHOLE_DOCUMENT` is enabled a `HTMLHtmlElement` will be returned instead
  203. */
  204. var RETURN_DOM = false;
  205. /* Decide if a DOM `DocumentFragment` should be returned, instead of a html string */
  206. var RETURN_DOM_FRAGMENT = false;
  207. /* If `RETURN_DOM` or `RETURN_DOM_FRAGMENT` is enabled, decide if the returned DOM
  208. * `Node` is imported into the current `Document`. If this flag is not enabled the
  209. * `Node` will belong (its ownerDocument) to a fresh `HTMLDocument`, created by
  210. * DOMPurify. */
  211. var RETURN_DOM_IMPORT = false;
  212. /* Output should be free from DOM clobbering attacks? */
  213. var SANITIZE_DOM = true;
  214. /* Keep element content when removing element? */
  215. var KEEP_CONTENT = true;
  216. /* Tags to ignore content of when KEEP_CONTENT is true */
  217. var FORBID_CONTENTS = _addToSet({}, [
  218. 'audio', 'head', 'math', 'script', 'style', 'svg', 'video'
  219. ]);
  220. /* Tags that are safe for data: URIs */
  221. var DATA_URI_TAGS = _addToSet({}, [
  222. 'audio', 'video', 'img', 'source'
  223. ]);
  224. /* Attributes safe for values like "javascript:" */
  225. var URI_SAFE_ATTRIBUTES = _addToSet({}, [
  226. 'alt', 'class', 'for', 'id', 'label', 'name', 'pattern', 'placeholder',
  227. 'summary', 'title', 'value', 'style', 'xmlns'
  228. ]);
  229. /* Keep a reference to config to pass to hooks */
  230. var CONFIG = null;
  231. /* Ideally, do not touch anything below this line */
  232. /* ______________________________________________ */
  233. var formElement = document.createElement('form');
  234. /**
  235. * _parseConfig
  236. *
  237. * @param optional config literal
  238. */
  239. var _parseConfig = function (cfg) {
  240. /* Shield configuration object from tampering */
  241. if (typeof cfg !== 'object') {
  242. cfg = {};
  243. }
  244. /* Set configuration parameters */
  245. ALLOWED_TAGS = 'ALLOWED_TAGS' in cfg ?
  246. _addToSet({}, cfg.ALLOWED_TAGS) : DEFAULT_ALLOWED_TAGS;
  247. ALLOWED_ATTR = 'ALLOWED_ATTR' in cfg ?
  248. _addToSet({}, cfg.ALLOWED_ATTR) : DEFAULT_ALLOWED_ATTR;
  249. FORBID_TAGS = 'FORBID_TAGS' in cfg ?
  250. _addToSet({}, cfg.FORBID_TAGS) : {};
  251. FORBID_ATTR = 'FORBID_ATTR' in cfg ?
  252. _addToSet({}, cfg.FORBID_ATTR) : {};
  253. ALLOW_DATA_ATTR = cfg.ALLOW_DATA_ATTR !== false; // Default true
  254. ALLOW_UNKNOWN_PROTOCOLS = cfg.ALLOW_UNKNOWN_PROTOCOLS || false; // Default false
  255. SAFE_FOR_JQUERY = cfg.SAFE_FOR_JQUERY || false; // Default false
  256. SAFE_FOR_TEMPLATES = cfg.SAFE_FOR_TEMPLATES || false; // Default false
  257. WHOLE_DOCUMENT = cfg.WHOLE_DOCUMENT || false; // Default false
  258. RETURN_DOM = cfg.RETURN_DOM || false; // Default false
  259. RETURN_DOM_FRAGMENT = cfg.RETURN_DOM_FRAGMENT || false; // Default false
  260. RETURN_DOM_IMPORT = cfg.RETURN_DOM_IMPORT || false; // Default false
  261. SANITIZE_DOM = cfg.SANITIZE_DOM !== false; // Default true
  262. KEEP_CONTENT = cfg.KEEP_CONTENT !== false; // Default true
  263. if (SAFE_FOR_TEMPLATES) {
  264. ALLOW_DATA_ATTR = false;
  265. }
  266. if (RETURN_DOM_FRAGMENT) {
  267. RETURN_DOM = true;
  268. }
  269. /* Merge configuration parameters */
  270. if (cfg.ADD_TAGS) {
  271. if (ALLOWED_TAGS === DEFAULT_ALLOWED_TAGS) {
  272. ALLOWED_TAGS = _cloneObj(ALLOWED_TAGS);
  273. }
  274. _addToSet(ALLOWED_TAGS, cfg.ADD_TAGS);
  275. }
  276. if (cfg.ADD_ATTR) {
  277. if (ALLOWED_ATTR === DEFAULT_ALLOWED_ATTR) {
  278. ALLOWED_ATTR = _cloneObj(ALLOWED_ATTR);
  279. }
  280. _addToSet(ALLOWED_ATTR, cfg.ADD_ATTR);
  281. }
  282. /* Add #text in case KEEP_CONTENT is set to true */
  283. if (KEEP_CONTENT) {
  284. ALLOWED_TAGS['#text'] = true;
  285. }
  286. // Prevent further manipulation of configuration.
  287. // Not available in IE8, Safari 5, etc.
  288. if (Object && 'freeze' in Object) {
  289. Object.freeze(cfg);
  290. }
  291. CONFIG = cfg;
  292. };
  293. /**
  294. * _forceRemove
  295. *
  296. * @param a DOM node
  297. */
  298. var _forceRemove = function (node) {
  299. try {
  300. node.parentNode.removeChild(node);
  301. } catch (e) {
  302. node.outerHTML = '';
  303. }
  304. };
  305. /**
  306. * _initDocument
  307. *
  308. * @param a string of dirty markup
  309. * @return a DOM, filled with the dirty markup
  310. */
  311. var _initDocument = function (dirty) {
  312. /* Create a HTML document using DOMParser */
  313. var doc, body;
  314. try {
  315. doc = new DOMParser().parseFromString(dirty, 'text/html');
  316. } catch (e) {
  317. }
  318. /* Some browsers throw, some browsers return null for the code above
  319. DOMParser with text/html support is only in very recent browsers. */
  320. if (!doc) {
  321. doc = implementation.createHTMLDocument('');
  322. body = doc.body;
  323. body.parentNode.removeChild(body.parentNode.firstElementChild);
  324. body.outerHTML = dirty;
  325. }
  326. /* Work on whole document or just its body */
  327. if (typeof doc.getElementsByTagName === 'function') {
  328. return doc.getElementsByTagName(
  329. WHOLE_DOCUMENT ? 'html' : 'body')[0];
  330. }
  331. return getElementsByTagName.call(doc,
  332. WHOLE_DOCUMENT ? 'html' : 'body')[0];
  333. };
  334. /**
  335. * _createIterator
  336. *
  337. * @param document/fragment to create iterator for
  338. * @return iterator instance
  339. */
  340. var _createIterator = function (root) {
  341. return createNodeIterator.call(root.ownerDocument || root,
  342. root,
  343. NodeFilter.SHOW_ELEMENT
  344. | NodeFilter.SHOW_COMMENT
  345. | NodeFilter.SHOW_TEXT,
  346. function () {
  347. return NodeFilter.FILTER_ACCEPT;
  348. },
  349. false
  350. );
  351. };
  352. /**
  353. * _isClobbered
  354. *
  355. * @param element to check for clobbering attacks
  356. * @return true if clobbered, false if safe
  357. */
  358. var _isClobbered = function (elm) {
  359. if (elm instanceof Text || elm instanceof Comment) {
  360. return false;
  361. }
  362. if (typeof elm.nodeName !== 'string'
  363. || typeof elm.textContent !== 'string'
  364. || typeof elm.removeChild !== 'function'
  365. || !(elm.attributes instanceof NamedNodeMap)
  366. || typeof elm.removeAttribute !== 'function'
  367. || typeof elm.setAttribute !== 'function'
  368. ) {
  369. return true;
  370. }
  371. return false;
  372. };
  373. /**
  374. * _sanitizeElements
  375. *
  376. * @protect nodeName
  377. * @protect textContent
  378. * @protect removeChild
  379. *
  380. * @param node to check for permission to exist
  381. * @return true if node was killed, false if left alive
  382. */
  383. var _sanitizeElements = function (currentNode) {
  384. var tagName, content;
  385. /* Execute a hook if present */
  386. _executeHook('beforeSanitizeElements', currentNode, null);
  387. /* Check if element is clobbered or can clobber */
  388. if (_isClobbered(currentNode)) {
  389. _forceRemove(currentNode);
  390. return true;
  391. }
  392. /* Now let's check the element's type and name */
  393. tagName = currentNode.nodeName.toLowerCase();
  394. /* Execute a hook if present */
  395. _executeHook('uponSanitizeElement', currentNode, {
  396. tagName: tagName
  397. });
  398. /* Remove element if anything forbids its presence */
  399. if (!ALLOWED_TAGS[tagName] || FORBID_TAGS[tagName]) {
  400. /* Keep content except for black-listed elements */
  401. if (KEEP_CONTENT && !FORBID_CONTENTS[tagName]
  402. && typeof currentNode.insertAdjacentHTML === 'function') {
  403. try {
  404. currentNode.insertAdjacentHTML('AfterEnd', currentNode.innerHTML);
  405. } catch (e) {
  406. }
  407. }
  408. _forceRemove(currentNode);
  409. return true;
  410. }
  411. /* Convert markup to cover jQuery behavior */
  412. if (SAFE_FOR_JQUERY && !currentNode.firstElementChild &&
  413. (!currentNode.content || !currentNode.content.firstElementChild)) {
  414. currentNode.innerHTML = currentNode.textContent.replace(/</g, '&lt;');
  415. }
  416. /* Sanitize element content to be template-safe */
  417. if (SAFE_FOR_TEMPLATES && currentNode.nodeType === 3) {
  418. /* Get the element's text content */
  419. content = currentNode.textContent;
  420. content = content.replace(MUSTACHE_EXPR, ' ');
  421. content = content.replace(ERB_EXPR, ' ');
  422. currentNode.textContent = content;
  423. }
  424. /* Execute a hook if present */
  425. _executeHook('afterSanitizeElements', currentNode, null);
  426. return false;
  427. };
  428. var DATA_ATTR = /^data-[\w.\u00B7-\uFFFF-]/;
  429. var IS_ALLOWED_URI = /^(?:(?:(?:f|ht)tps?|mailto|tel):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i;
  430. var IS_SCRIPT_OR_DATA = /^(?:\w+script|data):/i;
  431. /* This needs to be extensive thanks to Webkit/Blink's behavior */
  432. var ATTR_WHITESPACE = /[\x00-\x20\xA0\u1680\u180E\u2000-\u2029\u205f\u3000]/g;
  433. /**
  434. * _sanitizeAttributes
  435. *
  436. * @protect attributes
  437. * @protect nodeName
  438. * @protect removeAttribute
  439. * @protect setAttribute
  440. *
  441. * @param node to sanitize
  442. * @return void
  443. */
  444. var _sanitizeAttributes = function (currentNode) {
  445. var attr, name, value, lcName, idAttr, attributes, hookEvent, l;
  446. /* Execute a hook if present */
  447. _executeHook('beforeSanitizeAttributes', currentNode, null);
  448. attributes = currentNode.attributes;
  449. /* Check if we have attributes; if not we might have a text node */
  450. if (!attributes) {
  451. return;
  452. }
  453. hookEvent = {
  454. attrName: '',
  455. attrValue: '',
  456. keepAttr: true
  457. };
  458. l = attributes.length;
  459. /* Go backwards over all attributes; safely remove bad ones */
  460. while (l--) {
  461. attr = attributes[l];
  462. name = attr.name;
  463. value = attr.value;
  464. lcName = name.toLowerCase();
  465. /* Execute a hook if present */
  466. hookEvent.attrName = lcName;
  467. hookEvent.attrValue = value;
  468. hookEvent.keepAttr = true;
  469. _executeHook('uponSanitizeAttribute', currentNode, hookEvent);
  470. value = hookEvent.attrValue;
  471. /* Remove attribute */
  472. // Safari (iOS + Mac), last tested v8.0.5, crashes if you try to
  473. // remove a "name" attribute from an <img> tag that has an "id"
  474. // attribute at the time.
  475. if (lcName === 'name' &&
  476. currentNode.nodeName === 'IMG' && attributes.id) {
  477. idAttr = attributes.id;
  478. attributes = Array.prototype.slice.apply(attributes);
  479. currentNode.removeAttribute('id');
  480. currentNode.removeAttribute(name);
  481. if (attributes.indexOf(idAttr) > l) {
  482. currentNode.setAttribute('id', idAttr.value);
  483. }
  484. } else {
  485. // This avoids a crash in Safari v9.0 with double-ids.
  486. // The trick is to first set the id to be empty and then to
  487. // remove the attriubute
  488. if (name === 'id') {
  489. currentNode.setAttribute(name, '');
  490. }
  491. currentNode.removeAttribute(name);
  492. }
  493. /* Did the hooks approve of the attribute? */
  494. if (!hookEvent.keepAttr) {
  495. continue;
  496. }
  497. /* Make sure attribute cannot clobber */
  498. if (SANITIZE_DOM &&
  499. (lcName === 'id' || lcName === 'name') &&
  500. (value in window || value in document || value in formElement)) {
  501. continue;
  502. }
  503. /* Sanitize attribute content to be template-safe */
  504. if (SAFE_FOR_TEMPLATES) {
  505. value = value.replace(MUSTACHE_EXPR, ' ');
  506. value = value.replace(ERB_EXPR, ' ');
  507. }
  508. if (
  509. /* Check the name is permitted */
  510. (ALLOWED_ATTR[lcName] && !FORBID_ATTR[lcName] && (
  511. /* Check no script, data or unknown possibly unsafe URI
  512. unless we know URI values are safe for that attribute */
  513. URI_SAFE_ATTRIBUTES[lcName] ||
  514. IS_ALLOWED_URI.test(value.replace(ATTR_WHITESPACE, '')) ||
  515. /* Keep image data URIs alive if src is allowed */
  516. (lcName === 'src' && value.indexOf('data:') === 0 &&
  517. DATA_URI_TAGS[currentNode.nodeName.toLowerCase()])
  518. )) ||
  519. /* Allow potentially valid data-* attributes:
  520. * At least one character after "-" (https://html.spec.whatwg.org/multipage/dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes)
  521. * XML-compatible (https://html.spec.whatwg.org/multipage/infrastructure.html#xml-compatible and http://www.w3.org/TR/xml/#d0e804)
  522. * We don't need to check the value; it's always URI safe.
  523. */
  524. (ALLOW_DATA_ATTR && DATA_ATTR.test(lcName)) ||
  525. /* Allow unknown protocols:
  526. * This provides support for links that are handled by protocol handlers which may be unknown
  527. * ahead of time, e.g. fb:, spotify:
  528. */
  529. (ALLOW_UNKNOWN_PROTOCOLS && !IS_SCRIPT_OR_DATA.test(value.replace(ATTR_WHITESPACE, '')))
  530. ) {
  531. /* Handle invalid data-* attribute set by try-catching it */
  532. try {
  533. currentNode.setAttribute(name, value);
  534. } catch (e) {
  535. }
  536. }
  537. }
  538. /* Execute a hook if present */
  539. _executeHook('afterSanitizeAttributes', currentNode, null);
  540. };
  541. /**
  542. * _sanitizeShadowDOM
  543. *
  544. * @param fragment to iterate over recursively
  545. * @return void
  546. */
  547. var _sanitizeShadowDOM = function (fragment) {
  548. var shadowNode;
  549. var shadowIterator = _createIterator(fragment);
  550. /* Execute a hook if present */
  551. _executeHook('beforeSanitizeShadowDOM', fragment, null);
  552. while ((shadowNode = shadowIterator.nextNode())) {
  553. /* Execute a hook if present */
  554. _executeHook('uponSanitizeShadowNode', shadowNode, null);
  555. /* Sanitize tags and elements */
  556. if (_sanitizeElements(shadowNode)) {
  557. continue;
  558. }
  559. /* Deep shadow DOM detected */
  560. if (shadowNode.content instanceof DocumentFragment) {
  561. _sanitizeShadowDOM(shadowNode.content);
  562. }
  563. /* Check attributes, sanitize if necessary */
  564. _sanitizeAttributes(shadowNode);
  565. }
  566. /* Execute a hook if present */
  567. _executeHook('afterSanitizeShadowDOM', fragment, null);
  568. };
  569. /**
  570. * _executeHook
  571. * Execute user configurable hooks
  572. *
  573. * @param {String} entryPoint Name of the hook's entry point
  574. * @param {Node} currentNode
  575. */
  576. var _executeHook = function (entryPoint, currentNode, data) {
  577. if (!hooks[entryPoint]) {
  578. return;
  579. }
  580. hooks[entryPoint].forEach(function (hook) {
  581. hook.call(DOMPurify, currentNode, data, CONFIG);
  582. });
  583. };
  584. /**
  585. * sanitize
  586. * Public method providing core sanitation functionality
  587. *
  588. * @param {String} dirty string
  589. * @param {Object} configuration object
  590. */
  591. DOMPurify.sanitize = function (dirty, cfg) {
  592. var body, currentNode, oldNode, nodeIterator, returnNode;
  593. /* Make sure we have a string to sanitize.
  594. DO NOT return early, as this will return the wrong type if
  595. the user has requested a DOM object rather than a string */
  596. if (!dirty) {
  597. dirty = '';
  598. }
  599. /* Stringify, in case dirty is an object */
  600. if (typeof dirty !== 'string') {
  601. if (typeof dirty.toString !== 'function') {
  602. throw new TypeError('toString is not a function');
  603. } else {
  604. dirty = dirty.toString();
  605. }
  606. }
  607. /* Check we can run. Otherwise fall back or ignore */
  608. if (!DOMPurify.isSupported) {
  609. if (typeof window.toStaticHTML === 'object'
  610. || typeof window.toStaticHTML === 'function') {
  611. return window.toStaticHTML(dirty);
  612. }
  613. return dirty;
  614. }
  615. /* Assign config vars */
  616. _parseConfig(cfg);
  617. /* Exit directly if we have nothing to do */
  618. if (!RETURN_DOM && !WHOLE_DOCUMENT && dirty.indexOf('<') === -1) {
  619. return dirty;
  620. }
  621. /* Initialize the document to work on */
  622. body = _initDocument(dirty);
  623. /* Check we have a DOM node from the data */
  624. if (!body) {
  625. return RETURN_DOM ? null : '';
  626. }
  627. /* Get node iterator */
  628. nodeIterator = _createIterator(body);
  629. /* Now start iterating over the created document */
  630. while ((currentNode = nodeIterator.nextNode())) {
  631. /* Fix IE's strange behavior with manipulated textNodes #89 */
  632. if (currentNode.nodeType === 3 && currentNode === oldNode) {
  633. continue;
  634. }
  635. /* Sanitize tags and elements */
  636. if (_sanitizeElements(currentNode)) {
  637. continue;
  638. }
  639. /* Shadow DOM detected, sanitize it */
  640. if (currentNode.content instanceof DocumentFragment) {
  641. _sanitizeShadowDOM(currentNode.content);
  642. }
  643. /* Check attributes, sanitize if necessary */
  644. _sanitizeAttributes(currentNode);
  645. oldNode = currentNode;
  646. }
  647. /* Return sanitized string or DOM */
  648. if (RETURN_DOM) {
  649. if (RETURN_DOM_FRAGMENT) {
  650. returnNode = createDocumentFragment.call(body.ownerDocument);
  651. while (body.firstChild) {
  652. returnNode.appendChild(body.firstChild);
  653. }
  654. } else {
  655. returnNode = body;
  656. }
  657. if (RETURN_DOM_IMPORT) {
  658. /* adoptNode() is not used because internal state is not reset
  659. (e.g. the past names map of a HTMLFormElement), this is safe
  660. in theory but we would rather not risk another attack vector.
  661. The state that is cloned by importNode() is explicitly defined
  662. by the specs. */
  663. returnNode = importNode.call(originalDocument, returnNode, true);
  664. }
  665. return returnNode;
  666. }
  667. return WHOLE_DOCUMENT ? body.outerHTML : body.innerHTML;
  668. };
  669. /**
  670. * addHook
  671. * Public method to add DOMPurify hooks
  672. *
  673. * @param {String} entryPoint
  674. * @param {Function} hookFunction
  675. */
  676. DOMPurify.addHook = function (entryPoint, hookFunction) {
  677. if (typeof hookFunction !== 'function') {
  678. return;
  679. }
  680. hooks[entryPoint] = hooks[entryPoint] || [];
  681. hooks[entryPoint].push(hookFunction);
  682. };
  683. /**
  684. * removeHook
  685. * Public method to remove a DOMPurify hook at a given entryPoint
  686. * (pops it from the stack of hooks if more are present)
  687. *
  688. * @param {String} entryPoint
  689. * @return void
  690. */
  691. DOMPurify.removeHook = function (entryPoint) {
  692. if (hooks[entryPoint]) {
  693. hooks[entryPoint].pop();
  694. }
  695. };
  696. /**
  697. * removeHooks
  698. * Public method to remove all DOMPurify hooks at a given entryPoint
  699. *
  700. * @param {String} entryPoint
  701. * @return void
  702. */
  703. DOMPurify.removeHooks = function (entryPoint) {
  704. if (hooks[entryPoint]) {
  705. hooks[entryPoint] = [];
  706. }
  707. };
  708. /**
  709. * removeAllHooks
  710. * Public method to remove all DOMPurify hooks
  711. *
  712. * @return void
  713. */
  714. DOMPurify.removeAllHooks = function () {
  715. hooks = [];
  716. };
  717. return DOMPurify;
  718. }));