6056 |
efrain |
1 |
(function (global, factory) {
|
|
|
2 |
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
|
3 |
typeof define === 'function' && define.amd ? define(factory) :
|
|
|
4 |
(global.DOMPurify = factory());
|
|
|
5 |
}(this, (function () { 'use strict';
|
|
|
6 |
|
|
|
7 |
var html = ['a', 'abbr', 'acronym', 'address', 'area', 'article', 'aside', 'audio', 'b', 'bdi', 'bdo', 'big', 'blink', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'center', 'cite', 'code', 'col', 'colgroup', 'content', 'data', 'datalist', 'dd', 'decorator', 'del', 'details', 'dfn', 'dir', 'div', 'dl', 'dt', 'element', 'em', 'fieldset', 'figcaption', 'figure', 'font', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hgroup', 'hr', 'html', 'i', 'img', 'input', 'ins', 'kbd', 'label', 'legend', 'li', 'main', 'map', 'mark', 'marquee', 'menu', 'menuitem', 'meter', 'nav', 'nobr', 'ol', 'optgroup', 'option', 'output', 'p', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'section', 'select', 'shadow', 'small', 'source', 'spacer', 'span', 'strike', 'strong', 'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'template', 'textarea', 'tfoot', 'th', 'thead', 'time', 'tr', 'track', 'tt', 'u', 'ul', 'var', 'video', 'wbr'];
|
|
|
8 |
|
|
|
9 |
// SVG
|
|
|
10 |
var svg = ['svg', 'a', 'altglyph', 'altglyphdef', 'altglyphitem', 'animatecolor', 'animatemotion', 'animatetransform', 'audio', 'canvas', 'circle', 'clippath', 'defs', 'desc', 'ellipse', 'filter', 'font', 'g', 'glyph', 'glyphref', 'hkern', 'image', 'line', 'lineargradient', 'marker', 'mask', 'metadata', 'mpath', 'path', 'pattern', 'polygon', 'polyline', 'radialgradient', 'rect', 'stop', 'style', 'switch', 'symbol', 'text', 'textpath', 'title', 'tref', 'tspan', 'video', 'view', 'vkern'];
|
|
|
11 |
|
|
|
12 |
var svgFilters = ['feBlend', 'feColorMatrix', 'feComponentTransfer', 'feComposite', 'feConvolveMatrix', 'feDiffuseLighting', 'feDisplacementMap', 'feDistantLight', 'feFlood', 'feFuncA', 'feFuncB', 'feFuncG', 'feFuncR', 'feGaussianBlur', 'feMerge', 'feMergeNode', 'feMorphology', 'feOffset', 'fePointLight', 'feSpecularLighting', 'feSpotLight', 'feTile', 'feTurbulence'];
|
|
|
13 |
|
|
|
14 |
var mathMl = ['math', 'menclose', 'merror', 'mfenced', 'mfrac', 'mglyph', 'mi', 'mlabeledtr', 'mmuliscripts', 'mn', 'mo', 'mover', 'mpadded', 'mphantom', 'mroot', 'mrow', 'ms', 'mpspace', 'msqrt', 'mystyle', 'msub', 'msup', 'msubsup', 'mtable', 'mtd', 'mtext', 'mtr', 'munder', 'munderover'];
|
|
|
15 |
|
|
|
16 |
var text = ['#text'];
|
|
|
17 |
|
|
|
18 |
var html$1 = ['accept', 'action', 'align', 'alt', 'autocomplete', 'background', 'bgcolor', 'border', 'cellpadding', 'cellspacing', 'checked', 'cite', 'class', 'clear', 'color', 'cols', 'colspan', 'coords', 'crossorigin', 'datetime', 'default', 'dir', 'disabled', 'download', 'enctype', 'face', 'for', 'headers', 'height', 'hidden', 'high', 'href', 'hreflang', 'id', 'integrity', 'ismap', 'label', 'lang', 'list', 'loop', 'low', 'max', 'maxlength', 'media', 'method', 'min', 'multiple', 'name', 'noshade', 'novalidate', 'nowrap', 'open', 'optimum', 'pattern', 'placeholder', 'poster', 'preload', 'pubdate', 'radiogroup', 'readonly', 'rel', 'required', 'rev', 'reversed', 'role', 'rows', 'rowspan', 'spellcheck', 'scope', 'selected', 'shape', 'size', 'sizes', 'span', 'srclang', 'start', 'src', 'srcset', 'step', 'style', 'summary', 'tabindex', 'title', 'type', 'usemap', 'valign', 'value', 'width', 'xmlns'];
|
|
|
19 |
|
|
|
20 |
var svg$1 = ['accent-height', 'accumulate', 'additivive', 'alignment-baseline', 'ascent', 'attributename', 'attributetype', 'azimuth', 'basefrequency', 'baseline-shift', 'begin', 'bias', 'by', 'class', 'clip', 'clip-path', 'clip-rule', 'color', 'color-interpolation', 'color-interpolation-filters', 'color-profile', 'color-rendering', 'cx', 'cy', 'd', 'dx', 'dy', 'diffuseconstant', 'direction', 'display', 'divisor', 'dur', 'edgemode', 'elevation', 'end', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'flood-color', 'flood-opacity', 'font-family', 'font-size', 'font-size-adjust', 'font-stretch', 'font-style', 'font-variant', 'font-weight', 'fx', 'fy', 'g1', 'g2', 'glyph-name', 'glyphref', 'gradientunits', 'gradienttransform', 'height', 'href', 'id', 'image-rendering', 'in', 'in2', 'k', 'k1', 'k2', 'k3', 'k4', 'kerning', 'keypoints', 'keysplines', 'keytimes', 'lang', 'lengthadjust', 'letter-spacing', 'kernelmatrix', 'kernelunitlength', 'lighting-color', 'local', 'marker-end', 'marker-mid', 'marker-start', 'markerheight', 'markerunits', 'markerwidth', 'maskcontentunits', 'maskunits', 'max', 'mask', 'media', 'method', 'mode', 'min', 'name', 'numoctaves', 'offset', 'operator', 'opacity', 'order', 'orient', 'orientation', 'origin', 'overflow', 'paint-order', 'path', 'pathlength', 'patterncontentunits', 'patterntransform', 'patternunits', 'points', 'preservealpha', 'preserveaspectratio', 'r', 'rx', 'ry', 'radius', 'refx', 'refy', 'repeatcount', 'repeatdur', 'restart', 'result', 'rotate', 'scale', 'seed', 'shape-rendering', 'specularconstant', 'specularexponent', 'spreadmethod', 'stddeviation', 'stitchtiles', 'stop-color', 'stop-opacity', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke', 'stroke-width', 'style', 'surfacescale', 'tabindex', 'targetx', 'targety', 'transform', 'text-anchor', 'text-decoration', 'text-rendering', 'textlength', 'type', 'u1', 'u2', 'unicode', 'values', 'viewbox', 'visibility', 'vert-adv-y', 'vert-origin-x', 'vert-origin-y', 'width', 'word-spacing', 'wrap', 'writing-mode', 'xchannelselector', 'ychannelselector', 'x', 'x1', 'x2', 'xmlns', 'y', 'y1', 'y2', 'z', 'zoomandpan'];
|
|
|
21 |
|
|
|
22 |
var mathMl$1 = ['accent', 'accentunder', 'align', 'bevelled', 'close', 'columnsalign', 'columnlines', 'columnspan', 'denomalign', 'depth', 'dir', 'display', 'displaystyle', 'fence', 'frame', 'height', 'href', 'id', 'largeop', 'length', 'linethickness', 'lspace', 'lquote', 'mathbackground', 'mathcolor', 'mathsize', 'mathvariant', 'maxsize', 'minsize', 'movablelimits', 'notation', 'numalign', 'open', 'rowalign', 'rowlines', 'rowspacing', 'rowspan', 'rspace', 'rquote', 'scriptlevel', 'scriptminsize', 'scriptsizemultiplier', 'selection', 'separator', 'separators', 'stretchy', 'subscriptshift', 'supscriptshift', 'symmetric', 'voffset', 'width', 'xmlns'];
|
|
|
23 |
|
|
|
24 |
var xml = ['xlink:href', 'xml:id', 'xlink:title', 'xml:space', 'xmlns:xlink'];
|
|
|
25 |
|
|
|
26 |
/* Add properties to a lookup table */
|
|
|
27 |
function addToSet(set, array) {
|
|
|
28 |
var l = array.length;
|
|
|
29 |
while (l--) {
|
|
|
30 |
if (typeof array[l] === 'string') {
|
|
|
31 |
array[l] = array[l].toLowerCase();
|
|
|
32 |
}
|
|
|
33 |
set[array[l]] = true;
|
|
|
34 |
}
|
|
|
35 |
return set;
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
/* Shallow clone an object */
|
|
|
39 |
function clone(object) {
|
|
|
40 |
var newObject = {};
|
|
|
41 |
var property = void 0;
|
|
|
42 |
for (property in object) {
|
|
|
43 |
if (Object.prototype.hasOwnProperty.call(object, property)) {
|
|
|
44 |
newObject[property] = object[property];
|
|
|
45 |
}
|
|
|
46 |
}
|
|
|
47 |
return newObject;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
var MUSTACHE_EXPR = /\{\{[\s\S]*|[\s\S]*\}\}/gm; // Specify template detection regex for SAFE_FOR_TEMPLATES mode
|
|
|
51 |
var ERB_EXPR = /<%[\s\S]*|[\s\S]*%>/gm;
|
|
|
52 |
var DATA_ATTR = /^data-[\-\w.\u00B7-\uFFFF]/; // eslint-disable-line no-useless-escape
|
|
|
53 |
var ARIA_ATTR = /^aria-[\-\w]+$/; // eslint-disable-line no-useless-escape
|
|
|
54 |
var IS_ALLOWED_URI = /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|cid|xmpp):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i; // eslint-disable-line no-useless-escape
|
|
|
55 |
var IS_SCRIPT_OR_DATA = /^(?:\w+script|data):/i;
|
|
|
56 |
var ATTR_WHITESPACE = /[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u205f\u3000]/g; // eslint-disable-line no-control-regex
|
|
|
57 |
|
|
|
58 |
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
|
|
|
59 |
|
|
|
60 |
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
|
|
|
61 |
|
|
|
62 |
var getGlobal = function getGlobal() {
|
|
|
63 |
return typeof window === 'undefined' ? null : window;
|
|
|
64 |
};
|
|
|
65 |
|
|
|
66 |
function createDOMPurify() {
|
|
|
67 |
var window = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getGlobal();
|
|
|
68 |
|
|
|
69 |
var DOMPurify = function DOMPurify(root) {
|
|
|
70 |
return createDOMPurify(root);
|
|
|
71 |
};
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Version label, exposed for easier checks
|
|
|
75 |
* if DOMPurify is up to date or not
|
|
|
76 |
*/
|
|
|
77 |
DOMPurify.version = '1.0.7';
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* Array of elements that DOMPurify removed during sanitation.
|
|
|
81 |
* Empty if nothing was removed.
|
|
|
82 |
*/
|
|
|
83 |
DOMPurify.removed = [];
|
|
|
84 |
|
|
|
85 |
if (!window || !window.document || window.document.nodeType !== 9) {
|
|
|
86 |
// Not running in a browser, provide a factory function
|
|
|
87 |
// so that you can pass your own Window
|
|
|
88 |
DOMPurify.isSupported = false;
|
|
|
89 |
|
|
|
90 |
return DOMPurify;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
var originalDocument = window.document;
|
|
|
94 |
var useDOMParser = false; // See comment below
|
|
|
95 |
var removeTitle = false; // See comment below
|
|
|
96 |
|
|
|
97 |
var document = window.document;
|
|
|
98 |
var DocumentFragment = window.DocumentFragment,
|
|
|
99 |
HTMLTemplateElement = window.HTMLTemplateElement,
|
|
|
100 |
Node = window.Node,
|
|
|
101 |
NodeFilter = window.NodeFilter,
|
|
|
102 |
_window$NamedNodeMap = window.NamedNodeMap,
|
|
|
103 |
NamedNodeMap = _window$NamedNodeMap === undefined ? window.NamedNodeMap || window.MozNamedAttrMap : _window$NamedNodeMap,
|
|
|
104 |
Text = window.Text,
|
|
|
105 |
Comment = window.Comment,
|
|
|
106 |
DOMParser = window.DOMParser;
|
|
|
107 |
|
|
|
108 |
// As per issue #47, the web-components registry is inherited by a
|
|
|
109 |
// new document created via createHTMLDocument. As per the spec
|
|
|
110 |
// (http://w3c.github.io/webcomponents/spec/custom/#creating-and-passing-registries)
|
|
|
111 |
// a new empty registry is used when creating a template contents owner
|
|
|
112 |
// document, so we use that as our parent document to ensure nothing
|
|
|
113 |
// is inherited.
|
|
|
114 |
|
|
|
115 |
if (typeof HTMLTemplateElement === 'function') {
|
|
|
116 |
var template = document.createElement('template');
|
|
|
117 |
if (template.content && template.content.ownerDocument) {
|
|
|
118 |
document = template.content.ownerDocument;
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
var _document = document,
|
|
|
123 |
implementation = _document.implementation,
|
|
|
124 |
createNodeIterator = _document.createNodeIterator,
|
|
|
125 |
getElementsByTagName = _document.getElementsByTagName,
|
|
|
126 |
createDocumentFragment = _document.createDocumentFragment;
|
|
|
127 |
var importNode = originalDocument.importNode;
|
|
|
128 |
|
|
|
129 |
|
|
|
130 |
var hooks = {};
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Expose whether this browser supports running the full DOMPurify.
|
|
|
134 |
*/
|
|
|
135 |
DOMPurify.isSupported = implementation && typeof implementation.createHTMLDocument !== 'undefined' && document.documentMode !== 9;
|
|
|
136 |
|
|
|
137 |
var MUSTACHE_EXPR$$1 = MUSTACHE_EXPR,
|
|
|
138 |
ERB_EXPR$$1 = ERB_EXPR,
|
|
|
139 |
DATA_ATTR$$1 = DATA_ATTR,
|
|
|
140 |
ARIA_ATTR$$1 = ARIA_ATTR,
|
|
|
141 |
IS_SCRIPT_OR_DATA$$1 = IS_SCRIPT_OR_DATA,
|
|
|
142 |
ATTR_WHITESPACE$$1 = ATTR_WHITESPACE;
|
|
|
143 |
var IS_ALLOWED_URI$$1 = IS_ALLOWED_URI;
|
|
|
144 |
/**
|
|
|
145 |
* We consider the elements and attributes below to be safe. Ideally
|
|
|
146 |
* don't add any new ones but feel free to remove unwanted ones.
|
|
|
147 |
*/
|
|
|
148 |
|
|
|
149 |
/* allowed element names */
|
|
|
150 |
|
|
|
151 |
var ALLOWED_TAGS = null;
|
|
|
152 |
var DEFAULT_ALLOWED_TAGS = addToSet({}, [].concat(_toConsumableArray(html), _toConsumableArray(svg), _toConsumableArray(svgFilters), _toConsumableArray(mathMl), _toConsumableArray(text)));
|
|
|
153 |
|
|
|
154 |
/* Allowed attribute names */
|
|
|
155 |
var ALLOWED_ATTR = null;
|
|
|
156 |
var DEFAULT_ALLOWED_ATTR = addToSet({}, [].concat(_toConsumableArray(html$1), _toConsumableArray(svg$1), _toConsumableArray(mathMl$1), _toConsumableArray(xml)));
|
|
|
157 |
|
|
|
158 |
/* Explicitly forbidden tags (overrides ALLOWED_TAGS/ADD_TAGS) */
|
|
|
159 |
var FORBID_TAGS = null;
|
|
|
160 |
|
|
|
161 |
/* Explicitly forbidden attributes (overrides ALLOWED_ATTR/ADD_ATTR) */
|
|
|
162 |
var FORBID_ATTR = null;
|
|
|
163 |
|
|
|
164 |
/* Decide if ARIA attributes are okay */
|
|
|
165 |
var ALLOW_ARIA_ATTR = true;
|
|
|
166 |
|
|
|
167 |
/* Decide if custom data attributes are okay */
|
|
|
168 |
var ALLOW_DATA_ATTR = true;
|
|
|
169 |
|
|
|
170 |
/* Decide if unknown protocols are okay */
|
|
|
171 |
var ALLOW_UNKNOWN_PROTOCOLS = false;
|
|
|
172 |
|
|
|
173 |
/* Output should be safe for jQuery's $() factory? */
|
|
|
174 |
var SAFE_FOR_JQUERY = false;
|
|
|
175 |
|
|
|
176 |
/* Output should be safe for common template engines.
|
|
|
177 |
* This means, DOMPurify removes data attributes, mustaches and ERB
|
|
|
178 |
*/
|
|
|
179 |
var SAFE_FOR_TEMPLATES = false;
|
|
|
180 |
|
|
|
181 |
/* Decide if document with <html>... should be returned */
|
|
|
182 |
var WHOLE_DOCUMENT = false;
|
|
|
183 |
|
|
|
184 |
/* Track whether config is already set on this instance of DOMPurify. */
|
|
|
185 |
var SET_CONFIG = false;
|
|
|
186 |
|
|
|
187 |
/* Decide if all elements (e.g. style, script) must be children of
|
|
|
188 |
* document.body. By default, browsers might move them to document.head */
|
|
|
189 |
var FORCE_BODY = false;
|
|
|
190 |
|
|
|
191 |
/* Decide if a DOM `HTMLBodyElement` should be returned, instead of a html string.
|
|
|
192 |
* If `WHOLE_DOCUMENT` is enabled a `HTMLHtmlElement` will be returned instead
|
|
|
193 |
*/
|
|
|
194 |
var RETURN_DOM = false;
|
|
|
195 |
|
|
|
196 |
/* Decide if a DOM `DocumentFragment` should be returned, instead of a html string */
|
|
|
197 |
var RETURN_DOM_FRAGMENT = false;
|
|
|
198 |
|
|
|
199 |
/* If `RETURN_DOM` or `RETURN_DOM_FRAGMENT` is enabled, decide if the returned DOM
|
|
|
200 |
* `Node` is imported into the current `Document`. If this flag is not enabled the
|
|
|
201 |
* `Node` will belong (its ownerDocument) to a fresh `HTMLDocument`, created by
|
|
|
202 |
* DOMPurify. */
|
|
|
203 |
var RETURN_DOM_IMPORT = false;
|
|
|
204 |
|
|
|
205 |
/* Output should be free from DOM clobbering attacks? */
|
|
|
206 |
var SANITIZE_DOM = true;
|
|
|
207 |
|
|
|
208 |
/* Keep element content when removing element? */
|
|
|
209 |
var KEEP_CONTENT = true;
|
|
|
210 |
|
|
|
211 |
/* If a `Node` is passed to sanitize(), then performs sanitization in-place instead
|
|
|
212 |
* of importing it into a new Document and returning a sanitized copy */
|
|
|
213 |
var IN_PLACE = false;
|
|
|
214 |
|
|
|
215 |
/* Allow usage of profiles like html, svg and mathMl */
|
|
|
216 |
var USE_PROFILES = {};
|
|
|
217 |
|
|
|
218 |
/* Tags to ignore content of when KEEP_CONTENT is true */
|
|
|
219 |
var FORBID_CONTENTS = addToSet({}, ['audio', 'head', 'math', 'script', 'style', 'template', 'svg', 'video']);
|
|
|
220 |
|
|
|
221 |
/* Tags that are safe for data: URIs */
|
|
|
222 |
var DATA_URI_TAGS = addToSet({}, ['audio', 'video', 'img', 'source', 'image']);
|
|
|
223 |
|
|
|
224 |
/* Attributes safe for values like "javascript:" */
|
|
|
225 |
var URI_SAFE_ATTRIBUTES = addToSet({}, ['alt', 'class', 'for', 'id', 'label', 'name', 'pattern', 'placeholder', 'summary', 'title', 'value', 'style', 'xmlns']);
|
|
|
226 |
|
|
|
227 |
/* Keep a reference to config to pass to hooks */
|
|
|
228 |
var CONFIG = null;
|
|
|
229 |
|
|
|
230 |
/* Ideally, do not touch anything below this line */
|
|
|
231 |
/* ______________________________________________ */
|
|
|
232 |
|
|
|
233 |
var formElement = document.createElement('form');
|
|
|
234 |
|
|
|
235 |
/**
|
|
|
236 |
* _parseConfig
|
|
|
237 |
*
|
|
|
238 |
* @param {Object} cfg optional config literal
|
|
|
239 |
*/
|
|
|
240 |
// eslint-disable-next-line complexity
|
|
|
241 |
var _parseConfig = function _parseConfig(cfg) {
|
|
|
242 |
/* Shield configuration object from tampering */
|
|
|
243 |
if ((typeof cfg === 'undefined' ? 'undefined' : _typeof(cfg)) !== 'object') {
|
|
|
244 |
cfg = {};
|
|
|
245 |
}
|
|
|
246 |
/* Set configuration parameters */
|
|
|
247 |
ALLOWED_TAGS = 'ALLOWED_TAGS' in cfg ? addToSet({}, cfg.ALLOWED_TAGS) : DEFAULT_ALLOWED_TAGS;
|
|
|
248 |
ALLOWED_ATTR = 'ALLOWED_ATTR' in cfg ? addToSet({}, cfg.ALLOWED_ATTR) : DEFAULT_ALLOWED_ATTR;
|
|
|
249 |
FORBID_TAGS = 'FORBID_TAGS' in cfg ? addToSet({}, cfg.FORBID_TAGS) : {};
|
|
|
250 |
FORBID_ATTR = 'FORBID_ATTR' in cfg ? addToSet({}, cfg.FORBID_ATTR) : {};
|
|
|
251 |
USE_PROFILES = 'USE_PROFILES' in cfg ? cfg.USE_PROFILES : false;
|
|
|
252 |
ALLOW_ARIA_ATTR = cfg.ALLOW_ARIA_ATTR !== false; // Default true
|
|
|
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 |
FORCE_BODY = cfg.FORCE_BODY || false; // Default false
|
|
|
262 |
SANITIZE_DOM = cfg.SANITIZE_DOM !== false; // Default true
|
|
|
263 |
KEEP_CONTENT = cfg.KEEP_CONTENT !== false; // Default true
|
|
|
264 |
IN_PLACE = cfg.IN_PLACE || false; // Default false
|
|
|
265 |
|
|
|
266 |
IS_ALLOWED_URI$$1 = cfg.ALLOWED_URI_REGEXP || IS_ALLOWED_URI$$1;
|
|
|
267 |
|
|
|
268 |
if (SAFE_FOR_TEMPLATES) {
|
|
|
269 |
ALLOW_DATA_ATTR = false;
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
if (RETURN_DOM_FRAGMENT) {
|
|
|
273 |
RETURN_DOM = true;
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
/* Parse profile info */
|
|
|
277 |
if (USE_PROFILES) {
|
|
|
278 |
ALLOWED_TAGS = addToSet({}, [].concat(_toConsumableArray(text)));
|
|
|
279 |
ALLOWED_ATTR = [];
|
|
|
280 |
if (USE_PROFILES.html === true) {
|
|
|
281 |
addToSet(ALLOWED_TAGS, html);
|
|
|
282 |
addToSet(ALLOWED_ATTR, html$1);
|
|
|
283 |
}
|
|
|
284 |
if (USE_PROFILES.svg === true) {
|
|
|
285 |
addToSet(ALLOWED_TAGS, svg);
|
|
|
286 |
addToSet(ALLOWED_ATTR, svg$1);
|
|
|
287 |
addToSet(ALLOWED_ATTR, xml);
|
|
|
288 |
}
|
|
|
289 |
if (USE_PROFILES.svgFilters === true) {
|
|
|
290 |
addToSet(ALLOWED_TAGS, svgFilters);
|
|
|
291 |
addToSet(ALLOWED_ATTR, svg$1);
|
|
|
292 |
addToSet(ALLOWED_ATTR, xml);
|
|
|
293 |
}
|
|
|
294 |
if (USE_PROFILES.mathMl === true) {
|
|
|
295 |
addToSet(ALLOWED_TAGS, mathMl);
|
|
|
296 |
addToSet(ALLOWED_ATTR, mathMl$1);
|
|
|
297 |
addToSet(ALLOWED_ATTR, xml);
|
|
|
298 |
}
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
/* Merge configuration parameters */
|
|
|
302 |
if (cfg.ADD_TAGS) {
|
|
|
303 |
if (ALLOWED_TAGS === DEFAULT_ALLOWED_TAGS) {
|
|
|
304 |
ALLOWED_TAGS = clone(ALLOWED_TAGS);
|
|
|
305 |
}
|
|
|
306 |
addToSet(ALLOWED_TAGS, cfg.ADD_TAGS);
|
|
|
307 |
}
|
|
|
308 |
if (cfg.ADD_ATTR) {
|
|
|
309 |
if (ALLOWED_ATTR === DEFAULT_ALLOWED_ATTR) {
|
|
|
310 |
ALLOWED_ATTR = clone(ALLOWED_ATTR);
|
|
|
311 |
}
|
|
|
312 |
addToSet(ALLOWED_ATTR, cfg.ADD_ATTR);
|
|
|
313 |
}
|
|
|
314 |
if (cfg.ADD_URI_SAFE_ATTR) {
|
|
|
315 |
addToSet(URI_SAFE_ATTRIBUTES, cfg.ADD_URI_SAFE_ATTR);
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
/* Add #text in case KEEP_CONTENT is set to true */
|
|
|
319 |
if (KEEP_CONTENT) {
|
|
|
320 |
ALLOWED_TAGS['#text'] = true;
|
|
|
321 |
}
|
|
|
322 |
|
|
|
323 |
/* Add html, head and body to ALLOWED_TAGS in case WHOLE_DOCUMENT is true */
|
|
|
324 |
if (WHOLE_DOCUMENT) {
|
|
|
325 |
addToSet(ALLOWED_TAGS, ['html', 'head', 'body']);
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
/* Add tbody to ALLOWED_TAGS in case tables are permitted, see #286 */
|
|
|
329 |
if (ALLOWED_TAGS.table) {
|
|
|
330 |
addToSet(ALLOWED_TAGS, ['tbody']);
|
|
|
331 |
}
|
|
|
332 |
|
|
|
333 |
// Prevent further manipulation of configuration.
|
|
|
334 |
// Not available in IE8, Safari 5, etc.
|
|
|
335 |
if (Object && 'freeze' in Object) {
|
|
|
336 |
Object.freeze(cfg);
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
CONFIG = cfg;
|
|
|
340 |
};
|
|
|
341 |
|
|
|
342 |
/**
|
|
|
343 |
* _forceRemove
|
|
|
344 |
*
|
|
|
345 |
* @param {Node} node a DOM node
|
|
|
346 |
*/
|
|
|
347 |
var _forceRemove = function _forceRemove(node) {
|
|
|
348 |
DOMPurify.removed.push({ element: node });
|
|
|
349 |
try {
|
|
|
350 |
node.parentNode.removeChild(node);
|
|
|
351 |
} catch (err) {
|
|
|
352 |
node.outerHTML = '';
|
|
|
353 |
}
|
|
|
354 |
};
|
|
|
355 |
|
|
|
356 |
/**
|
|
|
357 |
* _removeAttribute
|
|
|
358 |
*
|
|
|
359 |
* @param {String} name an Attribute name
|
|
|
360 |
* @param {Node} node a DOM node
|
|
|
361 |
*/
|
|
|
362 |
var _removeAttribute = function _removeAttribute(name, node) {
|
|
|
363 |
try {
|
|
|
364 |
DOMPurify.removed.push({
|
|
|
365 |
attribute: node.getAttributeNode(name),
|
|
|
366 |
from: node
|
|
|
367 |
});
|
|
|
368 |
} catch (err) {
|
|
|
369 |
DOMPurify.removed.push({
|
|
|
370 |
attribute: null,
|
|
|
371 |
from: node
|
|
|
372 |
});
|
|
|
373 |
}
|
|
|
374 |
node.removeAttribute(name);
|
|
|
375 |
};
|
|
|
376 |
|
|
|
377 |
/**
|
|
|
378 |
* _initDocument
|
|
|
379 |
*
|
|
|
380 |
* @param {String} dirty a string of dirty markup
|
|
|
381 |
* @return {Document} a DOM, filled with the dirty markup
|
|
|
382 |
*/
|
|
|
383 |
var _initDocument = function _initDocument(dirty) {
|
|
|
384 |
/* Create a HTML document */
|
|
|
385 |
var doc = void 0;
|
|
|
386 |
|
|
|
387 |
if (FORCE_BODY) {
|
|
|
388 |
dirty = '<remove></remove>' + dirty;
|
|
|
389 |
}
|
|
|
390 |
|
|
|
391 |
/* Use DOMParser to workaround Firefox bug (see comment below) */
|
|
|
392 |
if (useDOMParser) {
|
|
|
393 |
try {
|
|
|
394 |
doc = new DOMParser().parseFromString(dirty, 'text/html');
|
|
|
395 |
} catch (err) {}
|
|
|
396 |
}
|
|
|
397 |
|
|
|
398 |
/* Remove title to fix an mXSS bug in older MS Edge */
|
|
|
399 |
if (removeTitle) {
|
|
|
400 |
addToSet(FORBID_TAGS, ['title']);
|
|
|
401 |
}
|
|
|
402 |
|
|
|
403 |
/* Otherwise use createHTMLDocument, because DOMParser is unsafe in
|
|
|
404 |
Safari (see comment below) */
|
|
|
405 |
if (!doc || !doc.documentElement) {
|
|
|
406 |
doc = implementation.createHTMLDocument('');
|
|
|
407 |
var _doc = doc,
|
|
|
408 |
body = _doc.body;
|
|
|
409 |
|
|
|
410 |
body.parentNode.removeChild(body.parentNode.firstElementChild);
|
|
|
411 |
body.outerHTML = dirty;
|
|
|
412 |
}
|
|
|
413 |
|
|
|
414 |
/* Work on whole document or just its body */
|
|
|
415 |
return getElementsByTagName.call(doc, WHOLE_DOCUMENT ? 'html' : 'body')[0];
|
|
|
416 |
};
|
|
|
417 |
|
|
|
418 |
// Firefox uses a different parser for innerHTML rather than
|
|
|
419 |
// DOMParser (see https://bugzilla.mozilla.org/show_bug.cgi?id=1205631)
|
|
|
420 |
// which means that you *must* use DOMParser, otherwise the output may
|
|
|
421 |
// not be safe if used in a document.write context later.
|
|
|
422 |
//
|
|
|
423 |
// So we feature detect the Firefox bug and use the DOMParser if necessary.
|
|
|
424 |
//
|
|
|
425 |
// MS Edge, in older versions, is affected by an mXSS behavior. The second
|
|
|
426 |
// check tests for the behavior and fixes it if necessary.
|
|
|
427 |
if (DOMPurify.isSupported) {
|
|
|
428 |
(function () {
|
|
|
429 |
try {
|
|
|
430 |
var doc = _initDocument('<svg><p><style><img src="</style><img src=x onerror=alert(1)//">');
|
|
|
431 |
if (doc.querySelector('svg img')) {
|
|
|
432 |
useDOMParser = true;
|
|
|
433 |
}
|
|
|
434 |
} catch (err) {}
|
|
|
435 |
})();
|
|
|
436 |
(function () {
|
|
|
437 |
try {
|
|
|
438 |
var doc = _initDocument('<x/><title></title><img>');
|
|
|
439 |
if (doc.querySelector('title').textContent.match(/<\/title/)) {
|
|
|
440 |
removeTitle = true;
|
|
|
441 |
}
|
|
|
442 |
} catch (err) {}
|
|
|
443 |
})();
|
|
|
444 |
}
|
|
|
445 |
|
|
|
446 |
/**
|
|
|
447 |
* _createIterator
|
|
|
448 |
*
|
|
|
449 |
* @param {Document} root document/fragment to create iterator for
|
|
|
450 |
* @return {Iterator} iterator instance
|
|
|
451 |
*/
|
|
|
452 |
var _createIterator = function _createIterator(root) {
|
|
|
453 |
return createNodeIterator.call(root.ownerDocument || root, root, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_COMMENT | NodeFilter.SHOW_TEXT, function () {
|
|
|
454 |
return NodeFilter.FILTER_ACCEPT;
|
|
|
455 |
}, false);
|
|
|
456 |
};
|
|
|
457 |
|
|
|
458 |
/**
|
|
|
459 |
* _isClobbered
|
|
|
460 |
*
|
|
|
461 |
* @param {Node} elm element to check for clobbering attacks
|
|
|
462 |
* @return {Boolean} true if clobbered, false if safe
|
|
|
463 |
*/
|
|
|
464 |
var _isClobbered = function _isClobbered(elm) {
|
|
|
465 |
if (elm instanceof Text || elm instanceof Comment) {
|
|
|
466 |
return false;
|
|
|
467 |
}
|
|
|
468 |
if (typeof elm.nodeName !== 'string' || typeof elm.textContent !== 'string' || typeof elm.removeChild !== 'function' || !(elm.attributes instanceof NamedNodeMap) || typeof elm.removeAttribute !== 'function' || typeof elm.setAttribute !== 'function') {
|
|
|
469 |
return true;
|
|
|
470 |
}
|
|
|
471 |
return false;
|
|
|
472 |
};
|
|
|
473 |
|
|
|
474 |
/**
|
|
|
475 |
* _isNode
|
|
|
476 |
*
|
|
|
477 |
* @param {Node} obj object to check whether it's a DOM node
|
|
|
478 |
* @return {Boolean} true is object is a DOM node
|
|
|
479 |
*/
|
|
|
480 |
var _isNode = function _isNode(obj) {
|
|
|
481 |
return (typeof Node === 'undefined' ? 'undefined' : _typeof(Node)) === 'object' ? obj instanceof Node : obj && (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object' && typeof obj.nodeType === 'number' && typeof obj.nodeName === 'string';
|
|
|
482 |
};
|
|
|
483 |
|
|
|
484 |
/**
|
|
|
485 |
* _executeHook
|
|
|
486 |
* Execute user configurable hooks
|
|
|
487 |
*
|
|
|
488 |
* @param {String} entryPoint Name of the hook's entry point
|
|
|
489 |
* @param {Node} currentNode node to work on with the hook
|
|
|
490 |
* @param {Object} data additional hook parameters
|
|
|
491 |
*/
|
|
|
492 |
var _executeHook = function _executeHook(entryPoint, currentNode, data) {
|
|
|
493 |
if (!hooks[entryPoint]) {
|
|
|
494 |
return;
|
|
|
495 |
}
|
|
|
496 |
|
|
|
497 |
hooks[entryPoint].forEach(function (hook) {
|
|
|
498 |
hook.call(DOMPurify, currentNode, data, CONFIG);
|
|
|
499 |
});
|
|
|
500 |
};
|
|
|
501 |
|
|
|
502 |
/**
|
|
|
503 |
* _sanitizeElements
|
|
|
504 |
*
|
|
|
505 |
* @protect nodeName
|
|
|
506 |
* @protect textContent
|
|
|
507 |
* @protect removeChild
|
|
|
508 |
*
|
|
|
509 |
* @param {Node} currentNode to check for permission to exist
|
|
|
510 |
* @return {Boolean} true if node was killed, false if left alive
|
|
|
511 |
*/
|
|
|
512 |
var _sanitizeElements = function _sanitizeElements(currentNode) {
|
|
|
513 |
var content = void 0;
|
|
|
514 |
|
|
|
515 |
/* Execute a hook if present */
|
|
|
516 |
_executeHook('beforeSanitizeElements', currentNode, null);
|
|
|
517 |
|
|
|
518 |
/* Check if element is clobbered or can clobber */
|
|
|
519 |
if (_isClobbered(currentNode)) {
|
|
|
520 |
_forceRemove(currentNode);
|
|
|
521 |
return true;
|
|
|
522 |
}
|
|
|
523 |
|
|
|
524 |
/* Now let's check the element's type and name */
|
|
|
525 |
var tagName = currentNode.nodeName.toLowerCase();
|
|
|
526 |
|
|
|
527 |
/* Execute a hook if present */
|
|
|
528 |
_executeHook('uponSanitizeElement', currentNode, {
|
|
|
529 |
tagName: tagName,
|
|
|
530 |
allowedTags: ALLOWED_TAGS
|
|
|
531 |
});
|
|
|
532 |
|
|
|
533 |
/* Remove element if anything forbids its presence */
|
|
|
534 |
if (!ALLOWED_TAGS[tagName] || FORBID_TAGS[tagName]) {
|
|
|
535 |
/* Keep content except for black-listed elements */
|
|
|
536 |
if (KEEP_CONTENT && !FORBID_CONTENTS[tagName] && typeof currentNode.insertAdjacentHTML === 'function') {
|
|
|
537 |
try {
|
|
|
538 |
currentNode.insertAdjacentHTML('AfterEnd', currentNode.innerHTML);
|
|
|
539 |
} catch (err) {}
|
|
|
540 |
}
|
|
|
541 |
_forceRemove(currentNode);
|
|
|
542 |
return true;
|
|
|
543 |
}
|
|
|
544 |
|
|
|
545 |
/* Convert markup to cover jQuery behavior */
|
|
|
546 |
if (SAFE_FOR_JQUERY && !currentNode.firstElementChild && (!currentNode.content || !currentNode.content.firstElementChild) && /</g.test(currentNode.textContent)) {
|
|
|
547 |
DOMPurify.removed.push({ element: currentNode.cloneNode() });
|
|
|
548 |
if (currentNode.innerHTML) {
|
|
|
549 |
currentNode.innerHTML = currentNode.innerHTML.replace(/</g, '<');
|
|
|
550 |
} else {
|
|
|
551 |
currentNode.innerHTML = currentNode.textContent.replace(/</g, '<');
|
|
|
552 |
}
|
|
|
553 |
}
|
|
|
554 |
|
|
|
555 |
/* Sanitize element content to be template-safe */
|
|
|
556 |
if (SAFE_FOR_TEMPLATES && currentNode.nodeType === 3) {
|
|
|
557 |
/* Get the element's text content */
|
|
|
558 |
content = currentNode.textContent;
|
|
|
559 |
content = content.replace(MUSTACHE_EXPR$$1, ' ');
|
|
|
560 |
content = content.replace(ERB_EXPR$$1, ' ');
|
|
|
561 |
if (currentNode.textContent !== content) {
|
|
|
562 |
DOMPurify.removed.push({ element: currentNode.cloneNode() });
|
|
|
563 |
currentNode.textContent = content;
|
|
|
564 |
}
|
|
|
565 |
}
|
|
|
566 |
|
|
|
567 |
/* Execute a hook if present */
|
|
|
568 |
_executeHook('afterSanitizeElements', currentNode, null);
|
|
|
569 |
|
|
|
570 |
return false;
|
|
|
571 |
};
|
|
|
572 |
|
|
|
573 |
/**
|
|
|
574 |
* _isValidAttribute
|
|
|
575 |
*
|
|
|
576 |
* @param {string} lcTag Lowercase tag name of containing element.
|
|
|
577 |
* @param {string} lcName Lowercase attribute name.
|
|
|
578 |
* @param {string} value Attribute value.
|
|
|
579 |
* @return {Boolean} Returns true if `value` is valid, otherwise false.
|
|
|
580 |
*/
|
|
|
581 |
var _isValidAttribute = function _isValidAttribute(lcTag, lcName, value) {
|
|
|
582 |
/* Make sure attribute cannot clobber */
|
|
|
583 |
if (SANITIZE_DOM && (lcName === 'id' || lcName === 'name') && (value in document || value in formElement)) {
|
|
|
584 |
return false;
|
|
|
585 |
}
|
|
|
586 |
|
|
|
587 |
/* Sanitize attribute content to be template-safe */
|
|
|
588 |
if (SAFE_FOR_TEMPLATES) {
|
|
|
589 |
value = value.replace(MUSTACHE_EXPR$$1, ' ');
|
|
|
590 |
value = value.replace(ERB_EXPR$$1, ' ');
|
|
|
591 |
}
|
|
|
592 |
|
|
|
593 |
/* Allow valid data-* attributes: At least one character after "-"
|
|
|
594 |
(https://html.spec.whatwg.org/multipage/dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes)
|
|
|
595 |
XML-compatible (https://html.spec.whatwg.org/multipage/infrastructure.html#xml-compatible and http://www.w3.org/TR/xml/#d0e804)
|
|
|
596 |
We don't need to check the value; it's always URI safe. */
|
|
|
597 |
if (ALLOW_DATA_ATTR && DATA_ATTR$$1.test(lcName)) {
|
|
|
598 |
// This attribute is safe
|
|
|
599 |
} else if (ALLOW_ARIA_ATTR && ARIA_ATTR$$1.test(lcName)) {
|
|
|
600 |
// This attribute is safe
|
|
|
601 |
/* Otherwise, check the name is permitted */
|
|
|
602 |
} else if (!ALLOWED_ATTR[lcName] || FORBID_ATTR[lcName]) {
|
|
|
603 |
return false;
|
|
|
604 |
|
|
|
605 |
/* Check value is safe. First, is attr inert? If so, is safe */
|
|
|
606 |
} else if (URI_SAFE_ATTRIBUTES[lcName]) {
|
|
|
607 |
// This attribute is safe
|
|
|
608 |
/* Check no script, data or unknown possibly unsafe URI
|
|
|
609 |
unless we know URI values are safe for that attribute */
|
|
|
610 |
} else if (IS_ALLOWED_URI$$1.test(value.replace(ATTR_WHITESPACE$$1, ''))) {
|
|
|
611 |
// This attribute is safe
|
|
|
612 |
/* Keep image data URIs alive if src/xlink:href is allowed */
|
|
|
613 |
} else if ((lcName === 'src' || lcName === 'xlink:href') && value.indexOf('data:') === 0 && DATA_URI_TAGS[lcTag]) {
|
|
|
614 |
// This attribute is safe
|
|
|
615 |
/* Allow unknown protocols: This provides support for links that
|
|
|
616 |
are handled by protocol handlers which may be unknown ahead of
|
|
|
617 |
time, e.g. fb:, spotify: */
|
|
|
618 |
} else if (ALLOW_UNKNOWN_PROTOCOLS && !IS_SCRIPT_OR_DATA$$1.test(value.replace(ATTR_WHITESPACE$$1, ''))) {
|
|
|
619 |
// This attribute is safe
|
|
|
620 |
/* Check for binary attributes */
|
|
|
621 |
// eslint-disable-next-line no-negated-condition
|
|
|
622 |
} else if (!value) {
|
|
|
623 |
// Binary attributes are safe at this point
|
|
|
624 |
/* Anything else, presume unsafe, do not add it back */
|
|
|
625 |
} else {
|
|
|
626 |
return false;
|
|
|
627 |
}
|
|
|
628 |
return true;
|
|
|
629 |
};
|
|
|
630 |
|
|
|
631 |
/**
|
|
|
632 |
* _sanitizeAttributes
|
|
|
633 |
*
|
|
|
634 |
* @protect attributes
|
|
|
635 |
* @protect nodeName
|
|
|
636 |
* @protect removeAttribute
|
|
|
637 |
* @protect setAttribute
|
|
|
638 |
*
|
|
|
639 |
* @param {Node} node to sanitize
|
|
|
640 |
*/
|
|
|
641 |
// eslint-disable-next-line complexity
|
|
|
642 |
var _sanitizeAttributes = function _sanitizeAttributes(currentNode) {
|
|
|
643 |
var attr = void 0;
|
|
|
644 |
var value = void 0;
|
|
|
645 |
var lcName = void 0;
|
|
|
646 |
var idAttr = void 0;
|
|
|
647 |
var l = void 0;
|
|
|
648 |
/* Execute a hook if present */
|
|
|
649 |
_executeHook('beforeSanitizeAttributes', currentNode, null);
|
|
|
650 |
|
|
|
651 |
var attributes = currentNode.attributes;
|
|
|
652 |
|
|
|
653 |
/* Check if we have attributes; if not we might have a text node */
|
|
|
654 |
|
|
|
655 |
if (!attributes) {
|
|
|
656 |
return;
|
|
|
657 |
}
|
|
|
658 |
|
|
|
659 |
var hookEvent = {
|
|
|
660 |
attrName: '',
|
|
|
661 |
attrValue: '',
|
|
|
662 |
keepAttr: true,
|
|
|
663 |
allowedAttributes: ALLOWED_ATTR
|
|
|
664 |
};
|
|
|
665 |
l = attributes.length;
|
|
|
666 |
|
|
|
667 |
/* Go backwards over all attributes; safely remove bad ones */
|
|
|
668 |
while (l--) {
|
|
|
669 |
attr = attributes[l];
|
|
|
670 |
var _attr = attr,
|
|
|
671 |
name = _attr.name;
|
|
|
672 |
|
|
|
673 |
value = attr.value.trim();
|
|
|
674 |
lcName = name.toLowerCase();
|
|
|
675 |
|
|
|
676 |
/* Execute a hook if present */
|
|
|
677 |
hookEvent.attrName = lcName;
|
|
|
678 |
hookEvent.attrValue = value;
|
|
|
679 |
hookEvent.keepAttr = true;
|
|
|
680 |
_executeHook('uponSanitizeAttribute', currentNode, hookEvent);
|
|
|
681 |
value = hookEvent.attrValue;
|
|
|
682 |
|
|
|
683 |
/* Remove attribute */
|
|
|
684 |
// Safari (iOS + Mac), last tested v8.0.5, crashes if you try to
|
|
|
685 |
// remove a "name" attribute from an <img> tag that has an "id"
|
|
|
686 |
// attribute at the time.
|
|
|
687 |
if (lcName === 'name' && currentNode.nodeName === 'IMG' && attributes.id) {
|
|
|
688 |
idAttr = attributes.id;
|
|
|
689 |
attributes = Array.prototype.slice.apply(attributes);
|
|
|
690 |
_removeAttribute('id', currentNode);
|
|
|
691 |
_removeAttribute(name, currentNode);
|
|
|
692 |
if (attributes.indexOf(idAttr) > l) {
|
|
|
693 |
currentNode.setAttribute('id', idAttr.value);
|
|
|
694 |
}
|
|
|
695 |
} else if (
|
|
|
696 |
// This works around a bug in Safari, where input[type=file]
|
|
|
697 |
// cannot be dynamically set after type has been removed
|
|
|
698 |
currentNode.nodeName === 'INPUT' && lcName === 'type' && value === 'file' && (ALLOWED_ATTR[lcName] || !FORBID_ATTR[lcName])) {
|
|
|
699 |
continue;
|
|
|
700 |
} else {
|
|
|
701 |
// This avoids a crash in Safari v9.0 with double-ids.
|
|
|
702 |
// The trick is to first set the id to be empty and then to
|
|
|
703 |
// remove the attribute
|
|
|
704 |
if (name === 'id') {
|
|
|
705 |
currentNode.setAttribute(name, '');
|
|
|
706 |
}
|
|
|
707 |
_removeAttribute(name, currentNode);
|
|
|
708 |
}
|
|
|
709 |
|
|
|
710 |
/* Did the hooks approve of the attribute? */
|
|
|
711 |
if (!hookEvent.keepAttr) {
|
|
|
712 |
continue;
|
|
|
713 |
}
|
|
|
714 |
|
|
|
715 |
/* Is `value` valid for this attribute? */
|
|
|
716 |
var lcTag = currentNode.nodeName.toLowerCase();
|
|
|
717 |
if (!_isValidAttribute(lcTag, lcName, value)) {
|
|
|
718 |
continue;
|
|
|
719 |
}
|
|
|
720 |
|
|
|
721 |
/* Handle invalid data-* attribute set by try-catching it */
|
|
|
722 |
try {
|
|
|
723 |
currentNode.setAttribute(name, value);
|
|
|
724 |
DOMPurify.removed.pop();
|
|
|
725 |
} catch (err) {}
|
|
|
726 |
}
|
|
|
727 |
|
|
|
728 |
/* Execute a hook if present */
|
|
|
729 |
_executeHook('afterSanitizeAttributes', currentNode, null);
|
|
|
730 |
};
|
|
|
731 |
|
|
|
732 |
/**
|
|
|
733 |
* _sanitizeShadowDOM
|
|
|
734 |
*
|
|
|
735 |
* @param {DocumentFragment} fragment to iterate over recursively
|
|
|
736 |
*/
|
|
|
737 |
var _sanitizeShadowDOM = function _sanitizeShadowDOM(fragment) {
|
|
|
738 |
var shadowNode = void 0;
|
|
|
739 |
var shadowIterator = _createIterator(fragment);
|
|
|
740 |
|
|
|
741 |
/* Execute a hook if present */
|
|
|
742 |
_executeHook('beforeSanitizeShadowDOM', fragment, null);
|
|
|
743 |
|
|
|
744 |
while (shadowNode = shadowIterator.nextNode()) {
|
|
|
745 |
/* Execute a hook if present */
|
|
|
746 |
_executeHook('uponSanitizeShadowNode', shadowNode, null);
|
|
|
747 |
|
|
|
748 |
/* Sanitize tags and elements */
|
|
|
749 |
if (_sanitizeElements(shadowNode)) {
|
|
|
750 |
continue;
|
|
|
751 |
}
|
|
|
752 |
|
|
|
753 |
/* Deep shadow DOM detected */
|
|
|
754 |
if (shadowNode.content instanceof DocumentFragment) {
|
|
|
755 |
_sanitizeShadowDOM(shadowNode.content);
|
|
|
756 |
}
|
|
|
757 |
|
|
|
758 |
/* Check attributes, sanitize if necessary */
|
|
|
759 |
_sanitizeAttributes(shadowNode);
|
|
|
760 |
}
|
|
|
761 |
|
|
|
762 |
/* Execute a hook if present */
|
|
|
763 |
_executeHook('afterSanitizeShadowDOM', fragment, null);
|
|
|
764 |
};
|
|
|
765 |
|
|
|
766 |
/**
|
|
|
767 |
* Sanitize
|
|
|
768 |
* Public method providing core sanitation functionality
|
|
|
769 |
*
|
|
|
770 |
* @param {String|Node} dirty string or DOM node
|
|
|
771 |
* @param {Object} configuration object
|
|
|
772 |
*/
|
|
|
773 |
// eslint-disable-next-line complexity
|
|
|
774 |
DOMPurify.sanitize = function (dirty, cfg) {
|
|
|
775 |
var body = void 0;
|
|
|
776 |
var importedNode = void 0;
|
|
|
777 |
var currentNode = void 0;
|
|
|
778 |
var oldNode = void 0;
|
|
|
779 |
var returnNode = void 0;
|
|
|
780 |
/* Make sure we have a string to sanitize.
|
|
|
781 |
DO NOT return early, as this will return the wrong type if
|
|
|
782 |
the user has requested a DOM object rather than a string */
|
|
|
783 |
if (!dirty) {
|
|
|
784 |
dirty = '<!-->';
|
|
|
785 |
}
|
|
|
786 |
|
|
|
787 |
/* Stringify, in case dirty is an object */
|
|
|
788 |
if (typeof dirty !== 'string' && !_isNode(dirty)) {
|
|
|
789 |
// eslint-disable-next-line no-negated-condition
|
|
|
790 |
if (typeof dirty.toString !== 'function') {
|
|
|
791 |
throw new TypeError('toString is not a function');
|
|
|
792 |
} else {
|
|
|
793 |
dirty = dirty.toString();
|
|
|
794 |
if (typeof dirty !== 'string') {
|
|
|
795 |
throw new TypeError('dirty is not a string, aborting');
|
|
|
796 |
}
|
|
|
797 |
}
|
|
|
798 |
}
|
|
|
799 |
|
|
|
800 |
/* Check we can run. Otherwise fall back or ignore */
|
|
|
801 |
if (!DOMPurify.isSupported) {
|
|
|
802 |
if (_typeof(window.toStaticHTML) === 'object' || typeof window.toStaticHTML === 'function') {
|
|
|
803 |
if (typeof dirty === 'string') {
|
|
|
804 |
return window.toStaticHTML(dirty);
|
|
|
805 |
}
|
|
|
806 |
if (_isNode(dirty)) {
|
|
|
807 |
return window.toStaticHTML(dirty.outerHTML);
|
|
|
808 |
}
|
|
|
809 |
}
|
|
|
810 |
return dirty;
|
|
|
811 |
}
|
|
|
812 |
|
|
|
813 |
/* Assign config vars */
|
|
|
814 |
if (!SET_CONFIG) {
|
|
|
815 |
_parseConfig(cfg);
|
|
|
816 |
}
|
|
|
817 |
|
|
|
818 |
/* Clean up removed elements */
|
|
|
819 |
DOMPurify.removed = [];
|
|
|
820 |
|
|
|
821 |
if (IN_PLACE) {
|
|
|
822 |
/* No special handling necessary for in-place sanitization */
|
|
|
823 |
} else if (dirty instanceof Node) {
|
|
|
824 |
/* If dirty is a DOM element, append to an empty document to avoid
|
|
|
825 |
elements being stripped by the parser */
|
|
|
826 |
body = _initDocument('<!-->');
|
|
|
827 |
importedNode = body.ownerDocument.importNode(dirty, true);
|
|
|
828 |
if (importedNode.nodeType === 1 && importedNode.nodeName === 'BODY') {
|
|
|
829 |
/* Node is already a body, use as is */
|
|
|
830 |
body = importedNode;
|
|
|
831 |
} else {
|
|
|
832 |
body.appendChild(importedNode);
|
|
|
833 |
}
|
|
|
834 |
} else {
|
|
|
835 |
/* Exit directly if we have nothing to do */
|
|
|
836 |
if (!RETURN_DOM && !WHOLE_DOCUMENT && dirty.indexOf('<') === -1) {
|
|
|
837 |
return dirty;
|
|
|
838 |
}
|
|
|
839 |
|
|
|
840 |
/* Initialize the document to work on */
|
|
|
841 |
body = _initDocument(dirty);
|
|
|
842 |
|
|
|
843 |
/* Check we have a DOM node from the data */
|
|
|
844 |
if (!body) {
|
|
|
845 |
return RETURN_DOM ? null : '';
|
|
|
846 |
}
|
|
|
847 |
}
|
|
|
848 |
|
|
|
849 |
/* Remove first element node (ours) if FORCE_BODY is set */
|
|
|
850 |
if (body && FORCE_BODY) {
|
|
|
851 |
_forceRemove(body.firstChild);
|
|
|
852 |
}
|
|
|
853 |
|
|
|
854 |
/* Get node iterator */
|
|
|
855 |
var nodeIterator = _createIterator(IN_PLACE ? dirty : body);
|
|
|
856 |
|
|
|
857 |
/* Now start iterating over the created document */
|
|
|
858 |
while (currentNode = nodeIterator.nextNode()) {
|
|
|
859 |
/* Fix IE's strange behavior with manipulated textNodes #89 */
|
|
|
860 |
if (currentNode.nodeType === 3 && currentNode === oldNode) {
|
|
|
861 |
continue;
|
|
|
862 |
}
|
|
|
863 |
|
|
|
864 |
/* Sanitize tags and elements */
|
|
|
865 |
if (_sanitizeElements(currentNode)) {
|
|
|
866 |
continue;
|
|
|
867 |
}
|
|
|
868 |
|
|
|
869 |
/* Shadow DOM detected, sanitize it */
|
|
|
870 |
if (currentNode.content instanceof DocumentFragment) {
|
|
|
871 |
_sanitizeShadowDOM(currentNode.content);
|
|
|
872 |
}
|
|
|
873 |
|
|
|
874 |
/* Check attributes, sanitize if necessary */
|
|
|
875 |
_sanitizeAttributes(currentNode);
|
|
|
876 |
|
|
|
877 |
oldNode = currentNode;
|
|
|
878 |
}
|
|
|
879 |
|
|
|
880 |
/* If we sanitized `dirty` in-place, return it. */
|
|
|
881 |
if (IN_PLACE) {
|
|
|
882 |
return dirty;
|
|
|
883 |
}
|
|
|
884 |
|
|
|
885 |
/* Return sanitized string or DOM */
|
|
|
886 |
if (RETURN_DOM) {
|
|
|
887 |
if (RETURN_DOM_FRAGMENT) {
|
|
|
888 |
returnNode = createDocumentFragment.call(body.ownerDocument);
|
|
|
889 |
|
|
|
890 |
while (body.firstChild) {
|
|
|
891 |
returnNode.appendChild(body.firstChild);
|
|
|
892 |
}
|
|
|
893 |
} else {
|
|
|
894 |
returnNode = body;
|
|
|
895 |
}
|
|
|
896 |
|
|
|
897 |
if (RETURN_DOM_IMPORT) {
|
|
|
898 |
/* AdoptNode() is not used because internal state is not reset
|
|
|
899 |
(e.g. the past names map of a HTMLFormElement), this is safe
|
|
|
900 |
in theory but we would rather not risk another attack vector.
|
|
|
901 |
The state that is cloned by importNode() is explicitly defined
|
|
|
902 |
by the specs. */
|
|
|
903 |
returnNode = importNode.call(originalDocument, returnNode, true);
|
|
|
904 |
}
|
|
|
905 |
|
|
|
906 |
return returnNode;
|
|
|
907 |
}
|
|
|
908 |
|
|
|
909 |
return WHOLE_DOCUMENT ? body.outerHTML : body.innerHTML;
|
|
|
910 |
};
|
|
|
911 |
|
|
|
912 |
/**
|
|
|
913 |
* Public method to set the configuration once
|
|
|
914 |
* setConfig
|
|
|
915 |
*
|
|
|
916 |
* @param {Object} cfg configuration object
|
|
|
917 |
*/
|
|
|
918 |
DOMPurify.setConfig = function (cfg) {
|
|
|
919 |
_parseConfig(cfg);
|
|
|
920 |
SET_CONFIG = true;
|
|
|
921 |
};
|
|
|
922 |
|
|
|
923 |
/**
|
|
|
924 |
* Public method to remove the configuration
|
|
|
925 |
* clearConfig
|
|
|
926 |
*
|
|
|
927 |
*/
|
|
|
928 |
DOMPurify.clearConfig = function () {
|
|
|
929 |
CONFIG = null;
|
|
|
930 |
SET_CONFIG = false;
|
|
|
931 |
};
|
|
|
932 |
|
|
|
933 |
/**
|
|
|
934 |
* Public method to check if an attribute value is valid.
|
|
|
935 |
* Uses last set config, if any. Otherwise, uses config defaults.
|
|
|
936 |
* isValidAttribute
|
|
|
937 |
*
|
|
|
938 |
* @param {string} tag Tag name of containing element.
|
|
|
939 |
* @param {string} attr Attribute name.
|
|
|
940 |
* @param {string} value Attribute value.
|
|
|
941 |
* @return {Boolean} Returns true if `value` is valid. Otherwise, returns false.
|
|
|
942 |
*/
|
|
|
943 |
DOMPurify.isValidAttribute = function (tag, attr, value) {
|
|
|
944 |
/* Initialize shared config vars if necessary. */
|
|
|
945 |
if (!CONFIG) {
|
|
|
946 |
_parseConfig({});
|
|
|
947 |
}
|
|
|
948 |
var lcTag = tag.toLowerCase();
|
|
|
949 |
var lcName = attr.toLowerCase();
|
|
|
950 |
return _isValidAttribute(lcTag, lcName, value);
|
|
|
951 |
};
|
|
|
952 |
|
|
|
953 |
/**
|
|
|
954 |
* AddHook
|
|
|
955 |
* Public method to add DOMPurify hooks
|
|
|
956 |
*
|
|
|
957 |
* @param {String} entryPoint entry point for the hook to add
|
|
|
958 |
* @param {Function} hookFunction function to execute
|
|
|
959 |
*/
|
|
|
960 |
DOMPurify.addHook = function (entryPoint, hookFunction) {
|
|
|
961 |
if (typeof hookFunction !== 'function') {
|
|
|
962 |
return;
|
|
|
963 |
}
|
|
|
964 |
hooks[entryPoint] = hooks[entryPoint] || [];
|
|
|
965 |
hooks[entryPoint].push(hookFunction);
|
|
|
966 |
};
|
|
|
967 |
|
|
|
968 |
/**
|
|
|
969 |
* RemoveHook
|
|
|
970 |
* Public method to remove a DOMPurify hook at a given entryPoint
|
|
|
971 |
* (pops it from the stack of hooks if more are present)
|
|
|
972 |
*
|
|
|
973 |
* @param {String} entryPoint entry point for the hook to remove
|
|
|
974 |
*/
|
|
|
975 |
DOMPurify.removeHook = function (entryPoint) {
|
|
|
976 |
if (hooks[entryPoint]) {
|
|
|
977 |
hooks[entryPoint].pop();
|
|
|
978 |
}
|
|
|
979 |
};
|
|
|
980 |
|
|
|
981 |
/**
|
|
|
982 |
* RemoveHooks
|
|
|
983 |
* Public method to remove all DOMPurify hooks at a given entryPoint
|
|
|
984 |
*
|
|
|
985 |
* @param {String} entryPoint entry point for the hooks to remove
|
|
|
986 |
*/
|
|
|
987 |
DOMPurify.removeHooks = function (entryPoint) {
|
|
|
988 |
if (hooks[entryPoint]) {
|
|
|
989 |
hooks[entryPoint] = [];
|
|
|
990 |
}
|
|
|
991 |
};
|
|
|
992 |
|
|
|
993 |
/**
|
|
|
994 |
* RemoveAllHooks
|
|
|
995 |
* Public method to remove all DOMPurify hooks
|
|
|
996 |
*
|
|
|
997 |
*/
|
|
|
998 |
DOMPurify.removeAllHooks = function () {
|
|
|
999 |
hooks = {};
|
|
|
1000 |
};
|
|
|
1001 |
|
|
|
1002 |
return DOMPurify;
|
|
|
1003 |
}
|
|
|
1004 |
|
|
|
1005 |
var purify = createDOMPurify();
|
|
|
1006 |
|
|
|
1007 |
return purify;
|
|
|
1008 |
|
|
|
1009 |
})));
|