validation.js 3.1 KB

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