xss.d.ts 5.3 KB

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