{"id":"1b9c80cce3f99981","repo":"mongodb/node-mongodb-native","slug":"cannot-have-empty-uri-params-in-dns-txt-record","errorCode":null,"errorMessage":"Cannot have empty URI params in DNS TXT Record","messagePattern":"Cannot have empty URI params in DNS TXT Record","errorType":"exception","errorClass":"MongoParseError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":127,"sourceCode":"  } catch (error) {\n    if (error.code !== 'ENODATA' && error.code !== 'ENOTFOUND') {\n      throw error;\n    }\n    return hostAddresses;\n  }\n\n  if (record.length > 1) {\n    throw new MongoParseError('Multiple text records not allowed');\n  }\n\n  const txtRecordOptions = new URLSearchParams(record[0].join(''));\n  const txtRecordOptionKeys = [...txtRecordOptions.keys()];\n  if (txtRecordOptionKeys.some(key => !VALID_TXT_RECORDS.includes(key))) {\n    throw new MongoParseError(`Text record may only set any of: ${VALID_TXT_RECORDS.join(', ')}`);\n  }\n\n  if (VALID_TXT_RECORDS.some(option => txtRecordOptions.get(option) === '')) {\n    throw new MongoParseError('Cannot have empty URI params in DNS TXT Record');\n  }\n\n  const source = txtRecordOptions.get('authSource') ?? undefined;\n  const replicaSet = txtRecordOptions.get('replicaSet') ?? undefined;\n  const loadBalanced = txtRecordOptions.get('loadBalanced') ?? undefined;\n\n  if (\n    !options.userSpecifiedAuthSource &&\n    source &&\n    options.credentials &&\n    !AUTH_MECHS_AUTH_SRC_EXTERNAL.has(options.credentials.mechanism)\n  ) {\n    options.credentials = MongoCredentials.merge(options.credentials, { source });\n  }\n\n  if (!options.userSpecifiedReplicaSet && replicaSet) {\n    options.replicaSet = replicaSet;\n  }","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/connection_string.ts#L109-L145","documentation":"Thrown by resolveSRVRecord() when a TXT record (for a mongodb+srv:// URI) declares an allowed key but with an empty value, e.g. 'authSource='. The driver parses the TXT record as URLSearchParams and rejects any of authSource/replicaSet/loadBalanced whose value is the empty string. It is a MongoParseError.","triggerScenarios":"A TXT record formatted as 'authSource=&replicaSet=rs0' (trailing equals with nothing); a copy-paste error leaving a value blank; a DNS templating tool that emitted empty values for unset variables.","commonSituations":"Editing the TXT record and deleting a value but leaving the key; using a templating system that interpolates missing vars as empty strings; partial migration where one option was intended to be removed but the key was left.","solutions":["Inspect the TXT record: dig <host> TXT and check for trailing '='.","Remove keys that have no value, or supply the intended value.","Re-query DNS after the change and confirm each key has a non-empty value.","Validate the TXT record locally with new URLSearchParams(record) before publishing."],"exampleFix":"// DNS TXT before: \"authSource=&replicaSet=rs0\"\n// DNS TXT after:  \"replicaSet=rs0\"","handlingStrategy":"validation","validationCode":"import { promises as dns } from 'dns';\nconst ALLOWED = ['authSource', 'replicaSet', 'loadBalanced'];\nasync function validateTxtValues(host: string): Promise<void> {\n  const records = await dns.resolveTxt(host);\n  if (!records.length) return;\n  const params = new URLSearchParams(records[0].join(''));\n  for (const k of ALLOWED) {\n    if (params.has(k) && (params.get(k) ?? '') === '') {\n      throw new Error(`TXT key '${k}' must not have an empty value`);\n    }\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  await client.connect();\n} catch (e) {\n  if (e instanceof MongoParseError && /empty URI params in DNS TXT/.test(e.message)) {\n    throw new Error('DNS TXT record has an empty value; fix or remove the key');\n  }\n  throw e;\n}","preventionTips":["Never publish a TXT key with a trailing '=' and no value.","Remove keys you intend to disable rather than blanking them.","Re-query DNS after edits."],"tags":["connection-string","dns","srv","config"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}