xss.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. function keysToLowerCase(obj) {
  59. var ret = {};
  60. for (var i in obj) {
  61. if (Array.isArray(obj[i])) {
  62. ret[i.toLowerCase()] = obj[i].map(function (item) {
  63. return item.toLowerCase();
  64. });
  65. } else {
  66. ret[i.toLowerCase()] = obj[i];
  67. }
  68. }
  69. return ret;
  70. }
  71. /**
  72. * FilterXSS class
  73. *
  74. * @param {Object} options
  75. * whiteList (or allowList), onTag, onTagAttr, onIgnoreTag,
  76. * onIgnoreTagAttr, safeAttrValue, escapeHtml
  77. * stripIgnoreTagBody, allowCommentTag, stripBlankChar
  78. * css{whiteList, onAttr, onIgnoreAttr} `css=false` means don't use `cssfilter`
  79. */
  80. function FilterXSS(options) {
  81. options = shallowCopyObject(options || {});
  82. if (options.stripIgnoreTag) {
  83. if (options.onIgnoreTag) {
  84. console.error(
  85. 'Notes: cannot use these two options "stripIgnoreTag" and "onIgnoreTag" at the same time'
  86. );
  87. }
  88. options.onIgnoreTag = DEFAULT.onIgnoreTagStripAll;
  89. }
  90. if (options.whiteList || options.allowList) {
  91. options.whiteList = keysToLowerCase(options.whiteList || options.allowList);
  92. } else {
  93. options.whiteList = DEFAULT.whiteList;
  94. }
  95. options.onTag = options.onTag || DEFAULT.onTag;
  96. options.onTagAttr = options.onTagAttr || DEFAULT.onTagAttr;
  97. options.onIgnoreTag = options.onIgnoreTag || DEFAULT.onIgnoreTag;
  98. options.onIgnoreTagAttr = options.onIgnoreTagAttr || DEFAULT.onIgnoreTagAttr;
  99. options.safeAttrValue = options.safeAttrValue || DEFAULT.safeAttrValue;
  100. options.escapeHtml = options.escapeHtml || DEFAULT.escapeHtml;
  101. this.options = options;
  102. if (options.css === false) {
  103. this.cssFilter = false;
  104. } else {
  105. options.css = options.css || {};
  106. this.cssFilter = new FilterCSS(options.css);
  107. }
  108. }
  109. /**
  110. * start process and returns result
  111. *
  112. * @param {String} html
  113. * @return {String}
  114. */
  115. FilterXSS.prototype.process = function (html) {
  116. // compatible with the input
  117. html = html || "";
  118. html = html.toString();
  119. if (!html) return "";
  120. var me = this;
  121. var options = me.options;
  122. var whiteList = options.whiteList;
  123. var onTag = options.onTag;
  124. var onIgnoreTag = options.onIgnoreTag;
  125. var onTagAttr = options.onTagAttr;
  126. var onIgnoreTagAttr = options.onIgnoreTagAttr;
  127. var safeAttrValue = options.safeAttrValue;
  128. var escapeHtml = options.escapeHtml;
  129. var cssFilter = me.cssFilter;
  130. // remove invisible characters
  131. if (options.stripBlankChar) {
  132. html = DEFAULT.stripBlankChar(html);
  133. }
  134. // remove html comments
  135. if (!options.allowCommentTag) {
  136. html = DEFAULT.stripCommentTag(html);
  137. }
  138. // if enable stripIgnoreTagBody
  139. var stripIgnoreTagBody = false;
  140. if (options.stripIgnoreTagBody) {
  141. stripIgnoreTagBody = DEFAULT.StripTagBody(
  142. options.stripIgnoreTagBody,
  143. onIgnoreTag
  144. );
  145. onIgnoreTag = stripIgnoreTagBody.onIgnoreTag;
  146. }
  147. var retHtml = parseTag(
  148. html,
  149. function (sourcePosition, position, tag, html, isClosing) {
  150. var info = {
  151. sourcePosition: sourcePosition,
  152. position: position,
  153. isClosing: isClosing,
  154. isWhite: Object.prototype.hasOwnProperty.call(whiteList, tag),
  155. };
  156. // call `onTag()`
  157. var ret = onTag(tag, html, info);
  158. if (!isNull(ret)) return ret;
  159. if (info.isWhite) {
  160. if (info.isClosing) {
  161. return "</" + tag + ">";
  162. }
  163. var attrs = getAttrs(html);
  164. var whiteAttrList = whiteList[tag];
  165. var attrsHtml = parseAttr(attrs.html, function (name, value) {
  166. // call `onTagAttr()`
  167. var isWhiteAttr = _.indexOf(whiteAttrList, name) !== -1;
  168. var ret = onTagAttr(tag, name, value, isWhiteAttr);
  169. if (!isNull(ret)) return ret;
  170. if (isWhiteAttr) {
  171. // call `safeAttrValue()`
  172. value = safeAttrValue(tag, name, value, cssFilter);
  173. if (value) {
  174. return name + '="' + value + '"';
  175. } else {
  176. return name;
  177. }
  178. } else {
  179. // call `onIgnoreTagAttr()`
  180. ret = onIgnoreTagAttr(tag, name, value, isWhiteAttr);
  181. if (!isNull(ret)) return ret;
  182. return;
  183. }
  184. });
  185. // build new tag html
  186. html = "<" + tag;
  187. if (attrsHtml) html += " " + attrsHtml;
  188. if (attrs.closing) html += " /";
  189. html += ">";
  190. return html;
  191. } else {
  192. // call `onIgnoreTag()`
  193. ret = onIgnoreTag(tag, html, info);
  194. if (!isNull(ret)) return ret;
  195. return escapeHtml(html);
  196. }
  197. },
  198. escapeHtml
  199. );
  200. // if enable stripIgnoreTagBody
  201. if (stripIgnoreTagBody) {
  202. retHtml = stripIgnoreTagBody.remove(retHtml);
  203. }
  204. return retHtml;
  205. };
  206. module.exports = FilterXSS;