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

The CSPAPIToken option can change in unexpected ways #184

Open
keep94 opened this issue Jul 10, 2024 · 0 comments
Open

The CSPAPIToken option can change in unexpected ways #184

keep94 opened this issue Jul 10, 2024 · 0 comments

Comments

@keep94
Copy link
Contributor

keep94 commented Jul 10, 2024

The CSPAPIToken option can change in unexpected ways. Ideally an option should be immutable once created. Consider this code:

cspOptions := []CSPOption{CSPOrgId("orgId")}
option := CSAPIToken("tokenName", cspOptions...)
sender := NewSender(wavefrontURL, option)
cspOptions[0] = CSPOrgId("aDifferentOrgId")
// Oops, anotherSender will use "aDifferentOrgId" because option changed as a side effect
anotherSender := NewSender(wavefrontURL, option)

Solution, the CSPAPIToken() function should take a defensive copy of the slice of CSPOptions passed in. Then it should use that defensive copy in the returned function. This would be enough:

func CSPAPIToken(cspAPIToken string, options ...CSPOption) Option {
        optionsCopy := append([]CSPOption(nil), options...)
	return func(c *configuration) {
		cspTokenAuth := auth.CSPAPIToken{
			Token:   cspAPIToken,
			BaseURL: defaultCSPBaseURL,
		}
		for _, option := range optionsCopy {
			option(&cspTokenAuth)
		}
		c.Authentication = cspTokenAuth
	}
}
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