xss.js 51 KB

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