stackbuilders / dotenv-hs

Load environment variables from dotenv files for Haskell
https://hackage.haskell.org/package/dotenv
MIT License
65 stars 14 forks source link

parseFile doesn't seem to escape characters as expected #186

Closed Martinsos closed 3 months ago

Martinsos commented 12 months ago

Here is an .env file I used to illustrate the potential issue:

TEST1="a\nb"
TEST2='a\nb'
TEST3=a\nb
TEST4="a\\nb"

Now, when I run parseFile "testenv" in ghci, I get the following output: image

Formatted:

[ ("TEST1","anb"),
  ("TEST2","anb"),
  ("TEST3","anb"),
  ("TEST4","a\\nb"),
]

This is not the output I would expect though.

Output I would expect is the following:

[ ("TEST1","a\nb"),
  ("TEST2","a\nb"),
  ("TEST3","a\nb"),
  ("TEST4","a\\nb")  -- OK I am not 100% sure what to expect here, this might be ok.
]

Is my expectation wrong here, or is this potentially a bug in the Dotenv?

I am using dotenv ^>= 0.10.0.

Thanks!

CristhianMotoche commented 11 months ago

Hi @Martinsos Thanks for reporting that. I think what you mention makes sense but I need to take a look at the parser to clarify if this is expected and what would be the correct way to handle breaking lines.

Martinsos commented 11 months ago

Thanks @CristhianMotoche !

I don't care much about the "\n" case, but "\n" being parsed as "n" is quite tricky, it makes it impossible to enter a string with a newline inside the env (and for example, Google gives you PEM key as a secret and it has newlines!). The only solution we found so far was to encode it with base64, putting it in .env like that, and then decoding it after being red for dotenv.

Let me know if I can help in some way!

CristhianMotoche commented 10 months ago

Hey @Martinsos I was able to reproduce the issue. Indeed, it seems something wrong in the parser. FWIW, I think you can add new lines instead of the \n character to represent new lines. For example:

Given this .env:

BAR="a
b"

parseFile will return:

ghci> parseFile ".env"
[("BAR","a\nb")]

I hope that works as a workaround until we find a solution for this issue.