Skip to content
This repository was archived by the owner on Jan 16, 2026. It is now read-only.
This repository was archived by the owner on Jan 16, 2026. It is now read-only.

The CSPAPIToken option can change in unexpected ways #184

@keep94

Description

@keep94

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
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions