cli/internal/publish/plugins.go: nil pointer dereference in getDockerToken + dead code in GetSpecJsonScheme
CLI: Nil pointer dereference and dead code in cli/internal/publish/plugins.go
Bug 1: Nil pointer dereference in getDockerToken
File: cli/internal/publish/plugins.go:461-471
Problem: req.Header.Add is called on req before the err != nil check from http.NewRequestWithContext. If URL parsing fails or context is cancelled, req is nil and this panic with a nil pointer dereference instead of returning an error.
Current code:
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
basicAuth := base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
req.Header.Add("X-Meta-Plugin-Version", version) // panic if req is nil
req.Header.Add("X-Meta-User-Team-Name", team)
req.Header.Add("Authorization", "Basic "+basicAuth)
if err != nil {
return "", fmt.Errorf("client: could not create request: %s", err)
}Fix: Move the error check before accessing req.Header.
Bug 2: Dead code in GetSpecJsonScheme
File: cli/internal/publish/plugins.go:279-285
Problem: A second os.ReadFile call for specJsonSchemaPath can never reach its os.IsNotExist branch — the file was already confirmed to exist by the first os.ReadFile on line 276, which would have returned an error if the file didn't exist (and that error is handled). The second read is dead code.
Current code:
content, err := os.ReadFile(specJsonSchemaPath)
if err != nil {
// ...
}
// This second read can never fail with IsNotExist:
if _, err := os.ReadFile(specJsonSchemaPath); os.IsNotExist(err) {
return nil, nil
}Fix: Remove the redundant second read.
Reproduction
- For the nil pointer: call
plugin publishwith a malformed registry URL that causeshttp.NewRequestWithContextto fail — expect panic instead of graceful error. - For the dead code: code review only, no runtime impact.
Source: cloudquery/cloudquery