Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiline support #196

Closed
fcpauldiaz opened this issue May 13, 2017 · 15 comments
Closed

Multiline support #196

fcpauldiaz opened this issue May 13, 2017 · 15 comments

Comments

@fcpauldiaz
Copy link

I'm trying to load a multiline key but it includes the '=' character so it is dividing my key into multiple env vars.
screen shot 2017-05-13 at 12 27 49 pm

What can I do?

@maxbeatty
Copy link
Contributor

Can you provide a complete example of what you're trying? Are you surrounding the value with double quotes (see our test value)?

@mhoc
Copy link

mhoc commented Jun 5, 2017

Yeah, you can use double quotes and the newlines work properly.

CERT="-----BEGIN PUBLIC KEY-----\nabcdefg..."

There is one use case that the double quotes don't cover, and I'm curious if there is interest in accounting for this.

I embed JSON into some of my envs, specifically for arrays. This is easy to do with single quotes, or with double quotes and escaping the JSON-string double quotes inside the env. But neither of these work if I want to format my .env file a bit nicer; something like:

# Works
VALID_DOMAINS='["gmail.com","yahoo.com"]'

# Doesn't work (with single or double)
VALID_DOMAINS='[
  "gmail.com",
  "yahoo.com"
]'

# Parses Into
> process.env.VALID_DOMAINS === "["

Has this (admittedly very minor) use case been considered, and if so is there demand for a PR which investigates allowing it? I'd be happy to take a look.

@biblicalph
Copy link

@mhoc Any chance CERT="-----BEGIN PUBLIC KEY-----\nabcdefg..." can be replaced with

CERT="-----BEGIN PUBLIC KEY-----
            abcdefg..."

anytime soon? Having to convert a multiline cert into a single line value delimited by \n gets annoying quickly.

@maxbeatty
Copy link
Contributor

Closing this since it has been pretty quiet

@subversivo58
Copy link

I like this module but recently I needed to add PGP keys to the .env file and I had an enormous difficulty to do it by adding the whole key in line and adding \n to simulate a new line.

I am not sure about all the types of certificates and their declaration rules so I did not dare to send a "pull request" but, this modification solved for me.

    function parse (src) {
      var obj = {}    

      // certs
      var store_certs = [];
      var current_cert = '';
      var current_cert_key = '';
      var matching_cert = false;    

      // convert Buffers before splitting into lines and processing
      src.toString().split('\n').forEach(function (line) {
        // line length
        var line_len = line ? line.length : 0;    

        // matching "KEY' and 'VAL' in 'KEY=VAL'
        var keyValueArr = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/)
        // check if init cert declaration
        if ( /-----BEGIN/g.test(line) ) {
            matching_cert = true;
        }
        //
        if ( matching_cert ) {
            // check if end cert declaration
            if ( /-----END/g.test(line) && line_len > 0 && line.substring(line_len-5, line_len) === '-----' ) {
                current_cert += line + '\n';
                // strore
                store_certs.push({
                    key: current_cert_key,
                    value: current_cert
                });
                current_cert = '';
                current_cert_key = '';
                matching_cert = false;
            } else {
                if ( keyValueArr === null ) {
                    current_cert += line + '\n';
                } else {
                    current_cert_key = keyValueArr[1];
                    var value = keyValueArr[2] ? keyValueArr[2] : '';
                    current_cert += value + '\n';
                }
            }
        } else {
            // matched?
            if ( keyValueArr != null ) {
                var key = keyValueArr[1]
                // default undefined or missing values to empty string
                var value = keyValueArr[2] ? keyValueArr[2] : ''
                // expand newlines in quoted values
                var len = value ? value.length : 0
                if ( len > 0 && value.charAt(0) === '"' && value.charAt(len - 1) === '"' ) {
                    value = value.replace(/\\n/gm, '\n')
                }
                // remove any surrounding quotes and extra spaces
                value = value.replace(/(^['"]|['"]$)/g, '').trim()
                obj[key] = value
            }
        }
      });
      // if stored certs
      if ( store_certs.length > 0 ) {
          store_certs.forEach((item, i, array)=>{
              obj[item.key] = item.value;
          });
      }    

      return obj
    }

In me .env file e.g:

TOKEN_UNSUBSCRIBE=14wcn9tckujru2xirq8k38ulstgv7f9m1zg0te8gj9g
PRIV_KEY=-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: OpenPGP.js v2.5.5
Comment: https://openpgpjs.org

xcaGBFlniGYBD/9vTm8Vbdlp72l67hX/3g8mGCe2zq1l+KIq83mwMRW0Y7Um
... [omit full certificate content]
mEGD32E1Fym2dllgE4zNW20OJXw+Fd3XgI/pDxg=
=WtrI
-----END PGP PRIVATE KEY BLOCK-----
PUB_KEY=-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: OpenPGP.js v2.5.5
Comment: https://openpgpjs.org

xsFNBFlniGYBD/9vTm8Vbdlp72l67hX/3g8mGCe2zq1l+KIq83mwMRW0Y7Um
... [omit full certificate content]
fD4V3deAj+kPGA==
=nm8/
-----END PGP PUBLIC KEY BLOCK-----

And I use it normally:

    console.log(process.env.TOKEN_UNSUBSCRIBE) // 14wcn9tckujru2xirq8k38ulstgv7f9m1zg0te8gj9g

    console.log(process.env.PRIV_KEY) // output complete private key

    console.log(process.env.PRIV_KEY) // output complete private key

@caub
Copy link

caub commented Apr 3, 2018

@mhoc If it's a (possibly multiline) json, it won't be fun

single quotes maybe, ideally backticks

FOO=bar
SOME_KEY= ```
    {"foo": "bar", "qux": 1}
```
BIP_BOP='hello world'

@arcseldon
Copy link

See here too - #28 (comment)

@george-norris-salesforce
Copy link

george-norris-salesforce commented Aug 22, 2018

Currently running into the same issue. The solution to add \n is ugly.

@yidongw
Copy link

yidongw commented Oct 5, 2018

adding \n is tedious and ugly...

@pizzarob
Copy link

👍

@AChris07
Copy link

Bump. I feel the behavior for the library should at least match similar behavior for Unix env variables. For certs, I can manage this perfectly as a normal env variable, but having to implement \n for dotenv is ugly, tedious, and not what I expected. Had to fumble a fair bit to notice it was an issue with dotenv itself.

@silverwind
Copy link

silverwind commented Aug 18, 2020

I'd suggest implementing this format from the Ruby implementation:

PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
...
HkVN9...
...
-----END DSA PRIVATE KEY-----"

@cherviakovtaskworld
Copy link

cherviakovtaskworld commented Mar 23, 2021

Can this issue be re-opened? It is still a problem and using \n is sure quite troublesome. @motdotla @maxbeatty

@sb-LukeM
Copy link

sb-LukeM commented Mar 25, 2021

There's a PR open to address this, here: #486. Hopefully it can be merged soon! @cherviakovtaskworld

@motdotla
Copy link
Owner

motdotla commented Nov 8, 2023

This was added a while back but never comment here: https://github.com/motdotla/dotenv#multiline-values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests