From 99a0ab45bb7b2067e487f9388ee3dcc593d34ca3 Mon Sep 17 00:00:00 2001 From: w3cj Date: Tue, 24 Nov 2026 12:51:39 -0700 Subject: [PATCH] test(devcontainer): FuzzParse's check reads variables as devvm does The nightly fuzz run found an input that failed FuzzParse's own check, not devvm: {"remoteEnv":{"A":"${${localEnv:}"}}. devvm reads each ${ to the first } after it, so ${${localEnv:} is one variable whose name starts with ${, which it leaves as single-quoted text for the VM's shell, as the design note intends. The check's pattern matched ${localEnv:} inside it. The check now takes the variables as devvm reads them, non-overlapping ${...} to the first }, and flags only one whose name starts with localEnv: or env:, in the environment and in what's kept. TestParse has a nested ${${localEnv:HOME}} in a variable and in a command, kept as text, and the input is in the corpus. FuzzParse's row in Contributing's testing page says how it reads them. Co-Authored-By: Claude Opus 5.5 --- docs/content/docs/contributing/testing.md | 2 +- internal/devcontainer/devcontainer_test.go | 34 +++++++++++++------ .../testdata/fuzz/FuzzParse/a2573cd485ae1235 | 2 ++ 3 files changed, 27 insertions(+), 11 deletions(-) create mode 100644 internal/devcontainer/testdata/fuzz/FuzzParse/a2573cd485ae1235 diff --git a/docs/content/docs/contributing/testing.md b/docs/content/docs/contributing/testing.md index c4e6b8ae..9aab50be 100644 --- a/docs/content/docs/contributing/testing.md +++ b/docs/content/docs/contributing/testing.md @@ -354,7 +354,7 @@ The parsers of what a VM can write, its output and its files, and of what comes | `FuzzClientHello` | `internal/proxy` | Reading a tunnel's first bytes never hangs, a stream whose first byte starts a TLS record counts as TLS, and only a whole record of TLS's version is returned. | | `FuzzTransparent` | `internal/proxy` | Whatever its first bytes, a transparent connection to a proxy whose policy allows nothing reaches nothing. | | `FuzzNixPath` | `internal/proxy` | What `nixPath` takes is one of the cache's exact forms and names a file inside its folder. | -| `FuzzParse` | `internal/devcontainer` | A `devcontainer.json` refuses `${localEnv:…}`, its variables are names and shell words the environment file holds as they are, and nothing it keeps has a NUL. | +| `FuzzParse` | `internal/devcontainer` | A `devcontainer.json` refuses `${localEnv:…}` and `${env:…}`. The check reads each variable as devvm does, from `${` to the first `}` after it, so a nested one such as `${${localEnv:HOME}}` is left as text. Its environment variables are names and shell words the environment file holds as they are, and nothing it keeps has a NUL. | | `FuzzParseRef` | `internal/from` | Any image reference parses into a tag without `/` or `:`, and a valid one parses the same once written out in full. | | `FuzzRestrictNetdev` | `internal/backend/lima` | `RestrictNetdev` refuses, or returns at most one network, restricted, with nothing else changed. | | `FuzzQuote` | `internal/shell` | A string that `Quote` quotes reaches a shell as itself. | diff --git a/internal/devcontainer/devcontainer_test.go b/internal/devcontainer/devcontainer_test.go index 6a8d05f0..2c7cdfb9 100644 --- a/internal/devcontainer/devcontainer_test.go +++ b/internal/devcontainer/devcontainer_test.go @@ -23,9 +23,9 @@ const ws = "/Users/me/web app" // double-quoted. Nothing else a shell would run or split. var shellWord = regexp.MustCompile(`^(?:'[^']*'|\\'|"\$\{[A-Za-z_][A-Za-z0-9_]*\}")+$`) -// Whatever the file holds, Parse doesn't panic, refuses ${localEnv:…}, -// and its variables are names and shell words the environment file can -// hold as they are; nothing it keeps has a NUL. +// Whatever the file holds, Parse doesn't panic, refuses ${localEnv:…} +// and ${env:…}, and its variables are names and shell words the +// environment file can hold as they are; nothing it keeps has a NUL. func FuzzParse(f *testing.F) { for _, s := range []string{ `{"containerEnv": {"A": "x'y ${containerEnv:PATH} $(id) ${HOME}\n"}, "remoteEnv": {"B": null}}`, @@ -41,7 +41,7 @@ func FuzzParse(f *testing.F) { return } for _, v := range p.Env { - if !shell.EnvName(v.Name) || !shellWord.MatchString(v.Value) || hostVar.MatchString(v.Value) { + if !shell.EnvName(v.Name) || !shellWord.MatchString(v.Value) || hostVar(v.Value) { t.Errorf("variable %q=%q", v.Name, v.Value) } } @@ -50,15 +50,28 @@ func FuzzParse(f *testing.F) { kept = append(kept, c.Argv...) } for _, s := range kept { - if strings.IndexByte(s, 0) >= 0 || hostVar.MatchString(s) { + if strings.IndexByte(s, 0) >= 0 || hostVar(s) { t.Errorf("kept %q", s) } } }) } -// hostVar is a variable of this machine's environment, filled in. -var hostVar = regexp.MustCompile(`\$\{(localEnv|env):[^}]*\}`) +// hostVar reports whether s holds a variable of this machine's +// environment that Parse should have refused. It reads variables as +// subst does, each ${ to the first } after it, so ${${localEnv:} is one +// variable whose name starts with ${, left as text. +func hostVar(s string) bool { + for _, v := range variable.FindAllString(s, -1) { + if name := v[2 : len(v)-1]; strings.HasPrefix(name, "localEnv:") || strings.HasPrefix(name, "env:") { + return true + } + } + return false +} + +// variable is a ${…} as subst reads it, to the first } after its ${. +var variable = regexp.MustCompile(`\$\{[^}]*\}`) func parse(t *testing.T, src string) (Plan, error) { t.Helper() @@ -87,11 +100,11 @@ func TestParse(t *testing.T) { }, "forwardPorts": [3000, "localhost:5173", "127.0.0.1:8000", "db:5432", 3000, 0], "containerEnv": {"A": "it's ${containerWorkspaceFolder}", "B": "x"}, - "remoteEnv": {"B": "y", "PATH": "${containerEnv:PATH}:/opt/x", "C": null, "E": ""}, + "remoteEnv": {"B": "y", "PATH": "${containerEnv:PATH}:/opt/x", "C": null, "E": "", "N": "${${localEnv:HOME}}"}, "onCreateCommand": "echo ${containerWorkspaceFolderBasename} $HOME ${HOME}", "updateContentCommand": ["npm", "ci"], "postCreateCommand": {"b": "echo b", "a": ["echo", "a"], "empty": ""}, - "postStartCommand": "echo $PATH ${containerEnv:PATH}", + "postStartCommand": "echo $PATH ${containerEnv:PATH} ${${localEnv:HOME}}", "postAttachCommand": "x", "initializeCommand": "rm -rf ~", "runArgs": ["--privileged"], @@ -117,6 +130,7 @@ func TestParse(t *testing.T) { {"A", `'it'\''s ` + ws + `'`}, {"B", "'y'"}, {"E", "''"}, + {"N", `'${${localEnv:HOME}}'`}, // one variable named ${localEnv:HOME, left as text {"PATH", `"${PATH}"':/opt/x'`}, } if !slices.Equal(p.Env, wantEnv) { @@ -131,7 +145,7 @@ func TestParse(t *testing.T) { if !slices.EqualFunc(p.Create, wantCreate, cmdEqual) { t.Errorf("create = %q, want %q", p.Create, wantCreate) } - wantStart := []Command{{PostStart, []string{"sh", "-c", "echo $PATH ${PATH}"}}} + wantStart := []Command{{PostStart, []string{"sh", "-c", "echo $PATH ${PATH} ${${localEnv:HOME}}"}}} if !slices.EqualFunc(p.Start, wantStart, cmdEqual) { t.Errorf("start = %q, want %q", p.Start, wantStart) } diff --git a/internal/devcontainer/testdata/fuzz/FuzzParse/a2573cd485ae1235 b/internal/devcontainer/testdata/fuzz/FuzzParse/a2573cd485ae1235 new file mode 100644 index 00000000..ada172d7 --- /dev/null +++ b/internal/devcontainer/testdata/fuzz/FuzzParse/a2573cd485ae1235 @@ -0,0 +1,2 @@ +go test fuzz v1 +[]byte("{\"remoteEnv\":{\"A\":\"${${localEnv:}\"}}")