index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. 'use strict';
  2. const Punycode = require('punycode');
  3. const Abnf = require('./abnf');
  4. const Tlds = require('./tlds');
  5. const internals = {
  6. nonAsciiRx: /[^\x00-\x7f]/,
  7. minDomainSegments: 2,
  8. defaultTlds: { allow: Tlds, deny: null }
  9. };
  10. module.exports = {
  11. email: {
  12. analyze: function (email, options) {
  13. return internals.email(email, options);
  14. },
  15. isValid: function (email, options) {
  16. return !internals.email(email, options);
  17. }
  18. },
  19. domain: {
  20. analyze: function (domain, options = {}) {
  21. internals.options(domain, options);
  22. if (!domain) {
  23. return internals.error('Domain must be a non-empty string');
  24. }
  25. if (domain.length > 256) {
  26. return internals.error('Domain too long');
  27. }
  28. const ascii = !internals.nonAsciiRx.test(domain);
  29. if (!ascii) {
  30. if (options.allowUnicode === false) { // Defaults to true
  31. return internals.error('Domain contains forbidden Unicode characters');
  32. }
  33. const normalized = domain.normalize('NFC');
  34. domain = Punycode.toASCII(normalized);
  35. }
  36. return internals.domain(domain, options);
  37. },
  38. isValid: function (domain, options) {
  39. return !module.exports.domain.analyze(domain, options);
  40. }
  41. }
  42. };
  43. internals.email = function (email, options = {}) {
  44. internals.options(email, options);
  45. if (!email) {
  46. return internals.error('Address must be a non-empty string');
  47. }
  48. // Unicode
  49. const ascii = !internals.nonAsciiRx.test(email);
  50. if (!ascii) {
  51. if (options.allowUnicode === false) { // Defaults to true
  52. return internals.error('Address contains forbidden Unicode characters');
  53. }
  54. const normalized = email.normalize('NFC');
  55. email = Punycode.toASCII(normalized);
  56. }
  57. // Basic structure
  58. const parts = email.split('@');
  59. if (parts.length !== 2) {
  60. return internals.error(parts.length > 2 ? 'Address cannot contain more than one @ character' : 'Address must contain one @ character');
  61. }
  62. const local = parts[0];
  63. const domain = parts[1];
  64. if (!local) {
  65. return internals.error('Address local part cannot be empty');
  66. }
  67. if (!domain) {
  68. return internals.error('Domain cannot be empty');
  69. }
  70. if (email.length > 254) { // http://tools.ietf.org/html/rfc5321#section-4.5.3.1.3
  71. return internals.error('Address too long');
  72. }
  73. if (Buffer.byteLength(local, 'utf-8') > 64) { // http://tools.ietf.org/html/rfc5321#section-4.5.3.1.1
  74. return internals.error('Address local part too long');
  75. }
  76. // Validate parts
  77. return internals.local(local, ascii) || internals.domain(domain, options);
  78. };
  79. internals.options = function (value, options) {
  80. // Options validation
  81. if (options.tlds &&
  82. options.tlds !== true) {
  83. if (typeof options.tlds !== 'object') {
  84. throw new Error('Invalid options: tlds must be a boolean or an object');
  85. }
  86. if (options.tlds.allow !== undefined &&
  87. options.tlds.allow !== true &&
  88. options.tlds.allow instanceof Set === false) {
  89. throw new Error('Invalid options: tlds.allow must be a Set object or true');
  90. }
  91. if (options.tlds.deny) {
  92. if (options.tlds.deny instanceof Set === false) {
  93. throw new Error('Invalid options: tlds.deny must be a Set object');
  94. }
  95. if (options.tlds.allow instanceof Set) {
  96. throw new Error('Invalid options: cannot specify both tlds.allow and tlds.deny lists');
  97. }
  98. }
  99. }
  100. // Input validation
  101. if (typeof value !== 'string') {
  102. throw new Error('Invalid input: value must be a string');
  103. }
  104. };
  105. internals.local = function (local, ascii) {
  106. const segments = local.split('.');
  107. for (const segment of segments) {
  108. if (!segment.length) {
  109. return internals.error('Address local part contains empty dot-separated segment');
  110. }
  111. if (ascii) {
  112. if (!Abnf.atextRx.test(segment)) {
  113. return internals.error('Address local part contains invalid character');
  114. }
  115. }
  116. else {
  117. for (const char of segment) {
  118. const binary = Buffer.from(char).toString('binary');
  119. if (!Abnf.atomRx.test(binary)) {
  120. return internals.error('Address local part contains invalid character');
  121. }
  122. }
  123. }
  124. }
  125. };
  126. internals.tldSegmentRx = /^[a-zA-Z](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?$/;
  127. internals.domainSegmentRx = /^[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?$/;
  128. internals.domain = function (domain, options) {
  129. // https://tools.ietf.org/html/rfc1035 section 2.3.1
  130. const minDomainSegments = (options.minDomainSegments || internals.minDomainSegments);
  131. const segments = domain.split('.');
  132. if (segments.length < minDomainSegments) {
  133. return internals.error('Domain lacks the minimum required number of segments');
  134. }
  135. const tlds = internals.tlds(options);
  136. if (tlds) {
  137. const tld = segments[segments.length - 1].toLowerCase();
  138. if (tlds.deny && tlds.deny.has(tld) ||
  139. tlds.allow && !tlds.allow.has(tld)) {
  140. return internals.error('Domain uses forbidden TLD');
  141. }
  142. }
  143. for (let i = 0; i < segments.length; ++i) {
  144. const segment = segments[i];
  145. if (!segment.length) {
  146. return internals.error('Domain contains empty dot-separated segment');
  147. }
  148. if (segment.length > 63) {
  149. return internals.error('Domain contains dot-separated segment that is too long');
  150. }
  151. if (i < segments.length - 1) {
  152. if (!internals.domainSegmentRx.test(segment)) {
  153. return internals.error('Domain contains invalid character');
  154. }
  155. }
  156. else {
  157. if (!internals.tldSegmentRx.test(segment)) {
  158. return internals.error('Domain contains invalid tld character');
  159. }
  160. }
  161. }
  162. };
  163. internals.tlds = function (options) {
  164. if (options.tlds === false) { // Defaults to true
  165. return null;
  166. }
  167. if (!options.tlds ||
  168. options.tlds === true) {
  169. return internals.defaultTlds;
  170. }
  171. return {
  172. allow: options.tlds.allow === true ? null : options.tlds.allow || Tlds,
  173. deny: options.tlds.deny || null
  174. };
  175. };
  176. internals.error = function (reason) {
  177. return { error: reason };
  178. };