url_parser.js 20 KB

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