index.d.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. constructor(filterOptions?: XSS.IFilterXSSOptions, extendDefaults?: boolean);
  32. /**
  33. * Sanitizes value to remove invalid HTML tags.
  34. *
  35. * Note: If the value passed does not contain a valid JSON data type (String,
  36. * Number, JSON Object, Array, Boolean, or null), the value will be nullified.
  37. *
  38. * @param {any} value The value to sanitize.
  39. * @returns {any} The sanitized value.
  40. * @memberof Sanitizer
  41. */
  42. sanitize(value: any, options?: ISanitizeOptions): any;
  43. /**
  44. * Sanitizes a URL string following the allowed protocols and sanitization rules.
  45. *
  46. * @param {string} value The URL to sanitize.
  47. * @param {{ isProtocolRequired: boolean }} options Configuration options for URL checking.
  48. * @returns {string} The sanitized URL if it's valid, or an empty string if the URL is invalid.
  49. */
  50. sanitizeUrl(value: string, options?: {
  51. /** 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://`. */
  52. isProtocolRequired?: boolean;
  53. }): string;
  54. /**
  55. * Sanitizes an HTML attribute value.
  56. *
  57. * @param {string} tag The tagname of the HTML element.
  58. * @param {string} attribute The attribute name of the HTML element.
  59. * @param {string} value The raw value to be used for the HTML attribute value.
  60. * @param {XSS.ICSSFilter} [cssFilter] The CSS filter to be used.
  61. * @returns {string} The sanitized attribute value.
  62. * @memberof Sanitizer
  63. */
  64. sanitizeHTMLAttribute(tag: string, attribute: string, value: string, cssFilter?: XSS.ICSSFilter): string;
  65. /**
  66. * Checks if a value only contains valid HTML.
  67. *
  68. * @param {any} value The value to validate.
  69. * @returns {boolean}
  70. * @memberof Sanitizer
  71. */
  72. validate(value: any, options?: ISanitizeOptions): IValidationResponse;
  73. /**
  74. * Extends an object of arrays by by concatenating arrays of the same object
  75. * keys. If the if the previous key's value is not an array, the next key's
  76. * value will replace the previous key. This method is used for extending the
  77. * whiteList in the XSS filter options.
  78. *
  79. * @private
  80. * @param {Array<{}>} objects An array of objects.
  81. * @returns {{}} The extended object.
  82. * @memberof Sanitizer
  83. */
  84. private _extendObjectOfArrays;
  85. /**
  86. * Iterate over a plain object or array to deeply sanitize each value.
  87. *
  88. * @private
  89. * @param {object} obj The object to iterate over.
  90. * @returns {(object | null)} The sanitized object.
  91. * @memberof Sanitizer
  92. */
  93. private _iterateOverObject;
  94. /**
  95. * Trim whitespace from the start and ends of a string.
  96. * @param {string} val The string to trim.
  97. * @returns {string} The trimmed string.
  98. */
  99. private _trim;
  100. }
  101. export default Sanitizer;