validation.js 3.8 KB

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