xss.js 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  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. (function () {
  458. for (var i in DEFAULT) {
  459. exports[i] = DEFAULT[i];
  460. }
  461. for (var j in parser) {
  462. exports[j] = parser[j];
  463. }
  464. })();
  465. // using `xss` on the browser, output `filterXSS` to the globals
  466. if (typeof window !== "undefined") {
  467. window.filterXSS = module.exports;
  468. }
  469. // using `xss` on the WebWorker, output `filterXSS` to the globals
  470. function isWorkerEnv() {
  471. return (
  472. typeof self !== "undefined" &&
  473. typeof DedicatedWorkerGlobalScope !== "undefined" &&
  474. self instanceof DedicatedWorkerGlobalScope
  475. );
  476. }
  477. if (isWorkerEnv()) {
  478. self.filterXSS = module.exports;
  479. }
  480. },{"./default":1,"./parser":3,"./xss":5}],3:[function(require,module,exports){
  481. /**
  482. * Simple HTML Parser
  483. *
  484. * @author Zongmin Lei<leizongmin@gmail.com>
  485. */
  486. var _ = require("./util");
  487. /**
  488. * get tag name
  489. *
  490. * @param {String} html e.g. '<a hef="#">'
  491. * @return {String}
  492. */
  493. function getTagName(html) {
  494. var i = _.spaceIndex(html);
  495. var tagName;
  496. if (i === -1) {
  497. tagName = html.slice(1, -1);
  498. } else {
  499. tagName = html.slice(1, i + 1);
  500. }
  501. tagName = _.trim(tagName).toLowerCase();
  502. if (tagName.slice(0, 1) === "/") tagName = tagName.slice(1);
  503. if (tagName.slice(-1) === "/") tagName = tagName.slice(0, -1);
  504. return tagName;
  505. }
  506. /**
  507. * is close tag?
  508. *
  509. * @param {String} html 如:'<a hef="#">'
  510. * @return {Boolean}
  511. */
  512. function isClosing(html) {
  513. return html.slice(0, 2) === "</";
  514. }
  515. /**
  516. * parse input html and returns processed html
  517. *
  518. * @param {String} html
  519. * @param {Function} onTag e.g. function (sourcePosition, position, tag, html, isClosing)
  520. * @param {Function} escapeHtml
  521. * @return {String}
  522. */
  523. function parseTag(html, onTag, escapeHtml) {
  524. "use strict";
  525. var rethtml = "";
  526. var lastPos = 0;
  527. var tagStart = false;
  528. var quoteStart = false;
  529. var currentPos = 0;
  530. var len = html.length;
  531. var currentTagName = "";
  532. var currentHtml = "";
  533. chariterator: for (currentPos = 0; currentPos < len; currentPos++) {
  534. var c = html.charAt(currentPos);
  535. if (tagStart === false) {
  536. if (c === "<") {
  537. tagStart = currentPos;
  538. continue;
  539. }
  540. } else {
  541. if (quoteStart === false) {
  542. if (c === "<") {
  543. rethtml += escapeHtml(html.slice(lastPos, currentPos));
  544. tagStart = currentPos;
  545. lastPos = currentPos;
  546. continue;
  547. }
  548. if (c === ">") {
  549. rethtml += escapeHtml(html.slice(lastPos, tagStart));
  550. currentHtml = html.slice(tagStart, currentPos + 1);
  551. currentTagName = getTagName(currentHtml);
  552. rethtml += onTag(
  553. tagStart,
  554. rethtml.length,
  555. currentTagName,
  556. currentHtml,
  557. isClosing(currentHtml)
  558. );
  559. lastPos = currentPos + 1;
  560. tagStart = false;
  561. continue;
  562. }
  563. if (c === '"' || c === "'") {
  564. var i = 1;
  565. var ic = html.charAt(currentPos - i);
  566. while (ic.trim() === "" || ic === "=") {
  567. if (ic === "=") {
  568. quoteStart = c;
  569. continue chariterator;
  570. }
  571. ic = html.charAt(currentPos - ++i);
  572. }
  573. }
  574. } else {
  575. if (c === quoteStart) {
  576. quoteStart = false;
  577. continue;
  578. }
  579. }
  580. }
  581. }
  582. if (lastPos < html.length) {
  583. rethtml += escapeHtml(html.substr(lastPos));
  584. }
  585. return rethtml;
  586. }
  587. var REGEXP_ILLEGAL_ATTR_NAME = /[^a-zA-Z0-9\\_:.-]/gim;
  588. /**
  589. * parse input attributes and returns processed attributes
  590. *
  591. * @param {String} html e.g. `href="#" target="_blank"`
  592. * @param {Function} onAttr e.g. `function (name, value)`
  593. * @return {String}
  594. */
  595. function parseAttr(html, onAttr) {
  596. "use strict";
  597. var lastPos = 0;
  598. var lastMarkPos = 0;
  599. var retAttrs = [];
  600. var tmpName = false;
  601. var len = html.length;
  602. function addAttr(name, value) {
  603. name = _.trim(name);
  604. name = name.replace(REGEXP_ILLEGAL_ATTR_NAME, "").toLowerCase();
  605. if (name.length < 1) return;
  606. var ret = onAttr(name, value || "");
  607. if (ret) retAttrs.push(ret);
  608. }
  609. // 逐个分析字符
  610. for (var i = 0; i < len; i++) {
  611. var c = html.charAt(i);
  612. var v, j;
  613. if (tmpName === false && c === "=") {
  614. tmpName = html.slice(lastPos, i);
  615. lastPos = i + 1;
  616. lastMarkPos = html.charAt(lastPos) === '"' || html.charAt(lastPos) === "'" ? lastPos : findNextQuotationMark(html, i + 1);
  617. continue;
  618. }
  619. if (tmpName !== false) {
  620. if (
  621. i === lastMarkPos
  622. ) {
  623. j = html.indexOf(c, i + 1);
  624. if (j === -1) {
  625. break;
  626. } else {
  627. v = _.trim(html.slice(lastMarkPos + 1, j));
  628. addAttr(tmpName, v);
  629. tmpName = false;
  630. i = j;
  631. lastPos = i + 1;
  632. continue;
  633. }
  634. }
  635. }
  636. if (/\s|\n|\t/.test(c)) {
  637. html = html.replace(/\s|\n|\t/g, " ");
  638. if (tmpName === false) {
  639. j = findNextEqual(html, i);
  640. if (j === -1) {
  641. v = _.trim(html.slice(lastPos, i));
  642. addAttr(v);
  643. tmpName = false;
  644. lastPos = i + 1;
  645. continue;
  646. } else {
  647. i = j - 1;
  648. continue;
  649. }
  650. } else {
  651. j = findBeforeEqual(html, i - 1);
  652. if (j === -1) {
  653. v = _.trim(html.slice(lastPos, i));
  654. v = stripQuoteWrap(v);
  655. addAttr(tmpName, v);
  656. tmpName = false;
  657. lastPos = i + 1;
  658. continue;
  659. } else {
  660. continue;
  661. }
  662. }
  663. }
  664. }
  665. if (lastPos < html.length) {
  666. if (tmpName === false) {
  667. addAttr(html.slice(lastPos));
  668. } else {
  669. addAttr(tmpName, stripQuoteWrap(_.trim(html.slice(lastPos))));
  670. }
  671. }
  672. return _.trim(retAttrs.join(" "));
  673. }
  674. function findNextEqual(str, i) {
  675. for (; i < str.length; i++) {
  676. var c = str[i];
  677. if (c === " ") continue;
  678. if (c === "=") return i;
  679. return -1;
  680. }
  681. }
  682. function findNextQuotationMark(str, i) {
  683. for (; i < str.length; i++) {
  684. var c = str[i];
  685. if (c === " ") continue;
  686. if (c === "'" || c === '"') return i;
  687. return -1;
  688. }
  689. }
  690. function findBeforeEqual(str, i) {
  691. for (; i > 0; i--) {
  692. var c = str[i];
  693. if (c === " ") continue;
  694. if (c === "=") return i;
  695. return -1;
  696. }
  697. }
  698. function isQuoteWrapString(text) {
  699. if (
  700. (text[0] === '"' && text[text.length - 1] === '"') ||
  701. (text[0] === "'" && text[text.length - 1] === "'")
  702. ) {
  703. return true;
  704. } else {
  705. return false;
  706. }
  707. }
  708. function stripQuoteWrap(text) {
  709. if (isQuoteWrapString(text)) {
  710. return text.substr(1, text.length - 2);
  711. } else {
  712. return text;
  713. }
  714. }
  715. exports.parseTag = parseTag;
  716. exports.parseAttr = parseAttr;
  717. },{"./util":4}],4:[function(require,module,exports){
  718. module.exports = {
  719. indexOf: function (arr, item) {
  720. var i, j;
  721. if (Array.prototype.indexOf) {
  722. return arr.indexOf(item);
  723. }
  724. for (i = 0, j = arr.length; i < j; i++) {
  725. if (arr[i] === item) {
  726. return i;
  727. }
  728. }
  729. return -1;
  730. },
  731. forEach: function (arr, fn, scope) {
  732. var i, j;
  733. if (Array.prototype.forEach) {
  734. return arr.forEach(fn, scope);
  735. }
  736. for (i = 0, j = arr.length; i < j; i++) {
  737. fn.call(scope, arr[i], i, arr);
  738. }
  739. },
  740. trim: function (str) {
  741. if (String.prototype.trim) {
  742. return str.trim();
  743. }
  744. return str.replace(/(^\s*)|(\s*$)/g, "");
  745. },
  746. spaceIndex: function (str) {
  747. var reg = /\s|\n|\t/;
  748. var match = reg.exec(str);
  749. return match ? match.index : -1;
  750. },
  751. };
  752. },{}],5:[function(require,module,exports){
  753. /**
  754. * filter xss
  755. *
  756. * @author Zongmin Lei<leizongmin@gmail.com>
  757. */
  758. var FilterCSS = require("cssfilter").FilterCSS;
  759. var DEFAULT = require("./default");
  760. var parser = require("./parser");
  761. var parseTag = parser.parseTag;
  762. var parseAttr = parser.parseAttr;
  763. var _ = require("./util");
  764. /**
  765. * returns `true` if the input value is `undefined` or `null`
  766. *
  767. * @param {Object} obj
  768. * @return {Boolean}
  769. */
  770. function isNull(obj) {
  771. return obj === undefined || obj === null;
  772. }
  773. /**
  774. * get attributes for a tag
  775. *
  776. * @param {String} html
  777. * @return {Object}
  778. * - {String} html
  779. * - {Boolean} closing
  780. */
  781. function getAttrs(html) {
  782. var i = _.spaceIndex(html);
  783. if (i === -1) {
  784. return {
  785. html: "",
  786. closing: html[html.length - 2] === "/",
  787. };
  788. }
  789. html = _.trim(html.slice(i + 1, -1));
  790. var isClosing = html[html.length - 1] === "/";
  791. if (isClosing) html = _.trim(html.slice(0, -1));
  792. return {
  793. html: html,
  794. closing: isClosing,
  795. };
  796. }
  797. /**
  798. * shallow copy
  799. *
  800. * @param {Object} obj
  801. * @return {Object}
  802. */
  803. function shallowCopyObject(obj) {
  804. var ret = {};
  805. for (var i in obj) {
  806. ret[i] = obj[i];
  807. }
  808. return ret;
  809. }
  810. function keysToLowerCase(obj) {
  811. var ret = {};
  812. for (var i in obj) {
  813. if (Array.isArray(obj[i])) {
  814. ret[i.toLowerCase()] = obj[i].map(function (item) {
  815. return item.toLowerCase();
  816. });
  817. } else {
  818. ret[i.toLowerCase()] = obj[i];
  819. }
  820. }
  821. return ret;
  822. }
  823. /**
  824. * FilterXSS class
  825. *
  826. * @param {Object} options
  827. * whiteList (or allowList), onTag, onTagAttr, onIgnoreTag,
  828. * onIgnoreTagAttr, safeAttrValue, escapeHtml
  829. * stripIgnoreTagBody, allowCommentTag, stripBlankChar
  830. * css{whiteList, onAttr, onIgnoreAttr} `css=false` means don't use `cssfilter`
  831. */
  832. function FilterXSS(options) {
  833. options = shallowCopyObject(options || {});
  834. if (options.stripIgnoreTag) {
  835. if (options.onIgnoreTag) {
  836. console.error(
  837. 'Notes: cannot use these two options "stripIgnoreTag" and "onIgnoreTag" at the same time'
  838. );
  839. }
  840. options.onIgnoreTag = DEFAULT.onIgnoreTagStripAll;
  841. }
  842. if (options.whiteList || options.allowList) {
  843. options.whiteList = keysToLowerCase(options.whiteList || options.allowList);
  844. } else {
  845. options.whiteList = DEFAULT.whiteList;
  846. }
  847. options.onTag = options.onTag || DEFAULT.onTag;
  848. options.onTagAttr = options.onTagAttr || DEFAULT.onTagAttr;
  849. options.onIgnoreTag = options.onIgnoreTag || DEFAULT.onIgnoreTag;
  850. options.onIgnoreTagAttr = options.onIgnoreTagAttr || DEFAULT.onIgnoreTagAttr;
  851. options.safeAttrValue = options.safeAttrValue || DEFAULT.safeAttrValue;
  852. options.escapeHtml = options.escapeHtml || DEFAULT.escapeHtml;
  853. this.options = options;
  854. if (options.css === false) {
  855. this.cssFilter = false;
  856. } else {
  857. options.css = options.css || {};
  858. this.cssFilter = new FilterCSS(options.css);
  859. }
  860. }
  861. /**
  862. * start process and returns result
  863. *
  864. * @param {String} html
  865. * @return {String}
  866. */
  867. FilterXSS.prototype.process = function (html) {
  868. // compatible with the input
  869. html = html || "";
  870. html = html.toString();
  871. if (!html) return "";
  872. var me = this;
  873. var options = me.options;
  874. var whiteList = options.whiteList;
  875. var onTag = options.onTag;
  876. var onIgnoreTag = options.onIgnoreTag;
  877. var onTagAttr = options.onTagAttr;
  878. var onIgnoreTagAttr = options.onIgnoreTagAttr;
  879. var safeAttrValue = options.safeAttrValue;
  880. var escapeHtml = options.escapeHtml;
  881. var cssFilter = me.cssFilter;
  882. // remove invisible characters
  883. if (options.stripBlankChar) {
  884. html = DEFAULT.stripBlankChar(html);
  885. }
  886. // remove html comments
  887. if (!options.allowCommentTag) {
  888. html = DEFAULT.stripCommentTag(html);
  889. }
  890. // if enable stripIgnoreTagBody
  891. var stripIgnoreTagBody = false;
  892. if (options.stripIgnoreTagBody) {
  893. stripIgnoreTagBody = DEFAULT.StripTagBody(
  894. options.stripIgnoreTagBody,
  895. onIgnoreTag
  896. );
  897. onIgnoreTag = stripIgnoreTagBody.onIgnoreTag;
  898. }
  899. var retHtml = parseTag(
  900. html,
  901. function (sourcePosition, position, tag, html, isClosing) {
  902. var info = {
  903. sourcePosition: sourcePosition,
  904. position: position,
  905. isClosing: isClosing,
  906. isWhite: Object.prototype.hasOwnProperty.call(whiteList, tag),
  907. };
  908. // call `onTag()`
  909. var ret = onTag(tag, html, info);
  910. if (!isNull(ret)) return ret;
  911. if (info.isWhite) {
  912. if (info.isClosing) {
  913. return "</" + tag + ">";
  914. }
  915. var attrs = getAttrs(html);
  916. var whiteAttrList = whiteList[tag];
  917. var attrsHtml = parseAttr(attrs.html, function (name, value) {
  918. // call `onTagAttr()`
  919. var isWhiteAttr = _.indexOf(whiteAttrList, name) !== -1;
  920. var ret = onTagAttr(tag, name, value, isWhiteAttr);
  921. if (!isNull(ret)) return ret;
  922. if (isWhiteAttr) {
  923. // call `safeAttrValue()`
  924. value = safeAttrValue(tag, name, value, cssFilter);
  925. if (value) {
  926. return name + '="' + value + '"';
  927. } else {
  928. return name;
  929. }
  930. } else {
  931. // call `onIgnoreTagAttr()`
  932. ret = onIgnoreTagAttr(tag, name, value, isWhiteAttr);
  933. if (!isNull(ret)) return ret;
  934. return;
  935. }
  936. });
  937. // build new tag html
  938. html = "<" + tag;
  939. if (attrsHtml) html += " " + attrsHtml;
  940. if (attrs.closing) html += " /";
  941. html += ">";
  942. return html;
  943. } else {
  944. // call `onIgnoreTag()`
  945. ret = onIgnoreTag(tag, html, info);
  946. if (!isNull(ret)) return ret;
  947. return escapeHtml(html);
  948. }
  949. },
  950. escapeHtml
  951. );
  952. // if enable stripIgnoreTagBody
  953. if (stripIgnoreTagBody) {
  954. retHtml = stripIgnoreTagBody.remove(retHtml);
  955. }
  956. return retHtml;
  957. };
  958. module.exports = FilterXSS;
  959. },{"./default":1,"./parser":3,"./util":4,"cssfilter":8}],6:[function(require,module,exports){
  960. /**
  961. * cssfilter
  962. *
  963. * @author 老雷<leizongmin@gmail.com>
  964. */
  965. var DEFAULT = require('./default');
  966. var parseStyle = require('./parser');
  967. var _ = require('./util');
  968. /**
  969. * 返回值是否为空
  970. *
  971. * @param {Object} obj
  972. * @return {Boolean}
  973. */
  974. function isNull (obj) {
  975. return (obj === undefined || obj === null);
  976. }
  977. /**
  978. * 浅拷贝对象
  979. *
  980. * @param {Object} obj
  981. * @return {Object}
  982. */
  983. function shallowCopyObject (obj) {
  984. var ret = {};
  985. for (var i in obj) {
  986. ret[i] = obj[i];
  987. }
  988. return ret;
  989. }
  990. /**
  991. * 创建CSS过滤器
  992. *
  993. * @param {Object} options
  994. * - {Object} whiteList
  995. * - {Function} onAttr
  996. * - {Function} onIgnoreAttr
  997. * - {Function} safeAttrValue
  998. */
  999. function FilterCSS (options) {
  1000. options = shallowCopyObject(options || {});
  1001. options.whiteList = options.whiteList || DEFAULT.whiteList;
  1002. options.onAttr = options.onAttr || DEFAULT.onAttr;
  1003. options.onIgnoreAttr = options.onIgnoreAttr || DEFAULT.onIgnoreAttr;
  1004. options.safeAttrValue = options.safeAttrValue || DEFAULT.safeAttrValue;
  1005. this.options = options;
  1006. }
  1007. FilterCSS.prototype.process = function (css) {
  1008. // 兼容各种奇葩输入
  1009. css = css || '';
  1010. css = css.toString();
  1011. if (!css) return '';
  1012. var me = this;
  1013. var options = me.options;
  1014. var whiteList = options.whiteList;
  1015. var onAttr = options.onAttr;
  1016. var onIgnoreAttr = options.onIgnoreAttr;
  1017. var safeAttrValue = options.safeAttrValue;
  1018. var retCSS = parseStyle(css, function (sourcePosition, position, name, value, source) {
  1019. var check = whiteList[name];
  1020. var isWhite = false;
  1021. if (check === true) isWhite = check;
  1022. else if (typeof check === 'function') isWhite = check(value);
  1023. else if (check instanceof RegExp) isWhite = check.test(value);
  1024. if (isWhite !== true) isWhite = false;
  1025. // 如果过滤后 value 为空则直接忽略
  1026. value = safeAttrValue(name, value);
  1027. if (!value) return;
  1028. var opts = {
  1029. position: position,
  1030. sourcePosition: sourcePosition,
  1031. source: source,
  1032. isWhite: isWhite
  1033. };
  1034. if (isWhite) {
  1035. var ret = onAttr(name, value, opts);
  1036. if (isNull(ret)) {
  1037. return name + ':' + value;
  1038. } else {
  1039. return ret;
  1040. }
  1041. } else {
  1042. var ret = onIgnoreAttr(name, value, opts);
  1043. if (!isNull(ret)) {
  1044. return ret;
  1045. }
  1046. }
  1047. });
  1048. return retCSS;
  1049. };
  1050. module.exports = FilterCSS;
  1051. },{"./default":7,"./parser":9,"./util":10}],7:[function(require,module,exports){
  1052. /**
  1053. * cssfilter
  1054. *
  1055. * @author 老雷<leizongmin@gmail.com>
  1056. */
  1057. function getDefaultWhiteList () {
  1058. // 白名单值说明:
  1059. // true: 允许该属性
  1060. // Function: function (val) { } 返回true表示允许该属性,其他值均表示不允许
  1061. // RegExp: regexp.test(val) 返回true表示允许该属性,其他值均表示不允许
  1062. // 除上面列出的值外均表示不允许
  1063. var whiteList = {};
  1064. whiteList['align-content'] = false; // default: auto
  1065. whiteList['align-items'] = false; // default: auto
  1066. whiteList['align-self'] = false; // default: auto
  1067. whiteList['alignment-adjust'] = false; // default: auto
  1068. whiteList['alignment-baseline'] = false; // default: baseline
  1069. whiteList['all'] = false; // default: depending on individual properties
  1070. whiteList['anchor-point'] = false; // default: none
  1071. whiteList['animation'] = false; // default: depending on individual properties
  1072. whiteList['animation-delay'] = false; // default: 0
  1073. whiteList['animation-direction'] = false; // default: normal
  1074. whiteList['animation-duration'] = false; // default: 0
  1075. whiteList['animation-fill-mode'] = false; // default: none
  1076. whiteList['animation-iteration-count'] = false; // default: 1
  1077. whiteList['animation-name'] = false; // default: none
  1078. whiteList['animation-play-state'] = false; // default: running
  1079. whiteList['animation-timing-function'] = false; // default: ease
  1080. whiteList['azimuth'] = false; // default: center
  1081. whiteList['backface-visibility'] = false; // default: visible
  1082. whiteList['background'] = true; // default: depending on individual properties
  1083. whiteList['background-attachment'] = true; // default: scroll
  1084. whiteList['background-clip'] = true; // default: border-box
  1085. whiteList['background-color'] = true; // default: transparent
  1086. whiteList['background-image'] = true; // default: none
  1087. whiteList['background-origin'] = true; // default: padding-box
  1088. whiteList['background-position'] = true; // default: 0% 0%
  1089. whiteList['background-repeat'] = true; // default: repeat
  1090. whiteList['background-size'] = true; // default: auto
  1091. whiteList['baseline-shift'] = false; // default: baseline
  1092. whiteList['binding'] = false; // default: none
  1093. whiteList['bleed'] = false; // default: 6pt
  1094. whiteList['bookmark-label'] = false; // default: content()
  1095. whiteList['bookmark-level'] = false; // default: none
  1096. whiteList['bookmark-state'] = false; // default: open
  1097. whiteList['border'] = true; // default: depending on individual properties
  1098. whiteList['border-bottom'] = true; // default: depending on individual properties
  1099. whiteList['border-bottom-color'] = true; // default: current color
  1100. whiteList['border-bottom-left-radius'] = true; // default: 0
  1101. whiteList['border-bottom-right-radius'] = true; // default: 0
  1102. whiteList['border-bottom-style'] = true; // default: none
  1103. whiteList['border-bottom-width'] = true; // default: medium
  1104. whiteList['border-collapse'] = true; // default: separate
  1105. whiteList['border-color'] = true; // default: depending on individual properties
  1106. whiteList['border-image'] = true; // default: none
  1107. whiteList['border-image-outset'] = true; // default: 0
  1108. whiteList['border-image-repeat'] = true; // default: stretch
  1109. whiteList['border-image-slice'] = true; // default: 100%
  1110. whiteList['border-image-source'] = true; // default: none
  1111. whiteList['border-image-width'] = true; // default: 1
  1112. whiteList['border-left'] = true; // default: depending on individual properties
  1113. whiteList['border-left-color'] = true; // default: current color
  1114. whiteList['border-left-style'] = true; // default: none
  1115. whiteList['border-left-width'] = true; // default: medium
  1116. whiteList['border-radius'] = true; // default: 0
  1117. whiteList['border-right'] = true; // default: depending on individual properties
  1118. whiteList['border-right-color'] = true; // default: current color
  1119. whiteList['border-right-style'] = true; // default: none
  1120. whiteList['border-right-width'] = true; // default: medium
  1121. whiteList['border-spacing'] = true; // default: 0
  1122. whiteList['border-style'] = true; // default: depending on individual properties
  1123. whiteList['border-top'] = true; // default: depending on individual properties
  1124. whiteList['border-top-color'] = true; // default: current color
  1125. whiteList['border-top-left-radius'] = true; // default: 0
  1126. whiteList['border-top-right-radius'] = true; // default: 0
  1127. whiteList['border-top-style'] = true; // default: none
  1128. whiteList['border-top-width'] = true; // default: medium
  1129. whiteList['border-width'] = true; // default: depending on individual properties
  1130. whiteList['bottom'] = false; // default: auto
  1131. whiteList['box-decoration-break'] = true; // default: slice
  1132. whiteList['box-shadow'] = true; // default: none
  1133. whiteList['box-sizing'] = true; // default: content-box
  1134. whiteList['box-snap'] = true; // default: none
  1135. whiteList['box-suppress'] = true; // default: show
  1136. whiteList['break-after'] = true; // default: auto
  1137. whiteList['break-before'] = true; // default: auto
  1138. whiteList['break-inside'] = true; // default: auto
  1139. whiteList['caption-side'] = false; // default: top
  1140. whiteList['chains'] = false; // default: none
  1141. whiteList['clear'] = true; // default: none
  1142. whiteList['clip'] = false; // default: auto
  1143. whiteList['clip-path'] = false; // default: none
  1144. whiteList['clip-rule'] = false; // default: nonzero
  1145. whiteList['color'] = true; // default: implementation dependent
  1146. whiteList['color-interpolation-filters'] = true; // default: auto
  1147. whiteList['column-count'] = false; // default: auto
  1148. whiteList['column-fill'] = false; // default: balance
  1149. whiteList['column-gap'] = false; // default: normal
  1150. whiteList['column-rule'] = false; // default: depending on individual properties
  1151. whiteList['column-rule-color'] = false; // default: current color
  1152. whiteList['column-rule-style'] = false; // default: medium
  1153. whiteList['column-rule-width'] = false; // default: medium
  1154. whiteList['column-span'] = false; // default: none
  1155. whiteList['column-width'] = false; // default: auto
  1156. whiteList['columns'] = false; // default: depending on individual properties
  1157. whiteList['contain'] = false; // default: none
  1158. whiteList['content'] = false; // default: normal
  1159. whiteList['counter-increment'] = false; // default: none
  1160. whiteList['counter-reset'] = false; // default: none
  1161. whiteList['counter-set'] = false; // default: none
  1162. whiteList['crop'] = false; // default: auto
  1163. whiteList['cue'] = false; // default: depending on individual properties
  1164. whiteList['cue-after'] = false; // default: none
  1165. whiteList['cue-before'] = false; // default: none
  1166. whiteList['cursor'] = false; // default: auto
  1167. whiteList['direction'] = false; // default: ltr
  1168. whiteList['display'] = true; // default: depending on individual properties
  1169. whiteList['display-inside'] = true; // default: auto
  1170. whiteList['display-list'] = true; // default: none
  1171. whiteList['display-outside'] = true; // default: inline-level
  1172. whiteList['dominant-baseline'] = false; // default: auto
  1173. whiteList['elevation'] = false; // default: level
  1174. whiteList['empty-cells'] = false; // default: show
  1175. whiteList['filter'] = false; // default: none
  1176. whiteList['flex'] = false; // default: depending on individual properties
  1177. whiteList['flex-basis'] = false; // default: auto
  1178. whiteList['flex-direction'] = false; // default: row
  1179. whiteList['flex-flow'] = false; // default: depending on individual properties
  1180. whiteList['flex-grow'] = false; // default: 0
  1181. whiteList['flex-shrink'] = false; // default: 1
  1182. whiteList['flex-wrap'] = false; // default: nowrap
  1183. whiteList['float'] = false; // default: none
  1184. whiteList['float-offset'] = false; // default: 0 0
  1185. whiteList['flood-color'] = false; // default: black
  1186. whiteList['flood-opacity'] = false; // default: 1
  1187. whiteList['flow-from'] = false; // default: none
  1188. whiteList['flow-into'] = false; // default: none
  1189. whiteList['font'] = true; // default: depending on individual properties
  1190. whiteList['font-family'] = true; // default: implementation dependent
  1191. whiteList['font-feature-settings'] = true; // default: normal
  1192. whiteList['font-kerning'] = true; // default: auto
  1193. whiteList['font-language-override'] = true; // default: normal
  1194. whiteList['font-size'] = true; // default: medium
  1195. whiteList['font-size-adjust'] = true; // default: none
  1196. whiteList['font-stretch'] = true; // default: normal
  1197. whiteList['font-style'] = true; // default: normal
  1198. whiteList['font-synthesis'] = true; // default: weight style
  1199. whiteList['font-variant'] = true; // default: normal
  1200. whiteList['font-variant-alternates'] = true; // default: normal
  1201. whiteList['font-variant-caps'] = true; // default: normal
  1202. whiteList['font-variant-east-asian'] = true; // default: normal
  1203. whiteList['font-variant-ligatures'] = true; // default: normal
  1204. whiteList['font-variant-numeric'] = true; // default: normal
  1205. whiteList['font-variant-position'] = true; // default: normal
  1206. whiteList['font-weight'] = true; // default: normal
  1207. whiteList['grid'] = false; // default: depending on individual properties
  1208. whiteList['grid-area'] = false; // default: depending on individual properties
  1209. whiteList['grid-auto-columns'] = false; // default: auto
  1210. whiteList['grid-auto-flow'] = false; // default: none
  1211. whiteList['grid-auto-rows'] = false; // default: auto
  1212. whiteList['grid-column'] = false; // default: depending on individual properties
  1213. whiteList['grid-column-end'] = false; // default: auto
  1214. whiteList['grid-column-start'] = false; // default: auto
  1215. whiteList['grid-row'] = false; // default: depending on individual properties
  1216. whiteList['grid-row-end'] = false; // default: auto
  1217. whiteList['grid-row-start'] = false; // default: auto
  1218. whiteList['grid-template'] = false; // default: depending on individual properties
  1219. whiteList['grid-template-areas'] = false; // default: none
  1220. whiteList['grid-template-columns'] = false; // default: none
  1221. whiteList['grid-template-rows'] = false; // default: none
  1222. whiteList['hanging-punctuation'] = false; // default: none
  1223. whiteList['height'] = true; // default: auto
  1224. whiteList['hyphens'] = false; // default: manual
  1225. whiteList['icon'] = false; // default: auto
  1226. whiteList['image-orientation'] = false; // default: auto
  1227. whiteList['image-resolution'] = false; // default: normal
  1228. whiteList['ime-mode'] = false; // default: auto
  1229. whiteList['initial-letters'] = false; // default: normal
  1230. whiteList['inline-box-align'] = false; // default: last
  1231. whiteList['justify-content'] = false; // default: auto
  1232. whiteList['justify-items'] = false; // default: auto
  1233. whiteList['justify-self'] = false; // default: auto
  1234. whiteList['left'] = false; // default: auto
  1235. whiteList['letter-spacing'] = true; // default: normal
  1236. whiteList['lighting-color'] = true; // default: white
  1237. whiteList['line-box-contain'] = false; // default: block inline replaced
  1238. whiteList['line-break'] = false; // default: auto
  1239. whiteList['line-grid'] = false; // default: match-parent
  1240. whiteList['line-height'] = false; // default: normal
  1241. whiteList['line-snap'] = false; // default: none
  1242. whiteList['line-stacking'] = false; // default: depending on individual properties
  1243. whiteList['line-stacking-ruby'] = false; // default: exclude-ruby
  1244. whiteList['line-stacking-shift'] = false; // default: consider-shifts
  1245. whiteList['line-stacking-strategy'] = false; // default: inline-line-height
  1246. whiteList['list-style'] = true; // default: depending on individual properties
  1247. whiteList['list-style-image'] = true; // default: none
  1248. whiteList['list-style-position'] = true; // default: outside
  1249. whiteList['list-style-type'] = true; // default: disc
  1250. whiteList['margin'] = true; // default: depending on individual properties
  1251. whiteList['margin-bottom'] = true; // default: 0
  1252. whiteList['margin-left'] = true; // default: 0
  1253. whiteList['margin-right'] = true; // default: 0
  1254. whiteList['margin-top'] = true; // default: 0
  1255. whiteList['marker-offset'] = false; // default: auto
  1256. whiteList['marker-side'] = false; // default: list-item
  1257. whiteList['marks'] = false; // default: none
  1258. whiteList['mask'] = false; // default: border-box
  1259. whiteList['mask-box'] = false; // default: see individual properties
  1260. whiteList['mask-box-outset'] = false; // default: 0
  1261. whiteList['mask-box-repeat'] = false; // default: stretch
  1262. whiteList['mask-box-slice'] = false; // default: 0 fill
  1263. whiteList['mask-box-source'] = false; // default: none
  1264. whiteList['mask-box-width'] = false; // default: auto
  1265. whiteList['mask-clip'] = false; // default: border-box
  1266. whiteList['mask-image'] = false; // default: none
  1267. whiteList['mask-origin'] = false; // default: border-box
  1268. whiteList['mask-position'] = false; // default: center
  1269. whiteList['mask-repeat'] = false; // default: no-repeat
  1270. whiteList['mask-size'] = false; // default: border-box
  1271. whiteList['mask-source-type'] = false; // default: auto
  1272. whiteList['mask-type'] = false; // default: luminance
  1273. whiteList['max-height'] = true; // default: none
  1274. whiteList['max-lines'] = false; // default: none
  1275. whiteList['max-width'] = true; // default: none
  1276. whiteList['min-height'] = true; // default: 0
  1277. whiteList['min-width'] = true; // default: 0
  1278. whiteList['move-to'] = false; // default: normal
  1279. whiteList['nav-down'] = false; // default: auto
  1280. whiteList['nav-index'] = false; // default: auto
  1281. whiteList['nav-left'] = false; // default: auto
  1282. whiteList['nav-right'] = false; // default: auto
  1283. whiteList['nav-up'] = false; // default: auto
  1284. whiteList['object-fit'] = false; // default: fill
  1285. whiteList['object-position'] = false; // default: 50% 50%
  1286. whiteList['opacity'] = false; // default: 1
  1287. whiteList['order'] = false; // default: 0
  1288. whiteList['orphans'] = false; // default: 2
  1289. whiteList['outline'] = false; // default: depending on individual properties
  1290. whiteList['outline-color'] = false; // default: invert
  1291. whiteList['outline-offset'] = false; // default: 0
  1292. whiteList['outline-style'] = false; // default: none
  1293. whiteList['outline-width'] = false; // default: medium
  1294. whiteList['overflow'] = false; // default: depending on individual properties
  1295. whiteList['overflow-wrap'] = false; // default: normal
  1296. whiteList['overflow-x'] = false; // default: visible
  1297. whiteList['overflow-y'] = false; // default: visible
  1298. whiteList['padding'] = true; // default: depending on individual properties
  1299. whiteList['padding-bottom'] = true; // default: 0
  1300. whiteList['padding-left'] = true; // default: 0
  1301. whiteList['padding-right'] = true; // default: 0
  1302. whiteList['padding-top'] = true; // default: 0
  1303. whiteList['page'] = false; // default: auto
  1304. whiteList['page-break-after'] = false; // default: auto
  1305. whiteList['page-break-before'] = false; // default: auto
  1306. whiteList['page-break-inside'] = false; // default: auto
  1307. whiteList['page-policy'] = false; // default: start
  1308. whiteList['pause'] = false; // default: implementation dependent
  1309. whiteList['pause-after'] = false; // default: implementation dependent
  1310. whiteList['pause-before'] = false; // default: implementation dependent
  1311. whiteList['perspective'] = false; // default: none
  1312. whiteList['perspective-origin'] = false; // default: 50% 50%
  1313. whiteList['pitch'] = false; // default: medium
  1314. whiteList['pitch-range'] = false; // default: 50
  1315. whiteList['play-during'] = false; // default: auto
  1316. whiteList['position'] = false; // default: static
  1317. whiteList['presentation-level'] = false; // default: 0
  1318. whiteList['quotes'] = false; // default: text
  1319. whiteList['region-fragment'] = false; // default: auto
  1320. whiteList['resize'] = false; // default: none
  1321. whiteList['rest'] = false; // default: depending on individual properties
  1322. whiteList['rest-after'] = false; // default: none
  1323. whiteList['rest-before'] = false; // default: none
  1324. whiteList['richness'] = false; // default: 50
  1325. whiteList['right'] = false; // default: auto
  1326. whiteList['rotation'] = false; // default: 0
  1327. whiteList['rotation-point'] = false; // default: 50% 50%
  1328. whiteList['ruby-align'] = false; // default: auto
  1329. whiteList['ruby-merge'] = false; // default: separate
  1330. whiteList['ruby-position'] = false; // default: before
  1331. whiteList['shape-image-threshold'] = false; // default: 0.0
  1332. whiteList['shape-outside'] = false; // default: none
  1333. whiteList['shape-margin'] = false; // default: 0
  1334. whiteList['size'] = false; // default: auto
  1335. whiteList['speak'] = false; // default: auto
  1336. whiteList['speak-as'] = false; // default: normal
  1337. whiteList['speak-header'] = false; // default: once
  1338. whiteList['speak-numeral'] = false; // default: continuous
  1339. whiteList['speak-punctuation'] = false; // default: none
  1340. whiteList['speech-rate'] = false; // default: medium
  1341. whiteList['stress'] = false; // default: 50
  1342. whiteList['string-set'] = false; // default: none
  1343. whiteList['tab-size'] = false; // default: 8
  1344. whiteList['table-layout'] = false; // default: auto
  1345. whiteList['text-align'] = true; // default: start
  1346. whiteList['text-align-last'] = true; // default: auto
  1347. whiteList['text-combine-upright'] = true; // default: none
  1348. whiteList['text-decoration'] = true; // default: none
  1349. whiteList['text-decoration-color'] = true; // default: currentColor
  1350. whiteList['text-decoration-line'] = true; // default: none
  1351. whiteList['text-decoration-skip'] = true; // default: objects
  1352. whiteList['text-decoration-style'] = true; // default: solid
  1353. whiteList['text-emphasis'] = true; // default: depending on individual properties
  1354. whiteList['text-emphasis-color'] = true; // default: currentColor
  1355. whiteList['text-emphasis-position'] = true; // default: over right
  1356. whiteList['text-emphasis-style'] = true; // default: none
  1357. whiteList['text-height'] = true; // default: auto
  1358. whiteList['text-indent'] = true; // default: 0
  1359. whiteList['text-justify'] = true; // default: auto
  1360. whiteList['text-orientation'] = true; // default: mixed
  1361. whiteList['text-overflow'] = true; // default: clip
  1362. whiteList['text-shadow'] = true; // default: none
  1363. whiteList['text-space-collapse'] = true; // default: collapse
  1364. whiteList['text-transform'] = true; // default: none
  1365. whiteList['text-underline-position'] = true; // default: auto
  1366. whiteList['text-wrap'] = true; // default: normal
  1367. whiteList['top'] = false; // default: auto
  1368. whiteList['transform'] = false; // default: none
  1369. whiteList['transform-origin'] = false; // default: 50% 50% 0
  1370. whiteList['transform-style'] = false; // default: flat
  1371. whiteList['transition'] = false; // default: depending on individual properties
  1372. whiteList['transition-delay'] = false; // default: 0s
  1373. whiteList['transition-duration'] = false; // default: 0s
  1374. whiteList['transition-property'] = false; // default: all
  1375. whiteList['transition-timing-function'] = false; // default: ease
  1376. whiteList['unicode-bidi'] = false; // default: normal
  1377. whiteList['vertical-align'] = false; // default: baseline
  1378. whiteList['visibility'] = false; // default: visible
  1379. whiteList['voice-balance'] = false; // default: center
  1380. whiteList['voice-duration'] = false; // default: auto
  1381. whiteList['voice-family'] = false; // default: implementation dependent
  1382. whiteList['voice-pitch'] = false; // default: medium
  1383. whiteList['voice-range'] = false; // default: medium
  1384. whiteList['voice-rate'] = false; // default: normal
  1385. whiteList['voice-stress'] = false; // default: normal
  1386. whiteList['voice-volume'] = false; // default: medium
  1387. whiteList['volume'] = false; // default: medium
  1388. whiteList['white-space'] = false; // default: normal
  1389. whiteList['widows'] = false; // default: 2
  1390. whiteList['width'] = true; // default: auto
  1391. whiteList['will-change'] = false; // default: auto
  1392. whiteList['word-break'] = true; // default: normal
  1393. whiteList['word-spacing'] = true; // default: normal
  1394. whiteList['word-wrap'] = true; // default: normal
  1395. whiteList['wrap-flow'] = false; // default: auto
  1396. whiteList['wrap-through'] = false; // default: wrap
  1397. whiteList['writing-mode'] = false; // default: horizontal-tb
  1398. whiteList['z-index'] = false; // default: auto
  1399. return whiteList;
  1400. }
  1401. /**
  1402. * 匹配到白名单上的一个属性时
  1403. *
  1404. * @param {String} name
  1405. * @param {String} value
  1406. * @param {Object} options
  1407. * @return {String}
  1408. */
  1409. function onAttr (name, value, options) {
  1410. // do nothing
  1411. }
  1412. /**
  1413. * 匹配到不在白名单上的一个属性时
  1414. *
  1415. * @param {String} name
  1416. * @param {String} value
  1417. * @param {Object} options
  1418. * @return {String}
  1419. */
  1420. function onIgnoreAttr (name, value, options) {
  1421. // do nothing
  1422. }
  1423. var REGEXP_URL_JAVASCRIPT = /javascript\s*\:/img;
  1424. /**
  1425. * 过滤属性值
  1426. *
  1427. * @param {String} name
  1428. * @param {String} value
  1429. * @return {String}
  1430. */
  1431. function safeAttrValue(name, value) {
  1432. if (REGEXP_URL_JAVASCRIPT.test(value)) return '';
  1433. return value;
  1434. }
  1435. exports.whiteList = getDefaultWhiteList();
  1436. exports.getDefaultWhiteList = getDefaultWhiteList;
  1437. exports.onAttr = onAttr;
  1438. exports.onIgnoreAttr = onIgnoreAttr;
  1439. exports.safeAttrValue = safeAttrValue;
  1440. },{}],8:[function(require,module,exports){
  1441. /**
  1442. * cssfilter
  1443. *
  1444. * @author 老雷<leizongmin@gmail.com>
  1445. */
  1446. var DEFAULT = require('./default');
  1447. var FilterCSS = require('./css');
  1448. /**
  1449. * XSS过滤
  1450. *
  1451. * @param {String} css 要过滤的CSS代码
  1452. * @param {Object} options 选项:whiteList, onAttr, onIgnoreAttr
  1453. * @return {String}
  1454. */
  1455. function filterCSS (html, options) {
  1456. var xss = new FilterCSS(options);
  1457. return xss.process(html);
  1458. }
  1459. // 输出
  1460. exports = module.exports = filterCSS;
  1461. exports.FilterCSS = FilterCSS;
  1462. for (var i in DEFAULT) exports[i] = DEFAULT[i];
  1463. // 在浏览器端使用
  1464. if (typeof window !== 'undefined') {
  1465. window.filterCSS = module.exports;
  1466. }
  1467. },{"./css":6,"./default":7}],9:[function(require,module,exports){
  1468. /**
  1469. * cssfilter
  1470. *
  1471. * @author 老雷<leizongmin@gmail.com>
  1472. */
  1473. var _ = require('./util');
  1474. /**
  1475. * 解析style
  1476. *
  1477. * @param {String} css
  1478. * @param {Function} onAttr 处理属性的函数
  1479. * 参数格式: function (sourcePosition, position, name, value, source)
  1480. * @return {String}
  1481. */
  1482. function parseStyle (css, onAttr) {
  1483. css = _.trimRight(css);
  1484. if (css[css.length - 1] !== ';') css += ';';
  1485. var cssLength = css.length;
  1486. var isParenthesisOpen = false;
  1487. var lastPos = 0;
  1488. var i = 0;
  1489. var retCSS = '';
  1490. function addNewAttr () {
  1491. // 如果没有正常的闭合圆括号,则直接忽略当前属性
  1492. if (!isParenthesisOpen) {
  1493. var source = _.trim(css.slice(lastPos, i));
  1494. var j = source.indexOf(':');
  1495. if (j !== -1) {
  1496. var name = _.trim(source.slice(0, j));
  1497. var value = _.trim(source.slice(j + 1));
  1498. // 必须有属性名称
  1499. if (name) {
  1500. var ret = onAttr(lastPos, retCSS.length, name, value, source);
  1501. if (ret) retCSS += ret + '; ';
  1502. }
  1503. }
  1504. }
  1505. lastPos = i + 1;
  1506. }
  1507. for (; i < cssLength; i++) {
  1508. var c = css[i];
  1509. if (c === '/' && css[i + 1] === '*') {
  1510. // 备注开始
  1511. var j = css.indexOf('*/', i + 2);
  1512. // 如果没有正常的备注结束,则后面的部分全部跳过
  1513. if (j === -1) break;
  1514. // 直接将当前位置调到备注结尾,并且初始化状态
  1515. i = j + 1;
  1516. lastPos = i + 1;
  1517. isParenthesisOpen = false;
  1518. } else if (c === '(') {
  1519. isParenthesisOpen = true;
  1520. } else if (c === ')') {
  1521. isParenthesisOpen = false;
  1522. } else if (c === ';') {
  1523. if (isParenthesisOpen) {
  1524. // 在圆括号里面,忽略
  1525. } else {
  1526. addNewAttr();
  1527. }
  1528. } else if (c === '\n') {
  1529. addNewAttr();
  1530. }
  1531. }
  1532. return _.trim(retCSS);
  1533. }
  1534. module.exports = parseStyle;
  1535. },{"./util":10}],10:[function(require,module,exports){
  1536. module.exports = {
  1537. indexOf: function (arr, item) {
  1538. var i, j;
  1539. if (Array.prototype.indexOf) {
  1540. return arr.indexOf(item);
  1541. }
  1542. for (i = 0, j = arr.length; i < j; i++) {
  1543. if (arr[i] === item) {
  1544. return i;
  1545. }
  1546. }
  1547. return -1;
  1548. },
  1549. forEach: function (arr, fn, scope) {
  1550. var i, j;
  1551. if (Array.prototype.forEach) {
  1552. return arr.forEach(fn, scope);
  1553. }
  1554. for (i = 0, j = arr.length; i < j; i++) {
  1555. fn.call(scope, arr[i], i, arr);
  1556. }
  1557. },
  1558. trim: function (str) {
  1559. if (String.prototype.trim) {
  1560. return str.trim();
  1561. }
  1562. return str.replace(/(^\s*)|(\s*$)/g, '');
  1563. },
  1564. trimRight: function (str) {
  1565. if (String.prototype.trimRight) {
  1566. return str.trimRight();
  1567. }
  1568. return str.replace(/(\s*$)/g, '');
  1569. }
  1570. };
  1571. },{}]},{},[2]);