Files
w3cjandClaude Opus 5.5 a7cc12521e test(fuzz): fuzz targets with properties, fuzzed nightly
Twenty-three new fuzz targets, built-in Go fuzzing, each checking a
property rather than only that nothing panics: parsers of what a VM or
the network sends fail closed and keep to their caps, a secret goes only
where it's bound, and no planted secret survives a redaction.

- internal/proxy: FuzzCursorFrames (passes only what came; no message
  over frameMax or holding a JWT's start, decompressed and unescaped),
  FuzzInject (a binding's value only to a host it names, judged apart
  from the matcher in NormHost's form, for its path and methods, in its
  own header, which held the exact placeholder once; other headers
  untouched), FuzzParseBindings, FuzzLinkList, FuzzParseUpstream (errors
  hold no more of the URL than its scheme), FuzzNoProxy, FuzzParseNarinfo
  (only cache.nixos.org-1's signed narinfo is taken).
- internal/agentlogin: FuzzParseTokenRequest, FuzzRedact (secrets planted
  eight ways in JSON and TOML settings), FuzzCapture (the VM's reply holds
  none of the endpoint's tokens).
- internal/redact: FuzzSplit (a password written in parts through Split,
  as the run log writes it, never survives).
- internal/jsonc: FuzzStrip, differential against encoding/json.
- internal/upgrade: FuzzParsePublicKey, FuzzVerify, FuzzEntryName,
  FuzzUnpack.
- internal/vmssh: FuzzParseHostKeys, FuzzParsePorts.
- internal/vm: FuzzParseCloneState, FuzzParseInit, FuzzParseKept,
  FuzzParseLFSPointer.
- internal/desktop: FuzzHandshakeVM.

What they found, each fixed with a regression test:
- NO_PROXY: an entry with port 0 (git.test:0) was read as the host on
  every port, and git.test:080 or :+80 as port 80, which Go's clients
  match against nothing; an entry whose port no request has, or that
  isn't plain decimal, now matches nothing (TestNoProxy,
  testdata/fuzz/FuzzNoProxy). FuzzHostSites' NO_PROXY site allows this
  difference from its frozen copy, and the design note lists it.
- A JSON token request with more after its object was judged by its
  first object; it's refused now (TestCaptureRefuses).

The Fuzz workflow runs nightly rather than two minutes weekly: five
minutes a target, eight at a time, each target's corpus restored from
and saved to a cache of its own (a failing input added to it, so it
fails nightly until fixed), new inputs minimized in at most 1,000 runs,
and one issue, "Fuzzing found failing inputs", opened, commented on when
the failing targets change and closed on a clean night, by a separate
job with issues: write that runs no repo code, on main alone. Target
names must be unique across packages. bump.sh moves an action in a
folder of its repo (actions/cache/restore, /save) with its repo's
releases.

Docs: Contributing's Fuzzing (running a target, the nightly run, every
target's property), Maintaining's Fuzzing failures and its actions bump,
the proxy README, the NO_PROXY and token request facts in How it works'
network page and the design note.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-15 22:04:31 -07:00

117 lines
3.7 KiB
Go

package jsonc
import (
"bytes"
"encoding/json"
"reflect"
"strings"
"testing"
)
func TestStrip(t *testing.T) {
for _, tc := range []struct{ name, in, want string }{
{"line comment", "{\"a\": 1 // c\n}", "{\"a\": 1 \n}"},
{"block comment", "{/* x\ny */\"a\": 1}", "{ \n \"a\": 1}"},
{"trailing commas", `{"a": [1, 2,], "b": {"c": 3,},}`, `{"a": [1, 2 ], "b": {"c": 3 } }`},
{"comma before a comment", "{\"a\": 1, // c\n}", "{\"a\": 1 \n}"},
{"slashes in strings", `{"a": "http://x/*y*/", "b": "q\"//"}`, `{"a": "http://x/*y*/", "b": "q\"//"}`},
{"comma in a string", `{"a": ",}"}`, `{"a": ",}"}`},
{"escaped backslash", `{"a": "\\", "b": 1,}`, `{"a": "\\", "b": 1 }`},
{"bom", "\xef\xbb\xbf{}", " {}"},
{"unterminated block", "{} /* x", "{} "},
} {
t.Run(tc.name, func(t *testing.T) {
got := string(Strip([]byte(tc.in)))
if got != tc.want {
t.Errorf("Strip(%q) = %q, want %q", tc.in, got, tc.want)
}
if len(got) != len(tc.in) {
t.Errorf("length changed: %d, want %d", len(got), len(tc.in))
}
})
}
}
// withComments is the JSON in, indented a value a line, with a comment after each line marks
// says (a line's, or a block's) and a trailing comma before each } or ] that ends an object or a
// list, and a byte order mark first with bom.
func withComments(in []byte, comment string, marks []byte, bom bool) []byte {
var b bytes.Buffer
if json.Indent(&b, in, "", " ") != nil {
return nil
}
lines := strings.Split(b.String(), "\n")
var out strings.Builder
if bom {
out.WriteString("\xef\xbb\xbf")
}
for i, l := range lines {
out.WriteString(l)
if i+1 < len(lines) {
next := strings.TrimSpace(lines[i+1])
if l != "" && !strings.HasSuffix(l, "{") && !strings.HasSuffix(l, "[") && (strings.HasPrefix(next, "}") || strings.HasPrefix(next, "]")) {
out.WriteString(",")
}
}
var m byte
if i < len(marks) {
m = marks[i]
}
switch {
case m%3 == 1 && !strings.ContainsAny(comment, "\r\n"):
out.WriteString(" //" + comment)
case m%3 == 2 && !strings.Contains(comment, "*/"):
out.WriteString(" /*" + comment + "*/")
}
if i+1 < len(lines) {
out.WriteString("\n")
}
}
return []byte(out.String())
}
// decoded is b as encoding/json reads it, its numbers as written.
func decoded(b []byte) (any, error) {
d := json.NewDecoder(bytes.NewReader(b))
d.UseNumber()
var v any
err := d.Decode(&v)
return v, err
}
// FuzzStrip: Strip keeps every offset, blanking bytes with spaces but newlines; leaves JSON as it
// is; and, against encoding/json, makes JSON with comments, trailing commas and a byte order mark
// the value the JSON without them is.
func FuzzStrip(f *testing.F) {
for _, s := range []string{`{"a": [1, 2], "b": {"c": "//x/*y*/\""}}`, `[]`, `"s"`, `{"a":{"b":[{"c":null}]}}`, "{} /* x", `{"a":1,}`} {
f.Add([]byte(s), "c", []byte{1, 2, 0, 1}, false)
f.Add([]byte(s), "a\"b*", []byte{2, 2, 2}, true)
}
f.Fuzz(func(t *testing.T, data []byte, comment string, marks []byte, bom bool) {
got := Strip(data)
if len(got) != len(data) {
t.Fatalf("Strip(%q) = %q: another length", data, got)
}
for i := range data {
if got[i] != data[i] && (got[i] != ' ' || data[i] == '\n' || data[i] == '\r') {
t.Fatalf("Strip(%q) = %q: byte %d", data, got, i)
}
}
if !json.Valid(data) {
return
}
if !bytes.Equal(got, data) {
t.Fatalf("Strip(%q) = %q: JSON changed", data, got)
}
in := withComments(data, comment, marks, bom)
want, err := decoded(data)
if err != nil || in == nil {
t.Fatalf("%q: %v", data, err)
}
v, err := decoded(Strip(in))
if err != nil || !reflect.DeepEqual(v, want) {
t.Fatalf("Strip(%q) reads as %v, %v; want %v", in, v, err, want)
}
})
}