xss.js 49 KB

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