Skip to content

Commit 3123b5b

Browse files
dan-vclaude
andcommitted
Fix config loader to avoid parsing binary files as YAML
- Search for specific .yaml/.yml files instead of any file named lambda-nat-proxy - Prevents binary in current directory from being parsed as config - Only attempt to read config if actual config file found - Maintains all existing config search paths with proper extensions 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent e91e84a commit 3123b5b

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

internal/config/loader.go

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,47 @@ func LoadCLIConfig(configPath string) (*CLIConfig, error) {
1515

1616
// Initialize viper
1717
v := viper.New()
18-
v.SetConfigName("lambda-nat-proxy")
1918
v.SetConfigType("yaml")
2019

21-
// Add search paths
20+
// Determine config file to use
21+
var foundConfig bool
2222
if configPath != "" {
2323
// Use specific config file path if provided
2424
v.SetConfigFile(configPath)
25+
foundConfig = true
2526
} else {
26-
// Search in XDG-compliant locations
27-
v.AddConfigPath(".") // Current directory
28-
v.AddConfigPath(filepath.Join(xdg.ConfigHome, "lambda-nat-proxy")) // User config (~/.config/lambda-nat-proxy)
29-
v.AddConfigPath("/etc/lambda-nat-proxy") // System directory
27+
// Search for specific config files (not just any file named lambda-nat-proxy)
28+
configFiles := []string{
29+
"./lambda-nat-proxy.yaml", // Current directory
30+
"./lambda-nat-proxy.yml", // Current directory (alt extension)
31+
filepath.Join(xdg.ConfigHome, "lambda-nat-proxy", "lambda-nat-proxy.yaml"), // User config
32+
filepath.Join(xdg.ConfigHome, "lambda-nat-proxy", "lambda-nat-proxy.yml"), // User config (alt)
33+
"/etc/lambda-nat-proxy/lambda-nat-proxy.yaml", // System directory
34+
"/etc/lambda-nat-proxy/lambda-nat-proxy.yml", // System directory (alt)
35+
}
3036

3137
// Also check XDG config dirs
3238
for _, dir := range xdg.ConfigDirs {
33-
v.AddConfigPath(filepath.Join(dir, "lambda-nat-proxy"))
39+
configFiles = append(configFiles,
40+
filepath.Join(dir, "lambda-nat-proxy", "lambda-nat-proxy.yaml"),
41+
filepath.Join(dir, "lambda-nat-proxy", "lambda-nat-proxy.yml"),
42+
)
43+
}
44+
45+
// Find the first existing config file
46+
for _, configFile := range configFiles {
47+
if _, err := os.Stat(configFile); err == nil {
48+
v.SetConfigFile(configFile)
49+
foundConfig = true
50+
break
51+
}
52+
}
53+
}
54+
55+
// Try to read config file (only if one was found)
56+
if foundConfig {
57+
if err := v.ReadInConfig(); err != nil {
58+
return nil, fmt.Errorf("error reading config file: %w", err)
3459
}
3560
}
3661

@@ -44,15 +69,6 @@ func LoadCLIConfig(configPath string) (*CLIConfig, error) {
4469
v.BindEnv("deployment.mode", "MODE")
4570
v.BindEnv("proxy.port", "SOCKS5_PORT")
4671

47-
// Try to read config file
48-
if err := v.ReadInConfig(); err != nil {
49-
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
50-
// Config file was found but another error was produced
51-
return nil, fmt.Errorf("error reading config file: %w", err)
52-
}
53-
// Config file not found, continue with defaults and env vars
54-
}
55-
5672
// Unmarshal into our config struct
5773
if err := v.Unmarshal(cfg); err != nil {
5874
return nil, fmt.Errorf("error unmarshaling config: %w", err)

0 commit comments

Comments
 (0)