xss.d.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * xss
  3. *
  4. * @author Zongmin Lei<leizongmin@gmail.com>
  5. */
  6. declare module "xss" {
  7. global {
  8. function filterXSS(html: string, options?: IFilterXSSOptions): string;
  9. namespace XSS {
  10. export interface IFilterXSSOptions {
  11. whiteList?: IWhiteList;
  12. onTag?: OnTagHandler;
  13. onTagAttr?: OnTagAttrHandler;
  14. onIgnoreTag?: OnTagHandler;
  15. onIgnoreTagAttr?: OnTagAttrHandler;
  16. safeAttrValue?: SafeAttrValueHandler;
  17. escapeHtml?: EscapeHandler;
  18. stripIgnoreTag?: boolean;
  19. stripIgnoreTagBody?: boolean | string[];
  20. allowCommentTag?: boolean;
  21. stripBlankChar?: boolean;
  22. css?: {} | boolean;
  23. }
  24. interface IWhiteList extends Record<string, string[] | undefined> {
  25. a?: string[];
  26. abbr?: string[];
  27. address?: string[];
  28. area?: string[];
  29. article?: string[];
  30. aside?: string[];
  31. audio?: string[];
  32. b?: string[];
  33. bdi?: string[];
  34. bdo?: string[];
  35. big?: string[];
  36. blockquote?: string[];
  37. br?: string[];
  38. caption?: string[];
  39. center?: string[];
  40. cite?: string[];
  41. code?: string[];
  42. col?: string[];
  43. colgroup?: string[];
  44. dd?: string[];
  45. del?: string[];
  46. details?: string[];
  47. div?: string[];
  48. dl?: string[];
  49. dt?: string[];
  50. em?: string[];
  51. figure?: string[];
  52. figcaption?: string[];
  53. font?: string[];
  54. footer?: string[];
  55. h1?: string[];
  56. h2?: string[];
  57. h3?: string[];
  58. h4?: string[];
  59. h5?: string[];
  60. h6?: string[];
  61. header?: string[];
  62. hr?: string[];
  63. i?: string[];
  64. img?: string[];
  65. ins?: string[];
  66. li?: string[];
  67. mark?: string[];
  68. nav?: string[];
  69. ol?: string[];
  70. p?: string[];
  71. pre?: string[];
  72. s?: string[];
  73. section?: string[];
  74. small?: string[];
  75. span?: string[];
  76. sub?: string[];
  77. sup?: string[];
  78. strong?: string[];
  79. strike?: string[];
  80. summary?: string[];
  81. table?: string[];
  82. tbody?: string[];
  83. td?: string[];
  84. tfoot?: string[];
  85. th?: string[];
  86. thead?: string[];
  87. tr?: string[];
  88. tt?: string[];
  89. u?: string[];
  90. ul?: string[];
  91. video?: string[];
  92. }
  93. type OnTagHandler = (
  94. tag: string,
  95. html: string,
  96. options: {
  97. sourcePosition?: number;
  98. position?: number;
  99. isClosing?: boolean;
  100. isWhite?: boolean;
  101. }
  102. ) => string | void;
  103. type OnTagAttrHandler = (
  104. tag: string,
  105. name: string,
  106. value: string,
  107. isWhiteAttr: boolean
  108. ) => string | void;
  109. type SafeAttrValueHandler = (
  110. tag: string,
  111. name: string,
  112. value: string,
  113. cssFilter: ICSSFilter
  114. ) => string;
  115. type EscapeHandler = (str: string) => string;
  116. interface ICSSFilter {
  117. process(value: string): string;
  118. }
  119. }
  120. }
  121. export interface IFilterXSSOptions extends XSS.IFilterXSSOptions {}
  122. export interface IWhiteList extends XSS.IWhiteList {}
  123. export type OnTagHandler = XSS.OnTagHandler;
  124. export type OnTagAttrHandler = XSS.OnTagAttrHandler;
  125. export type SafeAttrValueHandler = XSS.SafeAttrValueHandler;
  126. export type EscapeHandler = XSS.EscapeHandler;
  127. export interface ICSSFilter extends XSS.ICSSFilter {}
  128. export function StripTagBody(
  129. tags: string[],
  130. next: () => void
  131. ): {
  132. onIgnoreTag(
  133. tag: string,
  134. html: string,
  135. options: {
  136. position: number;
  137. isClosing: boolean;
  138. }
  139. ): string;
  140. remove(html: string): string;
  141. };
  142. export class FilterXSS {
  143. constructor(options?: IFilterXSSOptions);
  144. process(html: string): string;
  145. }
  146. export function filterXSS(html: string, options?: IFilterXSSOptions): string;
  147. export function parseTag(
  148. html: string,
  149. onTag: (
  150. sourcePosition: number,
  151. position: number,
  152. tag: string,
  153. html: string,
  154. isClosing: boolean
  155. ) => string,
  156. escapeHtml: EscapeHandler
  157. ): string;
  158. export function parseAttr(
  159. html: string,
  160. onAttr: (name: string, value: string) => string
  161. ): string;
  162. export const whiteList: IWhiteList;
  163. export function getDefaultWhiteList(): IWhiteList;
  164. export const onTag: OnTagHandler;
  165. export const onIgnoreTag: OnTagHandler;
  166. export const onTagAttr: OnTagAttrHandler;
  167. export const onIgnoreTagAttr: OnTagAttrHandler;
  168. export const safeAttrValue: SafeAttrValueHandler;
  169. export const escapeHtml: EscapeHandler;
  170. export const escapeQuote: EscapeHandler;
  171. export const unescapeQuote: EscapeHandler;
  172. export const escapeHtmlEntities: EscapeHandler;
  173. export const escapeDangerHtml5Entities: EscapeHandler;
  174. export const clearNonPrintableCharacter: EscapeHandler;
  175. export const friendlyAttrValue: EscapeHandler;
  176. export const escapeAttrValue: EscapeHandler;
  177. export function onIgnoreTagStripAll(): string;
  178. export const stripCommentTag: EscapeHandler;
  179. export const stripBlankChar: EscapeHandler;
  180. export const cssFilter: ICSSFilter;
  181. export function getDefaultCSSWhiteList(): ICSSFilter;
  182. const xss: (html: string, options?: IFilterXSSOptions) => string;
  183. export default xss;
  184. }