lease: ReadHead refuses trailing delimiters after head.json
Decoder.More() is false before a closing "]" or "}", so a valid head followed by one read as found with no error. ReadHead now uses json.Unmarshal, which requires the whole body to be one JSON value. Tests refuse a trailing "]", "}" and whitespace then "]", and accept trailing whitespace alone.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+16
−6
internal/lease/head.go+3 −6internal/lease/head_test.go+13 −0
CI failed — run details
Intent
diff --git a/internal/lease/head.go b/internal/lease/head.go| index 3f03c435f..3477cec21 100644 |
| --- a/internal/lease/head.go |
| +++ b/internal/lease/head.go |
| @@ -1,7 +1,6 @@ |
| package lease |
| import ( |
| - "bytes" |
| "context" |
| "encoding/json" |
| "errors" |
| @@ -105,13 +104,11 @@ func ReadHead(ctx context.Context, st release.Store, loc string) (h Head, etag s |
| if err != nil { |
| return Head{}, "", false, fmt.Errorf("head.json: read %s: %w", key, err) |
| } |
| - dec := json.NewDecoder(bytes.NewReader(body)) |
| - if err := dec.Decode(&h); err != nil { |
| + // Unmarshal checks the whole body is one JSON value: trailing data, |
| + // even a lone "]" or "}", is an error (Decoder.More misses those). |
| + if err := json.Unmarshal(body, &h); err != nil { |
| return Head{}, "", false, fmt.Errorf("head.json: decode %s: %w", key, err) |
| } |
| - if dec.More() { |
| - return Head{}, "", false, fmt.Errorf("head.json: decode %s: data after the object", key) |
| - } |
| if err := h.validate(); err != nil { |
| return Head{}, "", false, fmt.Errorf("%s: %w", key, err) |
| } |
diff --git a/internal/lease/head_test.go b/internal/lease/head_test.go| index 435cf803a..10e51a0aa 100644 |
| --- a/internal/lease/head_test.go |
| +++ b/internal/lease/head_test.go |
| @@ -130,6 +130,9 @@ func TestReadHeadRefusesBadBodies(t *testing.T) { |
| "not json": []byte("{"), |
| "empty body": {}, |
| "trailing data": append(append([]byte{}, good...), []byte(`{}`)...), |
| + "trailing ]": append(append([]byte{}, good...), ']'), |
| + "trailing }": append(append([]byte{}, good...), '}'), |
| + "trailing ws ]": append(append([]byte{}, good...), []byte(" \n]")...), |
| } { |
| t.Run(name, func(t *testing.T) { |
| st := release.NewDirStore(t.TempDir()) |
| @@ -141,6 +144,16 @@ func TestReadHeadRefusesBadBodies(t *testing.T) { |
| } |
| }) |
| } |
| + // Trailing whitespace after the object is still one valid JSON value. |
| + { |
| + st := release.NewDirStore(t.TempDir()) |
| + if _, err := st.Put(ctx, headLoc+"/head.json", append(append([]byte{}, good...), []byte(" \n\t\r\n")...), ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got, _, found, err := lease.ReadHead(ctx, st, headLoc); err != nil || !found || !reflect.DeepEqual(got, sampleHead()) { |
| + t.Fatalf("ReadHead(trailing whitespace) = %+v, %v, %v; want the head", got, found, err) |
| + } |
| + } |
| // The root generation is a valid predecessor (the migration) and an |
| // empty one is a first wake. |
| for _, pred := range []string{replicagen.Root, ""} { |