xss.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. class Xss {
  2. constructor(crowi) {
  3. const xss = require('xss');
  4. const config = crowi.config;
  5. const isXSSPrevented = config.isXSSPrevented;
  6. const XSSOption = config.XSSOption;
  7. let tagWhiteList = config.tagWhiteList;
  8. let attrWhiteList = config.attrWhiteList;
  9. /**
  10. * reference: https://meta.stackexchange.com/questions/1777/what-html-tags-are-allowed-on-stack-exchange-sites
  11. */
  12. const recommendedTagWhiteList = [
  13. 'a', 'b', 'blockquote', 'blockquote', 'code', 'del', 'dd', 'dl', 'dt', 'em',
  14. 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'i', 'img', 'kbd', 'li', 'ol', 'p', 'pre',
  15. 's', 'sup', 'sub', 'strong', 'strike', 'ul', 'br', 'hr'
  16. ];
  17. const recommendedAttrWhiteList = ['src', 'width', 'height', 'alt', 'title', 'href'];
  18. let whiteListContent = {};
  19. // default
  20. let option = {
  21. stripIgnoreTag: true,
  22. stripIgnoreTagBody: true,
  23. css: false,
  24. whiteList: whiteListContent,
  25. escapeHtml: (html) => html, // resolve https://github.com/weseek/growi/issues/221
  26. };
  27. if (isXSSPrevented) {
  28. switch (XSSOption) {
  29. case 1: // ignore all: use default option
  30. break;
  31. case 2: // recommended
  32. recommendedTagWhiteList.forEach(tag => {
  33. whiteListContent[tag] = recommendedAttrWhiteList;
  34. });
  35. option['whiteList'] = whiteListContent;
  36. break;
  37. case 3: // custom white list
  38. tagWhiteList.forEach(tag => {
  39. whiteListContent[tag] = attrWhiteList;
  40. });
  41. option['whiteList'] = whiteListContent;
  42. break;
  43. default:
  44. }
  45. }
  46. else {
  47. option['stripIgnoreTag'] = false;
  48. option['stripIgnoreTagBody'] = false;
  49. }
  50. // create the XSS Filter instance
  51. this.myxss = new xss.FilterXSS(option);
  52. }
  53. process(markdown) {
  54. return this.myxss.process(markdown);
  55. }
  56. }
  57. module.exports = Xss;