From 3a26277c32158870957056e37d7db1518b1f1bc3 Mon Sep 17 00:00:00 2001 From: Ilker Manap Date: Wed, 30 Sep 2026 00:24:48 +0200 Subject: [PATCH] Refuse names that leave their directory, on the mirror and the agent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two places built a path from a name somebody else supplied and checked only for slashes. The mirror refused "a/b" as a model name but took "..", which is a single path element too: a fetch for model ".." wrote beside the mirror root rather than inside it. Model, version and file names now have to be one element that does not start with a dot — the mirror's own checksum files are dotted — on the fetch path and the API alike. The agent took the engine version from the control plane as given and made a directory of it under /opt/llmgw/engines, and a RemoveAll before replacing one. A version of "../x" would have done both outside the install root, as root. The agent now installs only versions that look like one — letters, digits and ._+- — whatever the control plane sent. Co-Authored-By: Claude Opus 5.5 (1M context) --- internal/agent/provision/install.go | 14 ++++++++++++++ internal/agent/provision/install_test.go | 12 ++++++++++++ internal/mirror/add.go | 9 ++++----- internal/mirror/api.go | 4 ++-- internal/mirror/mirror.go | 15 +++++++++++++++ internal/mirror/mirror_test.go | 15 +++++++++++++++ 6 files changed, 62 insertions(+), 7 deletions(-) diff --git a/internal/agent/provision/install.go b/internal/agent/provision/install.go index 4a31419e..e843d7d9 100644 --- a/internal/agent/provision/install.go +++ b/internal/agent/provision/install.go @@ -16,6 +16,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "strings" "time" @@ -77,6 +78,10 @@ type Installer struct { PyPIBase string } +// validVersion is what a version may look like: "b10991", "0.29.0", +// "0.11.0+cu128". No separators, and not starting with a dot. +var validVersion = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._+-]{0,63}$`) + // DefaultRoot is where engines are installed when nothing says otherwise. const DefaultRoot = "/opt/llmgw/engines" @@ -114,6 +119,15 @@ func (in *Installer) Converge(ctx context.Context, wants []Want) []Status { func (in *Installer) one(ctx context.Context, w Want) Status { st := Status{Engine: w.Engine, Version: w.Version} + // Checked here, on the node, whatever the control plane checked: the + // version names a directory under the install root and is passed to a + // package manager, and "../x" would put an install — and the RemoveAll + // that replaces one — outside it, as root. + if !validVersion.MatchString(w.Version) { + st.State = Unsupported + st.Detail = fmt.Sprintf("%q is not a version this agent will install", w.Version) + return st + } recipe, ok := Recipes[w.Engine] if !ok { st.State = Unsupported diff --git a/internal/agent/provision/install_test.go b/internal/agent/provision/install_test.go index bf8654d2..f8453f2c 100644 --- a/internal/agent/provision/install_test.go +++ b/internal/agent/provision/install_test.go @@ -330,3 +330,15 @@ func TestAPackageManagerBusyWithItsOwnUpdatesIsWaitedFor(t *testing.T) { t.Error("the wait was not reported") } } + +func TestAVersionThatIsAPathIsRefused(t *testing.T) { + // The version names a directory under the install root; "../" would put + // an install, and the RemoveAll that replaces one, outside it, as root. + in := &Installer{Root: t.TempDir(), Host: ubuntu} + for _, v := range []string{"../../etc", "..", ".hidden", "b1/../../x", "", "b1 && rm -rf /"} { + got := in.Converge(t.Context(), []Want{{Engine: "llamacpp", Version: v}}) + if got[0].State != Unsupported { + t.Errorf("version %q: %+v", v, got[0]) + } + } +} diff --git a/internal/mirror/add.go b/internal/mirror/add.go index b2c8210c..bf22f95f 100644 --- a/internal/mirror/add.go +++ b/internal/mirror/add.go @@ -7,7 +7,6 @@ import ( "net/http" "os" "path/filepath" - "strings" "time" ) @@ -32,10 +31,10 @@ func Add(ctx context.Context, root, source string, e Entry, progress func(done, if e.Model == "" || e.Version == "" || e.File == "" { return Added{}, fmt.Errorf("a model, a version and a filename are all required") } - if strings.ContainsAny(e.Model+e.Version+e.File, `/\`) { - // Each is one path element. A slash would put the file somewhere the - // layout does not describe, and List would not find it again. - return Added{}, fmt.Errorf("model, version and filename are single names, not paths") + for _, n := range []string{e.Model, e.Version, e.File} { + if err := ValidName(n); err != nil { + return Added{}, err + } } final := e.Path(root) diff --git a/internal/mirror/api.go b/internal/mirror/api.go index 9379688f..3e38312b 100644 --- a/internal/mirror/api.go +++ b/internal/mirror/api.go @@ -130,8 +130,8 @@ func (a *API) status(w http.ResponseWriter, r *http.Request) { func (a *API) remove(w http.ResponseWriter, r *http.Request) { model, version := r.PathValue("model"), r.PathValue("version") - if strings.ContainsAny(model+version, `/\`) { - fail(w, http.StatusBadRequest, errors.New("model and version are single names, not paths")) + if err := errors.Join(ValidName(model), ValidName(version)); err != nil { + fail(w, http.StatusBadRequest, err) return } diff --git a/internal/mirror/mirror.go b/internal/mirror/mirror.go index 75b2d198..a76895a3 100644 --- a/internal/mirror/mirror.go +++ b/internal/mirror/mirror.go @@ -37,6 +37,21 @@ type Entry struct { Bytes int64 } +// ValidName checks one element of an entry — a model, version or file name. +// +// Each is one path element, and a name that is not one writes somewhere the +// layout does not describe. Rejecting slashes was not enough: ".." is a +// single element too, and a model called ".." put its file beside the mirror +// root rather than inside it. A name starting with a dot is refused as well, +// because that is where the mirror keeps its own bookkeeping. +func ValidName(n string) error { + if n == "" || n[0] == '.' || strings.ContainsAny(n, "/\\\x00") { + return fmt.Errorf("%q is not a usable name: it must be a single path element "+ + "that does not start with a dot", n) + } + return nil +} + // Path is where this entry lives under a root. func (e Entry) Path(root string) string { return filepath.Join(root, e.Model, e.Version, e.File) diff --git a/internal/mirror/mirror_test.go b/internal/mirror/mirror_test.go index 1ff0f783..0341ac8e 100644 --- a/internal/mirror/mirror_test.go +++ b/internal/mirror/mirror_test.go @@ -162,3 +162,18 @@ func TestProgressIsReportedForALongDownload(t *testing.T) { t.Error("a download reported no progress at all") } } + +func TestANameThatLeavesTheMirrorIsRefused(t *testing.T) { + root := t.TempDir() + for _, e := range []Entry{ + {Model: "..", Version: "v", File: "f.gguf"}, + {Model: "m", Version: "..", File: "f.gguf"}, + {Model: "m", Version: "v", File: ".f.sha256"}, + {Model: "a/b", Version: "v", File: "f.gguf"}, + } { + if _, err := Add(t.Context(), root, "http://127.0.0.1:1/x", e, nil); err == nil || + !strings.Contains(err.Error(), "not a usable name") { + t.Errorf("%+v: %v", e, err) + } + } +}