index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. 'use strict';
  2. const {
  3. unassigned_code_points,
  4. commonly_mapped_to_nothing,
  5. non_ASCII_space_characters,
  6. prohibited_characters,
  7. bidirectional_r_al,
  8. bidirectional_l,
  9. } = require('./lib/memory-code-points');
  10. module.exports = saslprep;
  11. // 2.1. Mapping
  12. /**
  13. * non-ASCII space characters [StringPrep, C.1.2] that can be
  14. * mapped to SPACE (U+0020)
  15. */
  16. const mapping2space = non_ASCII_space_characters;
  17. /**
  18. * the "commonly mapped to nothing" characters [StringPrep, B.1]
  19. * that can be mapped to nothing.
  20. */
  21. const mapping2nothing = commonly_mapped_to_nothing;
  22. // utils
  23. const getCodePoint = character => character.codePointAt(0);
  24. const first = x => x[0];
  25. const last = x => x[x.length - 1];
  26. /**
  27. * Convert provided string into an array of Unicode Code Points.
  28. * Based on https://stackoverflow.com/a/21409165/1556249
  29. * and https://www.npmjs.com/package/code-point-at.
  30. * @param {string} input
  31. * @returns {number[]}
  32. */
  33. function toCodePoints(input) {
  34. const codepoints = [];
  35. const size = input.length;
  36. for (let i = 0; i < size; i += 1) {
  37. const before = input.charCodeAt(i);
  38. if (before >= 0xd800 && before <= 0xdbff && size > i + 1) {
  39. const next = input.charCodeAt(i + 1);
  40. if (next >= 0xdc00 && next <= 0xdfff) {
  41. codepoints.push((before - 0xd800) * 0x400 + next - 0xdc00 + 0x10000);
  42. i += 1;
  43. continue;
  44. }
  45. }
  46. codepoints.push(before);
  47. }
  48. return codepoints;
  49. }
  50. /**
  51. * SASLprep.
  52. * @param {string} input
  53. * @param {Object} opts
  54. * @param {boolean} opts.allowUnassigned
  55. * @returns {string}
  56. */
  57. function saslprep(input, opts = {}) {
  58. if (typeof input !== 'string') {
  59. throw new TypeError('Expected string.');
  60. }
  61. if (input.length === 0) {
  62. return '';
  63. }
  64. // 1. Map
  65. const mapped_input = toCodePoints(input)
  66. // 1.1 mapping to space
  67. .map(character => (mapping2space.get(character) ? 0x20 : character))
  68. // 1.2 mapping to nothing
  69. .filter(character => !mapping2nothing.get(character));
  70. // 2. Normalize
  71. const normalized_input = String.fromCodePoint
  72. .apply(null, mapped_input)
  73. .normalize('NFKC');
  74. const normalized_map = toCodePoints(normalized_input);
  75. // 3. Prohibit
  76. const hasProhibited = normalized_map.some(character =>
  77. prohibited_characters.get(character)
  78. );
  79. if (hasProhibited) {
  80. throw new Error(
  81. 'Prohibited character, see https://tools.ietf.org/html/rfc4013#section-2.3'
  82. );
  83. }
  84. // Unassigned Code Points
  85. if (opts.allowUnassigned !== true) {
  86. const hasUnassigned = normalized_map.some(character =>
  87. unassigned_code_points.get(character)
  88. );
  89. if (hasUnassigned) {
  90. throw new Error(
  91. 'Unassigned code point, see https://tools.ietf.org/html/rfc4013#section-2.5'
  92. );
  93. }
  94. }
  95. // 4. check bidi
  96. const hasBidiRAL = normalized_map.some(character =>
  97. bidirectional_r_al.get(character)
  98. );
  99. const hasBidiL = normalized_map.some(character =>
  100. bidirectional_l.get(character)
  101. );
  102. // 4.1 If a string contains any RandALCat character, the string MUST NOT
  103. // contain any LCat character.
  104. if (hasBidiRAL && hasBidiL) {
  105. throw new Error(
  106. 'String must not contain RandALCat and LCat at the same time,' +
  107. ' see https://tools.ietf.org/html/rfc3454#section-6'
  108. );
  109. }
  110. /**
  111. * 4.2 If a string contains any RandALCat character, a RandALCat
  112. * character MUST be the first character of the string, and a
  113. * RandALCat character MUST be the last character of the string.
  114. */
  115. const isFirstBidiRAL = bidirectional_r_al.get(
  116. getCodePoint(first(normalized_input))
  117. );
  118. const isLastBidiRAL = bidirectional_r_al.get(
  119. getCodePoint(last(normalized_input))
  120. );
  121. if (hasBidiRAL && !(isFirstBidiRAL && isLastBidiRAL)) {
  122. throw new Error(
  123. 'Bidirectional RandALCat character must be the first and the last' +
  124. ' character of the string, see https://tools.ietf.org/html/rfc3454#section-6'
  125. );
  126. }
  127. return normalized_input;
  128. }