index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { asciiAlphanumeric } from 'micromark-util-character';
  2. import { encode } from 'micromark-util-encode';
  3. /**
  4. * Make a value safe for injection as a URL.
  5. *
  6. * This encodes unsafe characters with percent-encoding and skips already
  7. * encoded sequences (see `normalizeUri`).
  8. * Further unsafe characters are encoded as character references (see
  9. * `micromark-util-encode`).
  10. *
  11. * A regex of allowed protocols can be given, in which case the URL is
  12. * sanitized.
  13. * For example, `/^(https?|ircs?|mailto|xmpp)$/i` can be used for `a[href]`, or
  14. * `/^https?$/i` for `img[src]` (this is what `github.com` allows).
  15. * If the URL includes an unknown protocol (one not matched by `protocol`, such
  16. * as a dangerous example, `javascript:`), the value is ignored.
  17. *
  18. * @param {string | null | undefined} url
  19. * URI to sanitize.
  20. * @param {RegExp | null | undefined} [protocol]
  21. * Allowed protocols.
  22. * @returns {string}
  23. * Sanitized URI.
  24. */
  25. export function sanitizeUri(url, protocol) {
  26. const value = encode(normalizeUri(url || ''));
  27. if (!protocol) {
  28. return value;
  29. }
  30. const colon = value.indexOf(':');
  31. const questionMark = value.indexOf('?');
  32. const numberSign = value.indexOf('#');
  33. const slash = value.indexOf('/');
  34. if (
  35. // If there is no protocol, it’s relative.
  36. colon < 0 ||
  37. // If the first colon is after a `?`, `#`, or `/`, it’s not a protocol.
  38. slash > -1 && colon > slash || questionMark > -1 && colon > questionMark || numberSign > -1 && colon > numberSign ||
  39. // It is a protocol, it should be allowed.
  40. protocol.test(value.slice(0, colon))) {
  41. return value;
  42. }
  43. return '';
  44. }
  45. /**
  46. * Normalize a URL.
  47. *
  48. * Encode unsafe characters with percent-encoding, skipping already encoded
  49. * sequences.
  50. *
  51. * @param {string} value
  52. * URI to normalize.
  53. * @returns {string}
  54. * Normalized URI.
  55. */
  56. export function normalizeUri(value) {
  57. /** @type {Array<string>} */
  58. const result = [];
  59. let index = -1;
  60. let start = 0;
  61. let skip = 0;
  62. while (++index < value.length) {
  63. const code = value.charCodeAt(index);
  64. /** @type {string} */
  65. let replace = '';
  66. // A correct percent encoded value.
  67. if (code === 37 && asciiAlphanumeric(value.charCodeAt(index + 1)) && asciiAlphanumeric(value.charCodeAt(index + 2))) {
  68. skip = 2;
  69. }
  70. // ASCII.
  71. else if (code < 128) {
  72. if (!/[!#$&-;=?-Z_a-z~]/.test(String.fromCharCode(code))) {
  73. replace = String.fromCharCode(code);
  74. }
  75. }
  76. // Astral.
  77. else if (code > 55_295 && code < 57_344) {
  78. const next = value.charCodeAt(index + 1);
  79. // A correct surrogate pair.
  80. if (code < 56_320 && next > 56_319 && next < 57_344) {
  81. replace = String.fromCharCode(code, next);
  82. skip = 1;
  83. }
  84. // Lone surrogate.
  85. else {
  86. replace = "\uFFFD";
  87. }
  88. }
  89. // Unicode.
  90. else {
  91. replace = String.fromCharCode(code);
  92. }
  93. if (replace) {
  94. result.push(value.slice(start, index), encodeURIComponent(replace));
  95. start = index + skip + 1;
  96. replace = '';
  97. }
  98. if (skip) {
  99. index += skip;
  100. skip = 0;
  101. }
  102. }
  103. return result.join('') + value.slice(start);
  104. }