carlos / platform Public

boxself: monitor records take exactly one JSON value (census P2)

readStrict decodes a second value and requires io.EOF instead of
Decoder.More, which misses a trailing } or ]. Tests for trailing },
], a malformed suffix and a second value on self.json, the assignment
and ReadVerify (json.Unmarshal already refused them). The write-failure
hook now restores on its own subtest's t, in a fixed case order.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 85903698e9f05b63c514e9a284067b4a740696a0 parent 8361c75
3 files changed, +70 −9
  • internal/boxself/boxself.go +8 −3
  • internal/boxself/boxself_test.go +40 −6
  • internal/metrics/verify_linux_test.go +22 −0
diff --git a/internal/boxself/boxself.go b/internal/boxself/boxself.go
index 98d3d6366..3cc49d9db 100644
--- a/internal/boxself/boxself.go
+++ b/internal/boxself/boxself.go
@@ -24,6 +24,7 @@ import (
"encoding/json"
"errors"
"fmt"
+ "io"
"os"
"path/filepath"
"slices"
@@ -284,7 +285,7 @@ func WriteFile(path string, body []byte, max int) error {
}
// readStrict decodes the one JSON value at path into v, refusing unknown
-// fields, a wrong type and anything after the value.
+// fields, a wrong type and anything but white space after the value.
func readStrict(path string, max int, v any) error {
body, err := ReadFile(path, max)
if err != nil {
@@ -295,8 +296,12 @@ func readStrict(path string, max int, v any) error {
if err := dec.Decode(v); err != nil {
return fmt.Errorf("boxself: %s: %w", path, err)
}
- if dec.More() {
- return fmt.Errorf("boxself: %s: more than one value", path)
+ // Exactly one value: anything after it but white space, a second
+ // value, a stray } or ], or malformed bytes, is refused. (Decoder.More
+ // reports false before a closing delimiter, so it cannot say this.)
+ var extra json.RawMessage
+ if err := dec.Decode(&extra); err != io.EOF {
+ return fmt.Errorf("boxself: %s: trailing data after the record", path)
}
return nil
}
diff --git a/internal/boxself/boxself_test.go b/internal/boxself/boxself_test.go
index c49cf9e6f..12ef80331 100644
--- a/internal/boxself/boxself_test.go
+++ b/internal/boxself/boxself_test.go
@@ -82,6 +82,10 @@ func TestBoxSelfRefusesEmpty(t *testing.T) {
"no version": `{"ref":"jolly-otter"}`,
"not json": `jolly-otter`,
"trailing": `{"v":1,"ref":"jolly-otter"}{}`,
+ "trailing }": `{"v":1,"ref":"jolly-otter","at":"2026-11-11T03:00:00Z"}}`,
+ "trailing ]": `{"v":1,"ref":"jolly-otter","at":"2026-11-11T03:00:00Z"}]`,
+ "bad suffix": `{"v":1,"ref":"jolly-otter","at":"2026-11-11T03:00:00Z"}x`,
+ "second num": `{"v":1,"ref":"jolly-otter","at":"2026-11-11T03:00:00Z"} 1`,
"wrong group": `{"v":1,"ref":"jolly-otter","group":"yes"}`,
"empty file": ``,
} {
@@ -139,6 +143,10 @@ func TestAssignmentRoundTrip(t *testing.T) {
"bad winner": `{"v":1,"at":"2026-11-11T03:00:00Z","hosts":{"shop.example.com":"a/b"}}`,
"empty host": `{"v":1,"at":"2026-11-11T03:00:00Z","hosts":{"":"jolly-otter"}}`,
"not json": `[`,
+ "trailing }": `{"v":1,"at":"2026-11-11T03:00:00Z","hosts":{},"unknown":[]}}`,
+ "trailing ]": `{"v":1,"at":"2026-11-11T03:00:00Z","hosts":{},"unknown":[]}]`,
+ "bad suffix": `{"v":1,"at":"2026-11-11T03:00:00Z","hosts":{},"unknown":[]}x`,
+ "two values": `{"v":1,"at":"2026-11-11T03:00:00Z","hosts":{},"unknown":[]}{}`,
} {
t.Run(name, func(t *testing.T) {
d := t.TempDir()
@@ -158,23 +166,28 @@ func TestAssignmentRoundTrip(t *testing.T) {
// publish under an old reference or the old group mode.
func TestBoxSelfFailedWriteWithdrawsTheOldSelf(t *testing.T) {
old := Self{Ref: "g-0123456789abcdef0123", Group: true, At: selfAt}
- for name, fail := range map[string]func(dir string) Self{
+ // A fixed order, and each case's hook restored on its own subtest's t,
+ // so no injected failure outlives its case.
+ for _, tc := range []struct {
+ name string
+ fail func(t *testing.T) Self
+ }{
// The new reference is refused before anything is written.
- "refused ref": func(string) Self { return Self{Ref: "", At: selfAt} },
+ {"refused ref", func(*testing.T) Self { return Self{Ref: "", At: selfAt} }},
// The write itself fails (a full disk, say).
- "write fails": func(string) Self {
+ {"write fails", func(t *testing.T) Self {
was := write
write = func(string, []byte) error { return errors.New("no space left on device") }
t.Cleanup(func() { write = was })
return Self{Ref: "jolly-otter", At: selfAt}
- },
+ }},
} {
- t.Run(name, func(t *testing.T) {
+ t.Run(tc.name, func(t *testing.T) {
dir := t.TempDir()
if err := Write(dir, old); err != nil {
t.Fatal(err)
}
- if err := Write(dir, fail(dir)); err == nil {
+ if err := Write(dir, tc.fail(t)); err == nil {
t.Fatal("the replacement write succeeded")
}
if s, err := Read(dir); !errors.Is(err, os.ErrNotExist) {
@@ -277,3 +290,24 @@ func TestBoxSelfStaleWhenWriteAndRemovalFail(t *testing.T) {
check(t, dir, Write(dir, Self{Ref: "jolly-otter", At: now}))
})
}
+
+// TestReadStrictTakesExactlyOneValue: one JSON value and white space is
+// read; anything after the value is refused (see the refusal cases above).
+func TestReadStrictTakesExactlyOneValue(t *testing.T) {
+ dir := t.TempDir()
+ if err := os.MkdirAll(filepath.Join(dir, "monitor"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(SelfPath(dir), []byte(" {\"v\":1,\"ref\":\"jolly-otter\",\"at\":\"2026-11-11T03:00:00Z\"}\n\n"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if s, err := Read(dir); err != nil || s.Ref != "jolly-otter" {
+ t.Fatalf("one value with white space: %+v, %v", s, err)
+ }
+ if err := os.WriteFile(AssignmentPath(dir), []byte(`{"v":1,"at":"2026-11-11T03:00:00Z","hosts":{},"unknown":[]}`+"\n"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := ReadAssignment(dir); err != nil {
+ t.Fatalf("one assignment value: %v", err)
+ }
+}
diff --git a/internal/metrics/verify_linux_test.go b/internal/metrics/verify_linux_test.go
index 642224ed3..2535acb0e 100644
--- a/internal/metrics/verify_linux_test.go
+++ b/internal/metrics/verify_linux_test.go
@@ -79,3 +79,25 @@ func TestReadVerifyRefusesUnsafe(t *testing.T) {
t.Fatalf("a good record: %+v, %v", v, err)
}
}
+
+// TestReadVerifyTakesExactlyOneValue: json.Unmarshal accepts one value
+// and white space only, so a record with anything after it is refused.
+func TestReadVerifyTakesExactlyOneValue(t *testing.T) {
+ good := `{"v":2,"box":"jolly-otter","checked_at":"2026-11-11T03:00:00Z","hosts":{}}`
+ for name, body := range map[string]string{"trailing }": good + "}", "trailing ]": good + "]", "bad suffix": good + "x", "two values": good + "{}"} {
+ path := filepath.Join(t.TempDir(), "verify.json")
+ if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if v, err := ReadVerify(path, time.Now()); v != nil || err == nil {
+ t.Errorf("%s: ReadVerify = %+v, %v; want a refusal", name, v, err)
+ }
+ }
+ path := filepath.Join(t.TempDir(), "verify.json")
+ if err := os.WriteFile(path, []byte(good+"\n"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if v, err := ReadVerify(path, time.Now()); v == nil || err != nil {
+ t.Fatalf("one value with a newline: %+v, %v", v, err)
+ }
+}