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

extractVarValue assumes a quote is escaped if a backslash precedes it, even if the backslash is escaped #225

Open
monerowner opened this issue Feb 16, 2024 · 0 comments

Comments

@monerowner
Copy link

monerowner commented Feb 16, 2024

Given a .env with only one line:
FOO = "\\"

godotenv.Read(".env") will return an error because the prevChar == '\\', even though it's escaped.

// lookup quoted string terminator
for i := 1; i < len(src); i++ {
	if char := src[i]; char != quote {
		continue
	}

	// skip escaped quote symbol (\" or \', depends on quote)
	if prevChar := src[i-1]; prevChar == '\\' {
		continue
	}

	// trim quotes
	trimFunc := isCharFunc(rune(quote))
	value = string(bytes.TrimLeftFunc(bytes.TrimRightFunc(src[0:i], trimFunc), trimFunc))
	if quote == prefixDoubleQuote {
		// unescape newlines for double quote (this is compat feature)
		// and expand environment variables
		value = expandVariables(expandEscapes(value), vars)
	}

	return value, src[i+1:], nil
}

extractVarValue should instead check whether the quote symbol is preceded by an odd number of backslashes (escaped) or an even number (not escaped).

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

1 participant