url_parser.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. 'use strict';
  2. const ReadPreference = require('./core').ReadPreference,
  3. parser = require('url'),
  4. f = require('util').format,
  5. Logger = require('./core').Logger,
  6. dns = require('dns');
  7. const ReadConcern = require('./read_concern');
  8. module.exports = function(url, options, callback) {
  9. if (typeof options === 'function') (callback = options), (options = {});
  10. options = options || {};
  11. let result;
  12. try {
  13. result = parser.parse(url, true);
  14. } catch (e) {
  15. return callback(new Error('URL malformed, cannot be parsed'));
  16. }
  17. if (result.protocol !== 'mongodb:' && result.protocol !== 'mongodb+srv:') {
  18. return callback(new Error('Invalid schema, expected `mongodb` or `mongodb+srv`'));
  19. }
  20. if (result.protocol === 'mongodb:') {
  21. return parseHandler(url, options, callback);
  22. }
  23. // Otherwise parse this as an SRV record
  24. if (result.hostname.split('.').length < 3) {
  25. return callback(new Error('URI does not have hostname, domain name and tld'));
  26. }
  27. result.domainLength = result.hostname.split('.').length;
  28. if (result.pathname && result.pathname.match(',')) {
  29. return callback(new Error('Invalid URI, cannot contain multiple hostnames'));
  30. }
  31. if (result.port) {
  32. return callback(new Error('Ports not accepted with `mongodb+srv` URIs'));
  33. }
  34. let srvAddress = `_mongodb._tcp.${result.host}`;
  35. dns.resolveSrv(srvAddress, function(err, addresses) {
  36. if (err) return callback(err);
  37. if (addresses.length === 0) {
  38. return callback(new Error('No addresses found at host'));
  39. }
  40. for (let i = 0; i < addresses.length; i++) {
  41. if (!matchesParentDomain(addresses[i].name, result.hostname, result.domainLength)) {
  42. return callback(new Error('Server record does not share hostname with parent URI'));
  43. }
  44. }
  45. let base = result.auth ? `mongodb://${result.auth}@` : `mongodb://`;
  46. let connectionStrings = addresses.map(function(address, i) {
  47. if (i === 0) return `${base}${address.name}:${address.port}`;
  48. else return `${address.name}:${address.port}`;
  49. });
  50. let connectionString = connectionStrings.join(',') + '/';
  51. let connectionStringOptions = [];
  52. // Add the default database if needed
  53. if (result.path) {
  54. let defaultDb = result.path.slice(1);
  55. if (defaultDb.indexOf('?') !== -1) {
  56. defaultDb = defaultDb.slice(0, defaultDb.indexOf('?'));
  57. }
  58. connectionString += defaultDb;
  59. }
  60. // Default to SSL true
  61. if (!options.ssl && !result.search) {
  62. connectionStringOptions.push('ssl=true');
  63. } else if (!options.ssl && result.search && !result.search.match('ssl')) {
  64. connectionStringOptions.push('ssl=true');
  65. }
  66. // Keep original uri options
  67. if (result.search) {
  68. connectionStringOptions.push(result.search.replace('?', ''));
  69. }
  70. dns.resolveTxt(result.host, function(err, record) {
  71. if (err && err.code !== 'ENODATA') return callback(err);
  72. if (err && err.code === 'ENODATA') record = null;
  73. if (record) {
  74. if (record.length > 1) {
  75. return callback(new Error('Multiple text records not allowed'));
  76. }
  77. record = record[0];
  78. if (record.length > 1) record = record.join('');
  79. else record = record[0];
  80. if (!record.includes('authSource') && !record.includes('replicaSet')) {
  81. return callback(new Error('Text record must only set `authSource` or `replicaSet`'));
  82. }
  83. connectionStringOptions.push(record);
  84. }
  85. // Add any options to the connection string
  86. if (connectionStringOptions.length) {
  87. connectionString += `?${connectionStringOptions.join('&')}`;
  88. }
  89. parseHandler(connectionString, options, callback);
  90. });
  91. });
  92. };
  93. function matchesParentDomain(srvAddress, parentDomain) {
  94. let regex = /^.*?\./;
  95. let srv = `.${srvAddress.replace(regex, '')}`;
  96. let parent = `.${parentDomain.replace(regex, '')}`;
  97. if (srv.endsWith(parent)) return true;
  98. else return false;
  99. }
  100. function parseHandler(address, options, callback) {
  101. let result, err;
  102. try {
  103. result = parseConnectionString(address, options);
  104. } catch (e) {
  105. err = e;
  106. }
  107. return err ? callback(err, null) : callback(null, result);
  108. }
  109. function parseConnectionString(url, options) {
  110. // Variables
  111. let connection_part = '';
  112. let auth_part = '';
  113. let query_string_part = '';
  114. let dbName = 'admin';
  115. // Url parser result
  116. let result = parser.parse(url, true);
  117. if ((result.hostname == null || result.hostname === '') && url.indexOf('.sock') === -1) {
  118. throw new Error('No hostname or hostnames provided in connection string');
  119. }
  120. if (result.port === '0') {
  121. throw new Error('Invalid port (zero) with hostname');
  122. }
  123. if (!isNaN(parseInt(result.port, 10)) && parseInt(result.port, 10) > 65535) {
  124. throw new Error('Invalid port (larger than 65535) with hostname');
  125. }
  126. if (
  127. result.path &&
  128. result.path.length > 0 &&
  129. result.path[0] !== '/' &&
  130. url.indexOf('.sock') === -1
  131. ) {
  132. throw new Error('Missing delimiting slash between hosts and options');
  133. }
  134. if (result.query) {
  135. for (let name in result.query) {
  136. if (name.indexOf('::') !== -1) {
  137. throw new Error('Double colon in host identifier');
  138. }
  139. if (result.query[name] === '') {
  140. throw new Error('Query parameter ' + name + ' is an incomplete value pair');
  141. }
  142. }
  143. }
  144. if (result.auth) {
  145. let parts = result.auth.split(':');
  146. if (url.indexOf(result.auth) !== -1 && parts.length > 2) {
  147. throw new Error('Username with password containing an unescaped colon');
  148. }
  149. if (url.indexOf(result.auth) !== -1 && result.auth.indexOf('@') !== -1) {
  150. throw new Error('Username containing an unescaped at-sign');
  151. }
  152. }
  153. // Remove query
  154. let clean = url.split('?').shift();
  155. // Extract the list of hosts
  156. let strings = clean.split(',');
  157. let hosts = [];
  158. for (let i = 0; i < strings.length; i++) {
  159. let hostString = strings[i];
  160. if (hostString.indexOf('mongodb') !== -1) {
  161. if (hostString.indexOf('@') !== -1) {
  162. hosts.push(hostString.split('@').pop());
  163. } else {
  164. hosts.push(hostString.substr('mongodb://'.length));
  165. }
  166. } else if (hostString.indexOf('/') !== -1) {
  167. hosts.push(hostString.split('/').shift());
  168. } else if (hostString.indexOf('/') === -1) {
  169. hosts.push(hostString.trim());
  170. }
  171. }
  172. for (let i = 0; i < hosts.length; i++) {
  173. let r = parser.parse(f('mongodb://%s', hosts[i].trim()));
  174. if (r.path && r.path.indexOf('.sock') !== -1) continue;
  175. if (r.path && r.path.indexOf(':') !== -1) {
  176. // Not connecting to a socket so check for an extra slash in the hostname.
  177. // Using String#split as perf is better than match.
  178. if (r.path.split('/').length > 1 && r.path.indexOf('::') === -1) {
  179. throw new Error('Slash in host identifier');
  180. } else {
  181. throw new Error('Double colon in host identifier');
  182. }
  183. }
  184. }
  185. // If we have a ? mark cut the query elements off
  186. if (url.indexOf('?') !== -1) {
  187. query_string_part = url.substr(url.indexOf('?') + 1);
  188. connection_part = url.substring('mongodb://'.length, url.indexOf('?'));
  189. } else {
  190. connection_part = url.substring('mongodb://'.length);
  191. }
  192. // Check if we have auth params
  193. if (connection_part.indexOf('@') !== -1) {
  194. auth_part = connection_part.split('@')[0];
  195. connection_part = connection_part.split('@')[1];
  196. }
  197. // Check there is not more than one unescaped slash
  198. if (connection_part.split('/').length > 2) {
  199. throw new Error(
  200. "Unsupported host '" +
  201. connection_part.split('?')[0] +
  202. "', hosts must be URL encoded and contain at most one unencoded slash"
  203. );
  204. }
  205. // Check if the connection string has a db
  206. if (connection_part.indexOf('.sock') !== -1) {
  207. if (connection_part.indexOf('.sock/') !== -1) {
  208. dbName = connection_part.split('.sock/')[1];
  209. // Check if multiple database names provided, or just an illegal trailing backslash
  210. if (dbName.indexOf('/') !== -1) {
  211. if (dbName.split('/').length === 2 && dbName.split('/')[1].length === 0) {
  212. throw new Error('Illegal trailing backslash after database name');
  213. }
  214. throw new Error('More than 1 database name in URL');
  215. }
  216. connection_part = connection_part.split(
  217. '/',
  218. connection_part.indexOf('.sock') + '.sock'.length
  219. );
  220. }
  221. } else if (connection_part.indexOf('/') !== -1) {
  222. // Check if multiple database names provided, or just an illegal trailing backslash
  223. if (connection_part.split('/').length > 2) {
  224. if (connection_part.split('/')[2].length === 0) {
  225. throw new Error('Illegal trailing backslash after database name');
  226. }
  227. throw new Error('More than 1 database name in URL');
  228. }
  229. dbName = connection_part.split('/')[1];
  230. connection_part = connection_part.split('/')[0];
  231. }
  232. // URI decode the host information
  233. connection_part = decodeURIComponent(connection_part);
  234. // Result object
  235. let object = {};
  236. // Pick apart the authentication part of the string
  237. let authPart = auth_part || '';
  238. let auth = authPart.split(':', 2);
  239. // Decode the authentication URI components and verify integrity
  240. let user = decodeURIComponent(auth[0]);
  241. if (auth[0] !== encodeURIComponent(user)) {
  242. throw new Error('Username contains an illegal unescaped character');
  243. }
  244. auth[0] = user;
  245. if (auth[1]) {
  246. let pass = decodeURIComponent(auth[1]);
  247. if (auth[1] !== encodeURIComponent(pass)) {
  248. throw new Error('Password contains an illegal unescaped character');
  249. }
  250. auth[1] = pass;
  251. }
  252. // Add auth to final object if we have 2 elements
  253. if (auth.length === 2) object.auth = { user: auth[0], password: auth[1] };
  254. // if user provided auth options, use that
  255. if (options && options.auth != null) object.auth = options.auth;
  256. // Variables used for temporary storage
  257. let hostPart;
  258. let urlOptions;
  259. let servers;
  260. let compression;
  261. let serverOptions = { socketOptions: {} };
  262. let dbOptions = { read_preference_tags: [] };
  263. let replSetServersOptions = { socketOptions: {} };
  264. let mongosOptions = { socketOptions: {} };
  265. // Add server options to final object
  266. object.server_options = serverOptions;
  267. object.db_options = dbOptions;
  268. object.rs_options = replSetServersOptions;
  269. object.mongos_options = mongosOptions;
  270. // Let's check if we are using a domain socket
  271. if (url.match(/\.sock/)) {
  272. // Split out the socket part
  273. let domainSocket = url.substring(
  274. url.indexOf('mongodb://') + 'mongodb://'.length,
  275. url.lastIndexOf('.sock') + '.sock'.length
  276. );
  277. // Clean out any auth stuff if any
  278. if (domainSocket.indexOf('@') !== -1) domainSocket = domainSocket.split('@')[1];
  279. domainSocket = decodeURIComponent(domainSocket);
  280. servers = [{ domain_socket: domainSocket }];
  281. } else {
  282. // Split up the db
  283. hostPart = connection_part;
  284. // Deduplicate servers
  285. let deduplicatedServers = {};
  286. // Parse all server results
  287. servers = hostPart
  288. .split(',')
  289. .map(function(h) {
  290. let _host, _port, ipv6match;
  291. //check if it matches [IPv6]:port, where the port number is optional
  292. if ((ipv6match = /\[([^\]]+)\](?::(.+))?/.exec(h))) {
  293. _host = ipv6match[1];
  294. _port = parseInt(ipv6match[2], 10) || 27017;
  295. } else {
  296. //otherwise assume it's IPv4, or plain hostname
  297. let hostPort = h.split(':', 2);
  298. _host = hostPort[0] || 'localhost';
  299. _port = hostPort[1] != null ? parseInt(hostPort[1], 10) : 27017;
  300. // Check for localhost?safe=true style case
  301. if (_host.indexOf('?') !== -1) _host = _host.split(/\?/)[0];
  302. }
  303. // No entry returned for duplicate server
  304. if (deduplicatedServers[_host + '_' + _port]) return null;
  305. deduplicatedServers[_host + '_' + _port] = 1;
  306. // Return the mapped object
  307. return { host: _host, port: _port };
  308. })
  309. .filter(function(x) {
  310. return x != null;
  311. });
  312. }
  313. // Get the db name
  314. object.dbName = dbName || 'admin';
  315. // Split up all the options
  316. urlOptions = (query_string_part || '').split(/[&;]/);
  317. // Ugh, we have to figure out which options go to which constructor manually.
  318. urlOptions.forEach(function(opt) {
  319. if (!opt) return;
  320. var splitOpt = opt.split('='),
  321. name = splitOpt[0],
  322. value = splitOpt[1];
  323. // Options implementations
  324. switch (name) {
  325. case 'slaveOk':
  326. case 'slave_ok':
  327. serverOptions.slave_ok = value === 'true';
  328. dbOptions.slaveOk = value === 'true';
  329. break;
  330. case 'maxPoolSize':
  331. case 'poolSize':
  332. serverOptions.poolSize = parseInt(value, 10);
  333. replSetServersOptions.poolSize = parseInt(value, 10);
  334. break;
  335. case 'appname':
  336. object.appname = decodeURIComponent(value);
  337. break;
  338. case 'autoReconnect':
  339. case 'auto_reconnect':
  340. serverOptions.auto_reconnect = value === 'true';
  341. break;
  342. case 'ssl':
  343. if (value === 'prefer') {
  344. serverOptions.ssl = value;
  345. replSetServersOptions.ssl = value;
  346. mongosOptions.ssl = value;
  347. break;
  348. }
  349. serverOptions.ssl = value === 'true';
  350. replSetServersOptions.ssl = value === 'true';
  351. mongosOptions.ssl = value === 'true';
  352. break;
  353. case 'sslValidate':
  354. serverOptions.sslValidate = value === 'true';
  355. replSetServersOptions.sslValidate = value === 'true';
  356. mongosOptions.sslValidate = value === 'true';
  357. break;
  358. case 'replicaSet':
  359. case 'rs_name':
  360. replSetServersOptions.rs_name = value;
  361. break;
  362. case 'reconnectWait':
  363. replSetServersOptions.reconnectWait = parseInt(value, 10);
  364. break;
  365. case 'retries':
  366. replSetServersOptions.retries = parseInt(value, 10);
  367. break;
  368. case 'readSecondary':
  369. case 'read_secondary':
  370. replSetServersOptions.read_secondary = value === 'true';
  371. break;
  372. case 'fsync':
  373. dbOptions.fsync = value === 'true';
  374. break;
  375. case 'journal':
  376. dbOptions.j = value === 'true';
  377. break;
  378. case 'safe':
  379. dbOptions.safe = value === 'true';
  380. break;
  381. case 'nativeParser':
  382. case 'native_parser':
  383. dbOptions.native_parser = value === 'true';
  384. break;
  385. case 'readConcernLevel':
  386. dbOptions.readConcern = new ReadConcern(value);
  387. break;
  388. case 'connectTimeoutMS':
  389. serverOptions.socketOptions.connectTimeoutMS = parseInt(value, 10);
  390. replSetServersOptions.socketOptions.connectTimeoutMS = parseInt(value, 10);
  391. mongosOptions.socketOptions.connectTimeoutMS = parseInt(value, 10);
  392. break;
  393. case 'socketTimeoutMS':
  394. serverOptions.socketOptions.socketTimeoutMS = parseInt(value, 10);
  395. replSetServersOptions.socketOptions.socketTimeoutMS = parseInt(value, 10);
  396. mongosOptions.socketOptions.socketTimeoutMS = parseInt(value, 10);
  397. break;
  398. case 'w':
  399. dbOptions.w = parseInt(value, 10);
  400. if (isNaN(dbOptions.w)) dbOptions.w = value;
  401. break;
  402. case 'authSource':
  403. dbOptions.authSource = value;
  404. break;
  405. case 'gssapiServiceName':
  406. dbOptions.gssapiServiceName = value;
  407. break;
  408. case 'authMechanism':
  409. if (value === 'GSSAPI') {
  410. // If no password provided decode only the principal
  411. if (object.auth == null) {
  412. let urlDecodeAuthPart = decodeURIComponent(authPart);
  413. if (urlDecodeAuthPart.indexOf('@') === -1)
  414. throw new Error('GSSAPI requires a provided principal');
  415. object.auth = { user: urlDecodeAuthPart, password: null };
  416. } else {
  417. object.auth.user = decodeURIComponent(object.auth.user);
  418. }
  419. } else if (value === 'MONGODB-X509') {
  420. object.auth = { user: decodeURIComponent(authPart) };
  421. }
  422. // Only support GSSAPI or MONGODB-CR for now
  423. if (
  424. value !== 'GSSAPI' &&
  425. value !== 'MONGODB-X509' &&
  426. value !== 'MONGODB-CR' &&
  427. value !== 'DEFAULT' &&
  428. value !== 'SCRAM-SHA-1' &&
  429. value !== 'SCRAM-SHA-256' &&
  430. value !== 'PLAIN'
  431. )
  432. throw new Error(
  433. 'Only DEFAULT, GSSAPI, PLAIN, MONGODB-X509, or SCRAM-SHA-1 is supported by authMechanism'
  434. );
  435. // Authentication mechanism
  436. dbOptions.authMechanism = value;
  437. break;
  438. case 'authMechanismProperties':
  439. {
  440. // Split up into key, value pairs
  441. let values = value.split(',');
  442. let o = {};
  443. // For each value split into key, value
  444. values.forEach(function(x) {
  445. let v = x.split(':');
  446. o[v[0]] = v[1];
  447. });
  448. // Set all authMechanismProperties
  449. dbOptions.authMechanismProperties = o;
  450. // Set the service name value
  451. if (typeof o.SERVICE_NAME === 'string') dbOptions.gssapiServiceName = o.SERVICE_NAME;
  452. if (typeof o.SERVICE_REALM === 'string') dbOptions.gssapiServiceRealm = o.SERVICE_REALM;
  453. if (typeof o.CANONICALIZE_HOST_NAME === 'string')
  454. dbOptions.gssapiCanonicalizeHostName =
  455. o.CANONICALIZE_HOST_NAME === 'true' ? true : false;
  456. }
  457. break;
  458. case 'wtimeoutMS':
  459. dbOptions.wtimeout = parseInt(value, 10);
  460. break;
  461. case 'readPreference':
  462. if (!ReadPreference.isValid(value))
  463. throw new Error(
  464. 'readPreference must be either primary/primaryPreferred/secondary/secondaryPreferred/nearest'
  465. );
  466. dbOptions.readPreference = value;
  467. break;
  468. case 'maxStalenessSeconds':
  469. dbOptions.maxStalenessSeconds = parseInt(value, 10);
  470. break;
  471. case 'readPreferenceTags':
  472. {
  473. // Decode the value
  474. value = decodeURIComponent(value);
  475. // Contains the tag object
  476. let tagObject = {};
  477. if (value == null || value === '') {
  478. dbOptions.read_preference_tags.push(tagObject);
  479. break;
  480. }
  481. // Split up the tags
  482. let tags = value.split(/,/);
  483. for (let i = 0; i < tags.length; i++) {
  484. let parts = tags[i].trim().split(/:/);
  485. tagObject[parts[0]] = parts[1];
  486. }
  487. // Set the preferences tags
  488. dbOptions.read_preference_tags.push(tagObject);
  489. }
  490. break;
  491. case 'compressors':
  492. {
  493. compression = serverOptions.compression || {};
  494. let compressors = value.split(',');
  495. if (
  496. !compressors.every(function(compressor) {
  497. return compressor === 'snappy' || compressor === 'zlib';
  498. })
  499. ) {
  500. throw new Error('Compressors must be at least one of snappy or zlib');
  501. }
  502. compression.compressors = compressors;
  503. serverOptions.compression = compression;
  504. }
  505. break;
  506. case 'zlibCompressionLevel':
  507. {
  508. compression = serverOptions.compression || {};
  509. let zlibCompressionLevel = parseInt(value, 10);
  510. if (zlibCompressionLevel < -1 || zlibCompressionLevel > 9) {
  511. throw new Error('zlibCompressionLevel must be an integer between -1 and 9');
  512. }
  513. compression.zlibCompressionLevel = zlibCompressionLevel;
  514. serverOptions.compression = compression;
  515. }
  516. break;
  517. case 'retryWrites':
  518. dbOptions.retryWrites = value === 'true';
  519. break;
  520. case 'minSize':
  521. dbOptions.minSize = parseInt(value, 10);
  522. break;
  523. default:
  524. {
  525. let logger = Logger('URL Parser');
  526. logger.warn(`${name} is not supported as a connection string option`);
  527. }
  528. break;
  529. }
  530. });
  531. // No tags: should be null (not [])
  532. if (dbOptions.read_preference_tags.length === 0) {
  533. dbOptions.read_preference_tags = null;
  534. }
  535. // Validate if there are an invalid write concern combinations
  536. if (
  537. (dbOptions.w === -1 || dbOptions.w === 0) &&
  538. (dbOptions.journal === true || dbOptions.fsync === true || dbOptions.safe === true)
  539. )
  540. throw new Error('w set to -1 or 0 cannot be combined with safe/w/journal/fsync');
  541. // If no read preference set it to primary
  542. if (!dbOptions.readPreference) {
  543. dbOptions.readPreference = 'primary';
  544. }
  545. // make sure that user-provided options are applied with priority
  546. dbOptions = Object.assign(dbOptions, options);
  547. // Add servers to result
  548. object.servers = servers;
  549. // Returned parsed object
  550. return object;
  551. }