xss.js 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665
  1. (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
  2. /**
  3. * default settings
  4. *
  5. * @author Zongmin Lei<leizongmin@gmail.com>
  6. */
  7. var FilterCSS = require("cssfilter").FilterCSS;
  8. var getDefaultCSSWhiteList = require("cssfilter").getDefaultWhiteList;
  9. var _ = require("./util");
  10. function getDefaultWhiteList() {
  11. return {
  12. a: ["target", "href", "title"],
  13. abbr: ["title"],
  14. address: [],
  15. area: ["shape", "coords", "href", "alt"],
  16. article: [],
  17. aside: [],
  18. audio: [
  19. "autoplay",
  20. "controls",
  21. "crossorigin",
  22. "loop",
  23. "muted",
  24. "preload",
  25. "src",
  26. ],
  27. b: [],
  28. bdi: ["dir"],
  29. bdo: ["dir"],
  30. big: [],
  31. blockquote: ["cite"],
  32. br: [],
  33. caption: [],
  34. center: [],
  35. cite: [],
  36. code: [],
  37. col: ["align", "valign", "span", "width"],
  38. colgroup: ["align", "valign", "span", "width"],
  39. dd: [],
  40. del: ["datetime"],
  41. details: ["open"],
  42. div: [],
  43. dl: [],
  44. dt: [],
  45. em: [],
  46. figcaption: [],
  47. figure: [],
  48. font: ["color", "size", "face"],
  49. footer: [],
  50. h1: [],
  51. h2: [],
  52. h3: [],
  53. h4: [],
  54. h5: [],
  55. h6: [],
  56. header: [],
  57. hr: [],
  58. i: [],
  59. img: ["src", "alt", "title", "width", "height"],
  60. ins: ["datetime"],
  61. li: [],
  62. mark: [],
  63. nav: [],
  64. ol: [],
  65. p: [],
  66. pre: [],
  67. s: [],
  68. section: [],
  69. small: [],
  70. span: [],
  71. sub: [],
  72. summary: [],
  73. sup: [],
  74. strong: [],
  75. strike: [],
  76. table: ["width", "border", "align", "valign"],
  77. tbody: ["align", "valign"],
  78. td: ["width", "rowspan", "colspan", "align", "valign"],
  79. tfoot: ["align", "valign"],
  80. th: ["width", "rowspan", "colspan", "align", "valign"],
  81. thead: ["align", "valign"],
  82. tr: ["rowspan", "align", "valign"],
  83. tt: [],
  84. u: [],
  85. ul: [],
  86. video: [
  87. "autoplay",
  88. "controls",
  89. "crossorigin",
  90. "loop",
  91. "muted",
  92. "playsinline",
  93. "poster",
  94. "preload",
  95. "src",
  96. "height",
  97. "width",
  98. ],
  99. };
  100. }
  101. var defaultCSSFilter = new FilterCSS();
  102. /**
  103. * default onTag function
  104. *
  105. * @param {String} tag
  106. * @param {String} html
  107. * @param {Object} options
  108. * @return {String}
  109. */
  110. function onTag(tag, html, options) {
  111. // do nothing
  112. }
  113. /**
  114. * default onIgnoreTag function
  115. *
  116. * @param {String} tag
  117. * @param {String} html
  118. * @param {Object} options
  119. * @return {String}
  120. */
  121. function onIgnoreTag(tag, html, options) {
  122. // do nothing
  123. }
  124. /**
  125. * default onTagAttr function
  126. *
  127. * @param {String} tag
  128. * @param {String} name
  129. * @param {String} value
  130. * @return {String}
  131. */
  132. function onTagAttr(tag, name, value) {
  133. // do nothing
  134. }
  135. /**
  136. * default onIgnoreTagAttr function
  137. *
  138. * @param {String} tag
  139. * @param {String} name
  140. * @param {String} value
  141. * @return {String}
  142. */
  143. function onIgnoreTagAttr(tag, name, value) {
  144. // do nothing
  145. }
  146. /**
  147. * default escapeHtml function
  148. *
  149. * @param {String} html
  150. */
  151. function escapeHtml(html) {
  152. return html.replace(REGEXP_LT, "&lt;").replace(REGEXP_GT, "&gt;");
  153. }
  154. /**
  155. * default safeAttrValue function
  156. *
  157. * @param {String} tag
  158. * @param {String} name
  159. * @param {String} value
  160. * @param {Object} cssFilter
  161. * @return {String}
  162. */
  163. function safeAttrValue(tag, name, value, cssFilter) {
  164. // unescape attribute value firstly
  165. value = friendlyAttrValue(value);
  166. if (name === "href" || name === "src") {
  167. // filter `href` and `src` attribute
  168. // only allow the value that starts with `http://` | `https://` | `mailto:` | `/` | `#`
  169. value = _.trim(value);
  170. if (value === "#") return "#";
  171. if (
  172. !(
  173. value.substr(0, 7) === "http://" ||
  174. value.substr(0, 8) === "https://" ||
  175. value.substr(0, 7) === "mailto:" ||
  176. value.substr(0, 4) === "tel:" ||
  177. value.substr(0, 11) === "data:image/" ||
  178. value.substr(0, 6) === "ftp://" ||
  179. value.substr(0, 2) === "./" ||
  180. value.substr(0, 3) === "../" ||
  181. value[0] === "#" ||
  182. value[0] === "/"
  183. )
  184. ) {
  185. return "";
  186. }
  187. } else if (name === "background") {
  188. // filter `background` attribute (maybe no use)
  189. // `javascript:`
  190. REGEXP_DEFAULT_ON_TAG_ATTR_4.lastIndex = 0;
  191. if (REGEXP_DEFAULT_ON_TAG_ATTR_4.test(value)) {
  192. return "";
  193. }
  194. } else if (name === "style") {
  195. // `expression()`
  196. REGEXP_DEFAULT_ON_TAG_ATTR_7.lastIndex = 0;
  197. if (REGEXP_DEFAULT_ON_TAG_ATTR_7.test(value)) {
  198. return "";
  199. }
  200. // `url()`
  201. REGEXP_DEFAULT_ON_TAG_ATTR_8.lastIndex = 0;
  202. if (REGEXP_DEFAULT_ON_TAG_ATTR_8.test(value)) {
  203. REGEXP_DEFAULT_ON_TAG_ATTR_4.lastIndex = 0;
  204. if (REGEXP_DEFAULT_ON_TAG_ATTR_4.test(value)) {
  205. return "";
  206. }
  207. }
  208. if (cssFilter !== false) {
  209. cssFilter = cssFilter || defaultCSSFilter;
  210. value = cssFilter.process(value);
  211. }
  212. }
  213. // escape `<>"` before returns
  214. value = escapeAttrValue(value);
  215. return value;
  216. }
  217. // RegExp list
  218. var REGEXP_LT = /</g;
  219. var REGEXP_GT = />/g;
  220. var REGEXP_QUOTE = /"/g;
  221. var REGEXP_QUOTE_2 = /&quot;/g;
  222. var REGEXP_ATTR_VALUE_1 = /&#([a-zA-Z0-9]*);?/gim;
  223. var REGEXP_ATTR_VALUE_COLON = /&colon;?/gim;
  224. var REGEXP_ATTR_VALUE_NEWLINE = /&newline;?/gim;
  225. var REGEXP_DEFAULT_ON_TAG_ATTR_3 = /\/\*|\*\//gm;
  226. var REGEXP_DEFAULT_ON_TAG_ATTR_4 =
  227. /((j\s*a\s*v\s*a|v\s*b|l\s*i\s*v\s*e)\s*s\s*c\s*r\s*i\s*p\s*t\s*|m\s*o\s*c\s*h\s*a)\:/gi;
  228. var REGEXP_DEFAULT_ON_TAG_ATTR_5 = /^[\s"'`]*(d\s*a\s*t\s*a\s*)\:/gi;
  229. var REGEXP_DEFAULT_ON_TAG_ATTR_6 = /^[\s"'`]*(d\s*a\s*t\s*a\s*)\:\s*image\//gi;
  230. var REGEXP_DEFAULT_ON_TAG_ATTR_7 =
  231. /e\s*x\s*p\s*r\s*e\s*s\s*s\s*i\s*o\s*n\s*\(.*/gi;
  232. var REGEXP_DEFAULT_ON_TAG_ATTR_8 = /u\s*r\s*l\s*\(.*/gi;
  233. /**
  234. * escape double quote
  235. *
  236. * @param {String} str
  237. * @return {String} str
  238. */
  239. function escapeQuote(str) {
  240. return str.replace(REGEXP_QUOTE, "&quot;");
  241. }
  242. /**
  243. * unescape double quote
  244. *
  245. * @param {String} str
  246. * @return {String} str
  247. */
  248. function unescapeQuote(str) {
  249. return str.replace(REGEXP_QUOTE_2, '"');
  250. }
  251. /**
  252. * escape html entities
  253. *
  254. * @param {String} str
  255. * @return {String}
  256. */
  257. function escapeHtmlEntities(str) {
  258. return str.replace(REGEXP_ATTR_VALUE_1, function replaceUnicode(str, code) {
  259. return code[0] === "x" || code[0] === "X"
  260. ? String.fromCharCode(parseInt(code.substr(1), 16))
  261. : String.fromCharCode(parseInt(code, 10));
  262. });
  263. }
  264. /**
  265. * escape html5 new danger entities
  266. *
  267. * @param {String} str
  268. * @return {String}
  269. */
  270. function escapeDangerHtml5Entities(str) {
  271. return str
  272. .replace(REGEXP_ATTR_VALUE_COLON, ":")
  273. .replace(REGEXP_ATTR_VALUE_NEWLINE, " ");
  274. }
  275. /**
  276. * clear nonprintable characters
  277. *
  278. * @param {String} str
  279. * @return {String}
  280. */
  281. function clearNonPrintableCharacter(str) {
  282. var str2 = "";
  283. for (var i = 0, len = str.length; i < len; i++) {
  284. str2 += str.charCodeAt(i) < 32 ? " " : str.charAt(i);
  285. }
  286. return _.trim(str2);
  287. }
  288. /**
  289. * get friendly attribute value
  290. *
  291. * @param {String} str
  292. * @return {String}
  293. */
  294. function friendlyAttrValue(str) {
  295. str = unescapeQuote(str);
  296. str = escapeHtmlEntities(str);
  297. str = escapeDangerHtml5Entities(str);
  298. str = clearNonPrintableCharacter(str);
  299. return str;
  300. }
  301. /**
  302. * unescape attribute value
  303. *
  304. * @param {String} str
  305. * @return {String}
  306. */
  307. function escapeAttrValue(str) {
  308. str = escapeQuote(str);
  309. str = escapeHtml(str);
  310. return str;
  311. }
  312. /**
  313. * `onIgnoreTag` function for removing all the tags that are not in whitelist
  314. */
  315. function onIgnoreTagStripAll() {
  316. return "";
  317. }
  318. /**
  319. * remove tag body
  320. * specify a `tags` list, if the tag is not in the `tags` list then process by the specify function (optional)
  321. *
  322. * @param {array} tags
  323. * @param {function} next
  324. */
  325. function StripTagBody(tags, next) {
  326. if (typeof next !== "function") {
  327. next = function () {};
  328. }
  329. var isRemoveAllTag = !Array.isArray(tags);
  330. function isRemoveTag(tag) {
  331. if (isRemoveAllTag) return true;
  332. return _.indexOf(tags, tag) !== -1;
  333. }
  334. var removeList = [];
  335. var posStart = false;
  336. return {
  337. onIgnoreTag: function (tag, html, options) {
  338. if (isRemoveTag(tag)) {
  339. if (options.isClosing) {
  340. var ret = "[/removed]";
  341. var end = options.position + ret.length;
  342. removeList.push([
  343. posStart !== false ? posStart : options.position,
  344. end,
  345. ]);
  346. posStart = false;
  347. return ret;
  348. } else {
  349. if (!posStart) {
  350. posStart = options.position;
  351. }
  352. return "[removed]";
  353. }
  354. } else {
  355. return next(tag, html, options);
  356. }
  357. },
  358. remove: function (html) {
  359. var rethtml = "";
  360. var lastPos = 0;
  361. _.forEach(removeList, function (pos) {
  362. rethtml += html.slice(lastPos, pos[0]);
  363. lastPos = pos[1];
  364. });
  365. rethtml += html.slice(lastPos);
  366. return rethtml;
  367. },
  368. };
  369. }
  370. /**
  371. * remove html comments
  372. *
  373. * @param {String} html
  374. * @return {String}
  375. */
  376. function stripCommentTag(html) {
  377. var retHtml = "";
  378. var lastPos = 0;
  379. while (lastPos < html.length) {
  380. var i = html.indexOf("<!--", lastPos);
  381. if (i === -1) {
  382. retHtml += html.slice(lastPos);
  383. break;
  384. }
  385. retHtml += html.slice(lastPos, i);
  386. var j = html.indexOf("-->", i);
  387. if (j === -1) {
  388. break;
  389. }
  390. lastPos = j + 3;
  391. }
  392. return retHtml;
  393. }
  394. /**
  395. * remove invisible characters
  396. *
  397. * @param {String} html
  398. * @return {String}
  399. */
  400. function stripBlankChar(html) {
  401. var chars = html.split("");
  402. chars = chars.filter(function (char) {
  403. var c = char.charCodeAt(0);
  404. if (c === 127) return false;
  405. if (c <= 31) {
  406. if (c === 10 || c === 13) return true;
  407. return false;
  408. }
  409. return true;
  410. });
  411. return chars.join("");
  412. }
  413. exports.whiteList = getDefaultWhiteList();
  414. exports.getDefaultWhiteList = getDefaultWhiteList;
  415. exports.onTag = onTag;
  416. exports.onIgnoreTag = onIgnoreTag;
  417. exports.onTagAttr = onTagAttr;
  418. exports.onIgnoreTagAttr = onIgnoreTagAttr;
  419. exports.safeAttrValue = safeAttrValue;
  420. exports.escapeHtml = escapeHtml;
  421. exports.escapeQuote = escapeQuote;
  422. exports.unescapeQuote = unescapeQuote;
  423. exports.escapeHtmlEntities = escapeHtmlEntities;
  424. exports.escapeDangerHtml5Entities = escapeDangerHtml5Entities;
  425. exports.clearNonPrintableCharacter = clearNonPrintableCharacter;
  426. exports.friendlyAttrValue = friendlyAttrValue;
  427. exports.escapeAttrValue = escapeAttrValue;
  428. exports.onIgnoreTagStripAll = onIgnoreTagStripAll;
  429. exports.StripTagBody = StripTagBody;
  430. exports.stripCommentTag = stripCommentTag;
  431. exports.stripBlankChar = stripBlankChar;
  432. exports.cssFilter = defaultCSSFilter;
  433. exports.getDefaultCSSWhiteList = getDefaultCSSWhiteList;
  434. },{"./util":4,"cssfilter":8}],2:[function(require,module,exports){
  435. /**
  436. * xss
  437. *
  438. * @author Zongmin Lei<leizongmin@gmail.com>
  439. */
  440. var DEFAULT = require("./default");
  441. var parser = require("./parser");
  442. var FilterXSS = require("./xss");
  443. /**
  444. * filter xss function
  445. *
  446. * @param {String} html
  447. * @param {Object} options { whiteList, onTag, onTagAttr, onIgnoreTag, onIgnoreTagAttr, safeAttrValue, escapeHtml }
  448. * @return {String}
  449. */
  450. function filterXSS(html, options) {
  451. var xss = new FilterXSS(options);
  452. return xss.process(html);
  453. }
  454. exports = module.exports = filterXSS;
  455. exports.filterXSS = filterXSS;
  456. exports.FilterXSS = FilterXSS;
  457. for (var i in DEFAULT) exports[i] = DEFAULT[i];
  458. for (var i in parser) exports[i] = parser[i];
  459. // using `xss` on the browser, output `filterXSS` to the globals
  460. if (typeof window !== "undefined") {
  461. window.filterXSS = module.exports;
  462. }
  463. // using `xss` on the WebWorker, output `filterXSS` to the globals
  464. function isWorkerEnv() {
  465. return (
  466. typeof self !== "undefined" &&
  467. typeof DedicatedWorkerGlobalScope !== "undefined" &&
  468. self instanceof DedicatedWorkerGlobalScope
  469. );
  470. }
  471. if (isWorkerEnv()) {
  472. self.filterXSS = module.exports;
  473. }
  474. },{"./default":1,"./parser":3,"./xss":5}],3:[function(require,module,exports){
  475. /**
  476. * Simple HTML Parser
  477. *
  478. * @author Zongmin Lei<leizongmin@gmail.com>
  479. */
  480. var _ = require("./util");
  481. /**
  482. * get tag name
  483. *
  484. * @param {String} html e.g. '<a hef="#">'
  485. * @return {String}
  486. */
  487. function getTagName(html) {
  488. var i = _.spaceIndex(html);
  489. if (i === -1) {
  490. var tagName = html.slice(1, -1);
  491. } else {
  492. var tagName = html.slice(1, i + 1);
  493. }
  494. tagName = _.trim(tagName).toLowerCase();
  495. if (tagName.slice(0, 1) === "/") tagName = tagName.slice(1);
  496. if (tagName.slice(-1) === "/") tagName = tagName.slice(0, -1);
  497. return tagName;
  498. }
  499. /**
  500. * is close tag?
  501. *
  502. * @param {String} html 如:'<a hef="#">'
  503. * @return {Boolean}
  504. */
  505. function isClosing(html) {
  506. return html.slice(0, 2) === "</";
  507. }
  508. /**
  509. * parse input html and returns processed html
  510. *
  511. * @param {String} html
  512. * @param {Function} onTag e.g. function (sourcePosition, position, tag, html, isClosing)
  513. * @param {Function} escapeHtml
  514. * @return {String}
  515. */
  516. function parseTag(html, onTag, escapeHtml) {
  517. "use strict";
  518. var rethtml = "";
  519. var lastPos = 0;
  520. var tagStart = false;
  521. var quoteStart = false;
  522. var currentPos = 0;
  523. var len = html.length;
  524. var currentTagName = "";
  525. var currentHtml = "";
  526. chariterator: for (currentPos = 0; currentPos < len; currentPos++) {
  527. var c = html.charAt(currentPos);
  528. if (tagStart === false) {
  529. if (c === "<") {
  530. tagStart = currentPos;
  531. continue;
  532. }
  533. } else {
  534. if (quoteStart === false) {
  535. if (c === "<") {
  536. rethtml += escapeHtml(html.slice(lastPos, currentPos));
  537. tagStart = currentPos;
  538. lastPos = currentPos;
  539. continue;
  540. }
  541. if (c === ">") {
  542. rethtml += escapeHtml(html.slice(lastPos, tagStart));
  543. currentHtml = html.slice(tagStart, currentPos + 1);
  544. currentTagName = getTagName(currentHtml);
  545. rethtml += onTag(
  546. tagStart,
  547. rethtml.length,
  548. currentTagName,
  549. currentHtml,
  550. isClosing(currentHtml)
  551. );
  552. lastPos = currentPos + 1;
  553. tagStart = false;
  554. continue;
  555. }
  556. if (c === '"' || c === "'") {
  557. var i = 1;
  558. var ic = html.charAt(currentPos - i);
  559. while (ic.trim() === "" || ic === "=") {
  560. if (ic === "=") {
  561. quoteStart = c;
  562. continue chariterator;
  563. }
  564. ic = html.charAt(currentPos - ++i);
  565. }
  566. }
  567. } else {
  568. if (c === quoteStart) {
  569. quoteStart = false;
  570. continue;
  571. }
  572. }
  573. }
  574. }
  575. if (lastPos < html.length) {
  576. rethtml += escapeHtml(html.substr(lastPos));
  577. }
  578. return rethtml;
  579. }
  580. var REGEXP_ILLEGAL_ATTR_NAME = /[^a-zA-Z0-9_:\.\-]/gim;
  581. /**
  582. * parse input attributes and returns processed attributes
  583. *
  584. * @param {String} html e.g. `href="#" target="_blank"`
  585. * @param {Function} onAttr e.g. `function (name, value)`
  586. * @return {String}
  587. */
  588. function parseAttr(html, onAttr) {
  589. "use strict";
  590. var lastPos = 0;
  591. var retAttrs = [];
  592. var tmpName = false;
  593. var len = html.length;
  594. function addAttr(name, value) {
  595. name = _.trim(name);
  596. name = name.replace(REGEXP_ILLEGAL_ATTR_NAME, "").toLowerCase();
  597. if (name.length < 1) return;
  598. var ret = onAttr(name, value || "");
  599. if (ret) retAttrs.push(ret);
  600. }
  601. // 逐个分析字符
  602. for (var i = 0; i < len; i++) {
  603. var c = html.charAt(i);
  604. var v, j;
  605. if (tmpName === false && c === "=") {
  606. tmpName = html.slice(lastPos, i);
  607. lastPos = i + 1;
  608. continue;
  609. }
  610. if (tmpName !== false) {
  611. if (
  612. i === lastPos &&
  613. (c === '"' || c === "'") &&
  614. html.charAt(i - 1) === "="
  615. ) {
  616. j = html.indexOf(c, i + 1);
  617. if (j === -1) {
  618. break;
  619. } else {
  620. v = _.trim(html.slice(lastPos + 1, j));
  621. addAttr(tmpName, v);
  622. tmpName = false;
  623. i = j;
  624. lastPos = i + 1;
  625. continue;
  626. }
  627. }
  628. }
  629. if (/\s|\n|\t/.test(c)) {
  630. html = html.replace(/\s|\n|\t/g, " ");
  631. if (tmpName === false) {
  632. j = findNextEqual(html, i);
  633. if (j === -1) {
  634. v = _.trim(html.slice(lastPos, i));
  635. addAttr(v);
  636. tmpName = false;
  637. lastPos = i + 1;
  638. continue;
  639. } else {
  640. i = j - 1;
  641. continue;
  642. }
  643. } else {
  644. j = findBeforeEqual(html, i - 1);
  645. if (j === -1) {
  646. v = _.trim(html.slice(lastPos, i));
  647. v = stripQuoteWrap(v);
  648. addAttr(tmpName, v);
  649. tmpName = false;
  650. lastPos = i + 1;
  651. continue;
  652. } else {
  653. continue;
  654. }
  655. }
  656. }
  657. }
  658. if (lastPos < html.length) {
  659. if (tmpName === false) {
  660. addAttr(html.slice(lastPos));
  661. } else {
  662. addAttr(tmpName, stripQuoteWrap(_.trim(html.slice(lastPos))));
  663. }
  664. }
  665. return _.trim(retAttrs.join(" "));
  666. }
  667. function findNextEqual(str, i) {
  668. for (; i < str.length; i++) {
  669. var c = str[i];
  670. if (c === " ") continue;
  671. if (c === "=") return i;
  672. return -1;
  673. }
  674. }
  675. function findBeforeEqual(str, i) {
  676. for (; i > 0; i--) {
  677. var c = str[i];
  678. if (c === " ") continue;
  679. if (c === "=") return i;
  680. return -1;
  681. }
  682. }
  683. function isQuoteWrapString(text) {
  684. if (
  685. (text[0] === '"' && text[text.length - 1] === '"') ||
  686. (text[0] === "'" && text[text.length - 1] === "'")
  687. ) {
  688. return true;
  689. } else {
  690. return false;
  691. }
  692. }
  693. function stripQuoteWrap(text) {
  694. if (isQuoteWrapString(text)) {
  695. return text.substr(1, text.length - 2);
  696. } else {
  697. return text;
  698. }
  699. }
  700. exports.parseTag = parseTag;
  701. exports.parseAttr = parseAttr;
  702. },{"./util":4}],4:[function(require,module,exports){
  703. module.exports = {
  704. indexOf: function (arr, item) {
  705. var i, j;
  706. if (Array.prototype.indexOf) {
  707. return arr.indexOf(item);
  708. }
  709. for (i = 0, j = arr.length; i < j; i++) {
  710. if (arr[i] === item) {
  711. return i;
  712. }
  713. }
  714. return -1;
  715. },
  716. forEach: function (arr, fn, scope) {
  717. var i, j;
  718. if (Array.prototype.forEach) {
  719. return arr.forEach(fn, scope);
  720. }
  721. for (i = 0, j = arr.length; i < j; i++) {
  722. fn.call(scope, arr[i], i, arr);
  723. }
  724. },
  725. trim: function (str) {
  726. if (String.prototype.trim) {
  727. return str.trim();
  728. }
  729. return str.replace(/(^\s*)|(\s*$)/g, "");
  730. },
  731. spaceIndex: function (str) {
  732. var reg = /\s|\n|\t/;
  733. var match = reg.exec(str);
  734. return match ? match.index : -1;
  735. },
  736. };
  737. },{}],5:[function(require,module,exports){
  738. /**
  739. * filter xss
  740. *
  741. * @author Zongmin Lei<leizongmin@gmail.com>
  742. */
  743. var FilterCSS = require("cssfilter").FilterCSS;
  744. var DEFAULT = require("./default");
  745. var parser = require("./parser");
  746. var parseTag = parser.parseTag;
  747. var parseAttr = parser.parseAttr;
  748. var _ = require("./util");
  749. /**
  750. * returns `true` if the input value is `undefined` or `null`
  751. *
  752. * @param {Object} obj
  753. * @return {Boolean}
  754. */
  755. function isNull(obj) {
  756. return obj === undefined || obj === null;
  757. }
  758. /**
  759. * get attributes for a tag
  760. *
  761. * @param {String} html
  762. * @return {Object}
  763. * - {String} html
  764. * - {Boolean} closing
  765. */
  766. function getAttrs(html) {
  767. var i = _.spaceIndex(html);
  768. if (i === -1) {
  769. return {
  770. html: "",
  771. closing: html[html.length - 2] === "/",
  772. };
  773. }
  774. html = _.trim(html.slice(i + 1, -1));
  775. var isClosing = html[html.length - 1] === "/";
  776. if (isClosing) html = _.trim(html.slice(0, -1));
  777. return {
  778. html: html,
  779. closing: isClosing,
  780. };
  781. }
  782. /**
  783. * shallow copy
  784. *
  785. * @param {Object} obj
  786. * @return {Object}
  787. */
  788. function shallowCopyObject(obj) {
  789. var ret = {};
  790. for (var i in obj) {
  791. ret[i] = obj[i];
  792. }
  793. return ret;
  794. }
  795. /**
  796. * FilterXSS class
  797. *
  798. * @param {Object} options
  799. * whiteList (or allowList), onTag, onTagAttr, onIgnoreTag,
  800. * onIgnoreTagAttr, safeAttrValue, escapeHtml
  801. * stripIgnoreTagBody, allowCommentTag, stripBlankChar
  802. * css{whiteList, onAttr, onIgnoreAttr} `css=false` means don't use `cssfilter`
  803. */
  804. function FilterXSS(options) {
  805. options = shallowCopyObject(options || {});
  806. if (options.stripIgnoreTag) {
  807. if (options.onIgnoreTag) {
  808. console.error(
  809. 'Notes: cannot use these two options "stripIgnoreTag" and "onIgnoreTag" at the same time'
  810. );
  811. }
  812. options.onIgnoreTag = DEFAULT.onIgnoreTagStripAll;
  813. }
  814. options.whiteList = options.whiteList || options.allowList || DEFAULT.whiteList;
  815. options.onTag = options.onTag || DEFAULT.onTag;
  816. options.onTagAttr = options.onTagAttr || DEFAULT.onTagAttr;
  817. options.onIgnoreTag = options.onIgnoreTag || DEFAULT.onIgnoreTag;
  818. options.onIgnoreTagAttr = options.onIgnoreTagAttr || DEFAULT.onIgnoreTagAttr;
  819. options.safeAttrValue = options.safeAttrValue || DEFAULT.safeAttrValue;
  820. options.escapeHtml = options.escapeHtml || DEFAULT.escapeHtml;
  821. this.options = options;
  822. if (options.css === false) {
  823. this.cssFilter = false;
  824. } else {
  825. options.css = options.css || {};
  826. this.cssFilter = new FilterCSS(options.css);
  827. }
  828. }
  829. /**
  830. * start process and returns result
  831. *
  832. * @param {String} html
  833. * @return {String}
  834. */
  835. FilterXSS.prototype.process = function (html) {
  836. // compatible with the input
  837. html = html || "";
  838. html = html.toString();
  839. if (!html) return "";
  840. var me = this;
  841. var options = me.options;
  842. var whiteList = options.whiteList;
  843. var onTag = options.onTag;
  844. var onIgnoreTag = options.onIgnoreTag;
  845. var onTagAttr = options.onTagAttr;
  846. var onIgnoreTagAttr = options.onIgnoreTagAttr;
  847. var safeAttrValue = options.safeAttrValue;
  848. var escapeHtml = options.escapeHtml;
  849. var cssFilter = me.cssFilter;
  850. // remove invisible characters
  851. if (options.stripBlankChar) {
  852. html = DEFAULT.stripBlankChar(html);
  853. }
  854. // remove html comments
  855. if (!options.allowCommentTag) {
  856. html = DEFAULT.stripCommentTag(html);
  857. }
  858. // if enable stripIgnoreTagBody
  859. var stripIgnoreTagBody = false;
  860. if (options.stripIgnoreTagBody) {
  861. var stripIgnoreTagBody = DEFAULT.StripTagBody(
  862. options.stripIgnoreTagBody,
  863. onIgnoreTag
  864. );
  865. onIgnoreTag = stripIgnoreTagBody.onIgnoreTag;
  866. }
  867. var retHtml = parseTag(
  868. html,
  869. function (sourcePosition, position, tag, html, isClosing) {
  870. var info = {
  871. sourcePosition: sourcePosition,
  872. position: position,
  873. isClosing: isClosing,
  874. isWhite: whiteList.hasOwnProperty(tag),
  875. };
  876. // call `onTag()`
  877. var ret = onTag(tag, html, info);
  878. if (!isNull(ret)) return ret;
  879. if (info.isWhite) {
  880. if (info.isClosing) {
  881. return "</" + tag + ">";
  882. }
  883. var attrs = getAttrs(html);
  884. var whiteAttrList = whiteList[tag];
  885. var attrsHtml = parseAttr(attrs.html, function (name, value) {
  886. // call `onTagAttr()`
  887. var isWhiteAttr = _.indexOf(whiteAttrList, name) !== -1;
  888. var ret = onTagAttr(tag, name, value, isWhiteAttr);
  889. if (!isNull(ret)) return ret;
  890. if (isWhiteAttr) {
  891. // call `safeAttrValue()`
  892. value = safeAttrValue(tag, name, value, cssFilter);
  893. if (value) {
  894. return name + '="' + value + '"';
  895. } else {
  896. return name;
  897. }
  898. } else {
  899. // call `onIgnoreTagAttr()`
  900. var ret = onIgnoreTagAttr(tag, name, value, isWhiteAttr);
  901. if (!isNull(ret)) return ret;
  902. return;
  903. }
  904. });
  905. // build new tag html
  906. var html = "<" + tag;
  907. if (attrsHtml) html += " " + attrsHtml;
  908. if (attrs.closing) html += " /";
  909. html += ">";
  910. return html;
  911. } else {
  912. // call `onIgnoreTag()`
  913. var ret = onIgnoreTag(tag, html, info);
  914. if (!isNull(ret)) return ret;
  915. return escapeHtml(html);
  916. }
  917. },
  918. escapeHtml
  919. );
  920. // if enable stripIgnoreTagBody
  921. if (stripIgnoreTagBody) {
  922. retHtml = stripIgnoreTagBody.remove(retHtml);
  923. }
  924. return retHtml;
  925. };
  926. module.exports = FilterXSS;
  927. },{"./default":1,"./parser":3,"./util":4,"cssfilter":8}],6:[function(require,module,exports){
  928. /**
  929. * cssfilter
  930. *
  931. * @author 老雷<leizongmin@gmail.com>
  932. */
  933. var DEFAULT = require('./default');
  934. var parseStyle = require('./parser');
  935. var _ = require('./util');
  936. /**
  937. * 返回值是否为空
  938. *
  939. * @param {Object} obj
  940. * @return {Boolean}
  941. */
  942. function isNull (obj) {
  943. return (obj === undefined || obj === null);
  944. }
  945. /**
  946. * 浅拷贝对象
  947. *
  948. * @param {Object} obj
  949. * @return {Object}
  950. */
  951. function shallowCopyObject (obj) {
  952. var ret = {};
  953. for (var i in obj) {
  954. ret[i] = obj[i];
  955. }
  956. return ret;
  957. }
  958. /**
  959. * 创建CSS过滤器
  960. *
  961. * @param {Object} options
  962. * - {Object} whiteList
  963. * - {Function} onAttr
  964. * - {Function} onIgnoreAttr
  965. * - {Function} safeAttrValue
  966. */
  967. function FilterCSS (options) {
  968. options = shallowCopyObject(options || {});
  969. options.whiteList = options.whiteList || DEFAULT.whiteList;
  970. options.onAttr = options.onAttr || DEFAULT.onAttr;
  971. options.onIgnoreAttr = options.onIgnoreAttr || DEFAULT.onIgnoreAttr;
  972. options.safeAttrValue = options.safeAttrValue || DEFAULT.safeAttrValue;
  973. this.options = options;
  974. }
  975. FilterCSS.prototype.process = function (css) {
  976. // 兼容各种奇葩输入
  977. css = css || '';
  978. css = css.toString();
  979. if (!css) return '';
  980. var me = this;
  981. var options = me.options;
  982. var whiteList = options.whiteList;
  983. var onAttr = options.onAttr;
  984. var onIgnoreAttr = options.onIgnoreAttr;
  985. var safeAttrValue = options.safeAttrValue;
  986. var retCSS = parseStyle(css, function (sourcePosition, position, name, value, source) {
  987. var check = whiteList[name];
  988. var isWhite = false;
  989. if (check === true) isWhite = check;
  990. else if (typeof check === 'function') isWhite = check(value);
  991. else if (check instanceof RegExp) isWhite = check.test(value);
  992. if (isWhite !== true) isWhite = false;
  993. // 如果过滤后 value 为空则直接忽略
  994. value = safeAttrValue(name, value);
  995. if (!value) return;
  996. var opts = {
  997. position: position,
  998. sourcePosition: sourcePosition,
  999. source: source,
  1000. isWhite: isWhite
  1001. };
  1002. if (isWhite) {
  1003. var ret = onAttr(name, value, opts);
  1004. if (isNull(ret)) {
  1005. return name + ':' + value;
  1006. } else {
  1007. return ret;
  1008. }
  1009. } else {
  1010. var ret = onIgnoreAttr(name, value, opts);
  1011. if (!isNull(ret)) {
  1012. return ret;
  1013. }
  1014. }
  1015. });
  1016. return retCSS;
  1017. };
  1018. module.exports = FilterCSS;
  1019. },{"./default":7,"./parser":9,"./util":10}],7:[function(require,module,exports){
  1020. /**
  1021. * cssfilter
  1022. *
  1023. * @author 老雷<leizongmin@gmail.com>
  1024. */
  1025. function getDefaultWhiteList () {
  1026. // 白名单值说明:
  1027. // true: 允许该属性
  1028. // Function: function (val) { } 返回true表示允许该属性,其他值均表示不允许
  1029. // RegExp: regexp.test(val) 返回true表示允许该属性,其他值均表示不允许
  1030. // 除上面列出的值外均表示不允许
  1031. var whiteList = {};
  1032. whiteList['align-content'] = false; // default: auto
  1033. whiteList['align-items'] = false; // default: auto
  1034. whiteList['align-self'] = false; // default: auto
  1035. whiteList['alignment-adjust'] = false; // default: auto
  1036. whiteList['alignment-baseline'] = false; // default: baseline
  1037. whiteList['all'] = false; // default: depending on individual properties
  1038. whiteList['anchor-point'] = false; // default: none
  1039. whiteList['animation'] = false; // default: depending on individual properties
  1040. whiteList['animation-delay'] = false; // default: 0
  1041. whiteList['animation-direction'] = false; // default: normal
  1042. whiteList['animation-duration'] = false; // default: 0
  1043. whiteList['animation-fill-mode'] = false; // default: none
  1044. whiteList['animation-iteration-count'] = false; // default: 1
  1045. whiteList['animation-name'] = false; // default: none
  1046. whiteList['animation-play-state'] = false; // default: running
  1047. whiteList['animation-timing-function'] = false; // default: ease
  1048. whiteList['azimuth'] = false; // default: center
  1049. whiteList['backface-visibility'] = false; // default: visible
  1050. whiteList['background'] = true; // default: depending on individual properties
  1051. whiteList['background-attachment'] = true; // default: scroll
  1052. whiteList['background-clip'] = true; // default: border-box
  1053. whiteList['background-color'] = true; // default: transparent
  1054. whiteList['background-image'] = true; // default: none
  1055. whiteList['background-origin'] = true; // default: padding-box
  1056. whiteList['background-position'] = true; // default: 0% 0%
  1057. whiteList['background-repeat'] = true; // default: repeat
  1058. whiteList['background-size'] = true; // default: auto
  1059. whiteList['baseline-shift'] = false; // default: baseline
  1060. whiteList['binding'] = false; // default: none
  1061. whiteList['bleed'] = false; // default: 6pt
  1062. whiteList['bookmark-label'] = false; // default: content()
  1063. whiteList['bookmark-level'] = false; // default: none
  1064. whiteList['bookmark-state'] = false; // default: open
  1065. whiteList['border'] = true; // default: depending on individual properties
  1066. whiteList['border-bottom'] = true; // default: depending on individual properties
  1067. whiteList['border-bottom-color'] = true; // default: current color
  1068. whiteList['border-bottom-left-radius'] = true; // default: 0
  1069. whiteList['border-bottom-right-radius'] = true; // default: 0
  1070. whiteList['border-bottom-style'] = true; // default: none
  1071. whiteList['border-bottom-width'] = true; // default: medium
  1072. whiteList['border-collapse'] = true; // default: separate
  1073. whiteList['border-color'] = true; // default: depending on individual properties
  1074. whiteList['border-image'] = true; // default: none
  1075. whiteList['border-image-outset'] = true; // default: 0
  1076. whiteList['border-image-repeat'] = true; // default: stretch
  1077. whiteList['border-image-slice'] = true; // default: 100%
  1078. whiteList['border-image-source'] = true; // default: none
  1079. whiteList['border-image-width'] = true; // default: 1
  1080. whiteList['border-left'] = true; // default: depending on individual properties
  1081. whiteList['border-left-color'] = true; // default: current color
  1082. whiteList['border-left-style'] = true; // default: none
  1083. whiteList['border-left-width'] = true; // default: medium
  1084. whiteList['border-radius'] = true; // default: 0
  1085. whiteList['border-right'] = true; // default: depending on individual properties
  1086. whiteList['border-right-color'] = true; // default: current color
  1087. whiteList['border-right-style'] = true; // default: none
  1088. whiteList['border-right-width'] = true; // default: medium
  1089. whiteList['border-spacing'] = true; // default: 0
  1090. whiteList['border-style'] = true; // default: depending on individual properties
  1091. whiteList['border-top'] = true; // default: depending on individual properties
  1092. whiteList['border-top-color'] = true; // default: current color
  1093. whiteList['border-top-left-radius'] = true; // default: 0
  1094. whiteList['border-top-right-radius'] = true; // default: 0
  1095. whiteList['border-top-style'] = true; // default: none
  1096. whiteList['border-top-width'] = true; // default: medium
  1097. whiteList['border-width'] = true; // default: depending on individual properties
  1098. whiteList['bottom'] = false; // default: auto
  1099. whiteList['box-decoration-break'] = true; // default: slice
  1100. whiteList['box-shadow'] = true; // default: none
  1101. whiteList['box-sizing'] = true; // default: content-box
  1102. whiteList['box-snap'] = true; // default: none
  1103. whiteList['box-suppress'] = true; // default: show
  1104. whiteList['break-after'] = true; // default: auto
  1105. whiteList['break-before'] = true; // default: auto
  1106. whiteList['break-inside'] = true; // default: auto
  1107. whiteList['caption-side'] = false; // default: top
  1108. whiteList['chains'] = false; // default: none
  1109. whiteList['clear'] = true; // default: none
  1110. whiteList['clip'] = false; // default: auto
  1111. whiteList['clip-path'] = false; // default: none
  1112. whiteList['clip-rule'] = false; // default: nonzero
  1113. whiteList['color'] = true; // default: implementation dependent
  1114. whiteList['color-interpolation-filters'] = true; // default: auto
  1115. whiteList['column-count'] = false; // default: auto
  1116. whiteList['column-fill'] = false; // default: balance
  1117. whiteList['column-gap'] = false; // default: normal
  1118. whiteList['column-rule'] = false; // default: depending on individual properties
  1119. whiteList['column-rule-color'] = false; // default: current color
  1120. whiteList['column-rule-style'] = false; // default: medium
  1121. whiteList['column-rule-width'] = false; // default: medium
  1122. whiteList['column-span'] = false; // default: none
  1123. whiteList['column-width'] = false; // default: auto
  1124. whiteList['columns'] = false; // default: depending on individual properties
  1125. whiteList['contain'] = false; // default: none
  1126. whiteList['content'] = false; // default: normal
  1127. whiteList['counter-increment'] = false; // default: none
  1128. whiteList['counter-reset'] = false; // default: none
  1129. whiteList['counter-set'] = false; // default: none
  1130. whiteList['crop'] = false; // default: auto
  1131. whiteList['cue'] = false; // default: depending on individual properties
  1132. whiteList['cue-after'] = false; // default: none
  1133. whiteList['cue-before'] = false; // default: none
  1134. whiteList['cursor'] = false; // default: auto
  1135. whiteList['direction'] = false; // default: ltr
  1136. whiteList['display'] = true; // default: depending on individual properties
  1137. whiteList['display-inside'] = true; // default: auto
  1138. whiteList['display-list'] = true; // default: none
  1139. whiteList['display-outside'] = true; // default: inline-level
  1140. whiteList['dominant-baseline'] = false; // default: auto
  1141. whiteList['elevation'] = false; // default: level
  1142. whiteList['empty-cells'] = false; // default: show
  1143. whiteList['filter'] = false; // default: none
  1144. whiteList['flex'] = false; // default: depending on individual properties
  1145. whiteList['flex-basis'] = false; // default: auto
  1146. whiteList['flex-direction'] = false; // default: row
  1147. whiteList['flex-flow'] = false; // default: depending on individual properties
  1148. whiteList['flex-grow'] = false; // default: 0
  1149. whiteList['flex-shrink'] = false; // default: 1
  1150. whiteList['flex-wrap'] = false; // default: nowrap
  1151. whiteList['float'] = false; // default: none
  1152. whiteList['float-offset'] = false; // default: 0 0
  1153. whiteList['flood-color'] = false; // default: black
  1154. whiteList['flood-opacity'] = false; // default: 1
  1155. whiteList['flow-from'] = false; // default: none
  1156. whiteList['flow-into'] = false; // default: none
  1157. whiteList['font'] = true; // default: depending on individual properties
  1158. whiteList['font-family'] = true; // default: implementation dependent
  1159. whiteList['font-feature-settings'] = true; // default: normal
  1160. whiteList['font-kerning'] = true; // default: auto
  1161. whiteList['font-language-override'] = true; // default: normal
  1162. whiteList['font-size'] = true; // default: medium
  1163. whiteList['font-size-adjust'] = true; // default: none
  1164. whiteList['font-stretch'] = true; // default: normal
  1165. whiteList['font-style'] = true; // default: normal
  1166. whiteList['font-synthesis'] = true; // default: weight style
  1167. whiteList['font-variant'] = true; // default: normal
  1168. whiteList['font-variant-alternates'] = true; // default: normal
  1169. whiteList['font-variant-caps'] = true; // default: normal
  1170. whiteList['font-variant-east-asian'] = true; // default: normal
  1171. whiteList['font-variant-ligatures'] = true; // default: normal
  1172. whiteList['font-variant-numeric'] = true; // default: normal
  1173. whiteList['font-variant-position'] = true; // default: normal
  1174. whiteList['font-weight'] = true; // default: normal
  1175. whiteList['grid'] = false; // default: depending on individual properties
  1176. whiteList['grid-area'] = false; // default: depending on individual properties
  1177. whiteList['grid-auto-columns'] = false; // default: auto
  1178. whiteList['grid-auto-flow'] = false; // default: none
  1179. whiteList['grid-auto-rows'] = false; // default: auto
  1180. whiteList['grid-column'] = false; // default: depending on individual properties
  1181. whiteList['grid-column-end'] = false; // default: auto
  1182. whiteList['grid-column-start'] = false; // default: auto
  1183. whiteList['grid-row'] = false; // default: depending on individual properties
  1184. whiteList['grid-row-end'] = false; // default: auto
  1185. whiteList['grid-row-start'] = false; // default: auto
  1186. whiteList['grid-template'] = false; // default: depending on individual properties
  1187. whiteList['grid-template-areas'] = false; // default: none
  1188. whiteList['grid-template-columns'] = false; // default: none
  1189. whiteList['grid-template-rows'] = false; // default: none
  1190. whiteList['hanging-punctuation'] = false; // default: none
  1191. whiteList['height'] = true; // default: auto
  1192. whiteList['hyphens'] = false; // default: manual
  1193. whiteList['icon'] = false; // default: auto
  1194. whiteList['image-orientation'] = false; // default: auto
  1195. whiteList['image-resolution'] = false; // default: normal
  1196. whiteList['ime-mode'] = false; // default: auto
  1197. whiteList['initial-letters'] = false; // default: normal
  1198. whiteList['inline-box-align'] = false; // default: last
  1199. whiteList['justify-content'] = false; // default: auto
  1200. whiteList['justify-items'] = false; // default: auto
  1201. whiteList['justify-self'] = false; // default: auto
  1202. whiteList['left'] = false; // default: auto
  1203. whiteList['letter-spacing'] = true; // default: normal
  1204. whiteList['lighting-color'] = true; // default: white
  1205. whiteList['line-box-contain'] = false; // default: block inline replaced
  1206. whiteList['line-break'] = false; // default: auto
  1207. whiteList['line-grid'] = false; // default: match-parent
  1208. whiteList['line-height'] = false; // default: normal
  1209. whiteList['line-snap'] = false; // default: none
  1210. whiteList['line-stacking'] = false; // default: depending on individual properties
  1211. whiteList['line-stacking-ruby'] = false; // default: exclude-ruby
  1212. whiteList['line-stacking-shift'] = false; // default: consider-shifts
  1213. whiteList['line-stacking-strategy'] = false; // default: inline-line-height
  1214. whiteList['list-style'] = true; // default: depending on individual properties
  1215. whiteList['list-style-image'] = true; // default: none
  1216. whiteList['list-style-position'] = true; // default: outside
  1217. whiteList['list-style-type'] = true; // default: disc
  1218. whiteList['margin'] = true; // default: depending on individual properties
  1219. whiteList['margin-bottom'] = true; // default: 0
  1220. whiteList['margin-left'] = true; // default: 0
  1221. whiteList['margin-right'] = true; // default: 0
  1222. whiteList['margin-top'] = true; // default: 0
  1223. whiteList['marker-offset'] = false; // default: auto
  1224. whiteList['marker-side'] = false; // default: list-item
  1225. whiteList['marks'] = false; // default: none
  1226. whiteList['mask'] = false; // default: border-box
  1227. whiteList['mask-box'] = false; // default: see individual properties
  1228. whiteList['mask-box-outset'] = false; // default: 0
  1229. whiteList['mask-box-repeat'] = false; // default: stretch
  1230. whiteList['mask-box-slice'] = false; // default: 0 fill
  1231. whiteList['mask-box-source'] = false; // default: none
  1232. whiteList['mask-box-width'] = false; // default: auto
  1233. whiteList['mask-clip'] = false; // default: border-box
  1234. whiteList['mask-image'] = false; // default: none
  1235. whiteList['mask-origin'] = false; // default: border-box
  1236. whiteList['mask-position'] = false; // default: center
  1237. whiteList['mask-repeat'] = false; // default: no-repeat
  1238. whiteList['mask-size'] = false; // default: border-box
  1239. whiteList['mask-source-type'] = false; // default: auto
  1240. whiteList['mask-type'] = false; // default: luminance
  1241. whiteList['max-height'] = true; // default: none
  1242. whiteList['max-lines'] = false; // default: none
  1243. whiteList['max-width'] = true; // default: none
  1244. whiteList['min-height'] = true; // default: 0
  1245. whiteList['min-width'] = true; // default: 0
  1246. whiteList['move-to'] = false; // default: normal
  1247. whiteList['nav-down'] = false; // default: auto
  1248. whiteList['nav-index'] = false; // default: auto
  1249. whiteList['nav-left'] = false; // default: auto
  1250. whiteList['nav-right'] = false; // default: auto
  1251. whiteList['nav-up'] = false; // default: auto
  1252. whiteList['object-fit'] = false; // default: fill
  1253. whiteList['object-position'] = false; // default: 50% 50%
  1254. whiteList['opacity'] = false; // default: 1
  1255. whiteList['order'] = false; // default: 0
  1256. whiteList['orphans'] = false; // default: 2
  1257. whiteList['outline'] = false; // default: depending on individual properties
  1258. whiteList['outline-color'] = false; // default: invert
  1259. whiteList['outline-offset'] = false; // default: 0
  1260. whiteList['outline-style'] = false; // default: none
  1261. whiteList['outline-width'] = false; // default: medium
  1262. whiteList['overflow'] = false; // default: depending on individual properties
  1263. whiteList['overflow-wrap'] = false; // default: normal
  1264. whiteList['overflow-x'] = false; // default: visible
  1265. whiteList['overflow-y'] = false; // default: visible
  1266. whiteList['padding'] = true; // default: depending on individual properties
  1267. whiteList['padding-bottom'] = true; // default: 0
  1268. whiteList['padding-left'] = true; // default: 0
  1269. whiteList['padding-right'] = true; // default: 0
  1270. whiteList['padding-top'] = true; // default: 0
  1271. whiteList['page'] = false; // default: auto
  1272. whiteList['page-break-after'] = false; // default: auto
  1273. whiteList['page-break-before'] = false; // default: auto
  1274. whiteList['page-break-inside'] = false; // default: auto
  1275. whiteList['page-policy'] = false; // default: start
  1276. whiteList['pause'] = false; // default: implementation dependent
  1277. whiteList['pause-after'] = false; // default: implementation dependent
  1278. whiteList['pause-before'] = false; // default: implementation dependent
  1279. whiteList['perspective'] = false; // default: none
  1280. whiteList['perspective-origin'] = false; // default: 50% 50%
  1281. whiteList['pitch'] = false; // default: medium
  1282. whiteList['pitch-range'] = false; // default: 50
  1283. whiteList['play-during'] = false; // default: auto
  1284. whiteList['position'] = false; // default: static
  1285. whiteList['presentation-level'] = false; // default: 0
  1286. whiteList['quotes'] = false; // default: text
  1287. whiteList['region-fragment'] = false; // default: auto
  1288. whiteList['resize'] = false; // default: none
  1289. whiteList['rest'] = false; // default: depending on individual properties
  1290. whiteList['rest-after'] = false; // default: none
  1291. whiteList['rest-before'] = false; // default: none
  1292. whiteList['richness'] = false; // default: 50
  1293. whiteList['right'] = false; // default: auto
  1294. whiteList['rotation'] = false; // default: 0
  1295. whiteList['rotation-point'] = false; // default: 50% 50%
  1296. whiteList['ruby-align'] = false; // default: auto
  1297. whiteList['ruby-merge'] = false; // default: separate
  1298. whiteList['ruby-position'] = false; // default: before
  1299. whiteList['shape-image-threshold'] = false; // default: 0.0
  1300. whiteList['shape-outside'] = false; // default: none
  1301. whiteList['shape-margin'] = false; // default: 0
  1302. whiteList['size'] = false; // default: auto
  1303. whiteList['speak'] = false; // default: auto
  1304. whiteList['speak-as'] = false; // default: normal
  1305. whiteList['speak-header'] = false; // default: once
  1306. whiteList['speak-numeral'] = false; // default: continuous
  1307. whiteList['speak-punctuation'] = false; // default: none
  1308. whiteList['speech-rate'] = false; // default: medium
  1309. whiteList['stress'] = false; // default: 50
  1310. whiteList['string-set'] = false; // default: none
  1311. whiteList['tab-size'] = false; // default: 8
  1312. whiteList['table-layout'] = false; // default: auto
  1313. whiteList['text-align'] = true; // default: start
  1314. whiteList['text-align-last'] = true; // default: auto
  1315. whiteList['text-combine-upright'] = true; // default: none
  1316. whiteList['text-decoration'] = true; // default: none
  1317. whiteList['text-decoration-color'] = true; // default: currentColor
  1318. whiteList['text-decoration-line'] = true; // default: none
  1319. whiteList['text-decoration-skip'] = true; // default: objects
  1320. whiteList['text-decoration-style'] = true; // default: solid
  1321. whiteList['text-emphasis'] = true; // default: depending on individual properties
  1322. whiteList['text-emphasis-color'] = true; // default: currentColor
  1323. whiteList['text-emphasis-position'] = true; // default: over right
  1324. whiteList['text-emphasis-style'] = true; // default: none
  1325. whiteList['text-height'] = true; // default: auto
  1326. whiteList['text-indent'] = true; // default: 0
  1327. whiteList['text-justify'] = true; // default: auto
  1328. whiteList['text-orientation'] = true; // default: mixed
  1329. whiteList['text-overflow'] = true; // default: clip
  1330. whiteList['text-shadow'] = true; // default: none
  1331. whiteList['text-space-collapse'] = true; // default: collapse
  1332. whiteList['text-transform'] = true; // default: none
  1333. whiteList['text-underline-position'] = true; // default: auto
  1334. whiteList['text-wrap'] = true; // default: normal
  1335. whiteList['top'] = false; // default: auto
  1336. whiteList['transform'] = false; // default: none
  1337. whiteList['transform-origin'] = false; // default: 50% 50% 0
  1338. whiteList['transform-style'] = false; // default: flat
  1339. whiteList['transition'] = false; // default: depending on individual properties
  1340. whiteList['transition-delay'] = false; // default: 0s
  1341. whiteList['transition-duration'] = false; // default: 0s
  1342. whiteList['transition-property'] = false; // default: all
  1343. whiteList['transition-timing-function'] = false; // default: ease
  1344. whiteList['unicode-bidi'] = false; // default: normal
  1345. whiteList['vertical-align'] = false; // default: baseline
  1346. whiteList['visibility'] = false; // default: visible
  1347. whiteList['voice-balance'] = false; // default: center
  1348. whiteList['voice-duration'] = false; // default: auto
  1349. whiteList['voice-family'] = false; // default: implementation dependent
  1350. whiteList['voice-pitch'] = false; // default: medium
  1351. whiteList['voice-range'] = false; // default: medium
  1352. whiteList['voice-rate'] = false; // default: normal
  1353. whiteList['voice-stress'] = false; // default: normal
  1354. whiteList['voice-volume'] = false; // default: medium
  1355. whiteList['volume'] = false; // default: medium
  1356. whiteList['white-space'] = false; // default: normal
  1357. whiteList['widows'] = false; // default: 2
  1358. whiteList['width'] = true; // default: auto
  1359. whiteList['will-change'] = false; // default: auto
  1360. whiteList['word-break'] = true; // default: normal
  1361. whiteList['word-spacing'] = true; // default: normal
  1362. whiteList['word-wrap'] = true; // default: normal
  1363. whiteList['wrap-flow'] = false; // default: auto
  1364. whiteList['wrap-through'] = false; // default: wrap
  1365. whiteList['writing-mode'] = false; // default: horizontal-tb
  1366. whiteList['z-index'] = false; // default: auto
  1367. return whiteList;
  1368. }
  1369. /**
  1370. * 匹配到白名单上的一个属性时
  1371. *
  1372. * @param {String} name
  1373. * @param {String} value
  1374. * @param {Object} options
  1375. * @return {String}
  1376. */
  1377. function onAttr (name, value, options) {
  1378. // do nothing
  1379. }
  1380. /**
  1381. * 匹配到不在白名单上的一个属性时
  1382. *
  1383. * @param {String} name
  1384. * @param {String} value
  1385. * @param {Object} options
  1386. * @return {String}
  1387. */
  1388. function onIgnoreAttr (name, value, options) {
  1389. // do nothing
  1390. }
  1391. var REGEXP_URL_JAVASCRIPT = /javascript\s*\:/img;
  1392. /**
  1393. * 过滤属性值
  1394. *
  1395. * @param {String} name
  1396. * @param {String} value
  1397. * @return {String}
  1398. */
  1399. function safeAttrValue(name, value) {
  1400. if (REGEXP_URL_JAVASCRIPT.test(value)) return '';
  1401. return value;
  1402. }
  1403. exports.whiteList = getDefaultWhiteList();
  1404. exports.getDefaultWhiteList = getDefaultWhiteList;
  1405. exports.onAttr = onAttr;
  1406. exports.onIgnoreAttr = onIgnoreAttr;
  1407. exports.safeAttrValue = safeAttrValue;
  1408. },{}],8:[function(require,module,exports){
  1409. /**
  1410. * cssfilter
  1411. *
  1412. * @author 老雷<leizongmin@gmail.com>
  1413. */
  1414. var DEFAULT = require('./default');
  1415. var FilterCSS = require('./css');
  1416. /**
  1417. * XSS过滤
  1418. *
  1419. * @param {String} css 要过滤的CSS代码
  1420. * @param {Object} options 选项:whiteList, onAttr, onIgnoreAttr
  1421. * @return {String}
  1422. */
  1423. function filterCSS (html, options) {
  1424. var xss = new FilterCSS(options);
  1425. return xss.process(html);
  1426. }
  1427. // 输出
  1428. exports = module.exports = filterCSS;
  1429. exports.FilterCSS = FilterCSS;
  1430. for (var i in DEFAULT) exports[i] = DEFAULT[i];
  1431. // 在浏览器端使用
  1432. if (typeof window !== 'undefined') {
  1433. window.filterCSS = module.exports;
  1434. }
  1435. },{"./css":6,"./default":7}],9:[function(require,module,exports){
  1436. /**
  1437. * cssfilter
  1438. *
  1439. * @author 老雷<leizongmin@gmail.com>
  1440. */
  1441. var _ = require('./util');
  1442. /**
  1443. * 解析style
  1444. *
  1445. * @param {String} css
  1446. * @param {Function} onAttr 处理属性的函数
  1447. * 参数格式: function (sourcePosition, position, name, value, source)
  1448. * @return {String}
  1449. */
  1450. function parseStyle (css, onAttr) {
  1451. css = _.trimRight(css);
  1452. if (css[css.length - 1] !== ';') css += ';';
  1453. var cssLength = css.length;
  1454. var isParenthesisOpen = false;
  1455. var lastPos = 0;
  1456. var i = 0;
  1457. var retCSS = '';
  1458. function addNewAttr () {
  1459. // 如果没有正常的闭合圆括号,则直接忽略当前属性
  1460. if (!isParenthesisOpen) {
  1461. var source = _.trim(css.slice(lastPos, i));
  1462. var j = source.indexOf(':');
  1463. if (j !== -1) {
  1464. var name = _.trim(source.slice(0, j));
  1465. var value = _.trim(source.slice(j + 1));
  1466. // 必须有属性名称
  1467. if (name) {
  1468. var ret = onAttr(lastPos, retCSS.length, name, value, source);
  1469. if (ret) retCSS += ret + '; ';
  1470. }
  1471. }
  1472. }
  1473. lastPos = i + 1;
  1474. }
  1475. for (; i < cssLength; i++) {
  1476. var c = css[i];
  1477. if (c === '/' && css[i + 1] === '*') {
  1478. // 备注开始
  1479. var j = css.indexOf('*/', i + 2);
  1480. // 如果没有正常的备注结束,则后面的部分全部跳过
  1481. if (j === -1) break;
  1482. // 直接将当前位置调到备注结尾,并且初始化状态
  1483. i = j + 1;
  1484. lastPos = i + 1;
  1485. isParenthesisOpen = false;
  1486. } else if (c === '(') {
  1487. isParenthesisOpen = true;
  1488. } else if (c === ')') {
  1489. isParenthesisOpen = false;
  1490. } else if (c === ';') {
  1491. if (isParenthesisOpen) {
  1492. // 在圆括号里面,忽略
  1493. } else {
  1494. addNewAttr();
  1495. }
  1496. } else if (c === '\n') {
  1497. addNewAttr();
  1498. }
  1499. }
  1500. return _.trim(retCSS);
  1501. }
  1502. module.exports = parseStyle;
  1503. },{"./util":10}],10:[function(require,module,exports){
  1504. module.exports = {
  1505. indexOf: function (arr, item) {
  1506. var i, j;
  1507. if (Array.prototype.indexOf) {
  1508. return arr.indexOf(item);
  1509. }
  1510. for (i = 0, j = arr.length; i < j; i++) {
  1511. if (arr[i] === item) {
  1512. return i;
  1513. }
  1514. }
  1515. return -1;
  1516. },
  1517. forEach: function (arr, fn, scope) {
  1518. var i, j;
  1519. if (Array.prototype.forEach) {
  1520. return arr.forEach(fn, scope);
  1521. }
  1522. for (i = 0, j = arr.length; i < j; i++) {
  1523. fn.call(scope, arr[i], i, arr);
  1524. }
  1525. },
  1526. trim: function (str) {
  1527. if (String.prototype.trim) {
  1528. return str.trim();
  1529. }
  1530. return str.replace(/(^\s*)|(\s*$)/g, '');
  1531. },
  1532. trimRight: function (str) {
  1533. if (String.prototype.trimRight) {
  1534. return str.trimRight();
  1535. }
  1536. return str.replace(/(\s*$)/g, '');
  1537. }
  1538. };
  1539. },{}]},{},[2]);