Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uri_parser.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. 'use strict';
  2. const URL = require('url');
  3. const qs = require('querystring');
  4. const dns = require('dns');
  5. const MongoParseError = require('./error').MongoParseError;
  6. const ReadPreference = require('./topologies/read_preference');
  7. /**
  8. * The following regular expression validates a connection string and breaks the
  9. * provide string into the following capture groups: [protocol, username, password, hosts]
  10. */
  11. const HOSTS_RX = /(mongodb(?:\+srv|)):\/\/(?: (?:[^:]*) (?: : ([^@]*) )? @ )?([^/?]*)(?:\/|)(.*)/;
  12. /**
  13. * Determines whether a provided address matches the provided parent domain in order
  14. * to avoid certain attack vectors.
  15. *
  16. * @param {String} srvAddress The address to check against a domain
  17. * @param {String} parentDomain The domain to check the provided address against
  18. * @return {Boolean} Whether the provided address matches the parent domain
  19. */
  20. function matchesParentDomain(srvAddress, parentDomain) {
  21. const regex = /^.*?\./;
  22. const srv = `.${srvAddress.replace(regex, '')}`;
  23. const parent = `.${parentDomain.replace(regex, '')}`;
  24. return srv.endsWith(parent);
  25. }
  26. /**
  27. * Lookup a `mongodb+srv` connection string, combine the parts and reparse it as a normal
  28. * connection string.
  29. *
  30. * @param {string} uri The connection string to parse
  31. * @param {object} options Optional user provided connection string options
  32. * @param {function} callback
  33. */
  34. function parseSrvConnectionString(uri, options, callback) {
  35. const result = URL.parse(uri, true);
  36. if (result.hostname.split('.').length < 3) {
  37. return callback(new MongoParseError('URI does not have hostname, domain name and tld'));
  38. }
  39. result.domainLength = result.hostname.split('.').length;
  40. if (result.pathname && result.pathname.match(',')) {
  41. return callback(new MongoParseError('Invalid URI, cannot contain multiple hostnames'));
  42. }
  43. if (result.port) {
  44. return callback(new MongoParseError(`Ports not accepted with '${PROTOCOL_MONGODB_SRV}' URIs`));
  45. }
  46. // Resolve the SRV record and use the result as the list of hosts to connect to.
  47. const lookupAddress = result.host;
  48. dns.resolveSrv(`_mongodb._tcp.${lookupAddress}`, (err, addresses) => {
  49. if (err) return callback(err);
  50. if (addresses.length === 0) {
  51. return callback(new MongoParseError('No addresses found at host'));
  52. }
  53. for (let i = 0; i < addresses.length; i++) {
  54. if (!matchesParentDomain(addresses[i].name, result.hostname, result.domainLength)) {
  55. return callback(
  56. new MongoParseError('Server record does not share hostname with parent URI')
  57. );
  58. }
  59. }
  60. // Convert the original URL to a non-SRV URL.
  61. result.protocol = 'mongodb';
  62. result.host = addresses.map(address => `${address.name}:${address.port}`).join(',');
  63. // Default to SSL true if it's not specified.
  64. if (
  65. !('ssl' in options) &&
  66. (!result.search || !('ssl' in result.query) || result.query.ssl === null)
  67. ) {
  68. result.query.ssl = true;
  69. }
  70. // Resolve TXT record and add options from there if they exist.
  71. dns.resolveTxt(lookupAddress, (err, record) => {
  72. if (err) {
  73. if (err.code !== 'ENODATA') {
  74. return callback(err);
  75. }
  76. record = null;
  77. }
  78. if (record) {
  79. if (record.length > 1) {
  80. return callback(new MongoParseError('Multiple text records not allowed'));
  81. }
  82. record = qs.parse(record[0].join(''));
  83. if (Object.keys(record).some(key => key !== 'authSource' && key !== 'replicaSet')) {
  84. return callback(
  85. new MongoParseError('Text record must only set `authSource` or `replicaSet`')
  86. );
  87. }
  88. Object.assign(result.query, record);
  89. }
  90. // Set completed options back into the URL object.
  91. result.search = qs.stringify(result.query);
  92. const finalString = URL.format(result);
  93. parseConnectionString(finalString, options, callback);
  94. });
  95. });
  96. }
  97. /**
  98. * Parses a query string item according to the connection string spec
  99. *
  100. * @param {string} key The key for the parsed value
  101. * @param {Array|String} value The value to parse
  102. * @return {Array|Object|String} The parsed value
  103. */
  104. function parseQueryStringItemValue(key, value) {
  105. if (Array.isArray(value)) {
  106. // deduplicate and simplify arrays
  107. value = value.filter((v, idx) => value.indexOf(v) === idx);
  108. if (value.length === 1) value = value[0];
  109. } else if (STRING_OPTIONS.has(key)) {
  110. // TODO: refactor function to make this early return not
  111. // stand out
  112. return value;
  113. } else if (value.indexOf(':') > 0) {
  114. value = value.split(',').reduce((result, pair) => {
  115. const parts = pair.split(':');
  116. result[parts[0]] = parseQueryStringItemValue(key, parts[1]);
  117. return result;
  118. }, {});
  119. } else if (value.indexOf(',') > 0) {
  120. value = value.split(',').map(v => {
  121. return parseQueryStringItemValue(key, v);
  122. });
  123. } else if (value.toLowerCase() === 'true' || value.toLowerCase() === 'false') {
  124. value = value.toLowerCase() === 'true';
  125. } else if (!Number.isNaN(value)) {
  126. const numericValue = parseFloat(value);
  127. if (!Number.isNaN(numericValue)) {
  128. value = parseFloat(value);
  129. }
  130. }
  131. return value;
  132. }
  133. // Options that are known boolean types
  134. const BOOLEAN_OPTIONS = new Set([
  135. 'slaveok',
  136. 'slave_ok',
  137. 'sslvalidate',
  138. 'fsync',
  139. 'safe',
  140. 'retrywrites',
  141. 'j'
  142. ]);
  143. // Known string options
  144. // TODO: Do this for more types
  145. const STRING_OPTIONS = new Set(['authsource', 'replicaset', 'appname']);
  146. // Supported text representations of auth mechanisms
  147. // NOTE: this list exists in native already, if it is merged here we should deduplicate
  148. const AUTH_MECHANISMS = new Set([
  149. 'GSSAPI',
  150. 'MONGODB-X509',
  151. 'MONGODB-CR',
  152. 'DEFAULT',
  153. 'SCRAM-SHA-1',
  154. 'SCRAM-SHA-256',
  155. 'PLAIN'
  156. ]);
  157. // Lookup table used to translate normalized (lower-cased) forms of connection string
  158. // options to their expected camelCase version
  159. const CASE_TRANSLATION = {
  160. replicaset: 'replicaSet',
  161. connecttimeoutms: 'connectTimeoutMS',
  162. sockettimeoutms: 'socketTimeoutMS',
  163. maxpoolsize: 'maxPoolSize',
  164. minpoolsize: 'minPoolSize',
  165. maxidletimems: 'maxIdleTimeMS',
  166. waitqueuemultiple: 'waitQueueMultiple',
  167. waitqueuetimeoutms: 'waitQueueTimeoutMS',
  168. wtimeoutms: 'wtimeoutMS',
  169. readconcern: 'readConcern',
  170. readconcernlevel: 'readConcernLevel',
  171. readpreference: 'readPreference',
  172. maxstalenessseconds: 'maxStalenessSeconds',
  173. readpreferencetags: 'readPreferenceTags',
  174. authsource: 'authSource',
  175. authmechanism: 'authMechanism',
  176. authmechanismproperties: 'authMechanismProperties',
  177. gssapiservicename: 'gssapiServiceName',
  178. localthresholdms: 'localThresholdMS',
  179. serverselectiontimeoutms: 'serverSelectionTimeoutMS',
  180. serverselectiontryonce: 'serverSelectionTryOnce',
  181. heartbeatfrequencyms: 'heartbeatFrequencyMS',
  182. retrywrites: 'retryWrites',
  183. uuidrepresentation: 'uuidRepresentation',
  184. zlibcompressionlevel: 'zlibCompressionLevel',
  185. tlsallowinvalidcertificates: 'tlsAllowInvalidCertificates',
  186. tlsallowinvalidhostnames: 'tlsAllowInvalidHostnames',
  187. tlsinsecure: 'tlsInsecure',
  188. tlscafile: 'tlsCAFile',
  189. tlscertificatekeyfile: 'tlsCertificateKeyFile',
  190. tlscertificatekeyfilepassword: 'tlsCertificateKeyFilePassword',
  191. wtimeout: 'wTimeoutMS',
  192. j: 'journal'
  193. };
  194. /**
  195. * Sets the value for `key`, allowing for any required translation
  196. *
  197. * @param {object} obj The object to set the key on
  198. * @param {string} key The key to set the value for
  199. * @param {*} value The value to set
  200. * @param {object} options The options used for option parsing
  201. */
  202. function applyConnectionStringOption(obj, key, value, options) {
  203. // simple key translation
  204. if (key === 'journal') {
  205. key = 'j';
  206. } else if (key === 'wtimeoutms') {
  207. key = 'wtimeout';
  208. }
  209. // more complicated translation
  210. if (BOOLEAN_OPTIONS.has(key)) {
  211. value = value === 'true' || value === true;
  212. } else if (key === 'appname') {
  213. value = decodeURIComponent(value);
  214. } else if (key === 'readconcernlevel') {
  215. obj['readConcernLevel'] = value;
  216. key = 'readconcern';
  217. value = { level: value };
  218. }
  219. // simple validation
  220. if (key === 'compressors') {
  221. value = Array.isArray(value) ? value : [value];
  222. if (!value.every(c => c === 'snappy' || c === 'zlib')) {
  223. throw new MongoParseError(
  224. 'Value for `compressors` must be at least one of: `snappy`, `zlib`'
  225. );
  226. }
  227. }
  228. if (key === 'authmechanism' && !AUTH_MECHANISMS.has(value)) {
  229. throw new MongoParseError(
  230. 'Value for `authMechanism` must be one of: `DEFAULT`, `GSSAPI`, `PLAIN`, `MONGODB-X509`, `SCRAM-SHA-1`, `SCRAM-SHA-256`'
  231. );
  232. }
  233. if (key === 'readpreference' && !ReadPreference.isValid(value)) {
  234. throw new MongoParseError(
  235. 'Value for `readPreference` must be one of: `primary`, `primaryPreferred`, `secondary`, `secondaryPreferred`, `nearest`'
  236. );
  237. }
  238. if (key === 'zlibcompressionlevel' && (value < -1 || value > 9)) {
  239. throw new MongoParseError('zlibCompressionLevel must be an integer between -1 and 9');
  240. }
  241. // special cases
  242. if (key === 'compressors' || key === 'zlibcompressionlevel') {
  243. obj.compression = obj.compression || {};
  244. obj = obj.compression;
  245. }
  246. if (key === 'authmechanismproperties') {
  247. if (typeof value.SERVICE_NAME === 'string') obj.gssapiServiceName = value.SERVICE_NAME;
  248. if (typeof value.SERVICE_REALM === 'string') obj.gssapiServiceRealm = value.SERVICE_REALM;
  249. if (typeof value.CANONICALIZE_HOST_NAME !== 'undefined') {
  250. obj.gssapiCanonicalizeHostName = value.CANONICALIZE_HOST_NAME;
  251. }
  252. }
  253. if (key === 'readpreferencetags' && Array.isArray(value)) {
  254. value = splitArrayOfMultipleReadPreferenceTags(value);
  255. }
  256. // set the actual value
  257. if (options.caseTranslate && CASE_TRANSLATION[key]) {
  258. obj[CASE_TRANSLATION[key]] = value;
  259. return;
  260. }
  261. obj[key] = value;
  262. }
  263. const USERNAME_REQUIRED_MECHANISMS = new Set([
  264. 'GSSAPI',
  265. 'MONGODB-CR',
  266. 'PLAIN',
  267. 'SCRAM-SHA-1',
  268. 'SCRAM-SHA-256'
  269. ]);
  270. function splitArrayOfMultipleReadPreferenceTags(value) {
  271. const parsedTags = [];
  272. for (let i = 0; i < value.length; i++) {
  273. parsedTags[i] = {};
  274. value[i].split(',').forEach(individualTag => {
  275. const splitTag = individualTag.split(':');
  276. parsedTags[i][splitTag[0]] = splitTag[1];
  277. });
  278. }
  279. return parsedTags;
  280. }
  281. /**
  282. * Modifies the parsed connection string object taking into account expectations we
  283. * have for authentication-related options.
  284. *
  285. * @param {object} parsed The parsed connection string result
  286. * @return The parsed connection string result possibly modified for auth expectations
  287. */
  288. function applyAuthExpectations(parsed) {
  289. if (parsed.options == null) {
  290. return;
  291. }
  292. const options = parsed.options;
  293. const authSource = options.authsource || options.authSource;
  294. if (authSource != null) {
  295. parsed.auth = Object.assign({}, parsed.auth, { db: authSource });
  296. }
  297. const authMechanism = options.authmechanism || options.authMechanism;
  298. if (authMechanism != null) {
  299. if (
  300. USERNAME_REQUIRED_MECHANISMS.has(authMechanism) &&
  301. (!parsed.auth || parsed.auth.username == null)
  302. ) {
  303. throw new MongoParseError(`Username required for mechanism \`${authMechanism}\``);
  304. }
  305. if (authMechanism === 'GSSAPI') {
  306. if (authSource != null && authSource !== '$external') {
  307. throw new MongoParseError(
  308. `Invalid source \`${authSource}\` for mechanism \`${authMechanism}\` specified.`
  309. );
  310. }
  311. parsed.auth = Object.assign({}, parsed.auth, { db: '$external' });
  312. }
  313. if (authMechanism === 'MONGODB-X509') {
  314. if (parsed.auth && parsed.auth.password != null) {
  315. throw new MongoParseError(`Password not allowed for mechanism \`${authMechanism}\``);
  316. }
  317. if (authSource != null && authSource !== '$external') {
  318. throw new MongoParseError(
  319. `Invalid source \`${authSource}\` for mechanism \`${authMechanism}\` specified.`
  320. );
  321. }
  322. parsed.auth = Object.assign({}, parsed.auth, { db: '$external' });
  323. }
  324. if (authMechanism === 'PLAIN') {
  325. if (parsed.auth && parsed.auth.db == null) {
  326. parsed.auth = Object.assign({}, parsed.auth, { db: '$external' });
  327. }
  328. }
  329. }
  330. // default to `admin` if nothing else was resolved
  331. if (parsed.auth && parsed.auth.db == null) {
  332. parsed.auth = Object.assign({}, parsed.auth, { db: 'admin' });
  333. }
  334. return parsed;
  335. }
  336. /**
  337. * Parses a query string according the connection string spec.
  338. *
  339. * @param {String} query The query string to parse
  340. * @param {object} [options] The options used for options parsing
  341. * @return {Object|Error} The parsed query string as an object, or an error if one was encountered
  342. */
  343. function parseQueryString(query, options) {
  344. const result = {};
  345. let parsedQueryString = qs.parse(query);
  346. checkTLSOptions(parsedQueryString);
  347. for (const key in parsedQueryString) {
  348. const value = parsedQueryString[key];
  349. if (value === '' || value == null) {
  350. throw new MongoParseError('Incomplete key value pair for option');
  351. }
  352. const normalizedKey = key.toLowerCase();
  353. const parsedValue = parseQueryStringItemValue(normalizedKey, value);
  354. applyConnectionStringOption(result, normalizedKey, parsedValue, options);
  355. }
  356. // special cases for known deprecated options
  357. if (result.wtimeout && result.wtimeoutms) {
  358. delete result.wtimeout;
  359. console.warn('Unsupported option `wtimeout` specified');
  360. }
  361. return Object.keys(result).length ? result : null;
  362. }
  363. /**
  364. * Checks a query string for invalid tls options according to the URI options spec.
  365. *
  366. * @param {string} queryString The query string to check
  367. * @throws {MongoParseError}
  368. */
  369. function checkTLSOptions(queryString) {
  370. const queryStringKeys = Object.keys(queryString);
  371. if (
  372. queryStringKeys.indexOf('tlsInsecure') !== -1 &&
  373. (queryStringKeys.indexOf('tlsAllowInvalidCertificates') !== -1 ||
  374. queryStringKeys.indexOf('tlsAllowInvalidHostnames') !== -1)
  375. ) {
  376. throw new MongoParseError(
  377. 'The `tlsInsecure` option cannot be used with `tlsAllowInvalidCertificates` or `tlsAllowInvalidHostnames`.'
  378. );
  379. }
  380. const tlsValue = assertTlsOptionsAreEqual('tls', queryString, queryStringKeys);
  381. const sslValue = assertTlsOptionsAreEqual('ssl', queryString, queryStringKeys);
  382. if (tlsValue != null && sslValue != null) {
  383. if (tlsValue !== sslValue) {
  384. throw new MongoParseError('All values of `tls` and `ssl` must be the same.');
  385. }
  386. }
  387. }
  388. /**
  389. * Checks a query string to ensure all tls/ssl options are the same.
  390. *
  391. * @param {string} key The key (tls or ssl) to check
  392. * @param {string} queryString The query string to check
  393. * @throws {MongoParseError}
  394. * @return The value of the tls/ssl option
  395. */
  396. function assertTlsOptionsAreEqual(optionName, queryString, queryStringKeys) {
  397. const queryStringHasTLSOption = queryStringKeys.indexOf(optionName) !== -1;
  398. let optionValue;
  399. if (Array.isArray(queryString[optionName])) {
  400. optionValue = queryString[optionName][0];
  401. } else {
  402. optionValue = queryString[optionName];
  403. }
  404. if (queryStringHasTLSOption) {
  405. if (Array.isArray(queryString[optionName])) {
  406. const firstValue = queryString[optionName][0];
  407. queryString[optionName].forEach(tlsValue => {
  408. if (tlsValue !== firstValue) {
  409. throw new MongoParseError('All values of ${optionName} must be the same.');
  410. }
  411. });
  412. }
  413. }
  414. return optionValue;
  415. }
  416. const PROTOCOL_MONGODB = 'mongodb';
  417. const PROTOCOL_MONGODB_SRV = 'mongodb+srv';
  418. const SUPPORTED_PROTOCOLS = [PROTOCOL_MONGODB, PROTOCOL_MONGODB_SRV];
  419. /**
  420. * Parses a MongoDB connection string
  421. *
  422. * @param {*} uri the MongoDB connection string to parse
  423. * @param {object} [options] Optional settings.
  424. * @param {boolean} [options.caseTranslate] Whether the parser should translate options back into camelCase after normalization
  425. * @param {parseCallback} callback
  426. */
  427. function parseConnectionString(uri, options, callback) {
  428. if (typeof options === 'function') (callback = options), (options = {});
  429. options = Object.assign({}, { caseTranslate: true }, options);
  430. // Check for bad uris before we parse
  431. try {
  432. URL.parse(uri);
  433. } catch (e) {
  434. return callback(new MongoParseError('URI malformed, cannot be parsed'));
  435. }
  436. const cap = uri.match(HOSTS_RX);
  437. if (!cap) {
  438. return callback(new MongoParseError('Invalid connection string'));
  439. }
  440. const protocol = cap[1];
  441. if (SUPPORTED_PROTOCOLS.indexOf(protocol) === -1) {
  442. return callback(new MongoParseError('Invalid protocol provided'));
  443. }
  444. if (protocol === PROTOCOL_MONGODB_SRV) {
  445. return parseSrvConnectionString(uri, options, callback);
  446. }
  447. const dbAndQuery = cap[4].split('?');
  448. const db = dbAndQuery.length > 0 ? dbAndQuery[0] : null;
  449. const query = dbAndQuery.length > 1 ? dbAndQuery[1] : null;
  450. let parsedOptions;
  451. try {
  452. parsedOptions = parseQueryString(query, options);
  453. } catch (parseError) {
  454. return callback(parseError);
  455. }
  456. parsedOptions = Object.assign({}, parsedOptions, options);
  457. const auth = { username: null, password: null, db: db && db !== '' ? qs.unescape(db) : null };
  458. if (parsedOptions.auth) {
  459. // maintain support for legacy options passed into `MongoClient`
  460. if (parsedOptions.auth.username) auth.username = parsedOptions.auth.username;
  461. if (parsedOptions.auth.user) auth.username = parsedOptions.auth.user;
  462. if (parsedOptions.auth.password) auth.password = parsedOptions.auth.password;
  463. }
  464. if (cap[4].split('?')[0].indexOf('@') !== -1) {
  465. return callback(new MongoParseError('Unescaped slash in userinfo section'));
  466. }
  467. const authorityParts = cap[3].split('@');
  468. if (authorityParts.length > 2) {
  469. return callback(new MongoParseError('Unescaped at-sign in authority section'));
  470. }
  471. if (authorityParts.length > 1) {
  472. const authParts = authorityParts.shift().split(':');
  473. if (authParts.length > 2) {
  474. return callback(new MongoParseError('Unescaped colon in authority section'));
  475. }
  476. auth.username = qs.unescape(authParts[0]);
  477. auth.password = authParts[1] ? qs.unescape(authParts[1]) : null;
  478. }
  479. let hostParsingError = null;
  480. const hosts = authorityParts
  481. .shift()
  482. .split(',')
  483. .map(host => {
  484. let parsedHost = URL.parse(`mongodb://${host}`);
  485. if (parsedHost.path === '/:') {
  486. hostParsingError = new MongoParseError('Double colon in host identifier');
  487. return null;
  488. }
  489. // heuristically determine if we're working with a domain socket
  490. if (host.match(/\.sock/)) {
  491. parsedHost.hostname = qs.unescape(host);
  492. parsedHost.port = null;
  493. }
  494. if (Number.isNaN(parsedHost.port)) {
  495. hostParsingError = new MongoParseError('Invalid port (non-numeric string)');
  496. return;
  497. }
  498. const result = {
  499. host: parsedHost.hostname,
  500. port: parsedHost.port ? parseInt(parsedHost.port) : 27017
  501. };
  502. if (result.port === 0) {
  503. hostParsingError = new MongoParseError('Invalid port (zero) with hostname');
  504. return;
  505. }
  506. if (result.port > 65535) {
  507. hostParsingError = new MongoParseError('Invalid port (larger than 65535) with hostname');
  508. return;
  509. }
  510. if (result.port < 0) {
  511. hostParsingError = new MongoParseError('Invalid port (negative number)');
  512. return;
  513. }
  514. return result;
  515. })
  516. .filter(host => !!host);
  517. if (hostParsingError) {
  518. return callback(hostParsingError);
  519. }
  520. if (hosts.length === 0 || hosts[0].host === '' || hosts[0].host === null) {
  521. return callback(new MongoParseError('No hostname or hostnames provided in connection string'));
  522. }
  523. const result = {
  524. hosts: hosts,
  525. auth: auth.db || auth.username ? auth : null,
  526. options: Object.keys(parsedOptions).length ? parsedOptions : null
  527. };
  528. if (result.auth && result.auth.db) {
  529. result.defaultDatabase = result.auth.db;
  530. }
  531. try {
  532. applyAuthExpectations(result);
  533. } catch (authError) {
  534. return callback(authError);
  535. }
  536. callback(null, result);
  537. }
  538. module.exports = parseConnectionString;