Skip to content

Commit cda9a4f

Browse files
committed
fix(api): registry success payloads must not deref a nil entry
redactedRegistrySummary (#1164) dereferences its argument, and all three registry handlers — add, edit and remove — called it on the SUCCESS path with no nil check. A controller reporting success without an entry therefore panicked inside the handler, and chi's recoverer turned that into a bare 500 with an EMPTY body: the caller saw an unexplained server error on a request that had actually succeeded, and the cause survived only as a stack in the log. Not hypothetical. MockServerController already returns (nil, nil, nil) from these methods, so every test driving those routes through it hit the panic. It surfaced as an intermittent windows-amd64 CI failure where the recovered panic escaped the test process — intermittent because the recoverer usually swallows it, which is exactly why it went unnoticed: the package stayed green while three routes were answering 500 to successful requests. The guard lives in the renderer rather than at the three call sites so a fourth caller cannot reintroduce it. A nil entry now renders the zero summary, which reports no identity and — deliberately — Trusted=false: nothing unknown may be described as trusted. Tests assert both that the routes do not panic and that the body is NON-EMPTY, since an empty 500 is the signature of a recovered panic and a test that only checked the status code would have passed throughout.
1 parent 7d8b87b commit cda9a4f

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

‎internal/httpapi/diagnostics_per_server.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,19 @@ func viewString(view map[string]interface{}, key, fallback string) string {
157157
// than persisting it over the credential, which is the same bind-or-refuse
158158
// answer the server write path gives.
159159
func redactedRegistrySummary(entry *config.RegistryEntry) contracts.RegistrySummary {
160+
// Nil-tolerant on purpose. This renders the SUCCESS payload for three
161+
// registry handlers, each of which reaches it whenever the controller
162+
// returned no error — and a controller may legitimately report success
163+
// without an entry. Dereferencing there panicked inside the handler, which
164+
// chi's recoverer turned into a bare 500 with an EMPTY body: the caller saw
165+
// an unexplained server error on a request that had in fact succeeded, and
166+
// the real cause only appeared as a stack in the log.
167+
//
168+
// The guard lives here rather than at the three call sites so a fourth
169+
// caller cannot reintroduce it.
170+
if entry == nil {
171+
return contracts.RegistrySummary{}
172+
}
160173
return contracts.RegistrySummary{
161174
ID: entry.ID,
162175
Name: entry.Name,
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package httpapi
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"strings"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
"go.uber.org/zap"
12+
)
13+
14+
// redactedRegistrySummary dereferences its argument, and all three registry
15+
// handlers called it on the SUCCESS path without checking the entry for nil.
16+
// A controller that reports success with no entry therefore panicked inside the
17+
// handler; chi's recoverer turned that into a bare 500 with an empty body, so
18+
// the failure looked like an unrelated server error rather than a nil deref.
19+
//
20+
// This is not hypothetical plumbing: MockServerController already returns
21+
// (nil, nil, nil) from these methods, so every caller exercising these routes
22+
// through it hit the panic — intermittently visible as a Windows CI failure
23+
// where the recovered panic escaped the test process.
24+
func TestRegistryHandlers_NilEntryOnSuccessDoesNotPanic(t *testing.T) {
25+
const adminKey = "admin-secret"
26+
body := `{"name":"n","url":"https://example.com","servers_url":"https://example.com/s"}`
27+
28+
for _, rt := range []struct {
29+
name, method, path, body string
30+
}{
31+
{"add", http.MethodPost, "/api/v1/registries", body},
32+
{"edit", http.MethodPut, "/api/v1/registries/reg1", body},
33+
{"remove", http.MethodDelete, "/api/v1/registries/reg1", ""},
34+
} {
35+
t.Run(rt.name, func(t *testing.T) {
36+
ctrl := &adminConfigController{ServerController: &MockServerController{}, apiKey: adminKey}
37+
srv := NewServer(ctrl, zap.NewNop().Sugar(), nil)
38+
39+
req := httptest.NewRequest(rt.method, rt.path, strings.NewReader(rt.body))
40+
req.Header.Set("X-API-Key", adminKey)
41+
req.Header.Set("Content-Type", "application/json")
42+
w := httptest.NewRecorder()
43+
44+
require.NotPanics(t, func() { srv.ServeHTTP(w, req) },
45+
"a nil entry on the success path must not deref")
46+
47+
// A recovered panic renders as a 500 with an EMPTY body. Whatever
48+
// this route decides to answer, it has to actually answer.
49+
assert.NotEmpty(t, w.Body.String(),
50+
"an empty 500 body is the signature of a recovered panic")
51+
})
52+
}
53+
}
54+
55+
// The renderer itself must tolerate nil rather than relying on every caller to
56+
// remember — it is called from three places and dereferences seven fields.
57+
func TestRedactedRegistrySummary_NilEntryIsZeroValue(t *testing.T) {
58+
require.NotPanics(t, func() {
59+
got := redactedRegistrySummary(nil)
60+
assert.Empty(t, got.ID, "a nil entry has no identity to report")
61+
assert.Empty(t, got.URL)
62+
assert.False(t, got.Trusted, "nothing unknown may be reported as trusted")
63+
})
64+
}

0 commit comments

Comments
 (0)