# All files main.js

100% Statements 361/361
100% Branches 96/96
100% Functions 14/14
100% Lines 361/361

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    3627x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    66x
    66x
    66x
    66x
    66x
    66x
    66x
    66x
    66x
    66x
    66x
    643x
    643x
    643x
    643x
    643x
    643x
    643x
    643x
    643x
    643x
    643x
    643x
    643x
    643x
    643x
    643x
    165x
    165x
    165x
    643x
    643x
    643x
    643x
    66x
    66x
    66x
    7x
    22x
    22x
    22x
    22x
    22x
    22x
    1x
    1x
    1x
    1x
    21x
    21x
    21x
    21x
    21x
    21x
    21x
    22x
    22x
    22x
    22x
    22x
    22x
    22x
    22x
    22x
    22x
    22x
    22x
    22x
    10x
    10x
    9x
    9x
    10x
    10x
    22x
    12x
    12x
    12x
    22x
    7x
    22x
    22x
    22x
    7x
    3x
    3x
    3x
    7x
    11x
    11x
    11x
    7x
    68x
    68x
    68x
    2x
    2x
    66x
    66x
    68x
    44x
    44x
    22x
    22x
    22x
    68x
    7x
    22x
    22x
    22x
    22x
    22x
    22x
    2x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    20x
    20x
    20x
    22x
    1x
    1x
    1x
    1x
    19x
    19x
    19x
    22x
    1x
    1x
    1x
    1x
    18x
    18x
    18x
    18x
    22x
    1x
    1x
    1x
    1x
    17x
    17x
    22x
    7x
    47x
    47x
    47x
    47x
    46x
    7x
    9x
    8x
    8x
    9x
    46x
    39x
    39x
    47x
    1x
    1x
    47x
    47x
    44x
    44x
    3x
    3x
    47x
    7x
    41x
    41x
    41x
    7x
    22x
    22x
    22x
    22x
    22x
    22x
    22x
    1x
    1x
    12x
    12x
    12x
    12x
    22x
    7x
    46x
    46x
    46x
    46x
    46x
    46x
    2x
    45x
    44x
    3x
    3x
    44x
    46x
    46x
    46x
    38x
    33x
    38x
    5x
    5x
    8x
    8x
    5x
    38x
    46x
    46x
    46x
    46x
    46x
    46x
    49x
    49x
    49x
    49x
    49x
    49x
    7x
    4x
    4x
    7x
    7x
    49x
    46x
    46x
    46x
    1x
    1x
    46x
    46x
    46x
    46x
    7x
    46x
    39x
    39x
    46x
    7x
    7x
    47x
    47x
    47x
    22x
    22x
    25x
    25x
    25x
    25x
    26x
    3x
    3x
    3x
    3x
    22x
    22x
    47x
    7x
    18x
    18x
    18x
    18x
    18x
    18x
    18x
    18x
    18x
    18x
    18x
    18x
    18x
    5x
    5x
    5x
    5x
    5x
    1x
    1x
    1x
    5x
    3x
    3x
    3x
    4x
    1x
    1x
    5x
    18x
    7x
    7x
    106x
    106x
    106x
    106x
    106x
    1x
    1x
    1x
    1x
    105x
    105x
    106x
    1123x
    453x
    74x
    74x
    453x
    453x
    4x
    2x
    2x
    2x
    2x
    4x
    1123x
    670x
    670x
    1123x
    106x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
     const fs = require('fs')
    const path = require('path')
    const os = require('os')
    const crypto = require('crypto')
    const packageJson = require('../package.json')

    const version = packageJson.version

    const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg

    // Parse src into an Object
    function parse (src) {
      const obj = {}

      // Convert buffer to string
      let lines = src.toString()

      // Convert line breaks to same format
      lines = lines.replace(/\r\n?/mg, '\n')

      let match
      while ((match = LINE.exec(lines)) != null) {
        const key = match[1]

        // Default undefined or null to empty string
        let value = (match[2] || '')

        // Remove whitespace
        value = value.trim()

        // Check if double quoted
        const maybeQuote = value[0]

        // Remove surrounding quotes
        value = value.replace(/^(['"`])([\s\S]*)\1$/mg, '$2')

        // Expand newlines if double quoted
        if (maybeQuote === '"') {
          value = value.replace(/\\n/g, '\n')
          value = value.replace(/\\r/g, '\r')
        }

        // Add to object
        obj[key] = value
      }

      return obj
    }

    function _parseVault (options) {
      const vaultPath = _vaultPath(options)

      // Parse .env.vault
      const result = DotenvModule.configDotenv({ path: vaultPath })
      if (!result.parsed) {
        const err = new Error(`MISSING_DATA: Cannot parse ${vaultPath} for an unknown reason`)
        err.code = 'MISSING_DATA'
        throw err
      }

      // handle scenario for comma separated keys - for use with key rotation
      // example: DOTENV_KEY="dotenv://:key_1234@dotenvx.com/vault/.env.vault?environment=prod,dotenv://:key_7890@dotenvx.com/vault/.env.vault?environment=prod"
      const keys = _dotenvKey(options).split(',')
      const length = keys.length

      let decrypted
      for (let i = 0; i < length; i++) {
        try {
          // Get full key
          const key = keys[i].trim()

          // Get instructions for decrypt
          const attrs = _instructions(result, key)

          // Decrypt
          decrypted = DotenvModule.decrypt(attrs.ciphertext, attrs.key)

          break
        } catch (error) {
          // last key
          if (i + 1 >= length) {
            throw error
          }
          // try next key
        }
      }

      // Parse decrypted .env string
      return DotenvModule.parse(decrypted)
    }

    function _log (message) {
      console.log(`[dotenv@${version}][INFO] ${message}`)
    }

    function _warn (message) {
      console.log(`[dotenv@${version}][WARN] ${message}`)
    }

    function _debug (message) {
      console.log(`[dotenv@${version}][DEBUG] ${message}`)
    }

    function _dotenvKey (options) {
      // prioritize developer directly setting options.DOTENV_KEY
      if (options && options.DOTENV_KEY && options.DOTENV_KEY.length > 0) {
        return options.DOTENV_KEY
      }

      // secondary infra already contains a DOTENV_KEY environment variable
      if (process.env.DOTENV_KEY && process.env.DOTENV_KEY.length > 0) {
        return process.env.DOTENV_KEY
      }

      // fallback to empty string
      return ''
    }

    function _instructions (result, dotenvKey) {
      // Parse DOTENV_KEY. Format is a URI
      let uri
      try {
        uri = new URL(dotenvKey)
      } catch (error) {
        if (error.code === 'ERR_INVALID_URL') {
          const err = new Error('INVALID_DOTENV_KEY: Wrong format. Must be in valid uri format like dotenv://:key_1234@dotenvx.com/vault/.env.vault?environment=development')
          err.code = 'INVALID_DOTENV_KEY'
          throw err
        }

        throw error
      }

      // Get decrypt key
      const key = uri.password
      if (!key) {
        const err = new Error('INVALID_DOTENV_KEY: Missing key part')
        err.code = 'INVALID_DOTENV_KEY'
        throw err
      }

      // Get environment
      const environment = uri.searchParams.get('environment')
      if (!environment) {
        const err = new Error('INVALID_DOTENV_KEY: Missing environment part')
        err.code = 'INVALID_DOTENV_KEY'
        throw err
      }

      // Get ciphertext payload
      const environmentKey = `DOTENV_VAULT_${environment.toUpperCase()}`
      const ciphertext = result.parsed[environmentKey] // DOTENV_VAULT_PRODUCTION
      if (!ciphertext) {
        const err = new Error(`NOT_FOUND_DOTENV_ENVIRONMENT: Cannot locate environment ${environmentKey} in your .env.vault file.`)
        err.code = 'NOT_FOUND_DOTENV_ENVIRONMENT'
        throw err
      }

      return { ciphertext, key }
    }

    function _vaultPath (options) {
      let possibleVaultPath = null

      if (options && options.path && options.path.length > 0) {
        if (Array.isArray(options.path)) {
          for (const filepath of options.path) {
            if (fs.existsSync(filepath)) {
              possibleVaultPath = filepath.endsWith('.vault') ? filepath : `${filepath}.vault`
            }
          }
        } else {
          possibleVaultPath = options.path.endsWith('.vault') ? options.path : `${options.path}.vault`
        }
      } else {
        possibleVaultPath = path.resolve(process.cwd(), '.env.vault')
      }

      if (fs.existsSync(possibleVaultPath)) {
        return possibleVaultPath
      }

      return null
    }

    function _resolveHome (envPath) {
      return envPath[0] === '~' ? path.join(os.homedir(), envPath.slice(1)) : envPath
    }

    function _configVault (options) {
      _log('Loading env from encrypted .env.vault')

      const parsed = DotenvModule._parseVault(options)

      let processEnv = process.env
      if (options && options.processEnv != null) {
        processEnv = options.processEnv
      }

      DotenvModule.populate(processEnv, parsed, options)

      return { parsed }
    }

    function configDotenv (options) {
      const dotenvPath = path.resolve(process.cwd(), '.env')
      let encoding = 'utf8'
      const debug = Boolean(options && options.debug)

      if (options && options.encoding) {
        encoding = options.encoding
      } else {
        if (debug) {
          _debug('No encoding is specified. UTF-8 is used by default')
        }
      }

      let optionPaths = [dotenvPath] // default, look for .env
      if (options && options.path) {
        if (!Array.isArray(options.path)) {
          optionPaths = [_resolveHome(options.path)]
        } else {
          optionPaths = [] // reset default
          for (const filepath of options.path) {
            optionPaths.push(_resolveHome(filepath))
          }
        }
      }

      // Build the parsed data in a temporary object (because we need to return it).  Once we have the final
      // parsed data, we will combine it with process.env (or options.processEnv if provided).
      let lastError
      const parsedAll = {}
      for (const path of optionPaths) {
        try {
          // Specifying an encoding returns a string instead of a buffer
          const parsed = DotenvModule.parse(fs.readFileSync(path, { encoding }))

          DotenvModule.populate(parsedAll, parsed, options)
        } catch (e) {
          if (debug) {
            _debug(`Failed to load ${path} ${e.message}`)
          }
          lastError = e
        }
      }

      let processEnv = process.env
      if (options && options.processEnv != null) {
        processEnv = options.processEnv
      }

      DotenvModule.populate(processEnv, parsedAll, options)

      if (lastError) {
        return { parsed: parsedAll, error: lastError }
      } else {
        return { parsed: parsedAll }
      }
    }

    // Populates process.env from .env file
    function config (options) {
      // fallback to original dotenv if DOTENV_KEY is not set
      if (_dotenvKey(options).length === 0) {
        return DotenvModule.configDotenv(options)
      }

      const vaultPath = _vaultPath(options)

      // dotenvKey exists but .env.vault file does not exist
      if (!vaultPath) {
        _warn(`You set DOTENV_KEY but you are missing a .env.vault file at ${vaultPath}. Did you forget to build it?`)

        return DotenvModule.configDotenv(options)
      }

      return DotenvModule._configVault(options)
    }

    function decrypt (encrypted, keyStr) {
      const key = Buffer.from(keyStr.slice(-64), 'hex')
      let ciphertext = Buffer.from(encrypted, 'base64')

      const nonce = ciphertext.subarray(0, 12)
      const authTag = ciphertext.subarray(-16)
      ciphertext = ciphertext.subarray(12, -16)

      try {
        const aesgcm = crypto.createDecipheriv('aes-256-gcm', key, nonce)
        aesgcm.setAuthTag(authTag)
        return `${aesgcm.update(ciphertext)}${aesgcm.final()}`
      } catch (error) {
        const isRange = error instanceof RangeError
        const invalidKeyLength = error.message === 'Invalid key length'
        const decryptionFailed = error.message === 'Unsupported state or unable to authenticate data'

        if (isRange || invalidKeyLength) {
          const err = new Error('INVALID_DOTENV_KEY: It must be 64 characters long (or more)')
          err.code = 'INVALID_DOTENV_KEY'
          throw err
        } else if (decryptionFailed) {
          const err = new Error('DECRYPTION_FAILED: Please check your DOTENV_KEY')
          err.code = 'DECRYPTION_FAILED'
          throw err
        } else {
          throw error
        }
      }
    }

    // Populate process.env with parsed values
    function populate (processEnv, parsed, options = {}) {
      const debug = Boolean(options && options.debug)
      const override = Boolean(options && options.override)

      if (typeof parsed !== 'object') {
        const err = new Error('OBJECT_REQUIRED: Please check the processEnv argument being passed to populate')
        err.code = 'OBJECT_REQUIRED'
        throw err
      }

      // Set process.env
      for (const key of Object.keys(parsed)) {
        if (Object.prototype.hasOwnProperty.call(processEnv, key)) {
          if (override === true) {
            processEnv[key] = parsed[key]
          }

          if (debug) {
            if (override === true) {
              _debug(`"${key}" is already defined and WAS overwritten`)
            } else {
              _debug(`"${key}" is already defined and was NOT overwritten`)
            }
          }
        } else {
          processEnv[key] = parsed[key]
        }
      }
    }

    const DotenvModule = {
      configDotenv,
      _configVault,
      _parseVault,
      config,
      decrypt,
      parse,
      populate
    }

    module.exports.configDotenv = DotenvModule.configDotenv
    module.exports._configVault = DotenvModule._configVault
    module.exports._parseVault = DotenvModule._parseVault
    module.exports.config = DotenvModule.config
    module.exports.decrypt = DotenvModule.decrypt
    module.exports.parse = DotenvModule.parse
    module.exports.populate = DotenvModule.populate

    module.exports = DotenvModule