-
Notifications
You must be signed in to change notification settings - Fork 208
Closed
Labels
kind/bugSomething isn't workingSomething isn't working
Description
Detail Bug Report
Summary
- Context: The
ReadEnvFilefunction inpkg/environment/env_files.goparses environment variable files (.envfiles) that are used to configure API keys, credentials, and other environment variables for the agent. - Bug: Keys and values are not trimmed after splitting on the
=delimiter, causing whitespace around the equals sign to be preserved in the parsed key-value pairs. - Actual vs. expected: When parsing
KEY = VALUE, the result is a key with trailing whitespace ("KEY ") and a value with leading whitespace (" VALUE"), but the expected behavior per Docker Compose specification is that "spaces before and after value are ignored" and both key and value should be"KEY"and"VALUE". - Impact: Environment variables with whitespace around the equals sign will not be found when the code looks them up, causing authentication failures for API keys and missing configuration values.
Code with bug
func ReadEnvFile(absolutePath string) ([]KeyValuePair, error) {
buf, err := os.ReadFile(absolutePath)
if err != nil {
return nil, err
}
var lines []KeyValuePair
for line := range strings.SplitSeq(string(buf), "\n") {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
k, v, ok := strings.Cut(line, "=") // <-- BUG 🔴 k and v are not trimmed after split
if !ok {
return nil, fmt.Errorf("invalid env file line: %s", line)
}
if strings.HasPrefix(v, `"`) && strings.HasSuffix(v, `"`) {
v = strings.TrimSuffix(strings.TrimPrefix(v, `"`), `"`)
}
lines = append(lines, KeyValuePair{
Key: k, // <-- BUG 🔴 key may have trailing whitespace
Value: v, // <-- BUG 🔴 value may have leading whitespace (or both if not quoted)
})
}
return lines, nil
}Example
Input .env content:
ANTHROPIC_API_KEY = sk-ant-xxxxx
Observed parsing:
- After
strings.Cut(line, "=")→k = "ANTHROPIC_API_KEY ",v = " sk-ant-xxxxx" - Stored pair:
{"ANTHROPIC_API_KEY ", " sk-ant-xxxxx"} - Lookup
env.Get(ctx, "ANTHROPIC_API_KEY")fails because the stored key has a trailing space
Expected behavior (per Docker Compose docs: “Spaces before and after value are ignored.”):
k = "ANTHROPIC_API_KEY",v = "sk-ant-xxxxx"- Lookup succeeds
Recommended fix
Trim both key and value after splitting, before quote handling, so quoted values still preserve internal whitespace:
k, v, ok := strings.Cut(line, "=")
if !ok {
return nil, fmt.Errorf("invalid env file line: %s", line)
}
k = strings.TrimSpace(k) // <-- FIX 🟢 trim key
v = strings.TrimSpace(v) // <-- FIX 🟢 trim value
if strings.HasPrefix(v, `"`) && strings.HasSuffix(v, `"`) {
v = strings.TrimSuffix(strings.TrimPrefix(v, `"`), `"`)
}This aligns parsing with .env conventions and resolves lookup failures for keys written as KEY = VALUE.
Metadata
Metadata
Assignees
Labels
kind/bugSomething isn't workingSomething isn't working