Skip to content

Commit 08a21df

Browse files
authored
env: fix a bunch of escaping/parsing edge cases (#308)
* Handle standard escape sequences based on XSI/XBD https://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html * Fix parsing with escaped quotes at end of quoted value * Handle key whose name starts with `export` (require whitespace between `export` keyword when stripping) * Fix parsing of unquoted values with embedded `#` (require a space before starting an inline comment) * Use correct parser methods throughout all test cases * Eliminate a lot of unused code that was not working right Signed-off-by: Milas Bowman <[email protected]>
1 parent be0dada commit 08a21df

File tree

5 files changed

+121
-755
lines changed

5 files changed

+121
-755
lines changed

dotenv/godotenv.go

Lines changed: 21 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@
44
//
55
// The TL;DR is that you make a .env file that looks something like
66
//
7-
// SOME_ENV_VAR=somevalue
7+
// SOME_ENV_VAR=somevalue
88
//
99
// and then in your go code you can call
1010
//
11-
// godotenv.Load()
11+
// godotenv.Load()
1212
//
1313
// and all the env vars declared in .env will be available through os.Getenv("SOME_ENV_VAR")
1414
package dotenv
1515

1616
import (
1717
"bytes"
18-
"errors"
1918
"fmt"
2019
"io"
2120
"os"
@@ -28,10 +27,10 @@ import (
2827
"github.com/compose-spec/compose-go/template"
2928
)
3029

31-
const doubleQuoteSpecialChars = "\\\n\r\"!$`"
32-
3330
var utf8BOM = []byte("\uFEFF")
3431

32+
var startsWithDigitRegex = regexp.MustCompile(`^\s*\d.*`) // Keys starting with numbers are ignored
33+
3534
// LookupFn represents a lookup function to resolve variables from
3635
type LookupFn func(string) (string, bool)
3736

@@ -60,13 +59,13 @@ func ParseWithLookup(r io.Reader, lookupFn LookupFn) (map[string]string, error)
6059

6160
// Load will read your env file(s) and load them into ENV for this process.
6261
//
63-
// Call this function as close as possible to the start of your program (ideally in main)
62+
// Call this function as close as possible to the start of your program (ideally in main).
6463
//
65-
// If you call Load without any args it will default to loading .env in the current path
64+
// If you call Load without any args it will default to loading .env in the current path.
6665
//
67-
// You can otherwise tell it which files to load (there can be more than one) like
66+
// You can otherwise tell it which files to load (there can be more than one) like:
6867
//
69-
// godotenv.Load("fileone", "filetwo")
68+
// godotenv.Load("fileone", "filetwo")
7069
//
7170
// It's important to note that it WILL NOT OVERRIDE an env variable that already exists - consider the .env file to set dev vars or sensible defaults
7271
func Load(filenames ...string) error {
@@ -75,13 +74,13 @@ func Load(filenames ...string) error {
7574

7675
// Overload will read your env file(s) and load them into ENV for this process.
7776
//
78-
// Call this function as close as possible to the start of your program (ideally in main)
77+
// Call this function as close as possible to the start of your program (ideally in main).
7978
//
80-
// If you call Overload without any args it will default to loading .env in the current path
79+
// If you call Overload without any args it will default to loading .env in the current path.
8180
//
82-
// You can otherwise tell it which files to load (there can be more than one) like
81+
// You can otherwise tell it which files to load (there can be more than one) like:
8382
//
84-
// godotenv.Overload("fileone", "filetwo")
83+
// godotenv.Overload("fileone", "filetwo")
8584
//
8685
// It's important to note this WILL OVERRIDE an env variable that already exists - consider the .env file to forcefilly set all vars.
8786
func Overload(filenames ...string) error {
@@ -99,8 +98,6 @@ func load(overload bool, filenames ...string) error {
9998
return nil
10099
}
101100

102-
var startsWithDigitRegex = regexp.MustCompile(`^\s*\d.*`) // Keys starting with numbers are ignored
103-
104101
// ReadWithLookup gets all env vars from the files and/or lookup function and return values as
105102
// a map rather than automatically writing values into env
106103
func ReadWithLookup(lookupFn LookupFn, filenames ...string) (map[string]string, error) {
@@ -155,6 +152,8 @@ func UnmarshalBytesWithLookup(src []byte, lookupFn LookupFn) (map[string]string,
155152
//
156153
// If you want more fine grained control over your command it's recommended
157154
// that you use `Load()` or `Read()` and the `os/exec` package yourself.
155+
//
156+
// Deprecated: Use the `os/exec` package directly.
158157
func Exec(filenames []string, cmd string, cmdArgs []string) error {
159158
if err := Load(filenames...); err != nil {
160159
return err
@@ -168,7 +167,10 @@ func Exec(filenames []string, cmd string, cmdArgs []string) error {
168167
}
169168

170169
// Write serializes the given environment and writes it to a file
170+
//
171+
// Deprecated: The serialization functions are untested and unmaintained.
171172
func Write(envMap map[string]string, filename string) error {
173+
//goland:noinspection GoDeprecation
172174
content, err := Marshal(envMap)
173175
if err != nil {
174176
return err
@@ -187,6 +189,8 @@ func Write(envMap map[string]string, filename string) error {
187189

188190
// Marshal outputs the given environment as a dotenv-formatted environment file.
189191
// Each line is in the format: KEY="VALUE" where VALUE is backslash-escaped.
192+
//
193+
// Deprecated: The serialization functions are untested and unmaintained.
190194
func Marshal(envMap map[string]string) (string, error) {
191195
lines := make([]string, 0, len(envMap))
192196
for k, v := range envMap {
@@ -239,104 +243,6 @@ func readFile(filename string, lookupFn LookupFn) (map[string]string, error) {
239243
return ParseWithLookup(file, lookupFn)
240244
}
241245

242-
var exportRegex = regexp.MustCompile(`^\s*(?:export\s+)?(.*?)\s*$`)
243-
244-
func parseLine(line string, envMap map[string]string) (string, string, error) {
245-
return parseLineWithLookup(line, envMap, nil)
246-
}
247-
func parseLineWithLookup(line string, envMap map[string]string, lookupFn LookupFn) (string, string, error) {
248-
if line == "" {
249-
return "", "", errors.New("zero length string")
250-
}
251-
252-
// ditch the comments (but keep quoted hashes)
253-
if strings.HasPrefix(strings.TrimSpace(line), "#") || strings.Contains(line, " #") {
254-
segmentsBetweenHashes := strings.Split(line, "#")
255-
quotesAreOpen := false
256-
var segmentsToKeep []string
257-
for _, segment := range segmentsBetweenHashes {
258-
if strings.Count(segment, "\"") == 1 || strings.Count(segment, "'") == 1 {
259-
if quotesAreOpen {
260-
segmentsToKeep = append(segmentsToKeep, segment)
261-
}
262-
quotesAreOpen = !quotesAreOpen
263-
}
264-
265-
if len(segmentsToKeep) == 0 || quotesAreOpen {
266-
segmentsToKeep = append(segmentsToKeep, segment)
267-
}
268-
}
269-
270-
line = strings.Join(segmentsToKeep, "#")
271-
}
272-
273-
firstEquals := strings.Index(line, "=")
274-
firstColon := strings.Index(line, ":")
275-
splitString := strings.SplitN(line, "=", 2)
276-
if firstColon != -1 && (firstColon < firstEquals || firstEquals == -1) {
277-
// This is a yaml-style line
278-
splitString = strings.SplitN(line, ":", 2)
279-
}
280-
281-
if len(splitString) != 2 {
282-
return "", "", errors.New("can't separate key from value")
283-
}
284-
key := exportRegex.ReplaceAllString(splitString[0], "$1")
285-
286-
// Parse the value
287-
value := parseValue(splitString[1], envMap, lookupFn)
288-
289-
return key, value, nil
290-
}
291-
292-
var (
293-
singleQuotesRegex = regexp.MustCompile(`\A'(.*)'\z`)
294-
doubleQuotesRegex = regexp.MustCompile(`\A"(.*)"\z`)
295-
escapeRegex = regexp.MustCompile(`\\.`)
296-
unescapeCharsRegex = regexp.MustCompile(`\\([^$])`)
297-
)
298-
299-
func parseValue(value string, envMap map[string]string, lookupFn LookupFn) string {
300-
301-
// trim
302-
value = strings.Trim(value, " ")
303-
304-
// check if we've got quoted values or possible escapes
305-
if len(value) > 1 {
306-
singleQuotes := singleQuotesRegex.FindStringSubmatch(value)
307-
308-
doubleQuotes := doubleQuotesRegex.FindStringSubmatch(value)
309-
310-
if singleQuotes != nil || doubleQuotes != nil {
311-
// pull the quotes off the edges
312-
value = value[1 : len(value)-1]
313-
}
314-
315-
if doubleQuotes != nil {
316-
// expand newlines
317-
value = escapeRegex.ReplaceAllStringFunc(value, func(match string) string {
318-
c := strings.TrimPrefix(match, `\`)
319-
switch c {
320-
case "n":
321-
return "\n"
322-
case "r":
323-
return "\r"
324-
default:
325-
return match
326-
}
327-
})
328-
// unescape characters
329-
value = unescapeCharsRegex.ReplaceAllString(value, "$1")
330-
}
331-
332-
if singleQuotes == nil {
333-
value, _ = expandVariables(value, envMap, lookupFn)
334-
}
335-
}
336-
337-
return value
338-
}
339-
340246
func expandVariables(value string, envMap map[string]string, lookupFn LookupFn) (string, error) {
341247
retVal, err := template.Substitute(value, func(k string) (string, bool) {
342248
if v, ok := envMap[k]; ok {
@@ -350,7 +256,9 @@ func expandVariables(value string, envMap map[string]string, lookupFn LookupFn)
350256
return retVal, nil
351257
}
352258

259+
// Deprecated: only used by unsupported/untested code for Marshal/Write.
353260
func doubleQuoteEscape(line string) string {
261+
const doubleQuoteSpecialChars = "\\\n\r\"!$`"
354262
for _, c := range doubleQuoteSpecialChars {
355263
toReplace := "\\" + string(c)
356264
if c == '\n' {

0 commit comments

Comments
 (0)