url_parser.js 21 KB

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