sanitize.js 9.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const sanitizeHtml = require('sanitize-html')
  2. const sanitize = html => sanitizeHtml(html, {
  3. allowedTags: [
  4. // HTML
  5. 'a', 'abbr', 'acronym', 'address', /*'applet',*/ 'area', 'article', 'aside', /*'audio'*/, 'b', 'base', 'basefont', 'bdi',
  6. 'bdo', 'big', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'center', 'cite', 'code', 'col', 'colgroup',
  7. 'data', 'datalist', 'dd', 'del', 'details', 'dfn', 'dialog', 'dir', 'div', 'dl', 'dt', 'em', /*'embed',*/ 'fieldset',
  8. 'figcaption', 'figure', 'font', 'footer', 'form', 'frame', 'frameset', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head',
  9. 'header', 'hr', 'html', 'i', /*'iframe',*/ 'img', 'input', 'ins', 'kbd', 'label', 'legend', 'li', 'link', 'main',
  10. 'map', 'mark', 'meta', 'meter', 'nav', 'noframes', 'noscript', /*'object',*/ 'ol', 'optgroup', 'option', 'output',
  11. 'p', /*'param',*/ 'picture', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', /*'script',*/ 'section', 'select',
  12. 'small', 'source', 'span', 'strike', 'strong', 'style', 'sub', 'summary', 'sup', 'svg', 'table', 'tbody', 'td', 'template',
  13. 'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'track', 'tt', 'u', 'ul', 'var', /*'video',*/ 'wbr',
  14. // SVG
  15. 'a', 'altGlyph', 'altGlyphDef', 'altGlyphItem', 'animate', 'animateColor', 'animateMotion', 'animateTransform', 'circle',
  16. 'clipPath', 'color-profile', 'cursor', 'defs', 'desc', 'discard', 'ellipse', 'feBlend', 'feColorMatrix',
  17. 'feComponentTransfer', 'feComposite', 'feConvolveMatrix', 'feDiffuseLighting', 'feDisplacementMap', 'feDistantLight',
  18. 'feDropShadow', 'feFlood', 'feFuncA', 'feFuncB', 'feFuncG', 'feFuncR', 'feGaussianBlur', 'feImage', 'feMerge', 'feMergeNode',
  19. 'feMorphology', 'feOffset', 'fePointLight', 'feSpecularLighting', 'feSpotLight', 'feTile', 'feTurbulence', 'filter', 'font',
  20. 'font-face', 'font-face-format', 'font-face-name', 'font-face-src', 'font-face-uri', 'foreignObject', 'g', 'glyph',
  21. 'glyphRef', 'hatch', 'hatchpath', 'hkern', 'image', 'line', 'linearGradient', 'marker', 'mask', 'mesh', 'meshgradient',
  22. 'meshpatch', 'meshrow', 'metadata', 'missing-glyph', 'mpath', 'path', 'pattern', 'polygon', 'polyline', 'radialGradient',
  23. 'rect', /*'script',*/ 'set', 'solidcolor', 'stop', 'style', 'svg', 'switch', 'symbol', 'text', 'textPath', 'title', 'tref',
  24. 'tspan', 'unknown', 'use', 'view', 'vkern'
  25. ],
  26. allowedAttributes: {
  27. '*': [
  28. // HTML
  29. 'accept', 'accept-charset', 'accesskey', 'action', 'align', 'alt', 'async', 'autocomplete', 'autofocus', 'autoplay',
  30. 'bgcolor', 'border', 'charset', 'checked', 'cite', 'class', 'color', 'cols', 'colspan', 'content', 'contenteditable',
  31. 'controls', 'coords', 'data', 'data-*', 'datetime', 'default', 'defer', 'dir', 'dirname', 'disabled', 'download',
  32. 'draggable', 'dropzone', 'enctype', 'for', 'form', 'formaction', 'headers', 'height', 'hidden', 'high', 'href',
  33. 'hreflang', /*'http-equiv'*/, 'id', 'ismap', 'kind', 'label', 'lang', 'list', 'loop', 'low', 'max', 'maxlength', 'media',
  34. 'method', 'min', 'multiple', 'muted', 'name', 'novalidate', /*'onabort',*/ /*'onafterprint',*/ /*'onbeforeprint',*/
  35. /*'onbeforeunload',*/ /*'onblur',*/ /*'oncanplay',*/ /*'oncanplaythrough',*/ /*'onchange',*/ /*'onclick',*/
  36. /*'oncontextmenu',*/ /*'oncopy',*/ /*'oncuechange',*/ /*'oncut',*/ /*'ondblclick',*/ /*'ondrag',*/ /*'ondragend',*/
  37. /*'ondragenter',*/ /*'ondragleave',*/ /*'ondragover',*/ /*'ondragstart',*/ /*'ondrop',*/ /*'ondurationchange',*/
  38. /*'onemptied',*/ /*'onended',*/ /*'onerror',*/ /*'onfocus',*/ /*'onhashchange',*/ /*'oninput',*/ /*'oninvalid',*/
  39. /*'onkeydown',*/ /*'onkeypress',*/ /*'onkeyup',*/ /*'onload',*/ /*'onloadeddata',*/ /*'onloadedmetadata',*/
  40. /*'onloadstart',*/ /*'onmousedown',*/ /*'onmousemove',*/ /*'onmouseout',*/ /*'onmouseover',*/ /*'onmouseup',*/
  41. /*'onmousewheel',*/ /*'onoffline',*/ /*'ononline',*/ /*'onpagehide',*/ /*'onpageshow',*/ /*'onpaste',*/ /*'onpause',*/
  42. /*'onplay',*/ /*'onplaying',*/ /*'onpopstate',*/ /*'onprogress',*/ /*'onratechange',*/ /*'onreset',*/ /*'onresize',*/
  43. /*'onscroll',*/ /*'onsearch',*/ /*'onseeked',*/ /*'onseeking',*/ /*'onselect',*/ /*'onstalled',*/ /*'onstorage',*/
  44. /*'onsubmit',*/ /*'onsuspend',*/ /*'ontimeupdate',*/ /*'ontoggle',*/ /*'onunload',*/ /*'onvolumechange',*/
  45. /*'onwaiting',*/ /*'onwheel',*/ 'open', 'optimum', 'pattern', 'placeholder', 'poster', 'preload', 'readonly', 'rel',
  46. 'required', 'reversed', 'rows', 'rowspan', 'sandbox', 'scope', 'selected', 'shape', 'size', 'sizes', 'span', 'spellcheck',
  47. 'src', 'srcdoc', 'srclang', 'srcset', 'start', 'step', 'style', 'tabindex', 'target', 'title', 'translate', 'type',
  48. 'usemap', 'value', 'width', 'wrap',
  49. // SVG
  50. 'accent-height', 'accumulate', 'additive', 'alignment-baseline', 'allowReorder', 'alphabetic', 'amplitude',
  51. 'arabic-form', 'ascent', 'attributeName', 'attributeType', 'autoReverse', 'azimuth', 'baseFrequency', 'baseline-shift',
  52. 'baseProfile', 'bbox', 'begin', 'bias', 'by', 'calcMode', 'cap-height', 'class', 'clip', 'clipPathUnits', 'clip-path',
  53. 'clip-rule', 'color', 'color-interpolation', 'color-interpolation-filters', 'color-profile', 'color-rendering',
  54. 'contentScriptType', 'contentStyleType', 'cursor', 'cx', 'cy', 'd', 'decelerate', 'descent', 'diffuseConstant',
  55. 'direction', 'display', 'divisor', 'dominant-baseline', 'dur', 'dx', 'dy', 'edgeMode', 'elevation', 'enable-background',
  56. 'end', 'exponent', 'externalResourcesRequired', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'filterRes', 'filterUnits',
  57. 'flood-color', 'flood-opacity', 'font-family', 'font-size', 'font-size-adjust', 'font-stretch', 'font-style',
  58. 'font-variant', 'font-weight', 'format', 'from', 'fr', 'fx', 'fy', 'g1', 'g2', 'glyph-name', 'glyph-orientation-horizontal',
  59. 'glyph-orientation-vertical', 'glyphRef', 'gradientTransform', 'gradientUnits', 'hanging', 'height', 'href', 'hreflang',
  60. 'horiz-adv-x', 'horiz-origin-x', 'id', 'ideographic', 'image-rendering', 'in', 'in2', 'intercept', 'k', 'k1', 'k2', 'k3',
  61. 'k4', 'kernelMatrix', 'kernelUnitLength', 'kerning', 'keyPoints', 'keySplines', 'keyTimes', 'lang', 'lengthAdjust',
  62. 'letter-spacing', 'lighting-color', 'limitingConeAngle', 'local', 'marker-end', 'marker-mid', 'marker-start',
  63. 'markerHeight', 'markerUnits', 'markerWidth', 'mask', 'maskContentUnits', 'maskUnits', 'mathematical', 'max', 'media',
  64. 'method', 'min', 'mode', 'name', 'numOctaves', 'offset', 'opacity', 'operator', 'order', 'orient', 'orientation', 'origin',
  65. 'overflow', 'overline-position', 'overline-thickness', 'panose-1', 'paint-order', 'path', 'pathLength',
  66. 'patternContentUnits', 'patternTransform', 'patternUnits', 'ping', 'pointer-events', 'points', 'pointsAtX', 'pointsAtY',
  67. 'pointsAtZ', 'preserveAlpha', 'preserveAspectRatio', 'primitiveUnits', 'r', 'radius', 'referrerPolicy', 'refX', 'refY',
  68. 'rel', 'rendering-intent', 'repeatCount', 'repeatDur', 'requiredExtensions', 'requiredFeatures', 'restart', 'result',
  69. 'rotate', 'rx', 'ry', 'scale', 'seed', 'shape-rendering', 'slope', 'spacing', 'specularConstant', 'specularExponent',
  70. 'speed', 'spreadMethod', 'startOffset', 'stdDeviation', 'stemh', 'stemv', 'stitchTiles', 'stop-color', 'stop-opacity',
  71. 'strikethrough-position', 'strikethrough-thickness', 'string', 'stroke', 'stroke-dasharray', 'stroke-dashoffset',
  72. 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'style', 'surfaceScale',
  73. 'systemLanguage', 'tabindex', 'tableValues', 'target', 'targetX', 'targetY', 'text-anchor', 'text-decoration',
  74. 'text-rendering', 'textLength', 'to', 'transform', 'type', 'u1', 'u2', 'underline-position', 'underline-thickness',
  75. 'unicode', 'unicode-bidi', 'unicode-range', 'units-per-em', 'v-alphabetic', 'v-hanging', 'v-ideographic',
  76. 'v-mathematical', 'values', 'vector-effect', 'version', 'vert-adv-y', 'vert-origin-x', 'vert-origin-y', 'viewBox',
  77. 'viewTarget', 'visibility', 'width', 'widths', 'word-spacing', 'writing-mode', 'x', 'x-height', 'x1', 'x2',
  78. 'xChannelSelector', 'xlink:actuate', 'xlink:arcrole', 'xlink:href', 'xlink:role', 'xlink:show', 'xlink:title',
  79. 'xlink:type', 'xml:base', 'xml:lang', 'xml:space', 'y', 'y1', 'y2', 'yChannelSelector', 'z', 'zoomAndPan'
  80. ]
  81. },
  82. selfClosing: [ 'img', 'br', 'hr', 'area', 'base', 'basefont', 'input', 'link', 'meta' ],
  83. allowedSchemes: [ 'http', 'https', 'ftp', 'mailto', 'data' ],
  84. allowedSchemesByTag: {},
  85. allowedSchemesAppliedToAttributes: [ 'href', 'src', 'cite' ],
  86. allowProtocolRelative: true,
  87. allowedIframeHostnames: []
  88. })
  89. module.exports = sanitize