index.d.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /// <reference types="xss" />
  2. /**
  3. * The response from the validate method
  4. *
  5. * @export
  6. * @interface IValidationResponse
  7. */
  8. export interface IValidationResponse {
  9. isValid: boolean;
  10. sanitized: any;
  11. }
  12. export interface IWhiteList extends XSS.IWhiteList {
  13. source?: string[];
  14. }
  15. /** Options to apply to sanitize method */
  16. export interface ISanitizeOptions {
  17. allowUndefined?: boolean;
  18. }
  19. /**
  20. * The Sanitizer Class
  21. *
  22. * @export
  23. * @class Sanitizer
  24. */
  25. export declare class Sanitizer {
  26. readonly arcgisWhiteList: IWhiteList;
  27. readonly allowedProtocols: string[];
  28. readonly arcgisFilterOptions: XSS.IFilterXSSOptions;
  29. readonly xssFilterOptions: XSS.IFilterXSSOptions;
  30. private _xssFilter;
  31. private readonly _entityMap;
  32. constructor(filterOptions?: XSS.IFilterXSSOptions, extendDefaults?: boolean);
  33. /**
  34. * Sanitizes value to remove invalid HTML tags.
  35. *
  36. * Note: If the value passed does not contain a valid JSON data type (String,
  37. * Number, JSON Object, Array, Boolean, or null), the value will be nullified.
  38. *
  39. * @param {any} value The value to sanitize.
  40. * @returns {any} The sanitized value.
  41. * @memberof Sanitizer
  42. */
  43. sanitize(value: any, options?: ISanitizeOptions): any;
  44. /**
  45. * Sanitizes a URL string following the allowed protocols and sanitization rules.
  46. *
  47. * @param {string} value The URL to sanitize.
  48. * @param {{ isProtocolRequired: boolean }} options Configuration options for URL checking.
  49. * @returns {string} The sanitized URL if it's valid, or an empty string if the URL is invalid.
  50. */
  51. sanitizeUrl(value: string, options?: {
  52. /** Whether a protocol must exist on the URL for it to be considered valid. Defaults to `true`. If `false` and the provided URL has no protocol, it will be automatically prefixed with `https://`. */
  53. isProtocolRequired?: boolean;
  54. }): string;
  55. /**
  56. * Sanitizes an HTML attribute value.
  57. *
  58. * @param {string} tag The tagname of the HTML element.
  59. * @param {string} attribute The attribute name of the HTML element.
  60. * @param {string} value The raw value to be used for the HTML attribute value.
  61. * @param {XSS.ICSSFilter} [cssFilter] The CSS filter to be used.
  62. * @returns {string} The sanitized attribute value.
  63. * @memberof Sanitizer
  64. */
  65. sanitizeHTMLAttribute(tag: string, attribute: string, value: string, cssFilter?: XSS.ICSSFilter): string;
  66. /**
  67. * Checks if a value only contains valid HTML.
  68. *
  69. * @param {any} value The value to validate.
  70. * @returns {boolean}
  71. * @memberof Sanitizer
  72. */
  73. validate(value: any, options?: ISanitizeOptions): IValidationResponse;
  74. /**
  75. * Encodes the following characters, `& < > \" ' /` to their hexadecimal HTML entity code.
  76. * Example: "&middot;" => "&#x38;middot;"
  77. *
  78. * @param {string} value The value to encode.
  79. * @returns {string} The encoded string value.
  80. * @memberof Sanitizer
  81. */
  82. encodeHTML(value: string): string;
  83. /**
  84. * Encodes all non-alphanumeric ASCII characters to their hexadecimal HTML entity codes.
  85. * Example: "alert(document.cookie)" => "alert&#x28;document&#x2e;cookie&#x29;"
  86. *
  87. * @param {string} value The value to encode.
  88. * @returns {string} The encoded string value.
  89. * @memberof Sanitizer
  90. */
  91. encodeAttrValue(value: string): string;
  92. /**
  93. * Extends an object of arrays by by concatenating arrays of the same object
  94. * keys. If the if the previous key's value is not an array, the next key's
  95. * value will replace the previous key. This method is used for extending the
  96. * whiteList in the XSS filter options.
  97. *
  98. * @private
  99. * @param {Array<{}>} objects An array of objects.
  100. * @returns {{}} The extended object.
  101. * @memberof Sanitizer
  102. */
  103. private _extendObjectOfArrays;
  104. /**
  105. * Iterate over a plain object or array to deeply sanitize each value.
  106. *
  107. * @private
  108. * @param {object} obj The object to iterate over.
  109. * @returns {(object | null)} The sanitized object.
  110. * @memberof Sanitizer
  111. */
  112. private _iterateOverObject;
  113. /**
  114. * Trim whitespace from the start and ends of a string.
  115. * @param {string} val The string to trim.
  116. * @returns {string} The trimmed string.
  117. */
  118. private _trim;
  119. }
  120. export default Sanitizer;