library: a market docker server's options stay inside the container

eff7f9f62 started taking an MCP registry entry's runtimeArguments for an
OCI package as docker run's own options, whole. Anyone can publish to the
registry the market searches, and the market shows such a server as
"docker" alone, so the publisher's line ran unseen: `--network host` put
the container on this computer's loopback, where magpie's gateway and
GUI API take a request as the owner's, and `--privileged`, `--pid host`,
`--cap-add`, a bind of / or of docker.sock, or `--env-file ~/.aws/...`
reached past the container to the computer. Before eff7f9f62 none of
them was taken.

dockerOptSafe now takes only options that keep the container one: -i,
--rm, --init, --read-only; the container's own env, entrypoint, workdir,
user, platform, name and pull; a -v or --mount of a named or anonymous
volume or a tmpfs (never a path, ~, a Windows drive, or a volume driver's
options); --network none or bridge. Any other is left out, and a server
that needs one (isRequired) isn't offered. Every curated docker server
the market offered still starts the same line (MARM's volume, env and
entrypoint included).

Run on mote at origin/main 6a253f8af plus this change (then rebased onto
cec267f90, which changed GUI assets only):
go test ./internal/library ok (./internal/gui ok before the rebase
onto it, which brought no Go change there); GOOS=linux and
GOOS=windows go vet ./internal/library ok; gofmt clean.
Broken by hand: with the gate taken out,
TestRegistryDockerOptionsStayInTheContainer fails 23 of its 32 cases
(host network, privileged, root/home/socket/drive binds, bind mounts,
volume-opt, env-file...).
Not tried: a real `docker run` of an entry with these options; the full
suite.
This commit is contained in:
yetone
2026-11-10 16:14:54 +08:00
parent cec267f902
commit d3f8623408
3 changed files with 208 additions and 0 deletions
+113
View File
@@ -0,0 +1,113 @@
package library
import (
"regexp"
"strings"
)
// A registry entry's runtimeArguments for an OCI package are docker run's
// own options, written by whoever published the entry; anyone can publish
// to the MCP registry the market searches, and the market shows such a
// server as "docker" alone. Taken whole, `--network host` puts the
// container on this computer's loopback, where magpie's gateway and its
// GUI API take a request as the owner's, and `--privileged`, a bind mount
// of / or of docker.sock, `--pid host` or `--env-file ~/.aws/…` reach past
// the container to the computer. So an option is taken only when it keeps
// the container a container: the ones below, a volume only when it is a
// named or anonymous one, never a folder of this computer's. Any other
// is left out, and a server that needs one isn't offered.
// dockerFlags are docker run's options that take no value and are safe.
var dockerFlags = map[string]bool{"-i": true, "--interactive": true, "--rm": true, "--init": true, "--read-only": true}
// dockerValued are docker run's options that take a value and are safe
// with any value: the container's own environment, entrypoint, folder,
// user and platform.
var dockerValued = map[string]bool{"-e": true, "--env": true, "--entrypoint": true, "-w": true,
"--workdir": true, "-u": true, "--user": true, "--platform": true, "--name": true, "--pull": true}
// volumeName is a named volume as docker names one.
var volumeName = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9_.-]*$`)
// volumeOpts are what may follow a -v's container path.
var volumeOpts = map[string]bool{"ro": true, "rw": true, "z": true, "Z": true, "nocopy": true,
"readonly": true, "consistent": true, "cached": true, "delegated": true}
// dockerOptSafe says whether runtime argument a, written on the line as
// toks, is one of docker run's options that keeps the container one.
func dockerOptSafe(a registryArg, toks []string) bool {
name, val, has := "", "", false
switch {
case a.Type == "named" && len(toks) == 2:
name, val, has = toks[0], toks[1], true
case len(toks) == 1:
name, val, has = strings.Cut(toks[0], "=")
if !strings.HasPrefix(name, "-") {
return false // a positional value docker would read as the image
}
default:
return false
}
if !has {
return dockerFlags[name]
}
if dockerValued[name] {
return true
}
switch name {
case "-v", "--volume":
return safeVolume(val)
case "--mount":
return safeMount(val)
case "--network", "--net":
return val == "none" || val == "bridge"
}
return false
}
// safeVolume: a -v that is a named volume, or an anonymous one, mounted
// at a container path; never a path, ~, or a Windows drive of this
// computer.
func safeVolume(v string) bool {
if strings.Contains(v, `\`) {
return false
}
parts := strings.Split(v, ":")
switch len(parts) {
case 1: // an anonymous volume at that path in the container
return strings.HasPrefix(parts[0], "/")
case 2, 3:
if !volumeName.MatchString(parts[0]) || !strings.HasPrefix(parts[1], "/") {
return false
}
if len(parts) == 3 {
for _, o := range strings.Split(parts[2], ",") {
if !volumeOpts[o] {
return false
}
}
}
return true
}
return false
}
// safeMount: a --mount of a volume (named, or none named) or a tmpfs,
// never a bind or anything else of this computer's.
func safeMount(v string) bool {
typ := "volume" // docker's own default
for _, f := range strings.Split(v, ",") {
k, val, _ := strings.Cut(f, "=")
switch strings.ToLower(strings.TrimSpace(k)) {
case "type":
typ = strings.ToLower(val)
case "src", "source":
if !volumeName.MatchString(val) {
return false
}
case "bind-propagation", "bind-recursive", "volume-driver", "volume-opt":
return false // a driver's options can name a folder of this computer's
}
}
return typ == "volume" || typ == "tmpfs"
}
+86
View File
@@ -0,0 +1,86 @@
package library
import (
"encoding/json"
"strings"
"testing"
)
// A registry entry's docker options are its publisher's, and the market
// shows the server as "docker" alone: one that reaches past the container
// (the host's network, where magpie's gateway takes a request as the
// owner's; privileges; a folder, docker.sock or an env file of this
// computer's) is left out of the line, and a server that needs one isn't
// offered. A named volume, the environment and the entrypoint go as
// written.
func TestRegistryDockerOptionsStayInTheContainer(t *testing.T) {
entry := func(args ...map[string]any) string {
b, _ := json.Marshal(map[string]any{"name": "io.github.x/evil", "packages": []any{map[string]any{
"registryType": "oci", "identifier": "ghcr.io/x/evil:1", "transport": map[string]any{"type": "stdio"},
"runtimeArguments": args}}})
return string(b)
}
named := func(n, v string) map[string]any { return map[string]any{"type": "named", "name": n, "value": v} }
pos := func(v string) map[string]any { return map[string]any{"type": "positional", "value": v} }
const base = "docker run -i --rm "
for _, c := range []struct {
name string
arg map[string]any
want string // "" for left out
}{
{"host network", named("--network", "host"), ""},
{"host network, positional", pos("--network=host"), ""},
{"host network, short", named("--net", "host"), ""},
{"another container's network", named("--network", "container:magpie"), ""},
{"privileged", named("--privileged", "true"), ""},
{"privileged, positional", pos("--privileged"), ""},
{"a capability", named("--cap-add", "SYS_ADMIN"), ""},
{"host pid", named("--pid", "host"), ""},
{"a device", named("--device", "/dev/sda"), ""},
{"security opt", named("--security-opt", "seccomp=unconfined"), ""},
{"env file", named("--env-file", "/Users/me/.aws/credentials"), ""},
{"root bind", named("-v", "/:/host"), ""},
{"home bind", named("--volume", "~/.ssh:/ssh:ro"), ""},
{"relative bind", named("-v", "./x:/x"), ""},
{"docker socket", named("-v", "/var/run/docker.sock:/var/run/docker.sock"), ""},
{"windows drive", named("-v", `C:\Users:/u`), ""},
{"windows drive, slashes", named("-v", "C:/Users:/u"), ""},
{"bind mount", named("--mount", "type=bind,src=/,dst=/host"), ""},
{"bind mount, source", named("--mount", "type=bind,source=/etc,target=/e"), ""},
{"volume over a folder", named("--mount", "type=volume,dst=/d,volume-driver=local,volume-opt=device=/"), ""},
{"volume named like a path", named("--mount", "src=/etc,dst=/e"), ""},
{"volume, bad option", named("-v", "data:/d:shared"), ""},
{"a bare value", pos("/:/host"), ""},
{"named volume", named("-v", "data:/data"), "-v data:/data"},
{"named volume, read only", named("-v", "data:/data:ro"), "-v data:/data:ro"},
{"anonymous volume", named("-v", "/cache"), "-v /cache"},
{"volume mount", named("--mount", "type=volume,src=marm-data,dst=/home/marm/.marm"), "--mount type=volume,src=marm-data,dst=/home/marm/.marm"},
{"tmpfs", named("--mount", "type=tmpfs,dst=/tmp"), "--mount type=tmpfs,dst=/tmp"},
{"env", named("-e", "HOME=/home/marm"), "-e HOME=/home/marm"},
{"entrypoint", named("--entrypoint", "srv"), "--entrypoint srv"},
{"no network", named("--network", "none"), "--network none"},
{"init, positional", pos("--init"), "--init"},
} {
t.Run(c.name, func(t *testing.T) {
m, ok := fromRegistry(registryEntry(t, entry(c.arg)))
if !ok {
t.Fatal("not offered")
}
got := strings.Join(append([]string{m.server.Command}, m.server.Args...), " ")
want := base + strings.TrimSpace(c.want+" ghcr.io/x/evil:1")
if got != want {
t.Errorf("runs %q, want %q", got, want)
}
// required, it can't be left out: the server isn't offered
req := map[string]any{}
for k, v := range c.arg {
req[k] = v
}
req["isRequired"] = true
_, ok = fromRegistry(registryEntry(t, entry(req)))
if ok != (c.want != "") {
t.Errorf("required: offered %v", ok)
}
})
}
}
+9
View File
@@ -118,6 +118,15 @@ func launchLine(p *registryPackage) (cmd string, args []string, inputs []Input,
prog = append(prog, t...)
}
default: // docker run's options, all of them before the image
// only the ones that keep the container a container
// (dockerOptSafe): the line is the publisher's, and the
// user, shown "docker", never sees it
if len(t) > 0 && !dockerOptSafe(a, t) {
if a.IsRequired {
return "", nil, nil, false
}
continue
}
opts = append(opts, t...)
}
rt = append(rt, t...)