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

loadFromEnv panics #3892

Open
tjayrush opened this issue Sep 23, 2024 · 0 comments
Open

loadFromEnv panics #3892

tjayrush opened this issue Sep 23, 2024 · 0 comments

Comments

@tjayrush
Copy link
Member

The following code fails with invalid input:

Invalid input here causes panic. Set an invalid environment variable (TB_CHAINS_MAINNETRPC
for example -- it should be TB_CHAINS_MAINNET_RPCPROVIDER) and run the tests to see the panic.

// loadFromEnv loads configuration from environment variables
func loadFromEnv(prefix string, destination *ConfigFile) (err error) {
	// First we get all env variables then filter by prefix and finally parse the values
	envs := os.Environ()
	for i := 0; i < len(envs); i++ {
		// Turn VAR=value into []string{"VAR", "value"}
		parsed := strings.Split(envs[i], "=")
		if !strings.HasPrefix(parsed[0], prefix) {
			continue
		}

		// Turn PARENT_CHILD into []string{"PARENT", "CHILD"}
		path := strings.Split(parsed[0][len(prefix):], "_")
		if err := setByPath(destination, path, parsed[1]); err != nil {
			return err
		}
	}
	return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant