godotenv icon indicating copy to clipboard operation
godotenv copied to clipboard

BUG: Variable not correct

Open pjebs opened this issue 4 years ago • 12 comments

In .env file:

zzz=kAjMfB<Qdq$7KJ!HbFO3o9Vi?acrxnI(eDl^GhEmSL4[2Xz*vs58Z+NY10R6Uw%y

Variable after being read back:

kAjMfB<Qdq$7KJ!HbFO3o9Vi?acrxnI(eDl^GhEmSL4[2Xz*vs58Z+NY10R6Uw%y

This is not correct:

kAjMfB<Qdq!HbFO3o9Vi?acrxnI(eDl^GhEmSL4[2Xz*vs58Z+NY10R6Uw%y
kAjMfB<Qdq$7KJ!HbFO3o9Vi?acrxnI(eDl^GhEmSL4[2Xz*vs58Z+NY10R6Uw%y

pjebs avatar Dec 04 '20 05:12 pjebs

I can observe similar issue, but my variable contains (hash) In .env file a=samplevalue#willcut After godotenv.Load() and os.Getenv a=samplevalue

grzany avatar Jan 14 '21 07:01 grzany

Also seeing the same as @grzany

technicaljones avatar Jan 14 '21 08:01 technicaljones

@grzany I believe # gets interpreted as comment.

pjebs avatar Jan 14 '21 09:01 pjebs

@pjebs yes, I suspect so

grzany avatar Jan 14 '21 13:01 grzany

This looks like intended behaviour - $7KJ is being interpreted as a variable and expanded (to nothing). $ should be escaped or removed.

LauSam09 avatar Jan 23 '21 15:01 LauSam09

How do you escape?

pjebs avatar Jan 23 '21 20:01 pjebs

How do you escape?

\$ should do it. You may need to escape the escape character itself like this \\$.

LauSam09 avatar Jan 23 '21 20:01 LauSam09

It doesn't say anywhere in docs that you need to escape. In fact, we should not have to escape at all because passwords etc may legitimately have $ characters

pjebs avatar Jan 23 '21 21:01 pjebs

Disclaimer - I'm not a maintainer of this library, but I would guess it doesn't mention it in the docs as it isn't a behaviour specific to this library. Variable expansion using $ happens both on the command line (tested in bash), and in other popular env packages such as dotenv through their dotenv-expand package.

Screenshot from 2021-01-23 21-26-21

LauSam09 avatar Jan 23 '21 21:01 LauSam09

same issue here, string with # is not properly loaded.

vx3r avatar Feb 18 '21 16:02 vx3r

I think the issue is related to the Regex operation performed in function expandVariables,

image

ENV(s): ENV1="MfNPjwKVDs273Q3J4w%k3utcuJpseWx$5EHSK9FY5wQzPP%vWme%X" ENV2="kAjMfB<Qdq$7KJ!HbFO3o9Vi?acrxnI(eDl^GhEmSL4[2Xz*vs58Z+NY10R6Uw%y"

Result: ENV1 = MfNPjwKVDs273Q3J4w%k3utcuJpseWxwQzPP%vWme%X ENV2 = kAjMfB<Qdq!HbFO3o9Vi?acrxnI(eDl^GhEmSL4[2Xz*vs58Z+NY10R6Uw%y

The Result over Commented expandVariables: ENV1 = MfNPjwKVDs273Q3J4w%k3utcuJpseWx$5EHSK9FY5wQzPP%vWme%X ENV2 = kAjMfB<Qdq$7KJ!HbFO3o9Vi?acrxnI(eDl^GhEmSL4[2Xz*vs58Z+NY10R6Uw%y

vaibhavmunjal avatar Feb 28 '21 09:02 vaibhavmunjal

Another solution: stripping off # and everything after should probably be replaced with stripping off <space># and everything after, so that a # in a string doesn't get treated as a comment: https://github.com/joho/godotenv/blob/ddf83eb33bbb136f62617a409142b74b91dbcff3/godotenv.go#L230-L232

elimisteve avatar Jun 09 '21 21:06 elimisteve