Refuse names that leave their directory, on the mirror and the agent

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) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-09-30 00:24:48 +02:00
1 parent 7062645bcb
commit 3a26277c32
6 files changed
+62 -7

No files matched your search

+14
View File
@@ -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
+12
View File
@@ -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])
}
}
}
+4 -5
View File
@@ -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)
+2 -2
View File
@@ -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
}
+15
View File
@@ -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)
+15
View File
@@ -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)
}
}
}