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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 (value.indexOf(':') > 0) {
  110. value = value.split(',').reduce((result, pair) => {
  111. const parts = pair.split(':');
  112. result[parts[0]] = parseQueryStringItemValue(key, parts[1]);
  113. return result;
  114. }, {});
  115. } else if (value.indexOf(',') > 0) {
  116. value = value.split(',').map(v => {
  117. return parseQueryStringItemValue(key, v);
  118. });
  119. } else if (value.toLowerCase() === 'true' || value.toLowerCase() === 'false') {
  120. value = value.toLowerCase() === 'true';
  121. } else if (!Number.isNaN(value) && !STRING_OPTIONS.has(key)) {
  122. const numericValue = parseFloat(value);
  123. if (!Number.isNaN(numericValue)) {
  124. value = parseFloat(value);
  125. }
  126. }
  127. return value;
  128. }
  129. // Options that are known boolean types
  130. const BOOLEAN_OPTIONS = new Set([
  131. 'slaveok',
  132. 'slave_ok',
  133. 'sslvalidate',
  134. 'fsync',
  135. 'safe',
  136. 'retrywrites',
  137. 'j'
  138. ]);
  139. // Known string options, only used to bypass Number coercion in `parseQueryStringItemValue`
  140. const STRING_OPTIONS = new Set(['authsource', 'replicaset']);
  141. // Supported text representations of auth mechanisms
  142. // NOTE: this list exists in native already, if it is merged here we should deduplicate
  143. const AUTH_MECHANISMS = new Set([
  144. 'GSSAPI',
  145. 'MONGODB-X509',
  146. 'MONGODB-CR',
  147. 'DEFAULT',
  148. 'SCRAM-SHA-1',
  149. 'SCRAM-SHA-256',
  150. 'PLAIN'
  151. ]);
  152. // Lookup table used to translate normalized (lower-cased) forms of connection string
  153. // options to their expected camelCase version
  154. const CASE_TRANSLATION = {
  155. replicaset: 'replicaSet',
  156. connecttimeoutms: 'connectTimeoutMS',
  157. sockettimeoutms: 'socketTimeoutMS',
  158. maxpoolsize: 'maxPoolSize',
  159. minpoolsize: 'minPoolSize',
  160. maxidletimems: 'maxIdleTimeMS',
  161. waitqueuemultiple: 'waitQueueMultiple',
  162. waitqueuetimeoutms: 'waitQueueTimeoutMS',
  163. wtimeoutms: 'wtimeoutMS',
  164. readconcern: 'readConcern',
  165. readconcernlevel: 'readConcernLevel',
  166. readpreference: 'readPreference',
  167. maxstalenessseconds: 'maxStalenessSeconds',
  168. readpreferencetags: 'readPreferenceTags',
  169. authsource: 'authSource',
  170. authmechanism: 'authMechanism',
  171. authmechanismproperties: 'authMechanismProperties',
  172. gssapiservicename: 'gssapiServiceName',
  173. localthresholdms: 'localThresholdMS',
  174. serverselectiontimeoutms: 'serverSelectionTimeoutMS',
  175. serverselectiontryonce: 'serverSelectionTryOnce',
  176. heartbeatfrequencyms: 'heartbeatFrequencyMS',
  177. appname: 'appName',
  178. retrywrites: 'retryWrites',
  179. uuidrepresentation: 'uuidRepresentation',
  180. zlibcompressionlevel: 'zlibCompressionLevel'
  181. };
  182. /**
  183. * Sets the value for `key`, allowing for any required translation
  184. *
  185. * @param {object} obj The object to set the key on
  186. * @param {string} key The key to set the value for
  187. * @param {*} value The value to set
  188. * @param {object} options The options used for option parsing
  189. */
  190. function applyConnectionStringOption(obj, key, value, options) {
  191. // simple key translation
  192. if (key === 'journal') {
  193. key = 'j';
  194. } else if (key === 'wtimeoutms') {
  195. key = 'wtimeout';
  196. }
  197. // more complicated translation
  198. if (BOOLEAN_OPTIONS.has(key)) {
  199. value = value === 'true' || value === true;
  200. } else if (key === 'appname') {
  201. value = decodeURIComponent(value);
  202. } else if (key === 'readconcernlevel') {
  203. key = 'readconcern';
  204. value = { level: value };
  205. }
  206. // simple validation
  207. if (key === 'compressors') {
  208. value = Array.isArray(value) ? value : [value];
  209. if (!value.every(c => c === 'snappy' || c === 'zlib')) {
  210. throw new MongoParseError(
  211. 'Value for `compressors` must be at least one of: `snappy`, `zlib`'
  212. );
  213. }
  214. }
  215. if (key === 'authmechanism' && !AUTH_MECHANISMS.has(value)) {
  216. throw new MongoParseError(
  217. 'Value for `authMechanism` must be one of: `DEFAULT`, `GSSAPI`, `PLAIN`, `MONGODB-X509`, `SCRAM-SHA-1`, `SCRAM-SHA-256`'
  218. );
  219. }
  220. if (key === 'readpreference' && !ReadPreference.isValid(value)) {
  221. throw new MongoParseError(
  222. 'Value for `readPreference` must be one of: `primary`, `primaryPreferred`, `secondary`, `secondaryPreferred`, `nearest`'
  223. );
  224. }
  225. if (key === 'zlibcompressionlevel' && (value < -1 || value > 9)) {
  226. throw new MongoParseError('zlibCompressionLevel must be an integer between -1 and 9');
  227. }
  228. // special cases
  229. if (key === 'compressors' || key === 'zlibcompressionlevel') {
  230. obj.compression = obj.compression || {};
  231. obj = obj.compression;
  232. }
  233. if (key === 'authmechanismproperties') {
  234. if (typeof value.SERVICE_NAME === 'string') obj.gssapiServiceName = value.SERVICE_NAME;
  235. if (typeof value.SERVICE_REALM === 'string') obj.gssapiServiceRealm = value.SERVICE_REALM;
  236. if (typeof value.CANONICALIZE_HOST_NAME !== 'undefined') {
  237. obj.gssapiCanonicalizeHostName = value.CANONICALIZE_HOST_NAME;
  238. }
  239. }
  240. // set the actual value
  241. if (options.caseTranslate && CASE_TRANSLATION[key]) {
  242. obj[CASE_TRANSLATION[key]] = value;
  243. return;
  244. }
  245. obj[key] = value;
  246. }
  247. const USERNAME_REQUIRED_MECHANISMS = new Set([
  248. 'GSSAPI',
  249. 'MONGODB-CR',
  250. 'PLAIN',
  251. 'SCRAM-SHA-1',
  252. 'SCRAM-SHA-256'
  253. ]);
  254. /**
  255. * Modifies the parsed connection string object taking into account expectations we
  256. * have for authentication-related options.
  257. *
  258. * @param {object} parsed The parsed connection string result
  259. * @return The parsed connection string result possibly modified for auth expectations
  260. */
  261. function applyAuthExpectations(parsed) {
  262. if (parsed.options == null) {
  263. return;
  264. }
  265. const options = parsed.options;
  266. const authSource = options.authsource || options.authSource;
  267. if (authSource != null) {
  268. parsed.auth = Object.assign({}, parsed.auth, { db: authSource });
  269. }
  270. const authMechanism = options.authmechanism || options.authMechanism;
  271. if (authMechanism != null) {
  272. if (
  273. USERNAME_REQUIRED_MECHANISMS.has(authMechanism) &&
  274. (!parsed.auth || parsed.auth.username == null)
  275. ) {
  276. throw new MongoParseError(`Username required for mechanism \`${authMechanism}\``);
  277. }
  278. if (authMechanism === 'GSSAPI') {
  279. if (authSource != null && authSource !== '$external') {
  280. throw new MongoParseError(
  281. `Invalid source \`${authSource}\` for mechanism \`${authMechanism}\` specified.`
  282. );
  283. }
  284. parsed.auth = Object.assign({}, parsed.auth, { db: '$external' });
  285. }
  286. if (authMechanism === 'MONGODB-X509') {
  287. if (parsed.auth && parsed.auth.password != null) {
  288. throw new MongoParseError(`Password not allowed for mechanism \`${authMechanism}\``);
  289. }
  290. if (authSource != null && authSource !== '$external') {
  291. throw new MongoParseError(
  292. `Invalid source \`${authSource}\` for mechanism \`${authMechanism}\` specified.`
  293. );
  294. }
  295. parsed.auth = Object.assign({}, parsed.auth, { db: '$external' });
  296. }
  297. if (authMechanism === 'PLAIN') {
  298. if (parsed.auth && parsed.auth.db == null) {
  299. parsed.auth = Object.assign({}, parsed.auth, { db: '$external' });
  300. }
  301. }
  302. }
  303. // default to `admin` if nothing else was resolved
  304. if (parsed.auth && parsed.auth.db == null) {
  305. parsed.auth = Object.assign({}, parsed.auth, { db: 'admin' });
  306. }
  307. return parsed;
  308. }
  309. /**
  310. * Parses a query string according the connection string spec.
  311. *
  312. * @param {String} query The query string to parse
  313. * @param {object} [options] The options used for options parsing
  314. * @return {Object|Error} The parsed query string as an object, or an error if one was encountered
  315. */
  316. function parseQueryString(query, options) {
  317. const result = {};
  318. let parsedQueryString = qs.parse(query);
  319. for (const key in parsedQueryString) {
  320. const value = parsedQueryString[key];
  321. if (value === '' || value == null) {
  322. throw new MongoParseError('Incomplete key value pair for option');
  323. }
  324. const normalizedKey = key.toLowerCase();
  325. const parsedValue = parseQueryStringItemValue(normalizedKey, value);
  326. applyConnectionStringOption(result, normalizedKey, parsedValue, options);
  327. }
  328. // special cases for known deprecated options
  329. if (result.wtimeout && result.wtimeoutms) {
  330. delete result.wtimeout;
  331. console.warn('Unsupported option `wtimeout` specified');
  332. }
  333. return Object.keys(result).length ? result : null;
  334. }
  335. const PROTOCOL_MONGODB = 'mongodb';
  336. const PROTOCOL_MONGODB_SRV = 'mongodb+srv';
  337. const SUPPORTED_PROTOCOLS = [PROTOCOL_MONGODB, PROTOCOL_MONGODB_SRV];
  338. /**
  339. * Parses a MongoDB connection string
  340. *
  341. * @param {*} uri the MongoDB connection string to parse
  342. * @param {object} [options] Optional settings.
  343. * @param {boolean} [options.caseTranslate] Whether the parser should translate options back into camelCase after normalization
  344. * @param {parseCallback} callback
  345. */
  346. function parseConnectionString(uri, options, callback) {
  347. if (typeof options === 'function') (callback = options), (options = {});
  348. options = Object.assign({}, { caseTranslate: true }, options);
  349. // Check for bad uris before we parse
  350. try {
  351. URL.parse(uri);
  352. } catch (e) {
  353. return callback(new MongoParseError('URI malformed, cannot be parsed'));
  354. }
  355. const cap = uri.match(HOSTS_RX);
  356. if (!cap) {
  357. return callback(new MongoParseError('Invalid connection string'));
  358. }
  359. const protocol = cap[1];
  360. if (SUPPORTED_PROTOCOLS.indexOf(protocol) === -1) {
  361. return callback(new MongoParseError('Invalid protocol provided'));
  362. }
  363. if (protocol === PROTOCOL_MONGODB_SRV) {
  364. return parseSrvConnectionString(uri, options, callback);
  365. }
  366. const dbAndQuery = cap[4].split('?');
  367. const db = dbAndQuery.length > 0 ? dbAndQuery[0] : null;
  368. const query = dbAndQuery.length > 1 ? dbAndQuery[1] : null;
  369. let parsedOptions;
  370. try {
  371. parsedOptions = parseQueryString(query, options);
  372. } catch (parseError) {
  373. return callback(parseError);
  374. }
  375. parsedOptions = Object.assign({}, parsedOptions, options);
  376. const auth = { username: null, password: null, db: db && db !== '' ? qs.unescape(db) : null };
  377. if (parsedOptions.auth) {
  378. // maintain support for legacy options passed into `MongoClient`
  379. if (parsedOptions.auth.username) auth.username = parsedOptions.auth.username;
  380. if (parsedOptions.auth.user) auth.username = parsedOptions.auth.user;
  381. if (parsedOptions.auth.password) auth.password = parsedOptions.auth.password;
  382. }
  383. if (cap[4].split('?')[0].indexOf('@') !== -1) {
  384. return callback(new MongoParseError('Unescaped slash in userinfo section'));
  385. }
  386. const authorityParts = cap[3].split('@');
  387. if (authorityParts.length > 2) {
  388. return callback(new MongoParseError('Unescaped at-sign in authority section'));
  389. }
  390. if (authorityParts.length > 1) {
  391. const authParts = authorityParts.shift().split(':');
  392. if (authParts.length > 2) {
  393. return callback(new MongoParseError('Unescaped colon in authority section'));
  394. }
  395. auth.username = qs.unescape(authParts[0]);
  396. auth.password = authParts[1] ? qs.unescape(authParts[1]) : null;
  397. }
  398. let hostParsingError = null;
  399. const hosts = authorityParts
  400. .shift()
  401. .split(',')
  402. .map(host => {
  403. let parsedHost = URL.parse(`mongodb://${host}`);
  404. if (parsedHost.path === '/:') {
  405. hostParsingError = new MongoParseError('Double colon in host identifier');
  406. return null;
  407. }
  408. // heuristically determine if we're working with a domain socket
  409. if (host.match(/\.sock/)) {
  410. parsedHost.hostname = qs.unescape(host);
  411. parsedHost.port = null;
  412. }
  413. if (Number.isNaN(parsedHost.port)) {
  414. hostParsingError = new MongoParseError('Invalid port (non-numeric string)');
  415. return;
  416. }
  417. const result = {
  418. host: parsedHost.hostname,
  419. port: parsedHost.port ? parseInt(parsedHost.port) : 27017
  420. };
  421. if (result.port === 0) {
  422. hostParsingError = new MongoParseError('Invalid port (zero) with hostname');
  423. return;
  424. }
  425. if (result.port > 65535) {
  426. hostParsingError = new MongoParseError('Invalid port (larger than 65535) with hostname');
  427. return;
  428. }
  429. if (result.port < 0) {
  430. hostParsingError = new MongoParseError('Invalid port (negative number)');
  431. return;
  432. }
  433. return result;
  434. })
  435. .filter(host => !!host);
  436. if (hostParsingError) {
  437. return callback(hostParsingError);
  438. }
  439. if (hosts.length === 0 || hosts[0].host === '' || hosts[0].host === null) {
  440. return callback(new MongoParseError('No hostname or hostnames provided in connection string'));
  441. }
  442. const result = {
  443. hosts: hosts,
  444. auth: auth.db || auth.username ? auth : null,
  445. options: Object.keys(parsedOptions).length ? parsedOptions : null
  446. };
  447. if (result.auth && result.auth.db) {
  448. result.defaultDatabase = result.auth.db;
  449. }
  450. try {
  451. applyAuthExpectations(result);
  452. } catch (authError) {
  453. return callback(authError);
  454. }
  455. callback(null, result);
  456. }
  457. module.exports = parseConnectionString;