validation.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use strict';
  2. const { isUtf8 } = require('buffer');
  3. //
  4. // Allowed token characters:
  5. //
  6. // '!', '#', '$', '%', '&', ''', '*', '+', '-',
  7. // '.', 0-9, A-Z, '^', '_', '`', a-z, '|', '~'
  8. //
  9. // tokenChars[32] === 0 // ' '
  10. // tokenChars[33] === 1 // '!'
  11. // tokenChars[34] === 0 // '"'
  12. // ...
  13. //
  14. // prettier-ignore
  15. const tokenChars = [
  16. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
  17. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
  18. 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, // 32 - 47
  19. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48 - 63
  20. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79
  21. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, // 80 - 95
  22. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
  23. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0 // 112 - 127
  24. ];
  25. /**
  26. * Checks if a status code is allowed in a close frame.
  27. *
  28. * @param {Number} code The status code
  29. * @return {Boolean} `true` if the status code is valid, else `false`
  30. * @public
  31. */
  32. function isValidStatusCode(code) {
  33. return (
  34. (code >= 1000 &&
  35. code <= 1014 &&
  36. code !== 1004 &&
  37. code !== 1005 &&
  38. code !== 1006) ||
  39. (code >= 3000 && code <= 4999)
  40. );
  41. }
  42. /**
  43. * Checks if a given buffer contains only correct UTF-8.
  44. * Ported from https://www.cl.cam.ac.uk/%7Emgk25/ucs/utf8_check.c by
  45. * Markus Kuhn.
  46. *
  47. * @param {Buffer} buf The buffer to check
  48. * @return {Boolean} `true` if `buf` contains only correct UTF-8, else `false`
  49. * @public
  50. */
  51. function _isValidUTF8(buf) {
  52. const len = buf.length;
  53. let i = 0;
  54. while (i < len) {
  55. if ((buf[i] & 0x80) === 0) {
  56. // 0xxxxxxx
  57. i++;
  58. } else if ((buf[i] & 0xe0) === 0xc0) {
  59. // 110xxxxx 10xxxxxx
  60. if (
  61. i + 1 === len ||
  62. (buf[i + 1] & 0xc0) !== 0x80 ||
  63. (buf[i] & 0xfe) === 0xc0 // Overlong
  64. ) {
  65. return false;
  66. }
  67. i += 2;
  68. } else if ((buf[i] & 0xf0) === 0xe0) {
  69. // 1110xxxx 10xxxxxx 10xxxxxx
  70. if (
  71. i + 2 >= len ||
  72. (buf[i + 1] & 0xc0) !== 0x80 ||
  73. (buf[i + 2] & 0xc0) !== 0x80 ||
  74. (buf[i] === 0xe0 && (buf[i + 1] & 0xe0) === 0x80) || // Overlong
  75. (buf[i] === 0xed && (buf[i + 1] & 0xe0) === 0xa0) // Surrogate (U+D800 - U+DFFF)
  76. ) {
  77. return false;
  78. }
  79. i += 3;
  80. } else if ((buf[i] & 0xf8) === 0xf0) {
  81. // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  82. if (
  83. i + 3 >= len ||
  84. (buf[i + 1] & 0xc0) !== 0x80 ||
  85. (buf[i + 2] & 0xc0) !== 0x80 ||
  86. (buf[i + 3] & 0xc0) !== 0x80 ||
  87. (buf[i] === 0xf0 && (buf[i + 1] & 0xf0) === 0x80) || // Overlong
  88. (buf[i] === 0xf4 && buf[i + 1] > 0x8f) ||
  89. buf[i] > 0xf4 // > U+10FFFF
  90. ) {
  91. return false;
  92. }
  93. i += 4;
  94. } else {
  95. return false;
  96. }
  97. }
  98. return true;
  99. }
  100. module.exports = {
  101. isValidStatusCode,
  102. isValidUTF8: _isValidUTF8,
  103. tokenChars
  104. };
  105. if (isUtf8) {
  106. module.exports.isValidUTF8 = function (buf) {
  107. return buf.length < 24 ? _isValidUTF8(buf) : isUtf8(buf);
  108. };
  109. } /* istanbul ignore else */ else if (!process.env.WS_NO_UTF_8_VALIDATE) {
  110. try {
  111. const isValidUTF8 = require('utf-8-validate');
  112. module.exports.isValidUTF8 = function (buf) {
  113. return buf.length < 32 ? _isValidUTF8(buf) : isValidUTF8(buf);
  114. };
  115. } catch (e) {
  116. // Continue regardless of the error.
  117. }
  118. }