secureDOMPurify.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import DOMPurify from 'dompurify';
  2. // Centralized secure DOMPurify configuration to prevent XSS and CSS injection attacks
  3. export function getSecureDOMPurifyConfig() {
  4. return {
  5. // Block dangerous elements that can cause XSS and CSS injection
  6. FORBID_TAGS: [
  7. 'svg', 'defs', 'use', 'g', 'symbol', 'marker', 'pattern', 'mask', 'clipPath',
  8. 'linearGradient', 'radialGradient', 'stop', 'animate', 'animateTransform',
  9. 'animateMotion', 'set', 'switch', 'foreignObject', 'script', 'style', 'link',
  10. 'meta', 'iframe', 'object', 'embed', 'applet', 'form', 'input', 'textarea',
  11. 'select', 'option', 'button', 'label', 'fieldset', 'legend', 'frameset',
  12. 'frame', 'noframes', 'base', 'basefont', 'isindex', 'dir', 'menu', 'menuitem'
  13. ],
  14. // Block dangerous attributes that can cause XSS and CSS injection
  15. FORBID_ATTR: [
  16. 'xlink:href', 'href', 'onload', 'onerror', 'onclick', 'onmouseover',
  17. 'onfocus', 'onblur', 'onchange', 'onsubmit', 'onreset', 'onselect',
  18. 'onunload', 'onresize', 'onscroll', 'onkeydown', 'onkeyup', 'onkeypress',
  19. 'onmousedown', 'onmouseup', 'onmouseover', 'onmouseout', 'onmousemove',
  20. 'ondblclick', 'oncontextmenu', 'onwheel', 'ontouchstart', 'ontouchend',
  21. 'ontouchmove', 'ontouchcancel', 'onabort', 'oncanplay', 'oncanplaythrough',
  22. 'ondurationchange', 'onemptied', 'onended', 'onerror', 'onloadeddata',
  23. 'onloadedmetadata', 'onloadstart', 'onpause', 'onplay', 'onplaying',
  24. 'onprogress', 'onratechange', 'onseeked', 'onseeking', 'onstalled',
  25. 'onsuspend', 'ontimeupdate', 'onvolumechange', 'onwaiting', 'onbeforeunload',
  26. 'onhashchange', 'onpagehide', 'onpageshow', 'onpopstate', 'onstorage',
  27. 'onunload', 'style', 'class', 'id', 'data-*', 'aria-*'
  28. ],
  29. // Allow only safe image formats and protocols
  30. ALLOWED_URI_REGEXP: /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|cid|xmpp):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i,
  31. // Remove dangerous protocols
  32. ALLOW_UNKNOWN_PROTOCOLS: false,
  33. // Sanitize URLs to prevent malicious content loading
  34. SANITIZE_DOM: true,
  35. // Remove dangerous elements completely
  36. KEEP_CONTENT: false,
  37. // Additional security measures
  38. ADD_ATTR: [],
  39. // Block data URIs that could contain malicious content
  40. ALLOW_DATA_ATTR: false,
  41. // Custom hook to further sanitize content
  42. HOOKS: {
  43. uponSanitizeElement: function(node, data) {
  44. // Block any remaining dangerous elements
  45. const dangerousTags = ['svg', 'style', 'script', 'link', 'meta', 'iframe', 'object', 'embed', 'applet'];
  46. if (node.tagName && dangerousTags.includes(node.tagName.toLowerCase())) {
  47. if (process.env.DEBUG === 'true') {
  48. console.warn('Blocked potentially dangerous element:', node.tagName);
  49. }
  50. return false;
  51. }
  52. // Block img tags with SVG data URIs
  53. if (node.tagName && node.tagName.toLowerCase() === 'img') {
  54. const src = node.getAttribute('src');
  55. if (src && (src.startsWith('data:image/svg') || src.endsWith('.svg'))) {
  56. if (process.env.DEBUG === 'true') {
  57. console.warn('Blocked potentially malicious SVG image:', src);
  58. }
  59. return false;
  60. }
  61. }
  62. // Block elements with dangerous attributes
  63. const dangerousAttrs = ['style', 'onload', 'onerror', 'onclick', 'onmouseover', 'onfocus', 'onblur'];
  64. for (const attr of dangerousAttrs) {
  65. if (node.hasAttribute && node.hasAttribute(attr)) {
  66. if (process.env.DEBUG === 'true') {
  67. console.warn('Blocked element with dangerous attribute:', node.tagName, attr);
  68. }
  69. return false;
  70. }
  71. }
  72. return true;
  73. },
  74. uponSanitizeAttribute: function(node, data) {
  75. // Block style attributes completely
  76. if (data.attrName === 'style') {
  77. if (process.env.DEBUG === 'true') {
  78. console.warn('Blocked style attribute');
  79. }
  80. return false;
  81. }
  82. // Block class and id attributes that might be used for CSS injection
  83. if (data.attrName === 'class' || data.attrName === 'id') {
  84. if (process.env.DEBUG === 'true') {
  85. console.warn('Blocked class/id attribute:', data.attrName, data.attrValue);
  86. }
  87. return false;
  88. }
  89. // Block data attributes
  90. if (data.attrName && data.attrName.startsWith('data-')) {
  91. if (process.env.DEBUG === 'true') {
  92. console.warn('Blocked data attribute:', data.attrName);
  93. }
  94. return false;
  95. }
  96. return true;
  97. }
  98. }
  99. };
  100. }
  101. // Convenience function for secure sanitization
  102. export function sanitizeHTML(html) {
  103. return DOMPurify.sanitize(html, getSecureDOMPurifyConfig());
  104. }
  105. // Convenience function for sanitizing text (no HTML)
  106. export function sanitizeText(text) {
  107. return DOMPurify.sanitize(text, {
  108. ALLOWED_TAGS: [],
  109. ALLOWED_ATTR: [],
  110. KEEP_CONTENT: true
  111. });
  112. }