index.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. 'use strict'
  2. //Parse method copied from https://github.com/brianc/node-postgres
  3. //Copyright (c) 2010-2014 Brian Carlson (brian.m.carlson@gmail.com)
  4. //MIT License
  5. //parses a connection string
  6. function parse(str, options = {}) {
  7. //unix socket
  8. if (str.charAt(0) === '/') {
  9. const config = str.split(' ')
  10. return { host: config[0], database: config[1] }
  11. }
  12. // Check for empty host in URL
  13. const config = {}
  14. let result
  15. let dummyHost = false
  16. if (/ |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(str)) {
  17. // Ensure spaces are encoded as %20
  18. str = encodeURI(str).replace(/%25(\d\d)/g, '%$1')
  19. }
  20. try {
  21. try {
  22. result = new URL(str, 'postgres://base')
  23. } catch (e) {
  24. // The URL is invalid so try again with a dummy host
  25. result = new URL(str.replace('@/', '@___DUMMY___/'), 'postgres://base')
  26. dummyHost = true
  27. }
  28. } catch (err) {
  29. // Remove the input from the error message to avoid leaking sensitive information
  30. err.input && (err.input = '*****REDACTED*****')
  31. throw err
  32. }
  33. // We'd like to use Object.fromEntries() here but Node.js 10 does not support it
  34. for (const entry of result.searchParams.entries()) {
  35. config[entry[0]] = entry[1]
  36. }
  37. config.user = config.user || decodeURIComponent(result.username)
  38. config.password = config.password || decodeURIComponent(result.password)
  39. if (result.protocol == 'socket:') {
  40. config.host = decodeURI(result.pathname)
  41. config.database = result.searchParams.get('db')
  42. config.client_encoding = result.searchParams.get('encoding')
  43. return config
  44. }
  45. const hostname = dummyHost ? '' : result.hostname
  46. if (!config.host) {
  47. // Only set the host if there is no equivalent query param.
  48. config.host = decodeURIComponent(hostname)
  49. } else if (hostname && /^%2f/i.test(hostname)) {
  50. // Only prepend the hostname to the pathname if it is not a URL encoded Unix socket host.
  51. result.pathname = hostname + result.pathname
  52. }
  53. if (!config.port) {
  54. // Only set the port if there is no equivalent query param.
  55. config.port = result.port
  56. }
  57. const pathname = result.pathname.slice(1) || null
  58. config.database = pathname ? decodeURI(pathname) : null
  59. if (config.ssl === 'true' || config.ssl === '1') {
  60. config.ssl = true
  61. }
  62. if (config.ssl === '0') {
  63. config.ssl = false
  64. }
  65. if (config.sslcert || config.sslkey || config.sslrootcert || config.sslmode) {
  66. config.ssl = {}
  67. }
  68. // Only try to load fs if we expect to read from the disk
  69. const fs = config.sslcert || config.sslkey || config.sslrootcert ? require('fs') : null
  70. if (config.sslcert) {
  71. config.ssl.cert = fs.readFileSync(config.sslcert).toString()
  72. }
  73. if (config.sslkey) {
  74. config.ssl.key = fs.readFileSync(config.sslkey).toString()
  75. }
  76. if (config.sslrootcert) {
  77. config.ssl.ca = fs.readFileSync(config.sslrootcert).toString()
  78. }
  79. if (options.useLibpqCompat && config.uselibpqcompat) {
  80. throw new Error('Both useLibpqCompat and uselibpqcompat are set. Please use only one of them.')
  81. }
  82. if (config.uselibpqcompat === 'true' || options.useLibpqCompat) {
  83. switch (config.sslmode) {
  84. case 'disable': {
  85. config.ssl = false
  86. break
  87. }
  88. case 'prefer': {
  89. config.ssl.rejectUnauthorized = false
  90. break
  91. }
  92. case 'require': {
  93. if (config.sslrootcert) {
  94. // If a root CA is specified, behavior of `sslmode=require` will be the same as that of `verify-ca`
  95. config.ssl.checkServerIdentity = function () {}
  96. } else {
  97. config.ssl.rejectUnauthorized = false
  98. }
  99. break
  100. }
  101. case 'verify-ca': {
  102. if (!config.ssl.ca) {
  103. throw new Error(
  104. 'SECURITY WARNING: Using sslmode=verify-ca requires specifying a CA with sslrootcert. If a public CA is used, verify-ca allows connections to a server that somebody else may have registered with the CA, making you vulnerable to Man-in-the-Middle attacks. Either specify a custom CA certificate with sslrootcert parameter or use sslmode=verify-full for proper security.'
  105. )
  106. }
  107. config.ssl.checkServerIdentity = function () {}
  108. break
  109. }
  110. case 'verify-full': {
  111. break
  112. }
  113. }
  114. } else {
  115. switch (config.sslmode) {
  116. case 'disable': {
  117. config.ssl = false
  118. break
  119. }
  120. case 'prefer':
  121. case 'require':
  122. case 'verify-ca':
  123. case 'verify-full': {
  124. if (config.sslmode !== 'verify-full') {
  125. deprecatedSslModeWarning(config.sslmode)
  126. }
  127. break
  128. }
  129. case 'no-verify': {
  130. config.ssl.rejectUnauthorized = false
  131. break
  132. }
  133. }
  134. }
  135. return config
  136. }
  137. // convert pg-connection-string ssl config to a ClientConfig.ConnectionOptions
  138. function toConnectionOptions(sslConfig) {
  139. const connectionOptions = Object.entries(sslConfig).reduce((c, [key, value]) => {
  140. // we explicitly check for undefined and null instead of `if (value)` because some
  141. // options accept falsy values. Example: `ssl.rejectUnauthorized = false`
  142. if (value !== undefined && value !== null) {
  143. c[key] = value
  144. }
  145. return c
  146. }, {})
  147. return connectionOptions
  148. }
  149. // convert pg-connection-string config to a ClientConfig
  150. function toClientConfig(config) {
  151. const poolConfig = Object.entries(config).reduce((c, [key, value]) => {
  152. if (key === 'ssl') {
  153. const sslConfig = value
  154. if (typeof sslConfig === 'boolean') {
  155. c[key] = sslConfig
  156. }
  157. if (typeof sslConfig === 'object') {
  158. c[key] = toConnectionOptions(sslConfig)
  159. }
  160. } else if (value !== undefined && value !== null) {
  161. if (key === 'port') {
  162. // when port is not specified, it is converted into an empty string
  163. // we want to avoid NaN or empty string as a values in ClientConfig
  164. if (value !== '') {
  165. const v = parseInt(value, 10)
  166. if (isNaN(v)) {
  167. throw new Error(`Invalid ${key}: ${value}`)
  168. }
  169. c[key] = v
  170. }
  171. } else {
  172. c[key] = value
  173. }
  174. }
  175. return c
  176. }, {})
  177. return poolConfig
  178. }
  179. // parses a connection string into ClientConfig
  180. function parseIntoClientConfig(str) {
  181. return toClientConfig(parse(str))
  182. }
  183. function deprecatedSslModeWarning(sslmode) {
  184. if (!deprecatedSslModeWarning.warned && typeof process !== 'undefined' && process.emitWarning) {
  185. deprecatedSslModeWarning.warned = true
  186. process.emitWarning(`SECURITY WARNING: The SSL modes 'prefer', 'require', and 'verify-ca' are treated as aliases for 'verify-full'.
  187. In the next major version (pg-connection-string v3.0.0 and pg v9.0.0), these modes will adopt standard libpq semantics, which have weaker security guarantees.
  188. To prepare for this change:
  189. - If you want the current behavior, explicitly use 'sslmode=verify-full'
  190. - If you want libpq compatibility now, use 'uselibpqcompat=true&sslmode=${sslmode}'
  191. See https://www.postgresql.org/docs/current/libpq-ssl.html for libpq SSL mode definitions.`)
  192. }
  193. }
  194. module.exports = parse
  195. parse.parse = parse
  196. parse.toClientConfig = toClientConfig
  197. parse.parseIntoClientConfig = parseIntoClientConfig