xss.d.ts 5.4 KB

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