Skip to content

Commit c0777a9

Browse files
committed
Reject invalid scan barrier keys
1 parent d2680a8 commit c0777a9

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

examples/kv/cmd/kvnode/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,12 @@ func (s *service) handleScan(w http.ResponseWriter, r *http.Request) {
257257
parts := strings.Split(raw, ",")
258258
keys := make([][]byte, 0, len(parts))
259259
for _, part := range parts {
260-
if part == "" {
260+
key := []byte(part)
261+
if err := kv.ValidateKey(key); err != nil {
261262
http.Error(w, "bad barrier key", http.StatusBadRequest)
262263
return
263264
}
264-
keys = append(keys, []byte(part))
265+
keys = append(keys, key)
265266
}
266267
if err := s.waitForKeys(r.Context(), keys...); err != nil {
267268
http.Error(w, err.Error(), http.StatusServiceUnavailable)

examples/kv/cmd/kvnode/main_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ func TestHandleScanRejectsBadQueryAndMethod(t *testing.T) {
242242
{name: "wrong method", method: http.MethodPost, target: "/scan", want: http.StatusMethodNotAllowed},
243243
{name: "bad limit", method: http.MethodGet, target: "/scan?limit=many", want: http.StatusBadRequest},
244244
{name: "bad reverse", method: http.MethodGet, target: "/scan?reverse=sideways", want: http.StatusBadRequest},
245+
{name: "empty barrier part", method: http.MethodGet, target: "/scan?barrier=alpha,,beta", want: http.StatusBadRequest},
246+
{name: "embedded separator barrier key", method: http.MethodGet, target: "/scan?barrier=alpha%00beta", want: http.StatusBadRequest},
245247
}
246248
for _, tc := range tests {
247249
t.Run(tc.name, func(t *testing.T) {
@@ -251,6 +253,12 @@ func TestHandleScanRejectsBadQueryAndMethod(t *testing.T) {
251253
if rr.Code != tc.want {
252254
t.Fatalf("status=%d body=%q", rr.Code, rr.Body.String())
253255
}
256+
if tc.want == http.StatusBadRequest {
257+
status := s.node.Status()
258+
if len(status.Instances) != 0 || len(status.Executed) != 0 {
259+
t.Fatalf("node status after rejected request: instances=%v executed=%v", status.Instances, status.Executed)
260+
}
261+
}
254262
})
255263
}
256264
}

0 commit comments

Comments
 (0)