carlos / platform Public

Key devbox socket ownership by the served link host

Replace the ambiguous path lookup with SocketOwnerFor and share host matching with AllowFor. Keep each link bound to its own owner when socket paths are reused, with regression coverage for both name orders. Address the scoped minor review findings.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 2ec6b38fa6c1bca4f168a1c5efad502ce632d1b2 parent e0d38be
3 files changed, +88 −27
  • internal/devbox/registrar.go +31 −18
  • internal/devbox/registrar_socket_linux_test.go +9 −9
  • internal/devbox/registrar_socket_test.go +48 −0

CI failed — run details

diff --git a/internal/devbox/registrar.go b/internal/devbox/registrar.go
index 56c548abb..b6d081cfe 100644
--- a/internal/devbox/registrar.go
+++ b/internal/devbox/registrar.go
@@ -64,7 +64,13 @@ const (
// MaxFolderBytes caps one folder upload, the compressed site.tar.gz.
MaxFolderBytes = 200 << 20
+ // DefaultSocketsDir is set by serve; an empty SocketsDir refuses socket shares.
DefaultSocketsDir = "/run/carlos-devbox/sockets"
+ // MaxAllowEntries caps the number of requested allow entries before deduplication.
+ MaxAllowEntries = 64
+ // MaxAllowLogin caps the length of a trimmed ASCII login.
+ MaxAllowLogin = 254
+
// AnswerTimeout bounds port and socket connection checks.
AnswerTimeout = 300 * time.Millisecond
@@ -105,7 +111,7 @@ const (
// Registrar owns the devbox's link names. The exported fields are its
// configuration; set them before the first request and leave them.
type Registrar struct {
- SocketsDir string
+ SocketsDir string // set by serve; empty refuses socket shares
Reg *registry.Registry
Links *Links
Domain string // wildcard parent, e.g. "devbox.example.com"
@@ -843,6 +849,8 @@ func (r *Registrar) list(ctx context.Context, uid uint32) []ListEntry {
wg.Add(1)
go func(i int, path string) {
defer wg.Done()
+ // This probe may follow a replaced symlink; it connects and closes
+ // without sending data. Forwarding checks the listener uid separately.
c, err := connectSocket(ctx, path)
if err == nil {
c.Close()
@@ -874,14 +882,14 @@ func answering(ctx context.Context, port int) bool {
}
func validateAllow(entries []string) ([]string, *refusal) {
- if len(entries) > 64 {
+ if len(entries) > MaxAllowEntries {
return nil, refuse(http.StatusBadRequest, CodeBadAllow)
}
var allow []string
seen := make(map[string]bool, len(entries))
for _, entry := range entries {
entry = strings.TrimSpace(entry)
- if len(entry) == 0 || len(entry) > 254 {
+ if len(entry) == 0 || len(entry) > MaxAllowLogin {
return nil, refuse(http.StatusBadRequest, CodeBadAllow)
}
for i := range entry {
@@ -899,35 +907,40 @@ func validateAllow(entries []string) ([]string, *refusal) {
}
// AllowFor returns a copy of the allow list for a host under Domain.
+// The copy comes from Links.Get through linkForHost.
func (r *Registrar) AllowFor(host string) []string {
+ link, ok := r.linkForHost(host)
+ if !ok || len(link.Allow) == 0 {
+ return nil
+ }
+ return link.Allow
+}
+
+// linkForHost gives both edge lookups the same host matching rules.
+func (r *Registrar) linkForHost(host string) (Link, bool) {
if h, _, err := net.SplitHostPort(host); err == nil {
host = h
}
host = strings.ToLower(strings.TrimSuffix(host, "."))
domain := "." + strings.ToLower(strings.TrimSuffix(r.Domain, "."))
if !strings.HasSuffix(host, domain) {
- return nil
+ return Link{}, false
}
name := strings.TrimSuffix(host, domain)
if ValidName(name) != nil {
- return nil
+ return Link{}, false
}
- link, ok := r.Links.Get(name)
- if !ok || len(link.Allow) == 0 {
- return nil
- }
- return link.Allow
+ return r.Links.Get(name)
}
-// SocketOwner returns the recorded owner of a socket target.
-func (r *Registrar) SocketOwner(path string) (uint32, bool) {
- path = filepath.Clean(path)
- for _, link := range r.Links.All() {
- if link.Kind == KindSocket && filepath.Clean(link.Socket) == path {
- return link.OwnerUID, true
- }
+// SocketOwnerFor returns the socket path and owner of the link served at host.
+// Several links can retain the same path after its listener changes owners.
+func (r *Registrar) SocketOwnerFor(host string) (path string, uid uint32, ok bool) {
+ link, ok := r.linkForHost(host)
+ if !ok || link.Kind != KindSocket {
+ return "", 0, false
}
- return 0, false
+ return link.Socket, link.OwnerUID, true
}
func (r *Registrar) serveShareSocket(uid uint32, w http.ResponseWriter, req *http.Request) (any, *refusal) {
diff --git a/internal/devbox/registrar_socket_linux_test.go b/internal/devbox/registrar_socket_linux_test.go
index 3c3a75630..005a212cc 100644
--- a/internal/devbox/registrar_socket_linux_test.go
+++ b/internal/devbox/registrar_socket_linux_test.go
@@ -156,20 +156,20 @@ func TestShareSocketUnreachable(t *testing.T) {
noSocketWrite(t, f)
}
-func TestSocketOwner(t *testing.T) {
+func TestSocketOwnerForLifecycle(t *testing.T) {
f, path, _ := socketFixture(t)
uid := uint32(os.Getuid())
wantOK(t, shareAllow(t, f, uid, KindSocket, "shop", path, nil))
- for _, path := range []string{path, filepath.Dir(path) + "/./shop.sock"} {
- if got, ok := f.r.SocketOwner(path); !ok || got != uid {
- t.Fatalf("SocketOwner(%q) = %d,%v", path, got, ok)
- }
+ host := "shop." + testDomain
+ if gotPath, gotUID, ok := f.r.SocketOwnerFor(host); !ok || gotUID != uid || gotPath != path {
+ t.Fatalf("SocketOwnerFor(%q) = %q, %d, %v", host, gotPath, gotUID, ok)
}
- if _, ok := f.r.SocketOwner(path + ".other"); ok {
+ if _, _, ok := f.r.SocketOwnerFor("missing." + testDomain); ok {
t.Fatal("unknown socket found")
}
+
wantOK(t, f.shareLive(t, uid, "shop", 8080))
- if _, ok := f.r.SocketOwner(path); ok {
+ if _, _, ok := f.r.SocketOwnerFor(host); ok {
t.Fatal("live link retained socket owner")
}
k, _ := f.r.Links.Get("shop")
@@ -177,12 +177,12 @@ func TestSocketOwner(t *testing.T) {
if err := f.r.Links.Put(k); err != nil {
t.Fatal(err)
}
- if _, ok := f.r.SocketOwner(path); ok {
+ if _, _, ok := f.r.SocketOwnerFor(host); ok {
t.Fatal("non-socket kind used as socket owner")
}
wantOK(t, shareAllow(t, f, uid, KindSocket, "shop", path, nil))
wantOK(t, f.unshare(t, uid, "shop"))
- if _, ok := f.r.SocketOwner(path); ok {
+ if _, _, ok := f.r.SocketOwnerFor(host); ok {
t.Fatal("unshared socket found")
}
if _, err := os.Stat(path); err != nil {
diff --git a/internal/devbox/registrar_socket_test.go b/internal/devbox/registrar_socket_test.go
index 3b6911972..023980677 100644
--- a/internal/devbox/registrar_socket_test.go
+++ b/internal/devbox/registrar_socket_test.go
@@ -250,3 +250,51 @@ func TestAllowForConcurrentReads(t *testing.T) {
}
wg.Wait()
}
+
+func TestSocketOwnerForSharedPath(t *testing.T) {
+ for _, names := range [][2]string{{"zzz", "copy"}, {"copy", "zzz"}} {
+ t.Run(names[0], func(t *testing.T) {
+ f := newFixture(t)
+ path := DefaultSocketsDir + "/shop.sock"
+ for i, name := range names {
+ link := sample(name)
+ link.Kind, link.Port, link.Socket = KindSocket, 0, path
+ link.OwnerUID = uint32(1001 + i)
+ if i == 1 {
+ link.Allow = []string{"bob@example.com"}
+ }
+ if err := f.r.Links.Put(link); err != nil {
+ t.Fatal(err)
+ }
+ }
+
+ for i, name := range names {
+ for _, host := range []string{name + "." + testDomain, strings.ToUpper(name + "." + testDomain), name + "." + testDomain + ".", name + "." + testDomain + ":443", strings.ToUpper(name+"."+testDomain) + ".:443"} {
+ gotPath, uid, ok := f.r.SocketOwnerFor(host)
+ if !ok || gotPath != path || uid != uint32(1001+i) {
+ t.Fatalf("SocketOwnerFor(%q) = %q, %d, %v", host, gotPath, uid, ok)
+ }
+ allow := f.r.AllowFor(host)
+ if i == 0 && allow != nil || i == 1 && !equal(allow, []string{"bob@example.com"}) {
+ t.Fatalf("AllowFor(%q) = %v", host, allow)
+ }
+ }
+ }
+ link := sample("live")
+ link.Socket = path
+ if err := f.r.Links.Put(link); err != nil {
+ t.Fatal(err)
+ }
+ for _, host := range []string{"live." + testDomain, "missing." + testDomain, "zzz.other.example.com", "copy." + testDomain + ".evil", "nested.copy." + testDomain, "copy", testDomain, "copy." + testDomain + ":443:444"} {
+ gotPath, uid, ok := f.r.SocketOwnerFor(host)
+ if ok || gotPath != "" || uid != 0 {
+ t.Fatalf("SocketOwnerFor(%q) = %q, %d, %v; want no socket", host, gotPath, uid, ok)
+ }
+ }
+ f.r.Domain = strings.ToUpper(testDomain) + "."
+ if gotPath, uid, ok := f.r.SocketOwnerFor(names[1] + "." + testDomain); !ok || gotPath != path || uid != 1002 {
+ t.Fatalf("configured domain match = %q, %d, %v", gotPath, uid, ok)
+ }
+ })
+ }
+}