xss.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * filter xss
  3. *
  4. * @author Zongmin Lei<leizongmin@gmail.com>
  5. */
  6. var FilterCSS = require("cssfilter").FilterCSS;
  7. var DEFAULT = require("./default");
  8. var parser = require("./parser");
  9. var parseTag = parser.parseTag;
  10. var parseAttr = parser.parseAttr;
  11. var _ = require("./util");
  12. /**
  13. * returns `true` if the input value is `undefined` or `null`
  14. *
  15. * @param {Object} obj
  16. * @return {Boolean}
  17. */
  18. function isNull(obj) {
  19. return obj === undefined || obj === null;
  20. }
  21. /**
  22. * get attributes for a tag
  23. *
  24. * @param {String} html
  25. * @return {Object}
  26. * - {String} html
  27. * - {Boolean} closing
  28. */
  29. function getAttrs(html) {
  30. var i = _.spaceIndex(html);
  31. if (i === -1) {
  32. return {
  33. html: "",
  34. closing: html[html.length - 2] === "/",
  35. };
  36. }
  37. html = _.trim(html.slice(i + 1, -1));
  38. var isClosing = html[html.length - 1] === "/";
  39. if (isClosing) html = _.trim(html.slice(0, -1));
  40. return {
  41. html: html,
  42. closing: isClosing,
  43. };
  44. }
  45. /**
  46. * shallow copy
  47. *
  48. * @param {Object} obj
  49. * @return {Object}
  50. */
  51. function shallowCopyObject(obj) {
  52. var ret = {};
  53. for (var i in obj) {
  54. ret[i] = obj[i];
  55. }
  56. return ret;
  57. }
  58. /**
  59. * FilterXSS class
  60. *
  61. * @param {Object} options
  62. * whiteList (or allowList), onTag, onTagAttr, onIgnoreTag,
  63. * onIgnoreTagAttr, safeAttrValue, escapeHtml
  64. * stripIgnoreTagBody, allowCommentTag, stripBlankChar
  65. * css{whiteList, onAttr, onIgnoreAttr} `css=false` means don't use `cssfilter`
  66. */
  67. function FilterXSS(options) {
  68. options = shallowCopyObject(options || {});
  69. if (options.stripIgnoreTag) {
  70. if (options.onIgnoreTag) {
  71. console.error(
  72. 'Notes: cannot use these two options "stripIgnoreTag" and "onIgnoreTag" at the same time'
  73. );
  74. }
  75. options.onIgnoreTag = DEFAULT.onIgnoreTagStripAll;
  76. }
  77. options.whiteList = options.whiteList || options.allowList || DEFAULT.whiteList;
  78. options.onTag = options.onTag || DEFAULT.onTag;
  79. options.onTagAttr = options.onTagAttr || DEFAULT.onTagAttr;
  80. options.onIgnoreTag = options.onIgnoreTag || DEFAULT.onIgnoreTag;
  81. options.onIgnoreTagAttr = options.onIgnoreTagAttr || DEFAULT.onIgnoreTagAttr;
  82. options.safeAttrValue = options.safeAttrValue || DEFAULT.safeAttrValue;
  83. options.escapeHtml = options.escapeHtml || DEFAULT.escapeHtml;
  84. this.options = options;
  85. if (options.css === false) {
  86. this.cssFilter = false;
  87. } else {
  88. options.css = options.css || {};
  89. this.cssFilter = new FilterCSS(options.css);
  90. }
  91. }
  92. /**
  93. * start process and returns result
  94. *
  95. * @param {String} html
  96. * @return {String}
  97. */
  98. FilterXSS.prototype.process = function (html) {
  99. // compatible with the input
  100. html = html || "";
  101. html = html.toString();
  102. if (!html) return "";
  103. var me = this;
  104. var options = me.options;
  105. var whiteList = options.whiteList;
  106. var onTag = options.onTag;
  107. var onIgnoreTag = options.onIgnoreTag;
  108. var onTagAttr = options.onTagAttr;
  109. var onIgnoreTagAttr = options.onIgnoreTagAttr;
  110. var safeAttrValue = options.safeAttrValue;
  111. var escapeHtml = options.escapeHtml;
  112. var cssFilter = me.cssFilter;
  113. // remove invisible characters
  114. if (options.stripBlankChar) {
  115. html = DEFAULT.stripBlankChar(html);
  116. }
  117. // remove html comments
  118. if (!options.allowCommentTag) {
  119. html = DEFAULT.stripCommentTag(html);
  120. }
  121. // if enable stripIgnoreTagBody
  122. var stripIgnoreTagBody = false;
  123. if (options.stripIgnoreTagBody) {
  124. var stripIgnoreTagBody = DEFAULT.StripTagBody(
  125. options.stripIgnoreTagBody,
  126. onIgnoreTag
  127. );
  128. onIgnoreTag = stripIgnoreTagBody.onIgnoreTag;
  129. }
  130. var retHtml = parseTag(
  131. html,
  132. function (sourcePosition, position, tag, html, isClosing) {
  133. var info = {
  134. sourcePosition: sourcePosition,
  135. position: position,
  136. isClosing: isClosing,
  137. isWhite: whiteList.hasOwnProperty(tag),
  138. };
  139. // call `onTag()`
  140. var ret = onTag(tag, html, info);
  141. if (!isNull(ret)) return ret;
  142. if (info.isWhite) {
  143. if (info.isClosing) {
  144. return "</" + tag + ">";
  145. }
  146. var attrs = getAttrs(html);
  147. var whiteAttrList = whiteList[tag];
  148. var attrsHtml = parseAttr(attrs.html, function (name, value) {
  149. // call `onTagAttr()`
  150. var isWhiteAttr = _.indexOf(whiteAttrList, name) !== -1;
  151. var ret = onTagAttr(tag, name, value, isWhiteAttr);
  152. if (!isNull(ret)) return ret;
  153. if (isWhiteAttr) {
  154. // call `safeAttrValue()`
  155. value = safeAttrValue(tag, name, value, cssFilter);
  156. if (value) {
  157. return name + '="' + value + '"';
  158. } else {
  159. return name;
  160. }
  161. } else {
  162. // call `onIgnoreTagAttr()`
  163. var ret = onIgnoreTagAttr(tag, name, value, isWhiteAttr);
  164. if (!isNull(ret)) return ret;
  165. return;
  166. }
  167. });
  168. // build new tag html
  169. var html = "<" + tag;
  170. if (attrsHtml) html += " " + attrsHtml;
  171. if (attrs.closing) html += " /";
  172. html += ">";
  173. return html;
  174. } else {
  175. // call `onIgnoreTag()`
  176. var ret = onIgnoreTag(tag, html, info);
  177. if (!isNull(ret)) return ret;
  178. return escapeHtml(html);
  179. }
  180. },
  181. escapeHtml
  182. );
  183. // if enable stripIgnoreTagBody
  184. if (stripIgnoreTagBody) {
  185. retHtml = stripIgnoreTagBody.remove(retHtml);
  186. }
  187. return retHtml;
  188. };
  189. module.exports = FilterXSS;